github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/src/go/types/testdata/stmt0.src (about)

     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 count mismatch" */ , 2
    19  	a, b, c = 1 /* ERROR "assignment count mismatch" */ , 2, 3, 4
    20  	_, _, _ = a, b, c
    21  
    22  	a = f0 /* ERROR "used as value" */ ()
    23  	a = f1()
    24  	a = f2 /* ERROR "assignment count mismatch" */ ()
    25  	a, b = f2()
    26  	a, b, c = f2 /* ERROR "assignment count mismatch" */ ()
    27  	a, b, c = f3()
    28  	a, b = f3 /* ERROR "assignment count mismatch" */ ()
    29  
    30  	a, b, c = <- /* ERROR "assignment count mismatch" */ 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 assign" */
    41  	i = f /* ERROR "cannot assign" */
    42  	f = c /* ERROR "cannot assign" */
    43  	c = s /* ERROR "cannot assign" */
    44  	s = b /* ERROR "cannot assign" */
    45  
    46  	v0, v1, v2 := 1 /* ERROR "mismatch" */ , 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 "too few arguments" */
    90  
    91  	// assignments to _
    92  	_ = nil /* ERROR "use of untyped nil" */
    93  	_ = 1 /* ERROR 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 count mismatch */ , 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 count mismatch */ , false
   120  
   121  	var x interface{}
   122  	_, b = x.(int)
   123  	x /* ERROR cannot assign */ .(int) = 0
   124  	x.(int) = 0 /* ERROR assignment count mismatch */ , 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" */ ["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 redeclared */ := 1, 2
   147  	_ = a
   148  	a, b, b /* ERROR redeclared */ := 1, 2, 3
   149  	_ = b
   150  	c, c /* ERROR redeclared */, 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 "cannot convert" */ --
   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 convert" */
   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 convert" */, x /* ERROR "cannot return" */
   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 convert */
   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 /* DISABLED "duplicate case" */ :
   442  	case 2, 3, 4:
   443  	case 1 /* DISABLED "duplicate case" */ :
   444  	}
   445  
   446  	switch uint64(x) {
   447  	case 1 /* DISABLED duplicate case */ <<64-1:
   448  	case 1 /* DISABLED duplicate case */ <<64-1:
   449  	}
   450  }
   451  
   452  func switches1() {
   453  	fallthrough /* ERROR "fallthrough statement out of place" */
   454  
   455  	var x int
   456  	switch x {
   457  	case 0:
   458  		fallthrough /* ERROR "fallthrough statement out of place" */
   459  		break
   460  	case 1:
   461  		fallthrough
   462  	case 2:
   463  	default:
   464  		fallthrough
   465  	case 3:
   466  		fallthrough /* ERROR "fallthrough statement out of place" */
   467  	}
   468  
   469  	var y interface{}
   470  	switch y.(type) {
   471  	case int:
   472  		fallthrough /* ERROR "fallthrough statement out of place" */
   473  	default:
   474  	}
   475  
   476  	switch x {
   477  	case 0:
   478  		if x == 0 {
   479  			fallthrough /* ERROR "fallthrough statement out of place" */
   480  		}
   481  	}
   482  
   483  	switch x {
   484  	case 0:
   485  		goto L1
   486  		L1: fallthrough
   487  	case 1:
   488  		goto L2
   489  		goto L3
   490  		goto L4
   491  		L2: L3: L4: fallthrough
   492  	default:
   493  	}
   494  
   495  	switch x {
   496  	case 0:
   497  		goto L5
   498  		L5: fallthrough
   499  	default:
   500  		goto L6
   501  		goto L7
   502  		goto L8
   503  		L6: L7: L8: fallthrough /* ERROR "fallthrough statement out of place" */
   504  	}
   505  
   506  	switch x {
   507  	case 0:
   508  		{
   509  			fallthrough /* ERROR "fallthrough statement out of place" */
   510  		}
   511  	default:
   512  	}
   513  }
   514  
   515  type I interface {
   516  	m()
   517  }
   518  
   519  type I2 interface {
   520  	m(int)
   521  }
   522  
   523  type T struct{}
   524  type T1 struct{}
   525  type T2 struct{}
   526  
   527  func (T) m() {}
   528  func (T2) m(int) {}
   529  
   530  func typeswitches() {
   531  	var i int
   532  	var x interface{}
   533  
   534  	switch x.(type) {}
   535  	switch (x /* ERROR "outside type switch" */ .(type)) {}
   536  
   537  	switch x.(type) {
   538  	default:
   539  	default /* ERROR "multiple defaults" */ :
   540  	}
   541  
   542  	switch x /* ERROR "declared but not used" */ := x.(type) {}
   543  	switch _ /* ERROR "no new variable on left side of :=" */ := x.(type) {}
   544  
   545  	switch x := x.(type) {
   546  	case int:
   547  		var y int = x
   548  		_ = y
   549  	}
   550  
   551  	switch x := i /* ERROR "not an interface" */ .(type) {}
   552  
   553  	switch t := x.(type) {
   554  	case nil:
   555  		var v bool = t /* ERROR "cannot initialize" */
   556  		_ = v
   557  	case int:
   558  		var v int = t
   559  		_ = v
   560  	case float32, complex64:
   561  		var v float32 = t /* ERROR "cannot initialize" */
   562  		_ = v
   563  	default:
   564  		var v float32 = t /* ERROR "cannot initialize" */
   565  		_ = v
   566  	}
   567  
   568  	var t I
   569  	switch t.(type) {
   570  	case T:
   571  	case T1 /* ERROR "missing method m" */ :
   572  	case T2 /* ERROR "wrong type for method m" */ :
   573  	case I2 /* STRICT "wrong type for method m" */ : // only an error in strict mode (issue 8561)
   574  	}
   575  }
   576  
   577  // Test that each case clause uses the correct type of the variable
   578  // declared by the type switch (issue 5504).
   579  func typeswitch0() {
   580  	switch y := interface{}(nil).(type) {
   581  	case int:
   582  		func() int { return y + 0 }()
   583  	case float32:
   584  		func() float32 { return y }()
   585  	}
   586  }
   587  
   588  // Test correct scope setup.
   589  // (no redeclaration errors expected in the type switch)
   590  func typeswitch1() {
   591  	var t I
   592  	switch t := t; t := t.(type) {
   593  	case nil:
   594  		var _ I = t
   595  	case T:
   596  		var _ T = t
   597  	default:
   598  		var _ I = t
   599  	}
   600  }
   601  
   602  // Test correct typeswitch against interface types.
   603  type A interface { a() }
   604  type B interface { b() }
   605  type C interface { a(int) }
   606  
   607  func typeswitch2() {
   608  	switch A(nil).(type) {
   609  	case A:
   610  	case B:
   611  	case C /* STRICT "cannot have dynamic type" */: // only an error in strict mode (issue 8561)
   612  	}
   613  }
   614  
   615  func typeswitch3(x interface{}) {
   616  	switch x.(type) {
   617  	case int:
   618  	case float64:
   619  	case int /* ERROR duplicate case */ :
   620  	}
   621  
   622  	switch x.(type) {
   623  	case nil:
   624  	case int:
   625  	case nil /* ERROR duplicate case */ , nil /* ERROR duplicate case */ :
   626  	}
   627  
   628  	type F func(int)
   629  	switch x.(type) {
   630  	case nil:
   631  	case int, func(int):
   632  	case float32, func /* ERROR duplicate case */ (x int):
   633  	case F:
   634  	}
   635  }
   636  
   637  func fors1() {
   638  	for {}
   639  	var i string
   640  	_ = i
   641  	for i := 0; i < 10; i++ {}
   642  	for i := 0; i < 10; j /* ERROR cannot declare */ := 0 {}
   643  }
   644  
   645  func rangeloops1() {
   646  	var (
   647  		x int
   648  		a [10]float32
   649  		b []string
   650  		p *[10]complex128
   651  		pp **[10]complex128
   652  		s string
   653  		m map[int]bool
   654  		c chan int
   655  		sc chan<- int
   656  		rc <-chan int
   657  	)
   658  
   659  	for range x /* ERROR "cannot range over" */ {}
   660  	for _ = range x /* ERROR "cannot range over" */ {}
   661  	for i := range x /* ERROR "cannot range over" */ {}
   662  
   663  	for range a {}
   664  	for i := range a {
   665  		var ii int
   666  		ii = i
   667  		_ = ii
   668  	}
   669  	for i, x := range a {
   670  		var ii int
   671  		ii = i
   672  		_ = ii
   673  		var xx float64
   674  		xx = x /* ERROR "cannot assign" */
   675  		_ = xx
   676  	}
   677  	var ii int
   678  	var xx float32
   679  	for ii, xx = range a {}
   680  	_, _ = ii, xx
   681  
   682  	for range b {}
   683  	for i := range b {
   684  		var ii int
   685  		ii = i
   686  		_ = ii
   687  	}
   688  	for i, x := range b {
   689  		var ii int
   690  		ii = i
   691  		_ = ii
   692  		var xx string
   693  		xx = x
   694  		_ = xx
   695  	}
   696  
   697  	for range s {}
   698  	for i := range s {
   699  		var ii int
   700  		ii = i
   701  		_ = ii
   702  	}
   703  	for i, x := range s {
   704  		var ii int
   705  		ii = i
   706  		_ = ii
   707  		var xx rune
   708  		xx = x
   709  		_ = xx
   710  	}
   711  
   712  	for range p {}
   713  	for _, x := range p {
   714  		var xx complex128
   715  		xx = x
   716  		_ = xx
   717  	}
   718  
   719  	for range pp /* ERROR "cannot range over" */ {}
   720  	for _, x := range pp /* ERROR "cannot range over" */ {}
   721  
   722  	for range m {}
   723  	for k := range m {
   724  		var kk int32
   725  		kk = k /* ERROR "cannot assign" */
   726  		_ = kk
   727  	}
   728  	for k, v := range m {
   729  		var kk int
   730  		kk = k
   731  		_ = kk
   732  		if v {}
   733  	}
   734  
   735  	for range c {}
   736  	for _, _ /* ERROR "only one iteration variable" */ = range c {}
   737  	for e := range c {
   738  		var ee int
   739  		ee = e
   740  		_ = ee
   741  	}
   742  	for _ = range sc /* ERROR "cannot range over send-only channel" */ {}
   743  	for _ = range rc {}
   744  
   745  	// constant strings
   746  	const cs = "foo"
   747  	for range cs {}
   748  	for range "" {}
   749  	for i, x := range cs { _, _ = i, x }
   750  	for i, x := range "" {
   751  		var ii int
   752  		ii = i
   753  		_ = ii
   754  		var xx rune
   755  		xx = x
   756  		_ = xx
   757  	}
   758  }
   759  
   760  func rangeloops2() {
   761  	type I int
   762  	type R rune
   763  
   764  	var a [10]int
   765  	var i I
   766  	_ = i
   767  	for i /* ERROR cannot assign */ = range a {}
   768  	for i /* ERROR cannot assign */ = range &a {}
   769  	for i /* ERROR cannot assign */ = range a[:] {}
   770  
   771  	var s string
   772  	var r R
   773  	_ = r
   774  	for i /* ERROR cannot assign */ = range s {}
   775  	for i /* ERROR cannot assign */ = range "foo" {}
   776  	for _, r /* ERROR cannot assign */ = range s {}
   777  	for _, r /* ERROR cannot assign */ = range "foo" {}
   778  }
   779  
   780  func issue6766b() {
   781  	for _ := /* ERROR no new variables */ range "" {}
   782  	for a, a /* ERROR redeclared */ := range "" { _ = a }
   783  	var a int
   784  	_ = a
   785  	for a, a /* ERROR redeclared */ := range []int{1, 2, 3} { _ = a }
   786  }
   787  
   788  // Test that despite errors in the range clause,
   789  // the loop body is still type-checked (and thus
   790  // errors reported).
   791  func issue10148() {
   792  	for y /* ERROR declared but not used */ := range "" {
   793  		_ = "" /* ERROR cannot convert */ + 1
   794  	}
   795  	for range 1 /* ERROR cannot range over 1 */ {
   796  		_ = "" /* ERROR cannot convert */ + 1
   797  	}
   798  	for y := range 1 /* ERROR cannot range over 1 */ {
   799  		_ = "" /* ERROR cannot convert */ + 1
   800  	}
   801  }
   802  
   803  func labels0() {
   804  	goto L0
   805  	goto L1
   806  	L0:
   807  	L1:
   808  	L1 /* ERROR "already declared" */ :
   809  	if true {
   810  		goto L2		
   811  		L2:
   812  		L0 /* ERROR "already declared" */ :
   813  	}
   814  	_ = func() {
   815  		goto L0
   816  		goto L1
   817  		goto L2
   818  		L0:
   819  		L1:
   820  		L2:
   821  	}
   822  }
   823  
   824  func expression_statements(ch chan int) {
   825  	expression_statements(ch)
   826  	<-ch
   827  	println()
   828  
   829  	0 /* ERROR "not used" */
   830  	1 /* ERROR "not used" */ +2
   831  	cap /* ERROR "not used" */ (ch)
   832  	println /* ERROR "must be called" */
   833  }