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

     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  const _ /* ERROR "missing init expr for _" */
    25  const _ = 1, 2 /* ERROR "extra init expr 2" */
    26  
    27  const _ /* ERROR "missing init expr for _" */ int
    28  const _ int = 1, 2 /* ERROR "extra init expr 2" */
    29  
    30  const (
    31  	_ /* ERROR "missing init expr for _" */
    32  	_ = 1, 2 /* ERROR "extra init expr 2" */
    33  
    34  	_ /* ERROR "missing init expr for _" */ int
    35  	_ int = 1, 2 /* ERROR "extra init expr 2" */
    36  )
    37  
    38  const (
    39  	_ = 1
    40  	_
    41  	_, _ /* ERROR "missing init expr for _" */
    42  	_
    43  )
    44  
    45  const (
    46  	_, _ = 1, 2
    47  	_, _
    48  	_ /* ERROR "extra init expr at" */
    49  	_, _
    50  	_, _, _ /* ERROR "missing init expr for _" */
    51  	_, _
    52  )
    53  
    54  func _() {
    55  	const _ /* ERROR "missing init expr for _" */
    56  	const _ = 1, 2 /* ERROR "extra init expr 2" */
    57  
    58  	const _ /* ERROR "missing init expr for _" */ int
    59  	const _ int = 1, 2 /* ERROR "extra init expr 2" */
    60  
    61  	const (
    62  		_ /* ERROR "missing init expr for _" */
    63  		_ = 1, 2 /* ERROR "extra init expr 2" */
    64  
    65  		_ /* ERROR "missing init expr for _" */ int
    66  		_ int = 1, 2 /* ERROR "extra init expr 2" */
    67  	)
    68  
    69  	const (
    70  		_ = 1
    71  		_
    72  		_, _ /* ERROR "missing init expr for _" */
    73  		_
    74  	)
    75  
    76  	const (
    77  		_, _ = 1, 2
    78  		_, _
    79  		_ /* ERROR "extra init expr at" */
    80  		_, _
    81  		_, _, _ /* ERROR "missing init expr for _" */
    82  		_, _
    83  	)
    84  }
    85  
    86  // Test case for constant with invalid initialization.
    87  // Caused panic because the constant value was not set up (gri - 7/8/2014).
    88  func _() {
    89  	const (
    90  	    x string = missing /* ERROR "undefined" */
    91  	    y = x + ""
    92  	)
    93  }
    94  
    95  // Test case for constants depending on function literals (see also #22992).
    96  const A /* ERROR "initialization cycle" */ = unsafe.Sizeof(func() { _ = A })
    97  
    98  func _() {
    99  	// The function literal below must not see a.
   100  	const a = unsafe.Sizeof(func() { _ = a /* ERROR "undefined" */ })
   101  	const b = unsafe.Sizeof(func() { _ = a })
   102  
   103  	// The function literal below must not see x, y, or z.
   104  	const x, y, z = 0, 1, unsafe.Sizeof(func() { _ = x /* ERROR "undefined" */ + y /* ERROR "undefined" */ + z /* ERROR "undefined" */ })
   105  }
   106  
   107  // Test cases for errors in inherited constant initialization expressions.
   108  // Errors related to inherited initialization expressions must appear at
   109  // the constant identifier being declared, not at the original expression
   110  // (issues #42991, #42992).
   111  const (
   112  	_ byte = 255 + iota
   113  	/* some gap */
   114  	_ // ERROR "overflows"
   115  	/* some gap */
   116  	/* some gap */ _ /* ERROR "overflows" */; _ /* ERROR "overflows" */
   117  	/* some gap */
   118  	_ = 255 + iota
   119  	_ = byte /* ERROR "overflows" */ (255) + iota
   120  	_ /* ERROR "overflows" */
   121  )
   122  
   123  // Test cases from issue.
   124  const (
   125  	ok = byte(iota + 253)
   126  	bad
   127  	barn
   128  	bard // ERROR "overflows"
   129  )
   130  
   131  const (
   132  	c = len([1 - iota]int{})
   133  	d
   134  	e // ERROR "invalid array length"
   135  	f // ERROR "invalid array length"
   136  )
   137  
   138  // Test that identifiers in implicit (omitted) RHS
   139  // expressions of constant declarations are resolved
   140  // in the correct context; see issues #49157, #53585.
   141  const X = 2
   142  
   143  func _() {
   144  	const (
   145  		A    = iota // 0
   146  		iota = iota // 1
   147  		B           // 1 (iota is declared locally on prev. line)
   148  		C           // 1
   149  	)
   150  	assert(A == 0 && B == 1 && C == 1)
   151  
   152  	const (
   153  		X = X + X
   154  		Y
   155  		Z = iota
   156  	)
   157  	assert(X == 4 && Y == 8 && Z == 1)
   158  }
   159  
   160  // TODO(gri) move extra tests from testdata/const0.src into here
   161  

View as plain text