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 "assignment mismatch: 3 variables but 2 values" */ , 2
19 a, b, c = 1 /* ERROR "assignment mismatch: 3 variables but 4 values" */ , 2, 3, 4
22 a = f0 /* ERROR "used as value" */ ()
24 a = f2 /* ERROR "assignment mismatch: 1 variable but f2 returns 2 values" */ ()
26 a, b, c = f2 /* ERROR "assignment mismatch: 3 variables but f2 returns 2 values" */ ()
28 a, b = f3 /* ERROR "assignment mismatch: 2 variables but f3 returns 3 values" */ ()
30 a, b, c = <- /* ERROR "assignment mismatch: 3 variables but 1 value" */ ch
32 return /* ERROR "not enough return values\n\thave ()\n\twant (int, int)" */
33 return 1 /* ERROR "not enough return values\n\thave (number)\n\twant (int, int)" */
35 return 1, 2, 3 /* ERROR "too many return values\n\thave (number, number, number)\n\twant (int, int)" */
39 b, i, f, c, s := false, 1, 1.0, 1i, "foo"
40 b = i /* ERRORx `cannot use .* in assignment` */
41 i = f /* ERRORx `cannot use .* in assignment` */
42 f = c /* ERRORx `cannot use .* in assignment` */
43 c = s /* ERRORx `cannot use .* in assignment` */
44 s = b /* ERRORx `cannot use .* in assignment` */
46 v0, v1, v2 := 1 /* ERROR "assignment mismatch" */ , 2, 3, 4
52 i /* ERROR "mismatched types int and untyped string" */+= "foo"
56 f = float32(0)/0 /* ERROR "division by zero" */
57 f /* ERROR "mismatched types float64 and untyped string" */-= "foo"
63 s /* ERROR "mismatched types string and untyped int" */+= 1
68 undefined /* ERROR "undefined" */ = 991
70 // test cases for issue 5800
72 _ int = nil /* ERROR "cannot use nil as int value in variable declaration" */
73 _ [10]int = nil /* ERROR "cannot use nil as [10]int value in variable declaration" */
75 _ struct{} = nil /* ERROR "cannot use nil as struct{} value in variable declaration" */
77 _ map[int]string = nil
81 // test cases for issue 5500
82 _ = func() (int, bool) {
84 return m /* ERROR "not enough return values" */ [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 "assignment mismatch: 1 variable but 2 values" */ , false
113 _ = append(m["foo"], true)
118 <- /* ERROR "cannot assign" */ c = 0
119 <-c = 0 /* ERROR "assignment mismatch: 1 variable but 2 values" */ , false
123 x /* ERROR "cannot assign" */ .(int) = 0
124 x.(int) = 0 /* ERROR "assignment mismatch: 1 variable but 2 values" */ , 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 "a repeated on left side of :=" */ := 1, 2
148 a, b, b /* ERROR "b repeated on left side of :=" */ := 1, 2, 3
150 c, c /* ERROR "c repeated on left side of :=" */, 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" /* ERRORx `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 and not used" */ chan bool
232 go 1; /* ERROR "must be function call" */
233 go int /* ERROR "go requires function call, not conversion" */ (0)
234 go ( /* ERROR "expression in go must not be parenthesized" */ gos())
238 go len /* ERROR "go discards result" */ (c)
242 defer 1; /* ERROR "must be function call" */
243 defer int /* ERROR "defer requires function call, not conversion" */ (0)
244 defer ( /* ERROR "expression in defer must not be parenthesized" */ defers())
248 defer len /* ERROR "defer discards result" */ (c)
254 break /* ERROR "break" */
256 break /* ERROR "break" */
259 break /* ERROR "break" */
314 continue /* ERROR "continue" */
316 continue /* ERROR "continue" */
320 continue /* ERROR "continue" */
325 continue /* ERROR "continue" */
331 continue /* ERROR "continue" */
337 continue /* ERROR "continue" */
340 for i := 0; i < 10; i++ {
380 return 0 /* ERROR "too many return values" */
383 func returns1(x float64) (int, *float64) {
385 return /* ERROR "not enough return values" */
386 return "foo" /* ERRORx `cannot .* in return statement` */, x /* ERRORx `cannot use .* in return statement` */
387 return 0, &x, 1 /* ERROR "too many return values" */
390 func returns2() (a, b int) {
392 return 1, "foo" /* ERRORx `cannot use .* in return statement` */
393 return 1, 2, 3 /* ERROR "too many return values" */
397 return /* ERROR "a not in scope at return" */
401 func returns3() (_ int) {
404 var _ int // blank (_) identifiers never shadow since they are in no scope
417 default /* ERROR "multiple defaults" */ :
421 case 1 /* ERROR "cannot convert" */ :
426 // A tagless switch is equivalent to the bool
427 // constant true, not the identifier 'true'.
429 case "false" /* ERROR "cannot convert" */:
434 case x /* ERROR "invalid case x in switch on int32(x) (mismatched types int and int32)" */ :
438 case 1 /* ERROR "overflows" */ << 100:
443 case 1 /* ERROR "duplicate case" */ :
444 case ( /* ERROR "duplicate case" */ 1):
446 case 5, 1 /* ERROR "duplicate case" */ :
451 case 1 /* ERROR "duplicate case" */ <<64 - 1:
453 case 5, 1 /* ERROR "duplicate case" */ <<64 - 1:
459 case 11/10: // integer division!
460 case 11. /* ERROR "duplicate case" */ /10:
462 case 5.2, 1.10 /* ERROR "duplicate case" */ :
468 case 11/10: // integer division!
469 case 11. /* ERROR "duplicate case" */ /10:
471 case 5.2, 1.10 /* ERROR "duplicate case" */ :
477 case "foo" /* ERROR "duplicate case" */ :
478 case "f" /* ERROR "duplicate case" */ + "oo":
479 case "abc", "def", "ghi":
480 case "jkl", "foo" /* ERROR "duplicate case" */ :
490 case nil: // no duplicate detection
492 case (*int)(nil): // do duplicate detection
495 case int /* ERROR "duplicate case" */ (1):
499 case F /* ERROR "duplicate case" */ (1.0):
502 case S /* ERROR "duplicate case" */ ("hello"):
510 case [3]int{1, 2, 3}:
511 case [3]int{1, 2, 3}: // no duplicate detection
512 case [ /* ERROR "mismatched types" */ 4]int{4, 5, 6}:
521 case c1, c2: // no duplicate detection
526 fallthrough /* ERROR "fallthrough statement out of place" */
531 fallthrough /* ERROR "fallthrough statement out of place" */
536 fallthrough; ; ; // trailing empty statements are ok
541 fallthrough /* ERROR "cannot fallthrough final case in switch" */
547 fallthrough /* ERROR "cannot fallthrough in type switch" */ ; ; ;
554 fallthrough /* ERROR "fallthrough statement out of place" */
566 L2: L3: L4: fallthrough
578 L6: L7: L8: fallthrough /* ERROR "cannot fallthrough final case in switch" */
586 fallthrough /* ERROR "fallthrough statement out of place" */
591 fallthrough /* ERROR "fallthrough statement out of place" */
592 { /* empty block is not an empty statement */ }; ;
594 fallthrough /* ERROR "cannot fallthrough final case in switch" */
600 fallthrough /* ERROR "fallthrough statement out of place" */
606 // untyped nil is not permitted as switch expression
607 switch nil /* ERROR "use of untyped nil" */ {
608 case 1, 2, "foo": // don't report additional errors here
611 // untyped constants are converted to default types
614 switch 1 /* ERRORx `cannot use .* as int value.*\(overflows\)` */ << 63 {
618 case 1.0, 2.0, x /* ERROR "mismatched types int and float64" */ :
624 // untyped bools become of type bool
628 case b /* ERROR "mismatched types B and bool" */ :
631 case b /* ERROR "mismatched types B and bool" */ :
636 switch 9223372036854775808 /* ERRORx `cannot use .* as int value.*\(overflows\)` */ {
638 switch 9223372036854775808 /* ERRORx `cannot use .* as int value.*\(overflows\)` */ {
639 case 9223372036854775808:
643 case 9223372036854775808 /* ERROR "overflows int" */ :
647 case 9223372036854775808:
652 f := func() (_, _ int) { return }
653 switch f /* ERROR "multiple-value f" */ () {
656 switch f /* ERROR "multiple-value f" */ () {
660 case f /* ERROR "multiple-value f" */ ():
679 func typeswitches() {
684 switch (x /* ERROR "outside type switch" */ .(type)) {}
688 default /* ERROR "multiple defaults" */ :
691 switch x /* ERROR "declared and not used" */ := x.(type) {}
692 switch _ /* ERROR "no new variable on left side of :=" */ := x.(type) {}
694 switch x := x.(type) {
700 switch x /* ERROR "x declared and not used" */ := i /* ERROR "not an interface" */ .(type) {}
702 switch t := x.(type) {
704 var v bool = t /* ERRORx `cannot use .* in variable declaration` */
709 case float32, complex64:
710 var v float32 = t /* ERRORx `cannot use .* in variable declaration` */
713 var v float32 = t /* ERRORx `cannot use .* in variable declaration` */
720 case T1 /* ERROR "missing method m" */ :
721 case T2 /* ERROR "wrong type for method m" */ :
722 case I2 /* STRICT "wrong type for method m" */ : // only an error in strict mode (issue 8561)
729 switch v /* ERROR "v (variable of type int) is not an interface" */ .(type) {
732 println(x / 0 /* ERROR "invalid operation: division by zero" */)
733 case 1 /* ERROR "1 is not a type" */:
738 // Test that each case clause uses the correct type of the variable
739 // declared by the type switch (issue 5504).
741 switch y := interface{}(nil).(type) {
743 func() int { return y + 0 }()
745 func() float32 { return y }()
749 // Test correct scope setup.
750 // (no redeclaration errors expected in the type switch)
753 switch t := t; t := t.(type) {
763 // Test correct typeswitch against interface types.
764 type A interface { a() }
765 type B interface { b() }
766 type C interface { a(int) }
769 switch A(nil).(type) {
772 case C /* STRICT "cannot have dynamic type" */: // only an error in strict mode (issue 8561)
776 func typeswitch3(x interface{}) {
780 case int /* ERROR "duplicate case" */ :
786 case nil /* ERROR "duplicate case" */ , nil /* ERROR "duplicate case" */ :
793 case float32, func /* ERROR "duplicate case" */ (x int):
802 for i := 0; i < 10; i++ {}
803 for i := 0; i < 10; j /* ERROR "cannot declare" */ := 0 {}
820 for range x /* ERROR "cannot range over" */ {}
821 for _ = range x /* ERROR "cannot range over" */ {}
822 for i := range x /* ERROR "cannot range over" */ {}
830 for i, x := range a {
835 xx = x /* ERRORx `cannot use .* in assignment` */
840 for ii, xx = range a {}
849 for i, x := range b {
864 for i, x := range s {
874 for _, x := range p {
880 for range pp /* ERROR "cannot range over" */ {}
881 for _, x := range pp /* ERROR "cannot range over" */ {}
886 kk = k /* ERRORx `cannot use .* in assignment` */
889 for k, v := range m {
897 for _, _ /* ERROR "only one iteration variable" */ = range c {}
903 for _ = range sc /* ERROR "cannot range over" */ {}
910 for i, x := range cs { _, _ = i, x }
911 for i, x := range "" {
928 for i /* ERRORx `cannot use .* in assignment` */ = range a {}
929 for i /* ERRORx `cannot use .* in assignment` */ = range &a {}
930 for i /* ERRORx `cannot use .* in assignment` */ = range a[:] {}
935 for i /* ERRORx `cannot use .* in assignment` */ = range s {}
936 for i /* ERRORx `cannot use .* in assignment` */ = range "foo" {}
937 for _, r /* ERRORx `cannot use .* in assignment` */ = range s {}
938 for _, r /* ERRORx `cannot use .* in assignment` */ = range "foo" {}
942 for _ := /* ERROR "no new variables" */ range "" {}
943 for a, a /* ERROR "redeclared" */ := range "" { _ = a }
946 for a, a /* ERROR "redeclared" */ := range []int{1, 2, 3} { _ = a }
949 // Test that despite errors in the range clause,
950 // the loop body is still type-checked (and thus
953 for y /* ERROR "declared and not used" */ := range "" {
954 _ = "" /* ERROR "mismatched types untyped string and untyped int" */ + 1
956 for range 1 /* ERROR "cannot range over 1" */ {
957 _ = "" /* ERROR "mismatched types untyped string and untyped int" */ + 1
959 for y := range 1 /* ERROR "cannot range over 1" */ {
960 _ = "" /* ERROR "mismatched types untyped string and untyped int" */ + 1
969 L1 /* ERROR "already declared" */ :
973 L0 /* ERROR "already declared" */ :
985 func expression_statements(ch chan int) {
986 expression_statements(ch)
990 0 /* ERROR "not used" */
991 1 /* ERROR "not used" */ +2
992 cap /* ERROR "not used" */ (ch)
993 println /* ERROR "must be called" */