Black Lives Matter. Support the Equal Justice Initiative.

Text file src/go/types/testdata/check/stmt0.src

Documentation: go/types/testdata/check

     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 "wrong number of return values" */
    33  	return /* ERROR "wrong number of return values" */ 1
    34  	return 1, 2
    35  	return /* ERROR "wrong number of return values" */ 1, 2, 3
    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 += "foo" /* ERROR "cannot convert.*int" */
    53  
    54  	f -= 1
    55  	f /= 0
    56  	f = float32(0)/0 /* ERROR "division by zero" */
    57  	f -= "foo" /* ERROR "cannot convert.*float64" */
    58  
    59  	c *= 1
    60  	c /= 0
    61  
    62  	s += "bar"
    63  	s += 1 /* ERROR "cannot convert.*string" */
    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 "untyped nil value" */
    73  		_ [10]int = nil /* ERROR "untyped nil value" */
    74  		_ []byte = nil
    75  		_ struct{} = nil /* ERROR "untyped nil value" */
    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 /* ERROR "wrong number of return values" */ m[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 no result values expected */
   379  }
   380  
   381  func returns1(x float64) (int, *float64) {
   382  	return 0, &x
   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
   386  }
   387  
   388  func returns2() (a, b int) {
   389  	return
   390  	return 1, "foo" /* ERROR cannot use .* in return statement */
   391  	return /* ERROR wrong number of return values */ 1, 2, 3
   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 "cannot compare" */ :
   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 := 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  // Test that each case clause uses the correct type of the variable
   725  // declared by the type switch (issue 5504).
   726  func typeswitch0() {
   727  	switch y := interface{}(nil).(type) {
   728  	case int:
   729  		func() int { return y + 0 }()
   730  	case float32:
   731  		func() float32 { return y }()
   732  	}
   733  }
   734  
   735  // Test correct scope setup.
   736  // (no redeclaration errors expected in the type switch)
   737  func typeswitch1() {
   738  	var t I
   739  	switch t := t; t := t.(type) {
   740  	case nil:
   741  		var _ I = t
   742  	case T:
   743  		var _ T = t
   744  	default:
   745  		var _ I = t
   746  	}
   747  }
   748  
   749  // Test correct typeswitch against interface types.
   750  type A interface { a() }
   751  type B interface { b() }
   752  type C interface { a(int) }
   753  
   754  func typeswitch2() {
   755  	switch A(nil).(type) {
   756  	case A:
   757  	case B:
   758  	case C /* STRICT "cannot have dynamic type" */: // only an error in strict mode (issue 8561)
   759  	}
   760  }
   761  
   762  func typeswitch3(x interface{}) {
   763  	switch x.(type) {
   764  	case int:
   765  	case float64:
   766  	case int /* ERROR duplicate case */ :
   767  	}
   768  
   769  	switch x.(type) {
   770  	case nil:
   771  	case int:
   772  	case nil /* ERROR duplicate case */ , nil /* ERROR duplicate case */ :
   773  	}
   774  
   775  	type F func(int)
   776  	switch x.(type) {
   777  	case nil:
   778  	case int, func(int):
   779  	case float32, func /* ERROR duplicate case */ (x int):
   780  	case F:
   781  	}
   782  }
   783  
   784  func fors1() {
   785  	for {}
   786  	var i string
   787  	_ = i
   788  	for i := 0; i < 10; i++ {}
   789  	for i := 0; i < 10; j /* ERROR cannot declare */ := 0 {}
   790  }
   791  
   792  func rangeloops1() {
   793  	var (
   794  		x int
   795  		a [10]float32
   796  		b []string
   797  		p *[10]complex128
   798  		pp **[10]complex128
   799  		s string
   800  		m map[int]bool
   801  		c chan int
   802  		sc chan<- int
   803  		rc <-chan int
   804  	)
   805  
   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" */ {}
   809  
   810  	for range a {}
   811  	for i := range a {
   812  		var ii int
   813  		ii = i
   814  		_ = ii
   815  	}
   816  	for i, x := range a {
   817  		var ii int
   818  		ii = i
   819  		_ = ii
   820  		var xx float64
   821  		xx = x /* ERROR "cannot use .* in assignment" */
   822  		_ = xx
   823  	}
   824  	var ii int
   825  	var xx float32
   826  	for ii, xx = range a {}
   827  	_, _ = ii, xx
   828  
   829  	for range b {}
   830  	for i := range b {
   831  		var ii int
   832  		ii = i
   833  		_ = ii
   834  	}
   835  	for i, x := range b {
   836  		var ii int
   837  		ii = i
   838  		_ = ii
   839  		var xx string
   840  		xx = x
   841  		_ = xx
   842  	}
   843  
   844  	for range s {}
   845  	for i := range s {
   846  		var ii int
   847  		ii = i
   848  		_ = ii
   849  	}
   850  	for i, x := range s {
   851  		var ii int
   852  		ii = i
   853  		_ = ii
   854  		var xx rune
   855  		xx = x
   856  		_ = xx
   857  	}
   858  
   859  	for range p {}
   860  	for _, x := range p {
   861  		var xx complex128
   862  		xx = x
   863  		_ = xx
   864  	}
   865  
   866  	for range pp /* ERROR "cannot range over" */ {}
   867  	for _, x := range pp /* ERROR "cannot range over" */ {}
   868  
   869  	for range m {}
   870  	for k := range m {
   871  		var kk int32
   872  		kk = k /* ERROR "cannot use .* in assignment" */
   873  		_ = kk
   874  	}
   875  	for k, v := range m {
   876  		var kk int
   877  		kk = k
   878  		_ = kk
   879  		if v {}
   880  	}
   881  
   882  	for range c {}
   883  	for _, _ /* ERROR "only one iteration variable" */ = range c {}
   884  	for e := range c {
   885  		var ee int
   886  		ee = e
   887  		_ = ee
   888  	}
   889  	for _ = range sc /* ERROR "cannot range over" */ {}
   890  	for _ = range rc {}
   891  
   892  	// constant strings
   893  	const cs = "foo"
   894  	for range cs {}
   895  	for range "" {}
   896  	for i, x := range cs { _, _ = i, x }
   897  	for i, x := range "" {
   898  		var ii int
   899  		ii = i
   900  		_ = ii
   901  		var xx rune
   902  		xx = x
   903  		_ = xx
   904  	}
   905  }
   906  
   907  func rangeloops2() {
   908  	type I int
   909  	type R rune
   910  
   911  	var a [10]int
   912  	var i I
   913  	_ = i
   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[:] {}
   917  
   918  	var s string
   919  	var r R
   920  	_ = r
   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" {}
   925  }
   926  
   927  func issue6766b() {
   928  	for _ := /* ERROR no new variables */ range "" {}
   929  	for a, a /* ERROR redeclared */ := range "" { _ = a }
   930  	var a int
   931  	_ = a
   932  	for a, a /* ERROR redeclared */ := range []int{1, 2, 3} { _ = a }
   933  }
   934  
   935  // Test that despite errors in the range clause,
   936  // the loop body is still type-checked (and thus
   937  // errors reported).
   938  func issue10148() {
   939  	for y /* ERROR declared but not used */ := range "" {
   940  		_ = "" /* ERROR cannot convert */ + 1
   941  	}
   942  	for range 1 /* ERROR cannot range over 1 */ {
   943  		_ = "" /* ERROR cannot convert */ + 1
   944  	}
   945  	for y := range 1 /* ERROR cannot range over 1 */ {
   946  		_ = "" /* ERROR cannot convert */ + 1
   947  	}
   948  }
   949  
   950  func labels0() {
   951  	goto L0
   952  	goto L1
   953  	L0:
   954  	L1:
   955  	L1 /* ERROR "already declared" */ :
   956  	if true {
   957  		goto L2
   958  		L2:
   959  		L0 /* ERROR "already declared" */ :
   960  	}
   961  	_ = func() {
   962  		goto L0
   963  		goto L1
   964  		goto L2
   965  		L0:
   966  		L1:
   967  		L2:
   968  	}
   969  }
   970  
   971  func expression_statements(ch chan int) {
   972  	expression_statements(ch)
   973  	<-ch
   974  	println()
   975  
   976  	0 /* ERROR "not used" */
   977  	1 /* ERROR "not used" */ +2
   978  	cap /* ERROR "not used" */ (ch)
   979  	println /* ERROR "must be called" */
   980  }
   981  

View as plain text