Source file src/internal/types/testdata/check/stmt0.go

     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 assignments0() (int, int) {
    10  	var a, b, c int
    11  	var ch chan int
    12  	f0 := func() {}
    13  	f1 := func() int { return 1 }
    14  	f2 := func() (int, int) { return 1, 2 }
    15  	f3 := func() (int, int, int) { return 1, 2, 3 }
    16  
    17  	a, b, c = 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
    20  	_, _, _ = a, b, c
    21  
    22  	a = f0 /* ERROR "used as value" */ ()
    23  	a = f1()
    24  	a = f2 /* ERROR "assignment mismatch: 1 variable but f2 returns 2 values" */ ()
    25  	a, b = f2()
    26  	a, b, c = f2 /* ERROR "assignment mismatch: 3 variables but f2 returns 2 values" */ ()
    27  	a, b, c = f3()
    28  	a, b = f3 /* ERROR "assignment mismatch: 2 variables but f3 returns 3 values" */ ()
    29  
    30  	a, b, c = <- /* ERROR "assignment mismatch: 3 variables but 1 value" */ ch
    31  
    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)" */
    34  	return 1, 2
    35  	return 1, 2, 3 /* ERROR "too many return values\n\thave (number, number, number)\n\twant (int, int)" */
    36  }
    37  
    38  func assignments1() {
    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` */
    45  
    46  	v0, v1, v2 := 1 /* ERROR "assignment mismatch" */ , 2, 3, 4
    47  	_, _, _ = v0, v1, v2
    48  
    49  	b = true
    50  
    51  	i += 1
    52  	i /* ERROR "mismatched types int and untyped string" */+= "foo"
    53  
    54  	f -= 1
    55  	f /= 0
    56  	f = float32(0)/0 /* ERROR "division by zero" */
    57  	f /* ERROR "mismatched types float64 and untyped string" */-= "foo"
    58  
    59  	c *= 1
    60  	c /= 0
    61  
    62  	s += "bar"
    63  	s /* ERROR "mismatched types string and untyped int" */+= 1
    64  
    65  	var u64 uint64
    66  	u64 += 1<<u64
    67  
    68  	undefined /* ERROR "undefined" */ = 991
    69  
    70  	// test cases for issue 5800
    71  	var (
    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" */
    74  		_ []byte = nil
    75  		_ struct{} = nil /* ERROR "cannot use nil as struct{} value in variable declaration" */
    76  		_ func() = nil
    77  		_ map[int]string = nil
    78  		_ chan int = nil
    79  	)
    80  
    81  	// test cases for issue 5500
    82  	_ = func() (int, bool) {
    83  		var m map[int]int
    84  		return m /* ERROR "not enough return values" */ [0]
    85  	}
    86  
    87  	g := func(int, bool){}
    88  	var m map[int]int
    89  	g(m[0]) /* ERROR "not enough arguments" */
    90  
    91  	// assignments to _
    92  	_ = nil /* ERROR "use of untyped nil" */
    93  	_ = 1  << /* ERROR "constant shift overflow" */ 1000
    94  	(_) = 0
    95  }
    96  
    97  func assignments2() {
    98  	type mybool bool
    99  	var m map[string][]bool
   100  	var s []bool
   101  	var b bool
   102  	var d mybool
   103  	_ = s
   104  	_ = b
   105  	_ = d
   106  
   107  	// assignments to map index expressions are ok
   108  	s, b = m["foo"]
   109  	_, d = m["bar"]
   110  	m["foo"] = nil
   111  	m["foo"] = nil /* ERROR "assignment mismatch: 1 variable but 2 values" */ , false
   112  	_ = append(m["foo"])
   113  	_ = append(m["foo"], true)
   114  
   115  	var c chan int
   116  	_, b = <-c
   117  	_, d = <-c
   118  	<- /* ERROR "cannot assign" */ c = 0
   119  	<-c = 0 /* ERROR "assignment mismatch: 1 variable but 2 values" */ , false
   120  
   121  	var x interface{}
   122  	_, b = x.(int)
   123  	x /* ERROR "cannot assign" */ .(int) = 0
   124  	x.(int) = 0 /* ERROR "assignment mismatch: 1 variable but 2 values" */ , false
   125  
   126  	assignments2 /* ERROR "used as value" */ () = nil
   127  	int /* ERROR "not an expression" */ = 0
   128  }
   129  
   130  func issue6487() {
   131  	type S struct{x int}
   132  	_ = &S /* ERROR "cannot take address" */ {}.x
   133  	_ = &( /* ERROR "cannot take address" */ S{}.x)
   134  	_ = (&S{}).x
   135  	S /* ERROR "cannot assign" */ {}.x = 0
   136  	(&S{}).x = 0
   137  
   138  	type M map[string]S
   139  	var m M
   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
   143  }
   144  
   145  func issue6766a() {
   146  	a, a /* ERROR "a repeated on left side of :=" */ := 1, 2
   147  	_ = a
   148  	a, b, b /* ERROR "b repeated on left side of :=" */ := 1, 2, 3
   149  	_ = b
   150  	c, c /* ERROR "c repeated on left side of :=" */, b := 1, 2, 3
   151  	_ = c
   152  	a, b := /* ERROR "no new variables" */ 1, 2
   153  }
   154  
   155  func shortVarDecls1() {
   156  	const c = 0
   157  	type d int
   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
   161  }
   162  
   163  func incdecs() {
   164  	const c = 3.14
   165  	c /* ERROR "cannot assign" */ ++
   166  	s := "foo"
   167  	s /* ERROR "invalid operation" */ --
   168  	3.14 /* ERROR "cannot assign" */ ++
   169  	var (
   170  		x int
   171  		y float32
   172  		z complex128
   173  	)
   174  	x++
   175  	y--
   176  	z++
   177  }
   178  
   179  func sends() {
   180  	var ch chan int
   181  	var rch <-chan int
   182  	var x int
   183  	x <- /* ERROR "cannot send" */ x
   184  	rch <- /* ERROR "cannot send" */ x
   185  	ch <- "foo" /* ERRORx `cannot use .* in send` */
   186  	ch <- x
   187  }
   188  
   189  func selects() {
   190  	select {}
   191  	var (
   192  		ch chan int
   193  		sc chan <- bool
   194  	)
   195  	select {
   196  	case <-ch:
   197  	case (<-ch):
   198  	case t := <-ch:
   199  		_ = t
   200  	case t := (<-ch):
   201  		_ = t
   202  	case t, ok := <-ch:
   203  		_, _ = t, ok
   204  	case t, ok := (<-ch):
   205  		_, _ = t, ok
   206  	case <-sc /* ERROR "cannot receive from send-only channel" */ :
   207  	}
   208  	select {
   209  	default:
   210  	default /* ERROR "multiple defaults" */ :
   211  	}
   212  	select {
   213  	case a, b := <-ch:
   214  		_, b = a, b
   215  	case x /* ERROR "send or receive" */ :
   216  	case a /* ERROR "send or receive" */ := ch:
   217  	}
   218  
   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)
   223  	select {
   224  	case <-ch1:
   225  		var ch2 /* ERROR "declared and not used: ch2" */ chan bool
   226  	case i := <-ch2:
   227  		print(i + 1)
   228  	}
   229  }
   230  
   231  func gos() {
   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())
   235  	go gos()
   236  	var c chan int
   237  	go close(c)
   238  	go len /* ERROR "go discards result" */ (c)
   239  }
   240  
   241  func defers() {
   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())
   245  	defer defers()
   246  	var c chan int
   247  	defer close(c)
   248  	defer len /* ERROR "defer discards result" */ (c)
   249  }
   250  
   251  func breaks() {
   252  	var x, y int
   253  
   254  	break /* ERROR "break" */
   255  	{
   256  		break /* ERROR "break" */
   257  	}
   258  	if x < y {
   259  		break /* ERROR "break" */
   260  	}
   261  
   262  	switch x {
   263  	case 0:
   264  		break
   265  	case 1:
   266  		if x == y {
   267  			break
   268  		}
   269  	default:
   270  		break
   271  		break
   272  	}
   273  
   274  	var z interface{}
   275  	switch z.(type) {
   276  	case int:
   277  		break
   278  	}
   279  
   280  	for {
   281  		break
   282  	}
   283  
   284  	var a []int
   285  	for _ = range a {
   286  		break
   287  	}
   288  
   289  	for {
   290  		if x == y {
   291  			break
   292  		}
   293  	}
   294  
   295  	var ch chan int
   296  	select {
   297  	case <-ch:
   298  		break
   299  	}
   300  
   301  	select {
   302  	case <-ch:
   303  		if x == y {
   304  			break
   305  		}
   306  	default:
   307  		break
   308  	}
   309  }
   310  
   311  func continues() {
   312  	var x, y int
   313  
   314  	continue /* ERROR "continue" */
   315  	{
   316  		continue /* ERROR "continue" */
   317  	}
   318  
   319  	if x < y {
   320  		continue /* ERROR "continue" */
   321  	}
   322  
   323  	switch x {
   324  	case 0:
   325  		continue /* ERROR "continue" */
   326  	}
   327  
   328  	var z interface{}
   329  	switch z.(type) {
   330  	case int:
   331  		continue /* ERROR "continue" */
   332  	}
   333  
   334  	var ch chan int
   335  	select {
   336  	case <-ch:
   337  		continue /* ERROR "continue" */
   338  	}
   339  
   340  	for i := 0; i < 10; i++ {
   341  		continue
   342  		if x < y {
   343  			continue
   344  			break
   345  		}
   346  		switch x {
   347  		case y:
   348  			continue
   349  		default:
   350  			break
   351  		}
   352  		select {
   353  		case <-ch:
   354  			continue
   355  		}
   356  	}
   357  
   358  	var a []int
   359  	for _ = range a {
   360  		continue
   361  		if x < y {
   362  			continue
   363  			break
   364  		}
   365  		switch x {
   366  		case y:
   367  			continue
   368  		default:
   369  			break
   370  		}
   371  		select {
   372  		case <-ch:
   373  			continue
   374  		}
   375  	}
   376  }
   377  
   378  func returns0() {
   379  	return
   380  	return 0 /* ERROR "too many return values" */
   381  }
   382  
   383  func returns1(x float64) (int, *float64) {
   384  	return 0, &x
   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" */
   388  }
   389  
   390  func returns2() (a, b int) {
   391  	return
   392  	return 1, "foo" /* ERRORx `cannot use .* in return statement` */
   393  	return 1, 2, 3 /* ERROR "too many return values" */
   394  	{
   395  		type a int
   396  		return 1, 2
   397  		return /* ERROR "result parameter a not in scope at return" */
   398  	}
   399  }
   400  
   401  func returns3() (_ int) {
   402  	return
   403  	{
   404  		var _ int // blank (_) identifiers never shadow since they are in no scope
   405  		return
   406  	}
   407  }
   408  
   409  func switches0() {
   410  	var x int
   411  
   412  	switch x {
   413  	}
   414  
   415  	switch x {
   416  	default:
   417  	default /* ERROR "multiple defaults" */ :
   418  	}
   419  
   420  	switch {
   421  	case 1  /* ERROR "cannot convert" */ :
   422  	}
   423  
   424  	true := "false"
   425  	_ = true
   426  	// A tagless switch is equivalent to the bool
   427          // constant true, not the identifier 'true'.
   428  	switch {
   429  	case "false" /* ERROR "cannot convert" */:
   430  	}
   431  
   432  	switch int32(x) {
   433  	case 1, 2:
   434  	case x /* ERROR "invalid case x in switch on int32(x) (mismatched types int and int32)" */ :
   435  	}
   436  
   437  	switch x {
   438  	case 1 /* ERROR "overflows" */ << 100:
   439  	}
   440  
   441  	switch x {
   442  	case 1:
   443  	case 1 /* ERROR "duplicate case" */ :
   444  	case ( /* ERROR "duplicate case" */ 1):
   445  	case 2, 3, 4:
   446  	case 5, 1 /* ERROR "duplicate case" */ :
   447  	}
   448  
   449  	switch uint64(x) {
   450  	case 1<<64 - 1:
   451  	case 1 /* ERROR "duplicate case" */ <<64 - 1:
   452  	case 2, 3, 4:
   453  	case 5, 1 /* ERROR "duplicate case" */ <<64 - 1:
   454  	}
   455  
   456  	var y32 float32
   457  	switch y32 {
   458  	case 1.1:
   459  	case 11/10: // integer division!
   460  	case 11. /* ERROR "duplicate case" */ /10:
   461  	case 2, 3.0, 4.1:
   462  	case 5.2, 1.10 /* ERROR "duplicate case" */ :
   463  	}
   464  
   465  	var y64 float64
   466  	switch y64 {
   467  	case 1.1:
   468  	case 11/10: // integer division!
   469  	case 11. /* ERROR "duplicate case" */ /10:
   470  	case 2, 3.0, 4.1:
   471  	case 5.2, 1.10 /* ERROR "duplicate case" */ :
   472  	}
   473  
   474  	var s string
   475  	switch s {
   476  	case "foo":
   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" */ :
   481  	}
   482  
   483  	type T int
   484  	type F float64
   485  	type S string
   486  	type B bool
   487  	var i interface{}
   488  	switch i {
   489  	case nil:
   490  	case nil: // no duplicate detection
   491  	case (*int)(nil):
   492  	case (*int)(nil): // do duplicate detection
   493  	case 1:
   494  	case byte(1):
   495  	case int /* ERROR "duplicate case" */ (1):
   496  	case T(1):
   497  	case 1.0:
   498  	case F(1.0):
   499  	case F /* ERROR "duplicate case" */ (1.0):
   500  	case "hello":
   501  	case S("hello"):
   502  	case S /* ERROR "duplicate case" */ ("hello"):
   503  	case 1==1, B(false):
   504  	case false, B(2==2):
   505  	}
   506  
   507  	// switch on array
   508  	var a [3]int
   509  	switch a {
   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}:
   513  	}
   514  
   515  	// switch on channel
   516  	var c1, c2 chan int
   517  	switch c1 {
   518  	case nil:
   519  	case c1:
   520  	case c2:
   521  	case c1, c2: // no duplicate detection
   522  	}
   523  }
   524  
   525  func switches1() {
   526  	fallthrough /* ERROR "fallthrough statement out of place" */
   527  
   528  	var x int
   529  	switch x {
   530  	case 0:
   531  		fallthrough /* ERROR "fallthrough statement out of place" */
   532  		break
   533  	case 1:
   534  		fallthrough
   535  	case 2:
   536  		fallthrough; ; ; // trailing empty statements are ok
   537  	case 3:
   538  	default:
   539  		fallthrough; ;
   540  	case 4:
   541  		fallthrough /* ERROR "cannot fallthrough final case in switch" */
   542  	}
   543  
   544  	var y interface{}
   545  	switch y.(type) {
   546  	case int:
   547  		fallthrough /* ERROR "cannot fallthrough in type switch" */ ; ; ;
   548  	default:
   549  	}
   550  
   551  	switch x {
   552  	case 0:
   553  		if x == 0 {
   554  			fallthrough /* ERROR "fallthrough statement out of place" */
   555  		}
   556  	}
   557  
   558  	switch x {
   559  	case 0:
   560  		goto L1
   561  		L1: fallthrough; ;
   562  	case 1:
   563  		goto L2
   564  		goto L3
   565  		goto L4
   566  		L2: L3: L4: fallthrough
   567  	default:
   568  	}
   569  
   570  	switch x {
   571  	case 0:
   572  		goto L5
   573  		L5: fallthrough
   574  	default:
   575  		goto L6
   576  		goto L7
   577  		goto L8
   578  		L6: L7: L8: fallthrough /* ERROR "cannot fallthrough final case in switch" */
   579  	}
   580  
   581  	switch x {
   582  	case 0:
   583  		fallthrough; ;
   584  	case 1:
   585  		{
   586  			fallthrough /* ERROR "fallthrough statement out of place" */
   587  		}
   588  	case 2:
   589  		fallthrough
   590  	case 3:
   591  		fallthrough /* ERROR "fallthrough statement out of place" */
   592  		{ /* empty block is not an empty statement */ }; ;
   593  	default:
   594  		fallthrough /* ERROR "cannot fallthrough final case in switch" */
   595  	}
   596  
   597  	switch x {
   598  	case 0:
   599  		{
   600  			fallthrough /* ERROR "fallthrough statement out of place" */
   601  		}
   602  	}
   603  }
   604  
   605  func switches2() {
   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
   609  	}
   610  
   611  	// untyped constants are converted to default types
   612  	switch 1<<63-1 {
   613  	}
   614  	switch 1 /* ERRORx `cannot use .* as int value.*\(overflows\)` */ << 63 {
   615  	}
   616  	var x int
   617  	switch 1.0 {
   618  	case 1.0, 2.0, x /* ERROR "mismatched types int and float64" */ :
   619  	}
   620  	switch x {
   621  	case 1.0:
   622  	}
   623  
   624  	// untyped bools become of type bool
   625  	type B bool
   626  	var b B = true
   627  	switch x == x {
   628  	case b /* ERROR "mismatched types B and bool" */ :
   629  	}
   630  	switch {
   631  	case b /* ERROR "mismatched types B and bool" */ :
   632  	}
   633  }
   634  
   635  func issue11667() {
   636  	switch 9223372036854775808 /* ERRORx `cannot use .* as int value.*\(overflows\)` */ {
   637  	}
   638  	switch 9223372036854775808 /* ERRORx `cannot use .* as int value.*\(overflows\)` */ {
   639  	case 9223372036854775808:
   640  	}
   641  	var x int
   642  	switch x {
   643  	case 9223372036854775808 /* ERROR "overflows int" */ :
   644  	}
   645  	var y float64
   646  	switch y {
   647  	case 9223372036854775808:
   648  	}
   649  }
   650  
   651  func issue11687() {
   652  	f := func() (_, _ int) { return }
   653  	switch f /* ERROR "multiple-value f" */ () {
   654  	}
   655  	var x int
   656  	switch f /* ERROR "multiple-value f" */ () {
   657  	case x:
   658  	}
   659  	switch x {
   660  	case f /* ERROR "multiple-value f" */ ():
   661  	}
   662  }
   663  
   664  type I interface {
   665  	m()
   666  }
   667  
   668  type I2 interface {
   669  	m(int)
   670  }
   671  
   672  type T struct{}
   673  type T1 struct{}
   674  type T2 struct{}
   675  
   676  func (T) m() {}
   677  func (T2) m(int) {}
   678  
   679  func typeswitches() {
   680  	var i int
   681  	var x interface{}
   682  
   683  	switch x.(type) {}
   684  	switch (x /* ERROR "outside type switch" */ .(type)) {}
   685  
   686  	switch x.(type) {
   687  	default:
   688  	default /* ERROR "multiple defaults" */ :
   689  	}
   690  
   691  	switch x /* ERROR "declared and not used" */ := x.(type) {}
   692  	switch _ /* ERROR "no new variable on left side of :=" */ := x.(type) {}
   693  
   694  	switch x := x.(type) {
   695  	case int:
   696  		var y int = x
   697  		_ = y
   698  	}
   699  
   700  	switch x /* ERROR "x declared and not used" */ := i /* ERROR "not an interface" */ .(type) {}
   701  
   702  	switch t := x.(type) {
   703  	case nil:
   704  		var v bool = t /* ERRORx `cannot use .* in variable declaration` */
   705  		_ = v
   706  	case int:
   707  		var v int = t
   708  		_ = v
   709  	case float32, complex64:
   710  		var v float32 = t /* ERRORx `cannot use .* in variable declaration` */
   711  		_ = v
   712  	default:
   713  		var v float32 = t /* ERRORx `cannot use .* in variable declaration` */
   714  		_ = v
   715  	}
   716  
   717  	var t I
   718  	switch t.(type) {
   719  	case T:
   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)
   723  	}
   724  
   725  
   726  	{
   727  		x := 1
   728  		v := 2
   729  		switch v /* ERROR "v (variable of type int) is not an interface" */ .(type) {
   730  		case int:
   731  			println(x)
   732  			println(x / 0 /* ERROR "invalid operation: division by zero" */)
   733  		case 1 /* ERROR "1 is not a type" */:
   734  		}
   735  	}
   736  }
   737  
   738  // Test that each case clause uses the correct type of the variable
   739  // declared by the type switch (issue 5504).
   740  func typeswitch0() {
   741  	switch y := interface{}(nil).(type) {
   742  	case int:
   743  		func() int { return y + 0 }()
   744  	case float32:
   745  		func() float32 { return y }()
   746  	}
   747  }
   748  
   749  // Test correct scope setup.
   750  // (no redeclaration errors expected in the type switch)
   751  func typeswitch1() {
   752  	var t I
   753  	switch t := t; t := t.(type) {
   754  	case nil:
   755  		var _ I = t
   756  	case T:
   757  		var _ T = t
   758  	default:
   759  		var _ I = t
   760  	}
   761  }
   762  
   763  // Test correct typeswitch against interface types.
   764  type A interface { a() }
   765  type B interface { b() }
   766  type C interface { a(int) }
   767  
   768  func typeswitch2() {
   769  	switch A(nil).(type) {
   770  	case A:
   771  	case B:
   772  	case C /* STRICT "cannot have dynamic type" */: // only an error in strict mode (issue 8561)
   773  	}
   774  }
   775  
   776  func typeswitch3(x interface{}) {
   777  	switch x.(type) {
   778  	case int:
   779  	case float64:
   780  	case int /* ERROR "duplicate case" */ :
   781  	}
   782  
   783  	switch x.(type) {
   784  	case nil:
   785  	case int:
   786  	case nil /* ERROR "duplicate case" */ , nil /* ERROR "duplicate case" */ :
   787  	}
   788  
   789  	type F func(int)
   790  	switch x.(type) {
   791  	case nil:
   792  	case int, func(int):
   793  	case float32, func /* ERROR "duplicate case" */ (x int):
   794  	case F:
   795  	}
   796  }
   797  
   798  func fors1() {
   799  	for {}
   800  	var i string
   801  	_ = i
   802  	for i := 0; i < 10; i++ {}
   803  	for i := 0; i < 10; j /* ERROR "cannot declare" */ := 0 {}
   804  }
   805  
   806  func rangeloops1() {
   807  	var (
   808  		a [10]float32
   809  		b []string
   810  		p *[10]complex128
   811  		pp **[10]complex128
   812  		s string
   813  		m map[int]bool
   814  		c chan int
   815  		sc chan<- int
   816  		rc <-chan int
   817  		xs struct{}
   818  	)
   819  
   820  	for range xs /* ERROR "cannot range over" */ {}
   821  	for _ = range xs /* ERROR "cannot range over" */ {}
   822  	for i := range xs /* ERROR "cannot range over" */ { _ = i }
   823  
   824  	for range a {}
   825  	for i := range a {
   826  		var ii int
   827  		ii = i
   828  		_ = ii
   829  	}
   830  	for i, x := range a {
   831  		var ii int
   832  		ii = i
   833  		_ = ii
   834  		var xx float64
   835  		xx = x /* ERRORx `cannot use .* in assignment` */
   836  		_ = xx
   837  	}
   838  	var ii int
   839  	var xx float32
   840  	for ii, xx = range a {}
   841  	_, _ = ii, xx
   842  
   843  	for range b {}
   844  	for i := range b {
   845  		var ii int
   846  		ii = i
   847  		_ = ii
   848  	}
   849  	for i, x := range b {
   850  		var ii int
   851  		ii = i
   852  		_ = ii
   853  		var xx string
   854  		xx = x
   855  		_ = xx
   856  	}
   857  
   858  	for range s {}
   859  	for i := range s {
   860  		var ii int
   861  		ii = i
   862  		_ = ii
   863  	}
   864  	for i, x := range s {
   865  		var ii int
   866  		ii = i
   867  		_ = ii
   868  		var xx rune
   869  		xx = x
   870  		_ = xx
   871  	}
   872  
   873  	for range p {}
   874  	for _, x := range p {
   875  		var xx complex128
   876  		xx = x
   877  		_ = xx
   878  	}
   879  
   880  	for range pp /* ERROR "cannot range over" */ {}
   881  	for _, x := range pp /* ERROR "cannot range over" */ {}
   882  
   883  	for range m {}
   884  	for k := range m {
   885  		var kk int32
   886  		kk = k /* ERRORx `cannot use .* in assignment` */
   887  		_ = kk
   888  	}
   889  	for k, v := range m {
   890  		var kk int
   891  		kk = k
   892  		_ = kk
   893  		if v {}
   894  	}
   895  
   896  	for range c {}
   897  	for _, _ /* ERROR "only one iteration variable" */ = range c {}
   898  	for e := range c {
   899  		var ee int
   900  		ee = e
   901  		_ = ee
   902  	}
   903  	for _ = range sc /* ERROR "cannot range over" */ {}
   904  	for _ = range rc {}
   905  
   906  	// constant strings
   907  	const cs = "foo"
   908  	for range cs {}
   909  	for range "" {}
   910  	for i, x := range cs { _, _ = i, x }
   911  	for i, x := range "" {
   912  		var ii int
   913  		ii = i
   914  		_ = ii
   915  		var xx rune
   916  		xx = x
   917  		_ = xx
   918  	}
   919  }
   920  
   921  func rangeloops2() {
   922  	type I int
   923  	type R rune
   924  
   925  	var a [10]int
   926  	var i I
   927  	_ = i
   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[:] {}
   931  
   932  	var s string
   933  	var r R
   934  	_ = r
   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" {}
   939  }
   940  
   941  func issue6766b() {
   942  	for _ := /* ERROR "no new variables" */ range "" {}
   943  	for a, a /* ERROR "redeclared" */ := range "" { _ = a }
   944  	var a int
   945  	_ = a
   946  	for a, a /* ERROR "redeclared" */ := range []int{1, 2, 3} { _ = a }
   947  }
   948  
   949  // Test that despite errors in the range clause,
   950  // the loop body is still type-checked (and thus
   951  // errors reported).
   952  func issue10148() {
   953  	for y /* ERROR "declared and not used" */ := range "" {
   954  		_ = "" /* ERROR "mismatched types untyped string and untyped int" */ + 1
   955  	}
   956  	for range 1.5 /* ERROR "cannot range over 1.5 (untyped float constant)" */ {
   957  		_ = "" /* ERROR "mismatched types untyped string and untyped int" */ + 1
   958  	}
   959  	for y := range 1.5 /* ERROR "cannot range over 1.5 (untyped float constant)" */ {
   960  		_ = "" /* ERROR "mismatched types untyped string and untyped int" */ + 1
   961  	}
   962  }
   963  
   964  func labels0() {
   965  	goto L0
   966  	goto L1
   967  	L0:
   968  	L1:
   969  	L1 /* ERROR "already declared" */ :
   970  	if true {
   971  		goto L2
   972  		L2:
   973  		L0 /* ERROR "already declared" */ :
   974  	}
   975  	_ = func() {
   976  		goto L0
   977  		goto L1
   978  		goto L2
   979  		L0:
   980  		L1:
   981  		L2:
   982  	}
   983  }
   984  
   985  func expression_statements(ch chan int) {
   986  	expression_statements(ch)
   987  	<-ch
   988  	println()
   989  
   990  	0 /* ERROR "not used" */
   991  	1 /* ERROR "not used" */ +2
   992  	cap /* ERROR "not used" */ (ch)
   993  	println /* ERROR "must be called" */
   994  }
   995  

View as plain text