...

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

Documentation: internal/types/testdata/check

     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  var _; /* ERROR "expected type" */
    18  var _, _; /* ERROR "expected type" */
    19  var _, _, _; /* ERROR "expected type" */
    20  
    21  // The initializer must be an expression.
    22  var _ = int /* ERROR "not an expression" */
    23  var _ = f /* ERROR "used as value" */ ()
    24  
    25  // Identifier and expression arity must match.
    26  var _, _ = 1, 2
    27  var _ = 1, 2 /* ERROR "extra init expr 2" */
    28  var _, _ = 1 /* ERRORx `assignment mismatch: [1-9]+ variables but.*[1-9]+ value(s)?` */
    29  var _, _, _ /* ERROR "missing init expr for _" */ = 1, 2
    30  
    31  var _ = g /* ERROR "multiple-value g" */ ()
    32  var _, _ = g()
    33  var _, _, _ = g /* ERRORx `assignment mismatch: [1-9]+ variables but.*[1-9]+ value(s)?` */ ()
    34  
    35  var _ = m["foo"]
    36  var _, _ = m["foo"]
    37  var _, _, _ = m  /* ERRORx `assignment mismatch: [1-9]+ variables but.*[1-9]+ value(s)?` */ ["foo"]
    38  
    39  var _, _ int = 1, 2
    40  var _ int = 1, 2 /* ERROR "extra init expr 2" */
    41  var _, _ int = 1 /* ERRORx `assignment mismatch: [1-9]+ variables but.*[1-9]+ value(s)?` */
    42  var _, _, _ /* ERROR "missing init expr for _" */ int = 1, 2
    43  
    44  var (
    45  	_, _ = 1, 2
    46  	_ = 1, 2 /* ERROR "extra init expr 2" */
    47  	_, _ = 1 /* ERRORx `assignment mismatch: [1-9]+ variables but.*[1-9]+ value(s)?` */
    48  	_, _, _ /* ERROR "missing init expr for _" */ = 1, 2
    49  
    50  	_ = g /* ERROR "multiple-value g" */ ()
    51  	_, _ = g()
    52  	_, _, _ = g /* ERRORx `assignment mismatch: [1-9]+ variables but.*[1-9]+ value(s)?` */ ()
    53  
    54  	_ = m["foo"]
    55  	_, _ = m["foo"]
    56  	_, _, _ = m /* ERRORx `assignment mismatch: [1-9]+ variables but.*[1-9]+ value(s)?` */ ["foo"]
    57  
    58  	_, _ int = 1, 2
    59  	_ int = 1, 2 /* ERROR "extra init expr 2" */
    60  	_, _ int = 1 /* ERRORx `assignment mismatch: [1-9]+ variables but.*[1-9]+ value(s)?` */
    61  	_, _, _ /* ERROR "missing init expr for _" */ int = 1, 2
    62  )
    63  
    64  // Variables declared in function bodies must be 'used'.
    65  type T struct{}
    66  func (r T) _(a, b, c int) (u, v, w int) {
    67  	var x1 /* ERROR "declared and not used" */ int
    68  	var x2 /* ERROR "declared and not used" */ int
    69  	x1 = 1
    70  	(x2) = 2
    71  
    72  	y1 /* ERROR "declared and not used" */ := 1
    73  	y2 /* ERROR "declared and not used" */ := 2
    74  	y1 = 1
    75  	(y1) = 2
    76  
    77  	{
    78  		var x1 /* ERROR "declared and not used" */ int
    79  		var x2 /* ERROR "declared and not used" */ int
    80  		x1 = 1
    81  		(x2) = 2
    82  
    83  		y1 /* ERROR "declared and not used" */ := 1
    84  		y2 /* ERROR "declared and not used" */ := 2
    85  		y1 = 1
    86  		(y1) = 2
    87  	}
    88  
    89  	if x /* ERROR "declared and not used" */ := 0; a < b {}
    90  
    91  	switch x /* ERROR "declared and not used" */, y := 0, 1; a {
    92  	case 0:
    93  		_ = y
    94  	case 1:
    95  		x /* ERROR "declared and not used" */ := 0
    96  	}
    97  
    98  	var t interface{}
    99  	switch t /* ERROR "declared and not used" */ := t.(type) {}
   100  
   101  	switch t /* ERROR "declared and not used" */ := t.(type) {
   102  	case int:
   103  	}
   104  
   105  	switch t /* ERROR "declared and not used" */ := t.(type) {
   106  	case int:
   107  	case float32, complex64:
   108  		t = nil
   109  	}
   110  
   111  	switch t := t.(type) {
   112  	case int:
   113  	case float32, complex64:
   114  		_ = t
   115  	}
   116  
   117  	switch t := t.(type) {
   118  	case int:
   119  	case float32:
   120  	case string:
   121  		_ = func() string {
   122  			return t
   123  		}
   124  	}
   125  
   126  	switch t := t; t /* ERROR "declared and not used" */ := t.(type) {}
   127  
   128  	var z1 /* ERROR "declared and not used" */ int
   129  	var z2 int
   130  	_ = func(a, b, c int) (u, v, w int) {
   131  		z1 = a
   132  		(z1) = b
   133  		a = z2
   134  		return
   135  	}
   136  
   137  	var s []int
   138  	var i /* ERROR "declared and not used" */ , j int
   139  	for i, j = range s {
   140  		_ = j
   141  	}
   142  
   143  	for i, j /* ERROR "declared and not used" */ := range s {
   144  		_ = func() int {
   145  			return i
   146  		}
   147  	}
   148  	return
   149  }
   150  
   151  // Unused variables in function literals must lead to only one error (issue #22524).
   152  func _() {
   153  	_ = func() {
   154  		var x /* ERROR "declared and not used" */ int
   155  	}
   156  }
   157  
   158  // Invalid variable declarations must not lead to "declared and not used errors".
   159  // TODO(gri) enable these tests once go/types follows types2 logic for declared and not used variables
   160  // func _() {
   161  //	var a x                        // DISABLED_ERROR undefined: x
   162  //	var b = x                      // DISABLED_ERROR undefined: x
   163  //	var c int = x                  // DISABLED_ERROR undefined: x
   164  //	var d, e, f x                  /* DISABLED_ERROR x */ /* DISABLED_ERROR x */ /* DISABLED_ERROR x */
   165  //	var g, h, i = x, x, x          /* DISABLED_ERROR x */ /* DISABLED_ERROR x */ /* DISABLED_ERROR x */
   166  //	var j, k, l float32 = x, x, x  /* DISABLED_ERROR x */ /* DISABLED_ERROR x */ /* DISABLED_ERROR x */
   167  //	// but no "declared and not used" errors
   168  // }
   169  
   170  // Invalid (unused) expressions must not lead to spurious "declared and not used errors".
   171  func _() {
   172  	var a, b, c int
   173  	var x, y int
   174  	x, y = a /* ERRORx `assignment mismatch: [1-9]+ variables but.*[1-9]+ value(s)?` */ , b, c
   175  	_ = x
   176  	_ = y
   177  }
   178  
   179  func _() {
   180  	var x int
   181  	return x /* ERROR "too many return values" */
   182  	return math /* ERROR "too many return values" */ .Sin(0)
   183  }
   184  
   185  func _() int {
   186  	var x, y int
   187  	return x, y /* ERROR "too many return values" */
   188  }
   189  
   190  // Short variable declarations must declare at least one new non-blank variable.
   191  func _() {
   192  	_ := /* ERROR "no new variables" */ 0
   193  	_, a := 0, 1
   194  	_, a := /* ERROR "no new variables" */ 0, 1
   195  	_, a, b := 0, 1, 2
   196  	_, _, _ := /* ERROR "no new variables" */ 0, 1, 2
   197  
   198  	_ = a
   199  	_ = b
   200  }
   201  
   202  // Test case for variables depending on function literals (see also #22992).
   203  var A /* ERROR "initialization cycle" */ = func() int { return A }()
   204  
   205  func _() {
   206  	// The function literal below must not see a.
   207  	var a = func() int { return a /* ERROR "undefined" */ }()
   208  	var _ = func() int { return a }()
   209  
   210  	// The function literal below must not see x, y, or z.
   211  	var x, y, z = 0, 1, func() int { return x /* ERROR "undefined" */ + y /* ERROR "undefined" */ + z /* ERROR "undefined" */ }()
   212  	_, _, _ = x, y, z
   213  }
   214  
   215  // TODO(gri) consolidate other var decl checks in this file

View as plain text