Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / libgo / go / go / types / testdata / stmt0.src
1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // statements
6
7 package stmt0
8
9 func _() {
10         b, i, f, c, s := false, 1, 1.0, 1i, "foo"
11         b = i /* ERROR "cannot assign" */
12         i = f /* ERROR "cannot assign" */
13         f = c /* ERROR "cannot assign" */
14         c = s /* ERROR "cannot assign" */
15         s = b /* ERROR "cannot assign" */
16
17         v0 /* ERROR "mismatch" */, v1, v2 := 1, 2, 3, 4
18
19         b = true
20
21         i += 1
22         i += "foo" /* ERROR "cannot convert.*int" */
23
24         f -= 1
25         f -= "foo" /* ERROR "cannot convert.*float64" */
26
27         c *= 1
28         c /= 0 /* ERROR "division by zero" */
29
30         s += "bar"
31         s += 1 /* ERROR "cannot convert.*string" */
32 }
33
34 func _incdecs() {
35         const c = 3.14
36         c /* ERROR "cannot assign" */ ++
37         s := "foo"
38         s /* ERROR "cannot convert" */ --
39         3.14 /* ERROR "cannot assign" */ ++
40         var (
41                 x int
42                 y float32
43                 z complex128
44         )
45         x++
46         y--
47         z++
48 }
49
50 func _sends() {
51         var ch chan int
52         var rch <-chan int
53         var x int
54         x /* ERROR "cannot send" */ <- x
55         rch /* ERROR "cannot send" */ <- x
56         ch /* ERROR "cannot send" */ <- "foo"
57         ch <- x
58 }
59
60 func _selects() {
61         select {}
62         var (
63                 ch chan int
64                 sc chan <- bool
65                 x int
66         )
67         select {
68         case <-ch:
69                 ch <- x
70         case t, ok := <-ch:
71                 x = t
72         case <-sc /* ERROR "cannot receive from send-only channel" */ :
73         }
74         select {
75         default:
76         default /* ERROR "multiple defaults" */ :
77         }
78 }
79
80 func _gos() {
81         go 1 /* ERROR "expected function/method call" */
82         go _gos()
83         var c chan int
84         go close(c)
85         go len(c) // TODO(gri) this should not be legal
86 }
87
88 func _defers() {
89         defer 1 /* ERROR "expected function/method call" */
90         defer _defers()
91         var c chan int
92         defer close(c)
93         defer len(c) // TODO(gri) this should not be legal
94 }
95
96 func _switches() {
97         var x int
98
99         switch x {
100         default:
101         default /* ERROR "multiple defaults" */ :
102         }
103
104         switch {
105         case 1  /* ERROR "cannot convert" */ :
106         }
107
108         switch int32(x) {
109         case 1, 2:
110         case x /* ERROR "cannot compare" */ :
111         }
112
113         switch x {
114         case 1 /* ERROR "overflows int" */ << 100:
115         }
116
117         switch x {
118         case 1:
119         case 1 /* ERROR "duplicate case" */ :
120         case 2, 3, 4:
121         case 1 /* ERROR "duplicate case" */ :
122         }
123
124         // TODO(gri) duplicate 64bit values that don't fit into an int64 are not yet detected
125         switch uint64(x) {
126         case 1<<64-1:
127         case 1<<64-1:
128         }
129 }
130
131 type I interface {
132         m()
133 }
134
135 type I2 interface {
136         m(int)
137 }
138
139 type T struct{}
140 type T1 struct{}
141 type T2 struct{}
142
143 func (T) m() {}
144 func (T2) m(int) {}
145
146 func _typeswitches() {
147         var i int
148         var x interface{}
149
150         switch x.(type) {}
151         switch (x /* ERROR "outside type switch" */ .(type)) {}
152
153         switch x.(type) {
154         default:
155         default /* ERROR "multiple defaults" */ :
156         }
157
158         switch x := x.(type) {}
159
160         switch x := x.(type) {
161         case int:
162                 var y int = x
163         }
164
165         switch x := i /* ERROR "not an interface" */ .(type) {}
166
167         switch t := x.(type) {
168         case nil:
169                 var v bool = t /* ERROR "cannot assign" */
170         case int:
171                 var v int = t
172         case float32, complex64:
173                 var v float32 = t /* ERROR "cannot assign" */
174         default:
175                 var v float32 = t /* ERROR "cannot assign" */
176         }
177
178         var t I
179         switch t.(type) {
180         case T:
181         case T1 /* ERROR "missing method m" */ :
182         case T2 /* ERROR "wrong type for method m" */ :
183         case I2 /* ERROR "wrong type for method m" */ :
184         }
185 }
186
187 func _rangeloops() {
188         var (
189                 x int
190                 a [10]float32
191                 b []string
192                 p *[10]complex128
193                 pp **[10]complex128
194                 s string
195                 m map[int]bool
196                 c chan int
197                 sc chan<- int
198                 rc <-chan int
199         )
200
201         for _ = range x /* ERROR "cannot range over" */ {}
202         for i := range x /* ERROR "cannot range over" */ {}
203
204         for i := range a {
205                 var ii int
206                 ii = i
207         }
208         for i, x := range a {
209                 var ii int
210                 ii = i
211                 var xx float64
212                 xx = x /* ERROR "cannot assign" */
213         }
214         var ii int
215         var xx float32
216         for ii, xx := range a {}
217
218         for i := range b {
219                 var ii int
220                 ii = i
221         }
222         for i, x := range b {
223                 var ii int
224                 ii = i
225                 var xx string
226                 xx = x
227         }
228
229         for i := range s {
230                 var ii int
231                 ii = i
232         }
233         for i, x := range s {
234                 var ii int
235                 ii = i
236                 var xx rune
237                 xx = x
238         }
239
240         for _, x := range p {
241                 var xx complex128
242                 xx = x
243         }
244
245         for _, x := range pp /* ERROR "cannot range over" */ {}
246
247         for k := range m {
248                 var kk int32
249                 kk = k /* ERROR "cannot assign" */
250         }
251         for k, v := range m {
252                 var kk int
253                 kk = k
254                 if v {}
255         }
256
257         for _, _ /* ERROR "only one iteration variable" */ = range c {}
258         for e := range c {
259                 var ee int
260                 ee = e
261         }
262         for _ = range sc /* ERROR "cannot range over send-only channel" */ {}
263         for _ = range rc {}
264
265         // constant strings
266         const cs = "foo"
267         for i, x := range cs {}
268         for i, x := range "" {
269                 var ii int
270                 ii = i
271                 var xx rune
272                 xx = x
273         }
274 }