Black Lives Matter. Support the Equal Justice Initiative.

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

Documentation: go/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 constdecl
     6  
     7  import "math"
     8  import "unsafe"
     9  
    10  var v int
    11  
    12  // Const decls must be initialized by constants.
    13  const _ = v /* ERROR "not constant" */
    14  const _ = math /* ERROR "not constant" */ .Sin(0)
    15  const _ = int /* ERROR "not an expression" */
    16  
    17  func _() {
    18  	const _ = v /* ERROR "not constant" */
    19  	const _ = math /* ERROR "not constant" */ .Sin(0)
    20  	const _ = int /* ERROR "not an expression" */
    21  }
    22  
    23  // Identifier and expression arity must match.
    24  // The first error message is produced by the parser.
    25  // In a real-world scenario, the type-checker would not be run
    26  // in this case and the 2nd error message would not appear.
    27  const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */
    28  const _ = 1, 2 /* ERROR "extra init expr 2" */
    29  
    30  const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int
    31  const _ int = 1, 2 /* ERROR "extra init expr 2" */
    32  
    33  const (
    34  	_ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */
    35  	_ = 1, 2 /* ERROR "extra init expr 2" */
    36  
    37  	_ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int
    38  	_ int = 1, 2 /* ERROR "extra init expr 2" */
    39  )
    40  
    41  const (
    42  	_ = 1
    43  	_
    44  	_, _ /* ERROR "missing init expr for _" */
    45  	_
    46  )
    47  
    48  const (
    49  	_, _ = 1, 2
    50  	_, _
    51  	_ /* ERROR "extra init expr at" */
    52  	_, _
    53  	_, _, _ /* ERROR "missing init expr for _" */
    54  	_, _
    55  )
    56  
    57  func _() {
    58  	const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */
    59  	const _ = 1, 2 /* ERROR "extra init expr 2" */
    60  
    61  	const _ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int
    62  	const _ int = 1, 2 /* ERROR "extra init expr 2" */
    63  
    64  	const (
    65  		_ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */
    66  		_ = 1, 2 /* ERROR "extra init expr 2" */
    67  
    68  		_ /* ERROR "missing constant value" */ /* ERROR "missing init expr for _" */ int
    69  		_ int = 1, 2 /* ERROR "extra init expr 2" */
    70  	)
    71  
    72  	const (
    73  		_ = 1
    74  		_
    75  		_, _ /* ERROR "missing init expr for _" */
    76  		_
    77  	)
    78  
    79  	const (
    80  		_, _ = 1, 2
    81  		_, _
    82  		_ /* ERROR "extra init expr at" */
    83  		_, _
    84  		_, _, _ /* ERROR "missing init expr for _" */
    85  		_, _
    86  	)
    87  }
    88  
    89  // Test case for constant with invalid initialization.
    90  // Caused panic because the constant value was not set up (gri - 7/8/2014).
    91  func _() {
    92  	const (
    93  	    x string = missing /* ERROR "undeclared name" */
    94  	    y = x + ""
    95  	)
    96  }
    97  
    98  // Test case for constants depending on function literals (see also #22992).
    99  const A /* ERROR initialization cycle */ = unsafe.Sizeof(func() { _ = A })
   100  
   101  func _() {
   102  	// The function literal below must not see a.
   103  	const a = unsafe.Sizeof(func() { _ = a /* ERROR "undeclared name" */ })
   104  	const b = unsafe.Sizeof(func() { _ = a })
   105  
   106  	// The function literal below must not see x, y, or z.
   107  	const x, y, z = 0, 1, unsafe.Sizeof(func() { _ = x /* ERROR "undeclared name" */ + y /* ERROR "undeclared name" */ + z /* ERROR "undeclared name" */ })
   108  }
   109  
   110  // Test cases for errors in inherited constant initialization expressions.
   111  // Errors related to inherited initialization expressions must appear at
   112  // the constant identifier being declared, not at the original expression
   113  // (issues #42991, #42992).
   114  const (
   115  	_ byte = 255 + iota
   116  	/* some gap */
   117  	_ // ERROR overflows
   118  	/* some gap */
   119  	/* some gap */ _ /* ERROR overflows */; _ /* ERROR overflows */
   120  	/* some gap */
   121  	_ = 255 + iota
   122  	_ = byte /* ERROR overflows */ (255) + iota
   123  	_ /* ERROR overflows */
   124  )
   125  
   126  // Test cases from issue.
   127  const (
   128  	ok = byte(iota + 253)
   129  	bad
   130  	barn
   131  	bard // ERROR cannot convert
   132  )
   133  
   134  const (
   135  	c = len([1 - iota]int{})
   136  	d
   137  	e // ERROR invalid array length
   138  	f // ERROR invalid array length
   139  )
   140  
   141  // TODO(gri) move extra tests from testdata/const0.src into here
   142  

View as plain text