summaryrefslogtreecommitdiffstats
path: root/src/internal/types/testdata/check/constdecl.go
blob: faa9b9d5cb8fa359960fdbca2ae465ad76d850ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package constdecl

import "math"
import "unsafe"

var v int

// Const decls must be initialized by constants.
const _ = v /* ERROR "not constant" */
const _ = math /* ERROR "not constant" */ .Sin(0)
const _ = int /* ERROR "not an expression" */

func _() {
	const _ = v /* ERROR "not constant" */
	const _ = math /* ERROR "not constant" */ .Sin(0)
	const _ = int /* ERROR "not an expression" */
}

// Identifier and expression arity must match.
const _ /* ERROR "missing init expr for _" */
const _ = 1, 2 /* ERROR "extra init expr 2" */

const _ /* ERROR "missing init expr for _" */ int
const _ int = 1, 2 /* ERROR "extra init expr 2" */

const (
	_ /* ERROR "missing init expr for _" */
	_ = 1, 2 /* ERROR "extra init expr 2" */

	_ /* ERROR "missing init expr for _" */ int
	_ int = 1, 2 /* ERROR "extra init expr 2" */
)

const (
	_ = 1
	_
	_, _ /* ERROR "missing init expr for _" */
	_
)

const (
	_, _ = 1, 2
	_, _
	_ /* ERROR "extra init expr at" */
	_, _
	_, _, _ /* ERROR "missing init expr for _" */
	_, _
)

func _() {
	const _ /* ERROR "missing init expr for _" */
	const _ = 1, 2 /* ERROR "extra init expr 2" */

	const _ /* ERROR "missing init expr for _" */ int
	const _ int = 1, 2 /* ERROR "extra init expr 2" */

	const (
		_ /* ERROR "missing init expr for _" */
		_ = 1, 2 /* ERROR "extra init expr 2" */

		_ /* ERROR "missing init expr for _" */ int
		_ int = 1, 2 /* ERROR "extra init expr 2" */
	)

	const (
		_ = 1
		_
		_, _ /* ERROR "missing init expr for _" */
		_
	)

	const (
		_, _ = 1, 2
		_, _
		_ /* ERROR "extra init expr at" */
		_, _
		_, _, _ /* ERROR "missing init expr for _" */
		_, _
	)
}

// Test case for constant with invalid initialization.
// Caused panic because the constant value was not set up (gri - 7/8/2014).
func _() {
	const (
	    x string = missing /* ERROR "undefined" */
	    y = x + ""
	)
}

// Test case for constants depending on function literals (see also #22992).
const A /* ERROR initialization cycle */ = unsafe.Sizeof(func() { _ = A })

func _() {
	// The function literal below must not see a.
	const a = unsafe.Sizeof(func() { _ = a /* ERROR "undefined" */ })
	const b = unsafe.Sizeof(func() { _ = a })

	// The function literal below must not see x, y, or z.
	const x, y, z = 0, 1, unsafe.Sizeof(func() { _ = x /* ERROR "undefined" */ + y /* ERROR "undefined" */ + z /* ERROR "undefined" */ })
}

// Test cases for errors in inherited constant initialization expressions.
// Errors related to inherited initialization expressions must appear at
// the constant identifier being declared, not at the original expression
// (issues #42991, #42992).
const (
	_ byte = 255 + iota
	/* some gap */
	_ // ERROR overflows
	/* some gap */
	/* some gap */ _ /* ERROR overflows */; _ /* ERROR overflows */
	/* some gap */
	_ = 255 + iota
	_ = byte /* ERROR overflows */ (255) + iota
	_ /* ERROR overflows */
)

// Test cases from issue.
const (
	ok = byte(iota + 253)
	bad
	barn
	bard // ERROR cannot convert
)

const (
	c = len([1 - iota]int{})
	d
	e // ERROR invalid array length
	f // ERROR invalid array length
)

// Test that identifiers in implicit (omitted) RHS
// expressions of constant declarations are resolved
// in the correct context; see issues #49157, #53585.
const X = 2

func _() {
	const (
		A    = iota // 0
		iota = iota // 1
		B           // 1 (iota is declared locally on prev. line)
		C           // 1
	)
	assert(A == 0 && B == 1 && C == 1)

	const (
		X = X + X
		Y
		Z = iota
	)
	assert(X == 4 && Y == 8 && Z == 1)
}

// TODO(gri) move extra tests from testdata/const0.src into here