github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/src/go/types/testdata/vardecl.src (about)

     1  // Copyright 2013 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  package vardecl
     6  
     7  // Prerequisites.
     8  import "math"
     9  func f() {}
    10  func g() (x, y int) { return }
    11  var m map[string]int
    12  
    13  // Var decls must have a type or an initializer.
    14  var _ int
    15  var _, _ int
    16  
    17  // The first error message is produced by the parser.
    18  // In a real-world scenario, the type-checker would not be run
    19  // in this case and the 2nd error message would not appear.
    20  var _ /* ERROR "missing variable type" */ /* ERROR "missing type or init expr" */
    21  var _ /* ERROR "missing variable type" */ /* ERROR "missing type or init expr" */, _
    22  var _ /* ERROR "missing variable type" */ /* ERROR "missing type or init expr" */, _, _
    23  
    24  // The initializer must be an expression.
    25  var _ = int /* ERROR "not an expression" */
    26  var _ = f /* ERROR "used as value" */ ()
    27  
    28  // Identifier and expression arity must match.
    29  var _, _ = 1, 2
    30  var _ = 1, 2 /* ERROR "extra init expr 2" */
    31  var _, _ = 1 /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */
    32  var _, _, _ /* ERROR "missing init expr for _" */ = 1, 2
    33  
    34  var _ = g /* ERROR "2-valued g" */ ()
    35  var _, _ = g()
    36  var _, _, _ = g /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */ ()
    37  
    38  var _ = m["foo"]
    39  var _, _ = m["foo"]
    40  var _, _, _ = m  /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */ ["foo"]
    41  
    42  var _, _ int = 1, 2
    43  var _ int = 1, 2 /* ERROR "extra init expr 2" */
    44  var _, _ int = 1 /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */
    45  var _, _, _ /* ERROR "missing init expr for _" */ int = 1, 2
    46  
    47  var (
    48  	_, _ = 1, 2
    49  	_ = 1, 2 /* ERROR "extra init expr 2" */
    50  	_, _ = 1 /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */
    51  	_, _, _ /* ERROR "missing init expr for _" */ = 1, 2
    52  
    53  	_ = g /* ERROR "2-valued g" */ ()
    54  	_, _ = g()
    55  	_, _, _ = g /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */ ()
    56  
    57  	_ = m["foo"]
    58  	_, _ = m["foo"]
    59  	_, _, _ = m /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */ ["foo"]
    60  
    61  	_, _ int = 1, 2
    62  	_ int = 1, 2 /* ERROR "extra init expr 2" */
    63  	_, _ int = 1 /* ERROR "cannot initialize [0-9]+ variables with [0-9]+ values" */
    64  	_, _, _ /* ERROR "missing init expr for _" */ int = 1, 2
    65  )
    66  
    67  // Variables declared in function bodies must be 'used'.
    68  type T struct{}
    69  func (r T) _(a, b, c int) (u, v, w int) {
    70  	var x1 /* ERROR "declared but not used" */ int
    71  	var x2 /* ERROR "declared but not used" */ int
    72  	x1 = 1
    73  	(x2) = 2
    74  
    75  	y1 /* ERROR "declared but not used" */ := 1
    76  	y2 /* ERROR "declared but not used" */ := 2
    77  	y1 = 1
    78  	(y1) = 2
    79  
    80  	{
    81  		var x1 /* ERROR "declared but not used" */ int
    82  		var x2 /* ERROR "declared but not used" */ int
    83  		x1 = 1
    84  		(x2) = 2
    85  
    86  		y1 /* ERROR "declared but not used" */ := 1
    87  		y2 /* ERROR "declared but not used" */ := 2
    88  		y1 = 1
    89  		(y1) = 2
    90  	}
    91  
    92  	if x /* ERROR "declared but not used" */ := 0; a < b {}
    93  
    94  	switch x /* ERROR "declared but not used" */, y := 0, 1; a {
    95  	case 0:
    96  		_ = y
    97  	case 1:
    98  		x /* ERROR "declared but not used" */ := 0
    99  	}
   100  
   101  	var t interface{}
   102  	switch t /* ERROR "declared but not used" */ := t.(type) {}
   103  
   104  	switch t /* ERROR "declared but not used" */ := t.(type) {
   105  	case int:
   106  	}
   107  
   108  	switch t /* ERROR "declared but not used" */ := t.(type) {
   109  	case int:
   110  	case float32, complex64:
   111  		t = nil
   112  	}
   113  
   114  	switch t := t.(type) {
   115  	case int:
   116  	case float32, complex64:
   117  		_ = t
   118  	}
   119  
   120  	switch t := t.(type) {
   121  	case int:
   122  	case float32:
   123  	case string:
   124  		_ = func() string {
   125  			return t
   126  		}
   127  	}
   128  
   129  	switch t := t; t /* ERROR "declared but not used" */ := t.(type) {}
   130  
   131  	var z1 /* ERROR "declared but not used" */ int
   132  	var z2 int
   133  	_ = func(a, b, c int) (u, v, w int) {
   134  		z1 = a
   135  		(z1) = b
   136  		a = z2
   137  		return
   138  	}
   139  
   140  	var s []int
   141  	var i /* ERROR "declared but not used" */ , j int
   142  	for i, j = range s {
   143  		_ = j
   144  	}
   145  
   146  	for i, j /* ERROR "declared but not used" */ := range s {
   147  		_ = func() int {
   148  			return i
   149  		}
   150  	}
   151  	return
   152  }
   153  
   154  // Invalid (unused) expressions must not lead to spurious "declared but not used errors"
   155  func _() {
   156  	var a, b, c int
   157  	var x, y int
   158  	x, y = a /* ERROR cannot assign [0-9]+ values to [0-9]+ variables */ , b, c
   159  	_ = x
   160  	_ = y
   161  }
   162  
   163  func _() {
   164  	var x int
   165  	return x /* ERROR no result values expected */
   166  	return math /* ERROR no result values expected */ .Sin(0)
   167  }
   168  
   169  func _() int {
   170  	var x, y int
   171  	return /* ERROR wrong number of return values */ x, y
   172  }
   173  
   174  // Short variable declarations must declare at least one new non-blank variable.
   175  func _() {
   176  	_ := /* ERROR no new variables */ 0
   177  	_, a := 0, 1
   178  	_, a := /* ERROR no new variables */ 0, 1
   179  	_, a, b := 0, 1, 2
   180  	_, _, _ := /* ERROR no new variables */ 0, 1, 2
   181  
   182  	_ = a
   183  	_ = b
   184  }
   185  
   186  // TODO(gri) consolidate other var decl checks in this file