1
2
3
4
5 package expr3
6
7 import "time"
8
9 func indexes() {
10 _ = 1 [0]
11 _ = indexes [0]
12 _ = ( 12 + 3)[1:2]
13
14 var a [10]int
15 _ = a[true ]
16 _ = a["foo" ]
17 _ = a[1.1 ]
18 _ = a[1.0]
19 _ = a[- 1]
20 _ = a[- 1 :]
21 _ = a[: - 1]
22 _ = a[: : ]
23 _ = a[0: : ]
24 _ = a[0: :10]
25 _ = a[:10:10]
26
27 var a0 int
28 a0 = a[0]
29 _ = a0
30 var a1 int32
31 a1 = a [1]
32 _ = a1
33
34 _ = a[9]
35 _ = a[10 ]
36 _ = a[1 <<100]
37 _ = a[1<< 1000]
38 _ = a[10:]
39 _ = a[:10]
40 _ = a[10:10]
41 _ = a[11 :]
42 _ = a[: 11 ]
43 _ = a[: 1 <<100]
44 _ = a[:10:10]
45 _ = a[:11 :10]
46 _ = a[:10:11 ]
47 _ = a[10:0 :10]
48 _ = a[0:10:0 ]
49 _ = a[10:0 :0]
50 _ = &a [:10]
51
52 pa := &a
53 _ = pa[9]
54 _ = pa[10 ]
55 _ = pa[1 <<100]
56 _ = pa[10:]
57 _ = pa[:10]
58 _ = pa[10:10]
59 _ = pa[11 :]
60 _ = pa[: 11 ]
61 _ = pa[: 1 <<100]
62 _ = pa[:10:10]
63 _ = pa[:11 :10]
64 _ = pa[:10:11 ]
65 _ = pa[10:0 :10]
66 _ = pa[0:10:0 ]
67 _ = pa[10:0 :0]
68 _ = &pa [:10]
69
70 var b [0]int
71 _ = b[0 ]
72 _ = b[:]
73 _ = b[0:]
74 _ = b[:0]
75 _ = b[0:0]
76 _ = b[0:0:0]
77 _ = b[1 :0:0]
78
79 var s []int
80 _ = s[- 1]
81 _ = s[- 1 :]
82 _ = s[: - 1]
83 _ = s[0]
84 _ = s[1:2]
85 _ = s[2:1 ]
86 _ = s[2:]
87 _ = s[: 1 <<100]
88 _ = s[1 <<100 :]
89 _ = s[1 <<100 : 1 <<100]
90 _ = s[: : ]
91 _ = s[:10:10]
92 _ = s[10:0 :10]
93 _ = s[0:10:0 ]
94 _ = s[10:0 :0]
95 _ = &s [:10]
96
97 var m map[string]int
98 _ = m[0 ]
99 _ = m ["foo" : "bar"]
100 _ = m["foo"]
101
102 type mybool bool
103 var ok mybool
104 _, ok = m["bar"]
105 _ = ok
106 _ = m[0 ] + "foo"
107
108 var t string
109 _ = t[- 1]
110 _ = t[- 1 :]
111 _ = t[: - 1]
112 _ = t[1:2:3 ]
113 _ = "foo"[1:2:3 ]
114 var t0 byte
115 t0 = t[0]
116 _ = t0
117 var t1 rune
118 t1 = t [2]
119 _ = t1
120 _ = ("foo" + "bar")[5]
121 _ = ("foo" + "bar")[6 ]
122
123 const c = "foo"
124 _ = c[- 1]
125 _ = c[- 1 :]
126 _ = c[: - 1]
127 var c0 byte
128 c0 = c[0]
129 _ = c0
130 var c2 float32
131 c2 = c [2]
132 _ = c[3 ]
133 _ = ""[0 ]
134 _ = c2
135
136 _ = s[1<<30]
137
138
139 type mystring string
140 var ss string
141 var ms mystring
142 var i, j int
143 ss = "foo"[1:2]
144 ss = "foo"[i:j]
145 ms = "foo" [1:2]
146 ms = "foo" [i:j]
147 _, _ = ss, ms
148 }
149
150 type T struct {
151 x int
152 y func()
153 }
154
155 func (*T) m() {}
156
157 func method_expressions() {
158 _ = T.a
159 _ = T.x
160 _ = T.m
161 _ = (*T).m
162
163 var f func(*T) = T.m
164 var g func(*T) = (*T).m
165 _, _ = f, g
166
167 _ = T.y
168 _ = (*T).y
169 }
170
171 func struct_literals() {
172 type T0 struct {
173 a, b, c int
174 }
175
176 type T1 struct {
177 T0
178 a, b int
179 u float64
180 s string
181 }
182
183
184 _ = T1{}
185 _ = T1{a: 0, 1 }
186 _ = T1{aa : 0}
187 _ = T1{1 : 0}
188 _ = T1{a: 0, s: "foo", u: 0, a : 10}
189 _ = T1{a: "foo" }
190 _ = T1{c : 0}
191 _ = T1{T0: { }}
192 _ = T1{T0: T0{}}
193 _ = T1{T0 .a: 0}
194
195
196 _ = T0{1, 2, 3}
197 _ = T0{1, b : 2, 3}
198 _ = T0{1, 2}
199 _ = T0{1, 2, 3, 4 }
200 _ = T0{1, "foo" , 3.4 }
201
202
203 type P *struct{
204 x int
205 }
206 _ = P {}
207
208
209 _ = time.Time{}
210 _ = time.Time{sec : 0}
211 _ = time.Time{
212 0 ,
213 0 ,
214 nil ,
215 }
216 }
217
218 func array_literals() {
219 type A0 [0]int
220 _ = A0{}
221 _ = A0{0 }
222 _ = A0{0 : 0}
223
224 type A1 [10]int
225 _ = A1{}
226 _ = A1{0, 1, 2}
227 _ = A1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
228 _ = A1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
229 _ = A1{- 1: 0}
230 _ = A1{8: 8, 9}
231 _ = A1{8: 8, 9, 10 }
232 _ = A1{0, 1, 2, 0 : 0, 3: 3, 4}
233 _ = A1{5: 5, 6, 7, 3: 3, 4}
234 _ = A1{5: 5, 6, 7, 3: 3, 4, 5 }
235 _ = A1{10 : 10, 10 : 10}
236 _ = A1{5: 5, 6, 7, 3: 3, 1 <<100: 4, 5 }
237 _ = A1{5: 5, 6, 7, 4: 4, 1 <<100: 4}
238 _ = A1{2.0}
239 _ = A1{2.1 }
240 _ = A1{"foo" }
241
242
243 i := 1
244 const f = 2.1
245 const s = "foo"
246 _ = A1{i : 0}
247 _ = A1{f : 0}
248 _ = A1{s : 0}
249
250 a0 := [...]int{}
251 assert(len(a0) == 0)
252
253 a1 := [...]int{0, 1, 2}
254 assert(len(a1) == 3)
255 var a13 [3]int
256 var a14 [4]int
257 a13 = a1
258 a14 = a1
259 _, _ = a13, a14
260
261 a2 := [...]int{- 1: 0}
262 _ = a2
263
264 a3 := [...]int{0, 1, 2, 0 : 0, 3: 3, 4}
265 assert(len(a3) == 5)
266
267 a4 := [...]complex128{0, 1, 2, 1<<10-2: -1i, 1i, 400: 10, 12, 14}
268 assert(len(a4) == 1024)
269
270
271 type T []int
272 _ = [10]T{T{}, {}, 5: T{1, 2, 3}, 7: {1, 2, 3}}
273 a6 := [...]T{T{}, {}, 5: T{1, 2, 3}, 7: {1, 2, 3}}
274 assert(len(a6) == 8)
275
276
277 _ = [10][10]T{{}, [10]T{{}}, {{1, 2, 3}}}
278
279
280 type Point struct { x, y float32 }
281 _ = [...]Point{Point{1.5, -3.5}, Point{0, 0}}
282 _ = [...]Point{{1.5, -3.5}, {0, 0}}
283 _ = [][]int{[]int{1, 2, 3}, []int{4, 5}}
284 _ = [][]int{{1, 2, 3}, {4, 5}}
285 _ = [...]*Point{&Point{1.5, -3.5}, &Point{0, 0}}
286 _ = [...]*Point{{1.5, -3.5}, {0, 0}}
287 }
288
289 func slice_literals() {
290 type S0 []int
291 _ = S0{}
292 _ = S0{0, 1, 2}
293 _ = S0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
294 _ = S0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
295 _ = S0{- 1: 0}
296 _ = S0{8: 8, 9}
297 _ = S0{8: 8, 9, 10}
298 _ = S0{0, 1, 2, 0 : 0, 3: 3, 4}
299 _ = S0{5: 5, 6, 7, 3: 3, 4}
300 _ = S0{5: 5, 6, 7, 3: 3, 4, 5 }
301 _ = S0{10: 10, 10 : 10}
302 _ = S0{5: 5, 6, 7, 3: 3, 1 <<100: 4, 5 }
303 _ = S0{5: 5, 6, 7, 4: 4, 1 <<100: 4}
304 _ = S0{2.0}
305 _ = S0{2.1 }
306 _ = S0{"foo" }
307
308
309 const index1 = 1
310 _ = S0{index1: 1}
311 _ = S0{index2: 2}
312 _ = S0{index3 : 3}
313
314
315 i := 1
316 const f = 2.1
317 const s = "foo"
318 _ = S0{i : 0}
319 _ = S0{f : 0}
320 _ = S0{s : 0}
321
322
323 type T []int
324 _ = []T{T{}, {}, 5: T{1, 2, 3}, 7: {1, 2, 3}}
325 _ = [][]int{{1, 2, 3}, {4, 5}}
326
327
328 _ = [][]T{{}, []T{{}}, {{1, 2, 3}}}
329
330
331 type T0 *struct { s string }
332 _ = []T0{{}}
333 _ = []T0{{"foo"}}
334
335 type T1 *struct{ int }
336 _ = []T1{}
337 _ = []T1{{0}, {1}, {2}}
338
339 type T2 T1
340 _ = []T2{}
341 _ = []T2{{0}, {1}, {2}}
342
343 _ = map[T0]T2{}
344 _ = map[T0]T2{{}: {}}
345 }
346
347 const index2 int = 2
348
349 type N int
350 func (N) f() {}
351
352 func map_literals() {
353 type M0 map[string]int
354 type M1 map[bool]int
355 type M2 map[*int]int
356
357 _ = M0{}
358 _ = M0{1 }
359 _ = M0{1 : 2}
360 _ = M0{"foo": "bar" }
361 _ = M0{"foo": 1, "bar": 2, "foo" : 3 }
362
363 _ = map[interface{}]int{2: 1, 2 : 1}
364 _ = map[interface{}]int{int(2): 1, int16(2): 1}
365 _ = map[interface{}]int{int16(2): 1, int16 (2): 1}
366
367 type S string
368
369 _ = map[interface{}]int{"a": 1, "a" : 1}
370 _ = map[interface{}]int{"a": 1, S("a"): 1}
371 _ = map[interface{}]int{S("a"): 1, S ("a"): 1}
372 _ = map[interface{}]int{1.0: 1, 1.0 : 1}
373 _ = map[interface{}]int{int64(-1): 1, int64 (-1) : 1}
374 _ = map[interface{}]int{^uint64(0): 1, ^ uint64(0): 1}
375 _ = map[interface{}]int{complex(1,2): 1, complex (1,2) : 1}
376
377 type I interface {
378 f()
379 }
380
381 _ = map[I]int{N(0): 1, N(2): 1}
382 _ = map[I]int{N(2): 1, N (2): 1}
383
384
385 key1 := "foo"
386 _ = M0{key1: 1}
387 _ = M0{key2: 2}
388 _ = M0{key3 : 2}
389
390 var value int
391 _ = M1{true: 1, false: 0}
392 _ = M2{nil: 0, &value: 1}
393
394
395 type T [2]int
396 _ = map[int]T{0: T{3, 4}, 1: {5, 6}}
397
398
399 _ = map[int][]T{0: {}, 1: {{}, T{1, 2}}}
400
401
402 _ = map[T]int{T{3, 4}: 0, {5, 6}: 1}
403
404
405 _ = map[[2]T]int{{}: 0, {{}}: 1, [2]T{{}}: 2, {T{1, 2}}: 3}
406
407
408 _ = map[T]T{{}: {}, {1, 2}: T{3, 4}, T{4, 5}: {}}
409 _ = map[T]M0{{} : {}, T{1, 2}: M0{"foo": 0}, {1, 3}: {"foo": 1}}
410
411
412 _ = map[[2]T][]T{{}: {}, {{}}: {{}, T{1, 2}}, [2]T{{}}: nil, {T{1, 2}}: {{}, {}}}
413
414
415 type Point struct { x, y float32 }
416 _ = map[string]Point{"orig": {0, 0}}
417 _ = map[*Point]string{{0, 0}: "orig"}
418
419
420 type T0 *struct{ s string }
421 type T1 *struct{ int }
422 type T2 T1
423
424 _ = map[T0]T2{}
425 _ = map[T0]T2{{}: {}}
426 }
427
428 var key2 string = "bar"
429
430 type I interface {
431 m()
432 }
433
434 type I2 interface {
435 m(int)
436 }
437
438 type T1 struct{}
439 type T2 struct{}
440
441 func (T2) m(int) {}
442
443 type mybool bool
444
445 func type_asserts() {
446 var x int
447 _ = x .(int)
448
449 var e interface{}
450 var ok bool
451 x, ok = e.(int)
452 _ = ok
453
454
455 var myok mybool
456 _, myok = e.(int)
457 _ = myok
458
459 var t I
460 _ = t .(type)
461 _ = t .(T)
462 _ = t.(*T)
463 _ = t .(T1)
464 _ = t .(T2)
465 _ = t .(I2)
466
467
468 _ = e.(I2)
469 }
470
471 func f0() {}
472 func f1(x int) {}
473 func f2(u float32, s string) {}
474 func fs(s []byte) {}
475 func fv(x ...int) {}
476 func fi(x ... interface{}) {}
477 func (T) fm(x ...int)
478
479 func g0() {}
480 func g1() int { return 0}
481 func g2() (u float32, s string) { return }
482 func gs() []byte { return nil }
483
484 func _calls() {
485 var x int
486 var y float32
487 var s []int
488
489 f0()
490 _ = f0 ()
491 f0(g0 )
492
493 f1(0)
494 f1(x)
495 f1(10.0)
496 f1()
497 f1(x, y )
498 f1(s )
499 f1(x ... )
500 f1(g0 ())
501 f1(g1())
502 f1(g2 ())
503
504 f2()
505 f2(3.14)
506 f2(3.14, "foo")
507 f2(x , "foo")
508 f2(g0 ())
509 f2(g1())
510 f2(g2())
511
512 fs()
513 fs(g0 ())
514 fs(g1 ())
515 fs(g2 ())
516 fs(gs())
517
518 fv()
519 fv(1, 2.0, x)
520 fv(s )
521 fv(s...)
522 fv(x ...)
523 fv(1, s ...)
524 fv(gs ())
525 fv(gs ()...)
526
527 var t T
528 t.fm()
529 t.fm(1, 2.0, x)
530 t.fm(s )
531 t.fm(g1())
532 t.fm(1, s ...)
533 t.fm(gs ())
534 t.fm(gs ()...)
535
536 T.fm(t, )
537 T.fm(t, 1, 2.0, x)
538 T.fm(t, s )
539 T.fm(t, g1())
540 T.fm(t, 1, s ...)
541 T.fm(t, gs ())
542 T.fm(t, gs ()...)
543
544 var i interface{ fm(x ...int) } = t
545 i.fm()
546 i.fm(1, 2.0, x)
547 i.fm(s )
548 i.fm(g1())
549 i.fm(1, s ...)
550 i.fm(gs ())
551 i.fm(gs ()...)
552
553 fi()
554 fi(1, 2.0, x, 3.14, "foo")
555 fi(g2())
556 fi(0, g2)
557 fi(0, g2 ())
558 }
559
560 func issue6344() {
561 type T []interface{}
562 var x T
563 fi(x...)
564 }
565
View as plain text