Text file src/go/types/testdata/check/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 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 "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
    20  	_, _, _ = a, b, c
    21  
    22  	a = f0 /* ERROR "used as value" */ ()
    23  	a = f1()
    24  	a = f2 /* ERROR "cannot assign [1-9]+ values to [1-9]+ variables" */ ()
    25  	a, b = f2()
    26  	a, b, c = f2 /* ERROR "cannot assign [1-9]+ values to [1-9]+ variables" */ ()
    27  	a, b, c = f3()
    28  	a, b = f3 /* ERROR "cannot assign [1-9]+ values to [1-9]+ variables" */ ()
    29  
    30  	a, b, c = <- /* ERROR "cannot assign [1-9]+ values to [1-9]+ variables" */ 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 /* 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" */
    45  
    46  	v0, v1, v2 := 1 /* ERROR "cannot initialize" */ , 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  	undeclared /* ERROR "undeclared" */ = 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 cannot assign [1-9]+ values to [1-9]+ variables */ , 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 cannot assign [1-9]+ values to [1-9]+ variables */ , false
   120  
   121  	var x interface{}
   122  	_, b = x.(int)
   123  	x /* ERROR cannot assign */ .(int) = 0
   124  	x.(int) = 0 /* ERROR cannot assign [1-9]+ values to [1-9]+ variables */ , 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" /* ERROR "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 ch2 declared but not used */ chan bool
   226  	case i := <-ch2:
   227  		print(i + 1)
   228  	}
   229  }
   230  
   231  func gos() {
   232  	go 1 /* ERROR HERE "function must be invoked" */
   233  	go int /* ERROR "go requires function call, not conversion" */ (0)
   234  	go gos()
   235  	var c chan int
   236  	go close(c)
   237  	go len /* ERROR "go discards result" */ (c)
   238  }
   239  
   240  func defers() {
   241  	defer 1 /* ERROR HERE "function must be invoked" */
   242  	defer int /* ERROR "defer requires function call, not conversion" */ (0)
   243  	defer defers()
   244  	var c chan int
   245  	defer close(c)
   246  	defer len /* ERROR "defer discards result" */ (c)
   247  }
   248  
   249  func breaks() {
   250  	var x, y int
   251  
   252  	break /* ERROR "break" */
   253  	{
   254  		break /* ERROR "break" */
   255  	}
   256  	if x < y {
   257  		break /* ERROR "break" */
   258  	}
   259  
   260  	switch x {
   261  	case 0:
   262  		break
   263  	case 1:
   264  		if x == y {
   265  			break
   266  		}
   267  	default:
   268  		break
   269  		break
   270  	}
   271  
   272  	var z interface{}
   273  	switch z.(type) {
   274  	case int:
   275  		break
   276  	}
   277  
   278  	for {
   279  		break
   280  	}
   281  
   282  	var a []int
   283  	for _ = range a {
   284  		break
   285  	}
   286  
   287  	for {
   288  		if x == y {
   289  			break
   290  		}
   291  	}
   292  
   293  	var ch chan int
   294  	select {
   295  	case <-ch:
   296  		break
   297  	}
   298  
   299  	select {
   300  	case <-ch:
   301  		if x == y {
   302  			break
   303  		}
   304  	default:
   305  		break
   306  	}
   307  }
   308  
   309  func continues() {
   310  	var x, y int
   311  
   312  	continue /* ERROR "continue" */
   313  	{
   314  		continue /* ERROR "continue" */
   315  	}
   316  
   317  	if x < y {
   318  		continue /* ERROR "continue" */
   319  	}
   320  
   321  	switch x {
   322  	case 0:
   323  		continue /* ERROR "continue" */
   324  	}
   325  
   326  	var z interface{}
   327  	switch z.(type) {
   328  	case int:
   329  		continue /* ERROR "continue" */
   330  	}
   331  
   332  	var ch chan int
   333  	select {
   334  	case <-ch:
   335  		continue /* ERROR "continue" */
   336  	}
   337  
   338  	for i := 0; i < 10; i++ {
   339  		continue
   340  		if x < y {
   341  			continue
   342  			break
   343  		}
   344  		switch x {
   345  		case y:
   346  			continue
   347  		default:
   348  			break
   349  		}
   350  		select {
   351  		case <-ch:
   352  			continue
   353  		}
   354  	}
   355  
   356  	var a []int
   357  	for _ = range a {
   358  		continue
   359  		if x < y {
   360  			continue
   361  			break
   362  		}
   363  		switch x {
   364  		case y:
   365  			continue
   366  		default:
   367  			break
   368  		}
   369  		select {
   370  		case <-ch:
   371  			continue
   372  		}
   373  	}
   374  }
   375  
   376  func returns0() {
   377  	return
   378  	return 0 /* ERROR too many return values */
   379  }
   380  
   381  func returns1(x float64) (int, *float64) {
   382  	return 0, &x
   383  	return /* ERROR not enough return values */
   384  	return "foo" /* ERROR "cannot .* in return statement" */, x /* ERROR "cannot use .* in return statement" */
   385  	return 0, &x, 1 /* ERROR too many return values */
   386  }
   387  
   388  func returns2() (a, b int) {
   389  	return
   390  	return 1, "foo" /* ERROR cannot use .* in return statement */
   391  	return 1, 2, 3 /* ERROR too many return values */
   392  	{
   393  		type a int
   394  		return 1, 2
   395  		return /* ERROR a not in scope at return */
   396  	}
   397  }
   398  
   399  func returns3() (_ int) {
   400  	return
   401  	{
   402  		var _ int // blank (_) identifiers never shadow since they are in no scope
   403  		return
   404  	}
   405  }
   406  
   407  func switches0() {
   408  	var x int
   409  
   410  	switch x {
   411  	}
   412  
   413  	switch x {
   414  	default:
   415  	default /* ERROR "multiple defaults" */ :
   416  	}
   417  
   418  	switch {
   419  	case 1  /* ERROR "cannot convert" */ :
   420  	}
   421  
   422  	true := "false"
   423  	_ = true
   424  	// A tagless switch is equivalent to the bool
   425          // constant true, not the identifier 'true'.
   426  	switch {
   427  	case "false" /* ERROR "cannot convert" */:
   428  	}
   429  
   430  	switch int32(x) {
   431  	case 1, 2:
   432  	case x /* ERROR "invalid case x in switch on int32\(x\) \(mismatched types int and int32\)" */ :
   433  	}
   434  
   435  	switch x {
   436  	case 1 /* ERROR "overflows" */ << 100:
   437  	}
   438  
   439  	switch x {
   440  	case 1:
   441  	case 1 /* ERROR "duplicate case" */ :
   442  	case ( /* ERROR "duplicate case" */ 1):
   443  	case 2, 3, 4:
   444  	case 5, 1 /* ERROR "duplicate case" */ :
   445  	}
   446  
   447  	switch uint64(x) {
   448  	case 1<<64 - 1:
   449  	case 1 /* ERROR duplicate case */ <<64 - 1:
   450  	case 2, 3, 4:
   451  	case 5, 1 /* ERROR duplicate case */ <<64 - 1:
   452  	}
   453  
   454  	var y32 float32
   455  	switch y32 {
   456  	case 1.1:
   457  	case 11/10: // integer division!
   458  	case 11. /* ERROR duplicate case */ /10:
   459  	case 2, 3.0, 4.1:
   460  	case 5.2, 1.10 /* ERROR duplicate case */ :
   461  	}
   462  
   463  	var y64 float64
   464  	switch y64 {
   465  	case 1.1:
   466  	case 11/10: // integer division!
   467  	case 11. /* ERROR duplicate case */ /10:
   468  	case 2, 3.0, 4.1:
   469  	case 5.2, 1.10 /* ERROR duplicate case */ :
   470  	}
   471  
   472  	var s string
   473  	switch s {
   474  	case "foo":
   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 */ :
   479  	}
   480  
   481  	type T int
   482  	type F float64
   483  	type S string
   484  	type B bool
   485  	var i interface{}
   486  	switch i {
   487  	case nil:
   488  	case nil: // no duplicate detection
   489  	case (*int)(nil):
   490  	case (*int)(nil): // do duplicate detection
   491  	case 1:
   492  	case byte(1):
   493  	case int /* ERROR duplicate case */ (1):
   494  	case T(1):
   495  	case 1.0:
   496  	case F(1.0):
   497  	case F /* ERROR duplicate case */ (1.0):
   498  	case "hello":
   499  	case S("hello"):
   500  	case S /* ERROR duplicate case */ ("hello"):
   501  	case 1==1, B(false):
   502  	case false, B(2==2):
   503  	}
   504  
   505  	// switch on array
   506  	var a [3]int
   507  	switch a {
   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}:
   511  	}
   512  
   513  	// switch on channel
   514  	var c1, c2 chan int
   515  	switch c1 {
   516  	case nil:
   517  	case c1:
   518  	case c2:
   519  	case c1, c2: // no duplicate detection
   520  	}
   521  }
   522  
   523  func switches1() {
   524  	fallthrough /* ERROR "fallthrough statement out of place" */
   525  
   526  	var x int
   527  	switch x {
   528  	case 0:
   529  		fallthrough /* ERROR "fallthrough statement out of place" */
   530  		break
   531  	case 1:
   532  		fallthrough
   533  	case 2:
   534  		fallthrough; ; ; // trailing empty statements are ok
   535  	case 3:
   536  	default:
   537  		fallthrough; ;
   538  	case 4:
   539  		fallthrough /* ERROR "cannot fallthrough final case in switch" */
   540  	}
   541  
   542  	var y interface{}
   543  	switch y.(type) {
   544  	case int:
   545  		fallthrough /* ERROR "fallthrough statement out of place" */ ; ; ;
   546  	default:
   547  	}
   548  
   549  	switch x {
   550  	case 0:
   551  		if x == 0 {
   552  			fallthrough /* ERROR "fallthrough statement out of place" */
   553  		}
   554  	}
   555  
   556  	switch x {
   557  	case 0:
   558  		goto L1
   559  		L1: fallthrough; ;
   560  	case 1:
   561  		goto L2
   562  		goto L3
   563  		goto L4
   564  		L2: L3: L4: fallthrough
   565  	default:
   566  	}
   567  
   568  	switch x {
   569  	case 0:
   570  		goto L5
   571  		L5: fallthrough
   572  	default:
   573  		goto L6
   574  		goto L7
   575  		goto L8
   576  		L6: L7: L8: fallthrough /* ERROR "cannot fallthrough final case in switch" */
   577  	}
   578  
   579  	switch x {
   580  	case 0:
   581  		fallthrough; ;
   582  	case 1:
   583  		{
   584  			fallthrough /* ERROR "fallthrough statement out of place" */
   585  		}
   586  	case 2:
   587  		fallthrough
   588  	case 3:
   589  		fallthrough /* ERROR "fallthrough statement out of place" */
   590  		{ /* empty block is not an empty statement */ }; ;
   591  	default:
   592  		fallthrough /* ERROR "cannot fallthrough final case in switch" */
   593  	}
   594  
   595  	switch x {
   596  	case 0:
   597  		{
   598  			fallthrough /* ERROR "fallthrough statement out of place" */
   599  		}
   600  	}
   601  }
   602  
   603  func switches2() {
   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
   607  	}
   608  
   609  	// untyped constants are converted to default types
   610  	switch 1<<63-1 {
   611  	}
   612  	switch 1 /* ERROR "cannot use .* as int value.*\(overflows\)" */ << 63 {
   613  	}
   614  	var x int
   615  	switch 1.0 {
   616  	case 1.0, 2.0, x /* ERROR "mismatched types int and float64" */ :
   617  	}
   618  	switch x {
   619  	case 1.0:
   620  	}
   621  
   622  	// untyped bools become of type bool
   623  	type B bool
   624  	var b B = true
   625  	switch x == x {
   626  	case b /* ERROR "mismatched types B and bool" */ :
   627  	}
   628  	switch {
   629  	case b /* ERROR "mismatched types B and bool" */ :
   630  	}
   631  }
   632  
   633  func issue11667() {
   634  	switch 9223372036854775808 /* ERROR "cannot use .* as int value.*\(overflows\)" */ {
   635  	}
   636  	switch 9223372036854775808 /* ERROR "cannot use .* as int value.*\(overflows\)" */ {
   637  	case 9223372036854775808:
   638  	}
   639  	var x int
   640  	switch x {
   641  	case 9223372036854775808 /* ERROR "overflows int" */ :
   642  	}
   643  	var y float64
   644  	switch y {
   645  	case 9223372036854775808:
   646  	}
   647  }
   648  
   649  func issue11687() {
   650  	f := func() (_, _ int) { return }
   651  	switch f /* ERROR "2-valued f" */ () {
   652  	}
   653  	var x int
   654  	switch f /* ERROR "2-valued f" */ () {
   655  	case x:
   656  	}
   657  	switch x {
   658  	case f /* ERROR "2-valued f" */ ():
   659  	}
   660  }
   661  
   662  type I interface {
   663  	m()
   664  }
   665  
   666  type I2 interface {
   667  	m(int)
   668  }
   669  
   670  type T struct{}
   671  type T1 struct{}
   672  type T2 struct{}
   673  
   674  func (T) m() {}
   675  func (T2) m(int) {}
   676  
   677  func typeswitches() {
   678  	var i int
   679  	var x interface{}
   680  
   681  	switch x.(type) {}
   682  	switch (x /* ERROR "outside type switch" */ .(type)) {}
   683  
   684  	switch x.(type) {
   685  	default:
   686  	default /* ERROR "multiple defaults" */ :
   687  	}
   688  
   689  	switch x /* ERROR "declared but not used" */ := x.(type) {}
   690  	switch _ /* ERROR "no new variable on left side of :=" */ := x.(type) {}
   691  
   692  	switch x := x.(type) {
   693  	case int:
   694  		var y int = x
   695  		_ = y
   696  	}
   697  
   698  	switch x /* ERROR "x declared but not used" */ := i /* ERROR "not an interface" */ .(type) {}
   699  
   700  	switch t := x.(type) {
   701  	case nil:
   702  		var v bool = t /* ERROR "cannot use .* in variable declaration" */
   703  		_ = v
   704  	case int:
   705  		var v int = t
   706  		_ = v
   707  	case float32, complex64:
   708  		var v float32 = t /* ERROR "cannot use .* in variable declaration" */
   709  		_ = v
   710  	default:
   711  		var v float32 = t /* ERROR "cannot use .* in variable declaration" */
   712  		_ = v
   713  	}
   714  
   715  	var t I
   716  	switch t.(type) {
   717  	case T:
   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)
   721  	}
   722  
   723  
   724  	{
   725  		x := 1
   726  		v := 2
   727  		switch v /* ERROR "v [(]variable of type int[)] is not an interface" */ .(type) {
   728  		case int:
   729  			println(x)
   730  			println(x / 0 /* ERROR "invalid operation: division by zero" */)
   731  		case 1 /* ERROR "expected type, found 1" */:
   732  		}
   733  	}
   734  }
   735  
   736  // Test that each case clause uses the correct type of the variable
   737  // declared by the type switch (issue 5504).
   738  func typeswitch0() {
   739  	switch y := interface{}(nil).(type) {
   740  	case int:
   741  		func() int { return y + 0 }()
   742  	case float32:
   743  		func() float32 { return y }()
   744  	}
   745  }
   746  
   747  // Test correct scope setup.
   748  // (no redeclaration errors expected in the type switch)
   749  func typeswitch1() {
   750  	var t I
   751  	switch t := t; t := t.(type) {
   752  	case nil:
   753  		var _ I = t
   754  	case T:
   755  		var _ T = t
   756  	default:
   757  		var _ I = t
   758  	}
   759  }
   760  
   761  // Test correct typeswitch against interface types.
   762  type A interface { a() }
   763  type B interface { b() }
   764  type C interface { a(int) }
   765  
   766  func typeswitch2() {
   767  	switch A(nil).(type) {
   768  	case A:
   769  	case B:
   770  	case C /* STRICT "cannot have dynamic type" */: // only an error in strict mode (issue 8561)
   771  	}
   772  }
   773  
   774  func typeswitch3(x interface{}) {
   775  	switch x.(type) {
   776  	case int:
   777  	case float64:
   778  	case int /* ERROR duplicate case */ :
   779  	}
   780  
   781  	switch x.(type) {
   782  	case nil:
   783  	case int:
   784  	case nil /* ERROR duplicate case */ , nil /* ERROR duplicate case */ :
   785  	}
   786  
   787  	type F func(int)
   788  	switch x.(type) {
   789  	case nil:
   790  	case int, func(int):
   791  	case float32, func /* ERROR duplicate case */ (x int):
   792  	case F:
   793  	}
   794  }
   795  
   796  func fors1() {
   797  	for {}
   798  	var i string
   799  	_ = i
   800  	for i := 0; i < 10; i++ {}
   801  	for i := 0; i < 10; j /* ERROR cannot declare */ := 0 {}
   802  }
   803  
   804  func rangeloops1() {
   805  	var (
   806  		x int
   807  		a [10]float32
   808  		b []string
   809  		p *[10]complex128
   810  		pp **[10]complex128
   811  		s string
   812  		m map[int]bool
   813  		c chan int
   814  		sc chan<- int
   815  		rc <-chan int
   816  	)
   817  
   818  	for range x /* ERROR "cannot range over" */ {}
   819  	for _ = range x /* ERROR "cannot range over" */ {}
   820  	for i := range x /* ERROR "cannot range over" */ {}
   821  
   822  	for range a {}
   823  	for i := range a {
   824  		var ii int
   825  		ii = i
   826  		_ = ii
   827  	}
   828  	for i, x := range a {
   829  		var ii int
   830  		ii = i
   831  		_ = ii
   832  		var xx float64
   833  		xx = x /* ERROR "cannot use .* in assignment" */
   834  		_ = xx
   835  	}
   836  	var ii int
   837  	var xx float32
   838  	for ii, xx = range a {}
   839  	_, _ = ii, xx
   840  
   841  	for range b {}
   842  	for i := range b {
   843  		var ii int
   844  		ii = i
   845  		_ = ii
   846  	}
   847  	for i, x := range b {
   848  		var ii int
   849  		ii = i
   850  		_ = ii
   851  		var xx string
   852  		xx = x
   853  		_ = xx
   854  	}
   855  
   856  	for range s {}
   857  	for i := range s {
   858  		var ii int
   859  		ii = i
   860  		_ = ii
   861  	}
   862  	for i, x := range s {
   863  		var ii int
   864  		ii = i
   865  		_ = ii
   866  		var xx rune
   867  		xx = x
   868  		_ = xx
   869  	}
   870  
   871  	for range p {}
   872  	for _, x := range p {
   873  		var xx complex128
   874  		xx = x
   875  		_ = xx
   876  	}
   877  
   878  	for range pp /* ERROR "cannot range over" */ {}
   879  	for _, x := range pp /* ERROR "cannot range over" */ {}
   880  
   881  	for range m {}
   882  	for k := range m {
   883  		var kk int32
   884  		kk = k /* ERROR "cannot use .* in assignment" */
   885  		_ = kk
   886  	}
   887  	for k, v := range m {
   888  		var kk int
   889  		kk = k
   890  		_ = kk
   891  		if v {}
   892  	}
   893  
   894  	for range c {}
   895  	for _, _ /* ERROR "only one iteration variable" */ = range c {}
   896  	for e := range c {
   897  		var ee int
   898  		ee = e
   899  		_ = ee
   900  	}
   901  	for _ = range sc /* ERROR "cannot range over" */ {}
   902  	for _ = range rc {}
   903  
   904  	// constant strings
   905  	const cs = "foo"
   906  	for range cs {}
   907  	for range "" {}
   908  	for i, x := range cs { _, _ = i, x }
   909  	for i, x := range "" {
   910  		var ii int
   911  		ii = i
   912  		_ = ii
   913  		var xx rune
   914  		xx = x
   915  		_ = xx
   916  	}
   917  }
   918  
   919  func rangeloops2() {
   920  	type I int
   921  	type R rune
   922  
   923  	var a [10]int
   924  	var i I
   925  	_ = i
   926  	for i /* ERROR cannot use .* in assignment */ = range a {}
   927  	for i /* ERROR cannot use .* in assignment */ = range &a {}
   928  	for i /* ERROR cannot use .* in assignment */ = range a[:] {}
   929  
   930  	var s string
   931  	var r R
   932  	_ = r
   933  	for i /* ERROR cannot use .* in assignment */ = range s {}
   934  	for i /* ERROR cannot use .* in assignment */ = range "foo" {}
   935  	for _, r /* ERROR cannot use .* in assignment */ = range s {}
   936  	for _, r /* ERROR cannot use .* in assignment */ = range "foo" {}
   937  }
   938  
   939  func issue6766b() {
   940  	for _ := /* ERROR no new variables */ range "" {}
   941  	for a, a /* ERROR redeclared */ := range "" { _ = a }
   942  	var a int
   943  	_ = a
   944  	for a, a /* ERROR redeclared */ := range []int{1, 2, 3} { _ = a }
   945  }
   946  
   947  // Test that despite errors in the range clause,
   948  // the loop body is still type-checked (and thus
   949  // errors reported).
   950  func issue10148() {
   951  	for y /* ERROR declared but not used */ := range "" {
   952  		_ = "" /* ERROR mismatched types untyped string and untyped int */ + 1
   953  	}
   954  	for range 1 /* ERROR cannot range over 1 */ {
   955  		_ = "" /* ERROR mismatched types untyped string and untyped int */ + 1
   956  	}
   957  	for y := range 1 /* ERROR cannot range over 1 */ {
   958  		_ = "" /* ERROR mismatched types untyped string and untyped int */ + 1
   959  	}
   960  }
   961  
   962  func labels0() {
   963  	goto L0
   964  	goto L1
   965  	L0:
   966  	L1:
   967  	L1 /* ERROR "already declared" */ :
   968  	if true {
   969  		goto L2
   970  		L2:
   971  		L0 /* ERROR "already declared" */ :
   972  	}
   973  	_ = func() {
   974  		goto L0
   975  		goto L1
   976  		goto L2
   977  		L0:
   978  		L1:
   979  		L2:
   980  	}
   981  }
   982  
   983  func expression_statements(ch chan int) {
   984  	expression_statements(ch)
   985  	<-ch
   986  	println()
   987  
   988  	0 /* ERROR "not used" */
   989  	1 /* ERROR "not used" */ +2
   990  	cap /* ERROR "not used" */ (ch)
   991  	println /* ERROR "must be called" */
   992  }
   993  

View as plain text