1
// Copyright 2012 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.
5
// constant declarations
9
// constants declarations must be initialized by constants
11
const c0 = x /* ERROR "not constant" */
13
// typed constants must have constant types
14
const _ interface /* ERROR invalid constant type */ {} = 0
17
const _ interface /* ERROR invalid constant type */ {} = 0
18
for i := 0; i < 10; i++ {} // don't crash with non-nil iota here
28
ub4 = true /* ERROR "cannot convert" */ == 0
34
ui3 = 3141592653589793238462643383279502884197169399375105820974944592307816406286
43
ui10 = 1 / 0 /* ERROR "division by zero" */
44
ui11 = ui1 / 0 /* ERROR "division by zero" */
45
ui12 = ui3 / ui0 /* ERROR "division by zero" */
46
ui13 = 1 % 0 /* ERROR "division by zero" */
47
ui14 = ui1 % 0 /* ERROR "division by zero" */
48
ui15 = ui3 % ui0 /* ERROR "division by zero" */
53
ui19 = 1 /* ERROR "invalid operation" */ % 1.0
55
// floating point values
59
uf3 = 3.141592653589793238462643383279502884197169399375105820974944592307816406286
66
uf9 = uf3 /* ERROR "not defined" */ % uf3
68
uf10 = 1 / 0 /* ERROR "division by zero" */
69
uf11 = uf1 / 0 /* ERROR "division by zero" */
70
uf12 = uf3 / uf0 /* ERROR "division by zero" */
72
uf16 = uf2 /* ERROR "not defined" */ & uf3
73
uf17 = uf2 /* ERROR "not defined" */ | uf3
74
uf18 = uf2 /* ERROR "not defined" */ ^ uf3
80
uc3 = 3.141592653589793238462643383279502884197169399375105820974944592307816406286i
87
uc9 = uc3 /* ERROR "not defined" */ % uc3
89
uc10 = 1 / 0 /* ERROR "division by zero" */
90
uc11 = uc1 / 0 /* ERROR "division by zero" */
91
uc12 = uc3 / uc0 /* ERROR "division by zero" */
93
uc16 = uc2 /* ERROR "not defined" */ & uc3
94
uc17 = uc2 /* ERROR "not defined" */ | uc3
95
uc18 = uc2 /* ERROR "not defined" */ ^ uc3
111
tb3 mybool = ti1 /* ERROR "mismatched types" */ == tf1
117
ti3 myint = ui3 /* ERROR "overflows" */
120
ti5 = ti0 /* ERROR "mismatched types" */ + ti1
122
ti7 = ti2 /* ERROR "mismatched types" */ * ti1
126
ti10 = 1 / 0 /* ERROR "division by zero" */
127
ti11 = ti1 / 0 /* ERROR "division by zero" */
128
ti12 = ti3 /* ERROR "mismatched types" */ / ti0
129
ti13 = 1 % 0 /* ERROR "division by zero" */
130
ti14 = ti1 % 0 /* ERROR "division by zero" */
131
ti15 = ti3 /* ERROR "mismatched types" */ % ti0
133
ti16 = ti2 /* ERROR "mismatched types" */ & ti3
134
ti17 = ti2 /* ERROR "mismatched types" */ | ti4
135
ti18 = ti2 ^ ti5 // no mismatched types error because the type of ti5 is unknown
137
// floating point values
141
tf3 myfloat = 3.141592653589793238462643383279502884197169399375105820974944592307816406286
146
tf7 = tf2 /* ERROR "mismatched types" */ * tf1
148
tf9 = tf3 /* ERROR "not defined" */ % tf3
150
tf10 = 1 / 0 /* ERROR "division by zero" */
151
tf11 = tf1 / 0 /* ERROR "division by zero" */
152
tf12 = tf3 /* ERROR "mismatched types" */ / tf0
154
tf16 = tf2 /* ERROR "mismatched types" */ & tf3
155
tf17 = tf2 /* ERROR "mismatched types" */ | tf3
156
tf18 = tf2 /* ERROR "mismatched types" */ ^ tf3
162
tc3 = 3.141592653589793238462643383279502884197169399375105820974944592307816406286i
169
tc9 = tc3 /* ERROR "not defined" */ % tc3
171
tc10 = 1 / 0 /* ERROR "division by zero" */
172
tc11 = tc1 / 0 /* ERROR "division by zero" */
173
tc12 = tc3 / tc0 /* ERROR "division by zero" */
175
tc16 = tc2 /* ERROR "not defined" */ & tc3
176
tc17 = tc2 /* ERROR "not defined" */ | tc3
177
tc18 = tc2 /* ERROR "not defined" */ ^ tc3
180
// initialization cycles
182
a /* ERROR "initialization cycle" */ = a
183
b /* ERROR "initialization cycle" */ , c /* ERROR "initialization cycle" */, d, e = e, d, c, b // TODO(gri) should only have one cycle error
187
// multiple initialization
189
a1, a2, a3 = 7, 3.1415926, "foo"
190
b1, b2, b3 = b3, b1, 42
191
c1, c2, c3 /* ERROR "missing init expr for c3" */ = 1, 2
192
d1, d2, d3 = 1, 2, 3, 4 /* ERROR "extra init expr 4" */
193
_p0 = assert(a1 == 7)
194
_p1 = assert(a2 == 3.1415926)
195
_p2 = assert(a3 == "foo")
196
_p3 = assert(b1 == 42)
197
_p4 = assert(b2 == 42)
198
_p5 = assert(b3 == 42)
203
a1, a2, a3 = 7, 3.1415926, "foo"
204
b1, b2, b3 = b3, b1, 42
205
c1, c2, c3 /* ERROR "missing init expr for c3" */ = 1, 2
206
d1, d2, d3 = 1, 2, 3, 4 /* ERROR "extra init expr 4" */
207
_p0 = assert(a1 == 7)
208
_p1 = assert(a2 == 3.1415926)
209
_p2 = assert(a3 == "foo")
210
_p3 = assert(b1 == 42)
211
_p4 = assert(b2 == 42)
212
_p5 = assert(b3 == 42)
221
_a0 = assert(iota0 == 0)
222
_a1 = assert(iota1 == 1)
223
_a2 = assert(iota2 == 4)
228
_a3 = assert(iota7 == 21)
229
_a4 = assert(iota8 == 24)
234
_b1 = assert(iota + iota2 == 5)
235
_b2 = len([iota]int{}) // iota may appear in a type!
236
_b3 = assert(_b2 == 2)
240
type A [iota /* ERROR "cannot use iota" */ ]int
242
// constant expressions with operands accross different
243
// constant declarations must use the right iota values
248
_x = _c2 + _d1 + _e0 // 3
260
var _ = assert(_x == 3)
264
_n0 = nil /* ERROR "not constant" */
265
_n1 = [ /* ERROR "not constant" */ ]int{}
268
// iotas must not be usable in expressions outside constant declarations
269
type _ [iota /* ERROR "iota outside constant decl" */ ]byte
270
var _ = iota /* ERROR "iota outside constant decl" */
272
_ = iota /* ERROR "iota outside constant decl" */
274
_ = iota /* ERROR "iota outside constant decl" */
279
const x = iota /* ERROR "is not constant" */