summaryrefslogtreecommitdiff
path: root/libgo/go/go/types/testdata/const0.src
blob: a2ca344c788e6c0eb98090d6f62e68bbfa3caea5 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2012 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.

// constant declarations

package const0

// constants declarations must be initialized by constants
var x = 0
const c0 = x /* ERROR "not constant" */

// untyped constants
const (
	// boolean values
	ub0 = false
	ub1 = true
	ub2 = 2 < 1
	ub3 = ui1 == uf1
	ub4 = true /* ERROR "cannot convert" */ == 0

	// integer values
	ui0 = 0
	ui1 = 1
	ui2 = 42
	ui3 = 3141592653589793238462643383279502884197169399375105820974944592307816406286
	ui4 = -10

	ui5 = ui0 + ui1
	ui6 = ui1 - ui1
	ui7 = ui2 * ui1
	ui8 = ui3 / ui3
	ui9 = ui3 % ui3

	ui10 = 1 / 0 /* ERROR "division by zero" */
	ui11 = ui1 / 0 /* ERROR "division by zero" */
	ui12 = ui3 / ui0 /* ERROR "division by zero" */
	ui13 = 1 % 0 /* ERROR "division by zero" */
	ui14 = ui1 % 0 /* ERROR "division by zero" */
	ui15 = ui3 % ui0 /* ERROR "division by zero" */

	ui16 = ui2 & ui3
	ui17 = ui2 | ui3
	ui18 = ui2 ^ ui3

	// floating point values
	uf0 = 0.
	uf1 = 1.
	uf2 = 4.2e1
	uf3 = 3.141592653589793238462643383279502884197169399375105820974944592307816406286
	uf4 = 1e-1

	uf5 = uf0 + uf1
	uf6 = uf1 - uf1
	uf7 = uf2 * uf1
	uf8 = uf3 / uf3
	uf9 = uf3 /* ERROR "not defined" */ % uf3

	uf10 = 1 / 0 /* ERROR "division by zero" */
	uf11 = uf1 / 0 /* ERROR "division by zero" */
	uf12 = uf3 / uf0 /* ERROR "division by zero" */

	uf16 = uf2 /* ERROR "not defined" */ & uf3
	uf17 = uf2 /* ERROR "not defined" */ | uf3
	uf18 = uf2 /* ERROR "not defined" */ ^ uf3

	// complex values
	uc0 = 0.i
	uc1 = 1.i
	uc2 = 4.2e1i
	uc3 = 3.141592653589793238462643383279502884197169399375105820974944592307816406286i
	uc4 = 1e-1i

	uc5 = uc0 + uc1
	uc6 = uc1 - uc1
	uc7 = uc2 * uc1
	uc8 = uc3 / uc3
	uc9 = uc3 /* ERROR "not defined" */ % uc3

	uc10 = 1 / 0 /* ERROR "division by zero" */
	uc11 = uc1 / 0 /* ERROR "division by zero" */
	uc12 = uc3 / uc0 /* ERROR "division by zero" */

	uc16 = uc2 /* ERROR "not defined" */ & uc3
	uc17 = uc2 /* ERROR "not defined" */ | uc3
	uc18 = uc2 /* ERROR "not defined" */ ^ uc3
)

type (
	mybool bool
	myint int
	myfloat float64
	mycomplex complex128
)

