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.
9 func assignments0() (int, int) {
13 f1 := func() int { return 1 }
14 f2 := func() (int, int) { return 1, 2 }
15 f3 := func() (int, int, int) { return 1, 2, 3 }
18 a, b, c = 1 /* ERROR "cannot assign [1-9]+ values to [1-9]+ variables" */ , 2
19 a, b, c = 1 /* ERROR "cannot assign [1-9]+ values to [1-9]+ variables" */ , 2, 3, 4
22 a = f0 /* ERROR "used as value" */ ()
24 a = f2 /* ERROR "cannot assign [1-9]+ values to [1-9]+ variables" */ ()
26 a, b, c = f2 /* ERROR "cannot assign [1-9]+ values to [1-9]+ variables" */ ()
28 a, b = f3 /* ERROR "cannot assign [1-9]+ values to [1-9]+ variables" */ ()
30 a, b, c = <- /* ERROR "cannot assign [1-9]+ values to [1-9]+ variables" */ ch
32 return /* ERROR "wrong number of return values" */
33 return /* ERROR "wrong number of return values" */ 1
35 return /* ERROR "wrong number of return values" */ 1, 2, 3
39 b, i, f, c, s := false, 1, 1.0, 1i, "foo"
40 b = i /* ERROR "cannot use .* in assignment" */
41 i = f /* ERROR "cannot use .* in assignment" */
42 f = c /* ERROR "cannot use .* in assignment" */
43 c = s /* ERROR "cannot use .* in assignment" */
44 s = b /* ERROR "cannot use .* in assignment" */
46 v0, v1, v2 := 1 /* ERROR "cannot initialize" */ , 2, 3, 4
52 i += "foo" /* ERROR "cannot convert.*int" */
56 f = float32(0)/0 /* ERROR "division by zero" */
57 f -= "foo" /* ERROR "cannot convert.*float64" */
63 s += 1 /* ERROR "cannot convert.*string" */
68 undeclared /* ERROR "undeclared" */ = 991
70 // test cases for issue 5800
72 _ int = nil /* ERROR "untyped nil value" */
73 _ [10]int = nil /* ERROR "untyped nil value" */
75 _ struct{} = nil /* ERROR "untyped nil value" */
77 _ map[int]string = nil
81 // test cases for issue 5500
82 _ = func() (int, bool) {
84 return /* ERROR "wrong number of return values" */ m[0]
87 g := func(int, bool){}
89 g(m[0]) /* ERROR "not enough arguments" */
92 _ = nil /* ERROR "use of untyped nil" */
93 _ = 1 << /* ERROR constant shift overflow */ 1000
99 var m map[string][]bool
107 // assignments to map index expressions are ok
111 m["foo"] = nil /* ERROR cannot assign [1-9]+ values to [1-9]+ variables */ , false
113 _ = append(m["foo"], true)
118 <- /* ERROR cannot assign */ c = 0
119 <-c = 0 /* ERROR cannot assign [1-9]+ values to [1-9]+ variables */ , false
123 x /* ERROR cannot assign */ .(int) = 0
124 x.(int) = 0 /* ERROR cannot assign [1-9]+ values to [1-9]+ variables */ , false
126 assignments2 /* ERROR used as value */ () = nil
127 int /* ERROR not an expression */ = 0
132 _ = &S /* ERROR "cannot take address" */ {}.x
133 _ = &( /* ERROR "cannot take address" */ S{}.x)
135 S /* ERROR "cannot assign" */ {}.x = 0
140 m /* ERROR "cannot assign to struct field" */ ["foo"].x = 0
141 _ = &( /* ERROR "cannot take address" */ m["foo"].x)
142 _ = &m /* ERROR "cannot take address" */ ["foo"].x
146 a, a /* ERROR redeclared */ := 1, 2
148 a, b, b /* ERROR redeclared */ := 1, 2, 3
150 c, c /* ERROR redeclared */, b := 1, 2, 3
152 a, b := /* ERROR no new variables */ 1, 2
155 func shortVarDecls1() {
158 a, b, c /* ERROR "cannot assign" */ , d /* ERROR "cannot assign" */ := 1, "zwei", 3.0, 4
159 var _ int = a // a is of type int
160 var _ string = b // b is of type string
165 c /* ERROR "cannot assign" */ ++
167 s /* ERROR "invalid operation" */ --
168 3.14 /* ERROR "cannot assign" */ ++
183 x <- /* ERROR "cannot send" */ x
184 rch <- /* ERROR "cannot send" */ x
185 ch <- "foo" /* ERROR "cannot use .* in send" */
204 case t, ok := (<-ch):
206 case <-sc /* ERROR "cannot receive from send-only channel" */ :
210 default /* ERROR "multiple defaults" */ :
215 case x /* ERROR send or receive */ :
216 case a /* ERROR send or receive */ := ch:
219 // test for issue 9570: ch2 in second case falsely resolved to
220 // ch2 declared in body of first case
221 ch1 := make(chan int)
222 ch2 := make(chan int)
225 var ch2 /* ERROR ch2 declared but not used */ chan bool
232 go 1 /* ERROR HERE "function must be invoked" */
233 go int /* ERROR "go requires function call, not conversion" */ (0)
237 go len /* ERROR "go discards result" */ (c)
241 defer 1 /* ERROR HERE "function must be invoked" */
242 defer int /* ERROR "defer requires function call, not conversion" */ (0)
246 defer len /* ERROR "defer discards result" */ (c)
252 break /* ERROR "break" */
254 break /* ERROR "break" */
257 break /* ERROR "break" */
312 continue /* ERROR "continue" */
314 continue /* ERROR "continue" */
318 continue /* ERROR "continue" */
323 continue /* ERROR "continue" */
329 continue /* ERROR "continue" */
335 continue /* ERROR "continue" */
338 for i := 0; i < 10; i++ {
378 return 0 /* ERROR no result values expected */
381 func returns1(x float64) (int, *float64) {
383 return /* ERROR wrong number of return values */
384 return "foo" /* ERROR "cannot .* in return statement" */, x /* ERROR "cannot use .* in return statement" */
385 return /* ERROR wrong number of return values */ 0, &x, 1
388 func returns2() (a, b int) {
390 return 1, "foo" /* ERROR cannot use .* in return statement */
391 return /* ERROR wrong number of return values */ 1, 2, 3
395 return /* ERROR a not in scope at return */
399 func returns3() (_ int) {
402 var _ int // blank (_) identifiers never shadow since they are in no scope
415 default /* ERROR "multiple defaults" */ :
419 case 1 /* ERROR "cannot convert" */ :
424 // A tagless switch is equivalent to the bool
425 // constant true, not the identifier 'true'.
427 case "false" /* ERROR "cannot convert" */:
432 case x /* ERROR "cannot compare" */ :
436 case 1 /* ERROR "overflows" */ << 100:
441 case 1 /* ERROR "duplicate case" */ :
442 case ( /* ERROR "duplicate case" */ 1):
444 case 5, 1 /* ERROR "duplicate case" */ :
449 case 1 /* ERROR duplicate case */ <<64 - 1:
451 case 5, 1 /* ERROR duplicate case */ <<64 - 1:
457 case 11/10: // integer division!
458 case 11. /* ERROR duplicate case */ /10:
460 case 5.2, 1.10 /* ERROR duplicate case */ :
466 case 11/10: // integer division!
467 case 11. /* ERROR duplicate case */ /10:
469 case 5.2, 1.10 /* ERROR duplicate case */ :
475 case "foo" /* ERROR duplicate case */ :
476 case "f" /* ERROR duplicate case */ + "oo":
477 case "abc", "def", "ghi":
478 case "jkl", "foo" /* ERROR duplicate case */ :
488 case nil: // no duplicate detection
490 case (*int)(nil): // do duplicate detection
493 case int /* ERROR duplicate case */ (1):
497 case F /* ERROR duplicate case */ (1.0):
500 case S /* ERROR duplicate case */ ("hello"):
508 case [3]int{1, 2, 3}:
509 case [3]int{1, 2, 3}: // no duplicate detection
510 case [ /* ERROR "mismatched types */ 4]int{4, 5, 6}:
519 case c1, c2: // no duplicate detection
524 fallthrough /* ERROR "fallthrough statement out of place" */
529 fallthrough /* ERROR "fallthrough statement out of place" */
534 fallthrough; ; ; // trailing empty statements are ok
539 fallthrough /* ERROR "cannot fallthrough final case in switch" */
545 fallthrough /* ERROR "fallthrough statement out of place" */ ; ; ;
552 fallthrough /* ERROR "fallthrough statement out of place" */
564 L2: L3: L4: fallthrough
576 L6: L7: L8: fallthrough /* ERROR "cannot fallthrough final case in switch" */
584 fallthrough /* ERROR "fallthrough statement out of place" */
589 fallthrough /* ERROR "fallthrough statement out of place" */
590 { /* empty block is not an empty statement */ }; ;
592 fallthrough /* ERROR "cannot fallthrough final case in switch" */
598 fallthrough /* ERROR "fallthrough statement out of place" */
604 // untyped nil is not permitted as switch expression
605 switch nil /* ERROR "use of untyped nil" */ {
606 case 1, 2, "foo": // don't report additional errors here
609 // untyped constants are converted to default types
612 switch 1 /* ERROR "cannot use .* as int value.*\(overflows\)" */ << 63 {
616 case 1.0, 2.0, x /* ERROR "mismatched types int and float64" */ :
622 // untyped bools become of type bool
626 case b /* ERROR "mismatched types B and bool" */ :
629 case b /* ERROR "mismatched types B and bool" */ :
634 switch 9223372036854775808 /* ERROR "cannot use .* as int value.*\(overflows\)" */ {
636 switch 9223372036854775808 /* ERROR "cannot use .* as int value.*\(overflows\)" */ {
637 case 9223372036854775808:
641 case 9223372036854775808 /* ERROR "overflows int" */ :
645 case 9223372036854775808:
650 f := func() (_, _ int) { return }
651 switch f /* ERROR "2-valued f" */ () {
654 switch f /* ERROR "2-valued f" */ () {
658 case f /* ERROR "2-valued f" */ ():
677 func typeswitches() {
682 switch (x /* ERROR "outside type switch" */ .(type)) {}
686 default /* ERROR "multiple defaults" */ :
689 switch x /* ERROR "declared but not used" */ := x.(type) {}
690 switch _ /* ERROR "no new variable on left side of :=" */ := x.(type) {}
692 switch x := x.(type) {
698 switch x := i /* ERROR "not an interface" */ .(type) {}
700 switch t := x.(type) {
702 var v bool = t /* ERROR "cannot use .* in variable declaration" */
707 case float32, complex64:
708 var v float32 = t /* ERROR "cannot use .* in variable declaration" */
711 var v float32 = t /* ERROR "cannot use .* in variable declaration" */
718 case T1 /* ERROR "missing method m" */ :
719 case T2 /* ERROR "wrong type for method m" */ :
720 case I2 /* STRICT "wrong type for method m" */ : // only an error in strict mode (issue 8561)
724 // Test that each case clause uses the correct type of the variable
725 // declared by the type switch (issue 5504).
727 switch y := interface{}(nil).(type) {
729 func() int { return y + 0 }()
731 func() float32 { return y }()
735 // Test correct scope setup.
736 // (no redeclaration errors expected in the type switch)
739 switch t := t; t := t.(type) {
749 // Test correct typeswitch against interface types.
750 type A interface { a() }
751 type B interface { b() }
752 type C interface { a(int) }
755 switch A(nil).(type) {
758 case C /* STRICT "cannot have dynamic type" */: // only an error in strict mode (issue 8561)
762 func typeswitch3(x interface{}) {
766 case int /* ERROR duplicate case */ :
772 case nil /* ERROR duplicate case */ , nil /* ERROR duplicate case */ :
779 case float32, func /* ERROR duplicate case */ (x int):
788 for i := 0; i < 10; i++ {}
789 for i := 0; i < 10; j /* ERROR cannot declare */ := 0 {}
806 for range x /* ERROR "cannot range over" */ {}
807 for _ = range x /* ERROR "cannot range over" */ {}
808 for i := range x /* ERROR "cannot range over" */ {}
816 for i, x := range a {
821 xx = x /* ERROR "cannot use .* in assignment" */
826 for ii, xx = range a {}
835 for i, x := range b {
850 for i, x := range s {
860 for _, x := range p {
866 for range pp /* ERROR "cannot range over" */ {}
867 for _, x := range pp /* ERROR "cannot range over" */ {}
872 kk = k /* ERROR "cannot use .* in assignment" */
875 for k, v := range m {
883 for _, _ /* ERROR "only one iteration variable" */ = range c {}
889 for _ = range sc /* ERROR "cannot range over" */ {}
896 for i, x := range cs { _, _ = i, x }
897 for i, x := range "" {
914 for i /* ERROR cannot use .* in assignment */ = range a {}
915 for i /* ERROR cannot use .* in assignment */ = range &a {}
916 for i /* ERROR cannot use .* in assignment */ = range a[:] {}
921 for i /* ERROR cannot use .* in assignment */ = range s {}
922 for i /* ERROR cannot use .* in assignment */ = range "foo" {}
923 for _, r /* ERROR cannot use .* in assignment */ = range s {}
924 for _, r /* ERROR cannot use .* in assignment */ = range "foo" {}
928 for _ := /* ERROR no new variables */ range "" {}
929 for a, a /* ERROR redeclared */ := range "" { _ = a }
932 for a, a /* ERROR redeclared */ := range []int{1, 2, 3} { _ = a }
935 // Test that despite errors in the range clause,
936 // the loop body is still type-checked (and thus
939 for y /* ERROR declared but not used */ := range "" {
940 _ = "" /* ERROR cannot convert */ + 1
942 for range 1 /* ERROR cannot range over 1 */ {
943 _ = "" /* ERROR cannot convert */ + 1
945 for y := range 1 /* ERROR cannot range over 1 */ {
946 _ = "" /* ERROR cannot convert */ + 1
955 L1 /* ERROR "already declared" */ :
959 L0 /* ERROR "already declared" */ :
971 func expression_statements(ch chan int) {
972 expression_statements(ch)
976 0 /* ERROR "not used" */
977 1 /* ERROR "not used" */ +2
978 cap /* ERROR "not used" */ (ch)
979 println /* ERROR "must be called" */