// typed constants
const (
	// boolean values
	tb0 bool = false
	tb1 bool = true
	tb2 mybool = 2 < 1
	tb3 mybool = ti1 /* ERROR "cannot compare" */ == tf1

	// integer values
	ti0 int8 = ui0
	ti1 int32 = ui1
	ti2 int64 = ui2
	ti3 myint = ui3 /* ERROR "overflows" */
	ti4 myint = ui4

	ti5 = ti0 /* ERROR "mismatched types" */ + ti1
	ti6 = ti1 - ti1
	ti7 = ti2 /* ERROR "mismatched types" */ * ti1
	//ti8 = ti3 / ti3 // TODO(gri) enable this
	//ti9 = ti3 % ti3 // TODO(gri) enable this

	ti10 = 1 / 0 /* ERROR "division by zero" */
	ti11 = ti1 / 0 /* ERROR "division by zero" */
	ti12 = ti3 /* ERROR "mismatched types" */ / ti0
	ti13 = 1 % 0 /* ERROR "division by zero" */
	ti14 = ti1 % 0 /* ERROR "division by zero" */
	ti15 = ti3 /* ERROR "mismatched types" */ % ti0

	ti16 = ti2 /* ERROR "mismatched types" */ & ti3
	ti17 = ti2 /* ERROR "mismatched types" */ | ti4
	ti18 = ti2 ^ ti5 // no mismatched types error because the type of ti5 is unknown

	// floating point values
	tf0 float32 = 0.
	tf1 float32 = 1.
	tf2 float64 = 4.2e1
	tf3 myfloat = 3.141592653589793238462643383279502884197169399375105820974944592307816406286
	tf4 myfloat = 1e-1

	tf5 = tf0 + tf1
	tf6 = tf1 - tf1
	tf7 = tf2 /* ERROR "mismatched types" */ * tf1
	// tf8 = tf3 / tf3 // TODO(gri) enable this
	tf9 = tf3 /* ERROR "not defined" */ % tf3

	tf10 = 1 / 0 /* ERROR "division by zero" */
	tf11 = tf1 / 0 /* ERROR "division by zero" */
	tf12 = tf3 /* ERROR "mismatched types" */ / tf0

	tf16 = tf2 /* ERROR "mismatched types" */ & tf3
	tf17 = tf2 /* ERROR "mismatched types" */ | tf3
	tf18 = tf2 /* ERROR "mismatched types" */ ^ tf3

	// complex values
	tc0 = 0.i
	tc1 = 1.i
	tc2 = 4.2e1i
	tc3 = 3.141592653589793238462643383279502884197169399375105820974944592307816406286i
	tc4 = 1e-1i

	tc5 = tc0 + tc1
	tc6 = tc1 - tc1
	tc7 = tc2 * tc1
	tc8 = tc3 / tc3
	tc9 = tc3 /* ERROR "not defined" */ % tc3

	tc10 = 1 / 0 /* ERROR "division by zero" */
	tc11 = tc1 / 0 /* ERROR "division by zero" */
	tc12 = tc3 / tc0 /* ERROR "division by zero" */

	tc16 = tc2 /* ERROR "not defined" */ & tc3
	tc17 = tc2 /* ERROR "not defined" */ | tc3
	tc18 = tc2 /* ERROR "not defined" */ ^ tc3
)

// initialization cycles
const (
	a /* ERROR "cycle" */ = a
	b /* ERROR "cycle" */ , c /* ERROR "cycle" */, d, e = e, d, c, b // TODO(gri) should only have one cycle error
	f float64 = d
)

// multiple initialization
const (
	a1, a2, a3 = 7, 3.1415926, "foo"
	b1, b2, b3 = b3, b1, 42
	_p0 = assert(a1 == 7)
	_p1 = assert(a2 == 3.1415926)
	_p2 = assert(a3 == "foo")
	_p3 = assert(b1 == 42)
	_p4 = assert(b2 == 42)
	_p5 = assert(b3 == 42)
)

// iota
const (
	iota0 = iota
	iota1 = iota
	iota2 = iota*2
	_a0 = assert(iota0 == 0)
	_a1 = assert(iota1 == 1)
	_a2 = assert(iota2 == 4)
	iota6 = iota*3

	iota7
	iota8
	_a3 = assert(iota7 == 21)
	_a4 = assert(iota8 == 24)
)

const (
	_b0 = iota
	_b1 = assert(iota + iota2 == 5)
)

// special cases
const (
	_n0 = nil /* ERROR "invalid constant type" */
	_n1 = [ /* ERROR "not constant" */ ]int{}
)