aboutsummaryrefslogtreecommitdiff
path: root/src/go/types/testdata/const1.src
blob: 56b6bd4ca55e75773e5cea3a96f88b788775ce05 (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// 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 conversions

package const1

const(
	mi = ^int(0)
	mu = ^uint(0)
	mp = ^uintptr(0)

	logSizeofInt     = uint(mi>>8&1 + mi>>16&1 + mi>>32&1)
	logSizeofUint    = uint(mu>>8&1 + mu>>16&1 + mu>>32&1)
	logSizeofUintptr = uint(mp>>8&1 + mp>>16&1 + mp>>32&1)
)

const (
	minInt8 = -1<<(8<<iota - 1)
	minInt16
	minInt32
	minInt64
	minInt = -1<<(8<<logSizeofInt - 1)
)

const (
	maxInt8 = 1<<(8<<iota - 1) - 1
	maxInt16
	maxInt32
	maxInt64
	maxInt = 1<<(8<<logSizeofInt - 1) - 1
)

const (
	maxUint8 = 1<<(8<<iota) - 1
	maxUint16
	maxUint32
	maxUint64
	maxUint    = 1<<(8<<logSizeofUint) - 1
	maxUintptr = 1<<(8<<logSizeofUintptr) - 1
)

const (
	smallestFloat32 = 1.0 / (1<<(127 - 1 + 23))
	// TODO(gri) The compiler limits integers to 512 bit and thus
	//           we cannot compute the value (1<<(1023 - 1 + 52))
	//           without overflow. For now we match the compiler.
	//           See also issue #44057.
	// smallestFloat64 = 1.0 / (1<<(1023 - 1 + 52))
	smallestFloat64 = 4.940656458412465441765687928682213723651e-324
)

const (
	_ = assert(smallestFloat32 > 0)
	_ = assert(smallestFloat64 > 0)
)

const (
	maxFloat32 = 1<<127 * (1<<24 - 1) / (1.0<<23)
	// TODO(gri) The compiler limits integers to 512 bit and thus
	//           we cannot compute the value 1<<1023
	//           without overflow. For now we match the compiler.
	//           See also issue #44057.
	// maxFloat64 = 1<<1023 * (1<<53 - 1) / (1.0<<52)
	maxFloat64 = 1.797693134862315708145274237317043567981e+308
)

const (
	_ int8 = minInt8 /* ERROR "overflows" */ - 1
	_ int8 = minInt8
	_ int8 = maxInt8
	_ int8 = maxInt8 /* ERROR "overflows" */ + 1
	_ int8 = smallestFloat64 /* ERROR "truncated" */

	_ = int8(minInt8 /* ERROR "cannot convert" */ - 1)
	_ = int8(minInt8)
	_ = int8(maxInt8)
	_ = int8(maxInt8 /* ERROR "cannot convert" */ + 1)
	_ = int8(smallestFloat64 /* ERROR "cannot convert" */)
)

const (
	_ int16 = minInt16 /* ERROR "overflows" */ - 1
	_ int16 = minInt16
	_ int16 = maxInt16
	_ int16 = maxInt16 /* ERROR "overflows" */ + 1
	_ int16 = smallestFloat64 /* ERROR "truncated" */

	_ = int16(minInt16 /* ERROR "cannot convert" */ - 1)
	_ = int16(minInt16)
	_ = int16(maxInt16)
	_ = int16(maxInt16 /* ERROR "cannot convert" */ + 1)
	_ = int16(smallestFloat64 /* ERROR "cannot convert" */)
)

const (
	_ int32 = minInt32 /* ERROR "overflows" */ - 1
	_ int32 = minInt32
	_ int32 = maxInt32
	_ int32 = maxInt32 /* ERROR "overflows" */ + 1
	_ int32 = smallestFloat64 /* ERROR "truncated" */

	_ = int32(minInt32 /* ERROR "cannot convert" */ - 1)
	_ = int32(minInt32)
	_ = int32(maxInt32)
	_ = int32(maxInt32 /* ERROR "cannot convert" */ + 1)
	_ = int32(smallestFloat64 /* ERROR "cannot convert" */)
)

const (
	_ int64 = minInt64 /* ERROR "overflows" */ - 1
	_ int64 = minInt64
	_ int64 = maxInt64
	_ int64 = maxInt64 /* ERROR "overflows" */ + 1
	_ int64 = smallestFloat64 /* ERROR "truncated" */

	_ = int64(minInt64 /* ERROR "cannot convert" */ - 1)
	_ = int64(minInt64)
	_ = int64(maxInt64)
	_ = int64(maxInt64 /* ERROR "cannot convert" */ + 1)
	_ = int64(smallestFloat64 /* ERROR "cannot convert" */)
)

const (
	_ int = minInt /* ERROR "overflows" */ - 1
	_ int = minInt
	_ int = maxInt
	_ int = maxInt /* ERROR "overflows" */ + 1
	_ int = smallestFloat64 /* ERROR "truncated" */

	_ = int(minInt /* ERROR "cannot convert" */ - 1)
	_ = int(minInt)
	_ = int(maxInt)
	_ = int(maxInt /* ERROR "cannot convert" */ + 1)
	_ = int(smallestFloat64 /* ERROR "cannot convert" */)
)

const (
	_ uint8 = 0 /* ERROR "overflows" */ - 1
	_ uint8 = 0
	_ uint8 = maxUint8
	_ uint8 = maxUint8 /* ERROR "overflows" */ + 1
	_ uint8 = smallestFloat64 /* ERROR "truncated" */

	_ = uint8(0 /* ERROR "cannot convert" */ - 1)
	_ = uint8(0)
	_ = uint8(maxUint8)
	_ = uint8(maxUint8 /* ERROR "cannot convert" */ + 1)
	_ = uint8(smallestFloat64 /* ERROR "cannot convert" */)
)

const (
	_ uint16 = 0 /* ERROR "overflows" */ - 1
	_ uint16 = 0
	_ uint16 = maxUint16
	_ uint16 = maxUint16 /* ERROR "overflows" */ + 1
	_ uint16 = smallestFloat64 /* ERROR "truncated" */

	_ = uint16(0 /* ERROR "cannot convert" */ - 1)
	_ = uint16(0)
	_ = uint16(maxUint16)
	_ = uint16(maxUint16 /* ERROR "cannot convert" */ + 1)
	_ = uint16(smallestFloat64 /* ERROR "cannot convert" */)
)

const (
	_ uint32 = 0 /* ERROR "overflows" */ - 1
	_ uint32 = 0
	_ uint32 = maxUint32
	_ uint32 = maxUint32 /* ERROR "overflows" */ + 1
	_ uint32 = smallestFloat64 /* ERROR "truncated" */

	_ = uint32(0 /* ERROR "cannot convert" */ - 1)
	_ = uint32(0)
	_ = uint32(maxUint32)
	_ = uint32(maxUint32 /* ERROR "cannot convert" */ + 1)
	_ = uint32(smallestFloat64 /* ERROR "cannot convert" */)
)

const (
	_ uint64 = 0 /* ERROR "overflows" */ - 1
	_ uint64 = 0
	_ uint64 = maxUint64
	_ uint64 = maxUint64 /* ERROR "overflows" */ + 1
	_ uint64 = smallestFloat64 /* ERROR "truncated" */

	_ = uint64(0 /* ERROR "cannot convert" */ - 1)
	_ = uint64(0)
	_ = uint64(maxUint64)
	_ = uint64(maxUint64 /* ERROR "cannot convert" */ + 1)
	_ = uint64(smallestFloat64 /* ERROR "cannot convert" */)
)

const (
	_ uint = 0 /* ERROR "overflows" */ - 1
	_ uint = 0
	_ uint = maxUint
	_ uint = maxUint /* ERROR "overflows" */ + 1
	_ uint = smallestFloat64 /* ERROR "truncated" */

	_ = uint(0 /* ERROR "cannot convert" */ - 1)
	_ = uint(0)
	_ = uint(maxUint)
	_ = uint(maxUint /* ERROR "cannot convert" */ + 1)
	_ = uint(smallestFloat64 /* ERROR "cannot convert" */)
)

const (
	_ uintptr = 0 /* ERROR "overflows" */ - 1
	_ uintptr = 0
	_ uintptr = maxUintptr
	_ uintptr = maxUintptr /* ERROR "overflows" */ + 1
	_ uintptr = smallestFloat64 /* ERROR "truncated" */

	_ = uintptr(0 /* ERROR "cannot convert" */ - 1)
	_ = uintptr(0)
	_ = uintptr(maxUintptr)
	_ = uintptr(maxUintptr /* ERROR "cannot convert" */ + 1)
	_ = uintptr(smallestFloat64 /* ERROR "cannot convert" */)
)

const (
	_ float32 = minInt64
	_ float64 = minInt64
	_ complex64 = minInt64
	_ complex128 = minInt64

	_ = float32(minInt64)
	_ = float64(minInt64)
	_ = complex64(minInt64)
	_ = complex128(minInt64)
)

const (
	_ float32 = maxUint64
	_ float64 = maxUint64
	_ complex64 = maxUint64
	_ complex128 = maxUint64

	_ = float32(maxUint64)
	_ = float64(maxUint64)
	_ = complex64(maxUint64)
	_ = complex128(maxUint64)
)

// TODO(gri) find smaller deltas below

const delta32 = maxFloat32/(1 << 23)

const (
	_ float32 = - /* ERROR "overflow" */ (maxFloat32 + delta32)
	_ float32 = -maxFloat32
	_ float32 = maxFloat32
	_ float32 = maxFloat32 /* ERROR "overflow" */ + delta32

	_ = float32(- /* ERROR "cannot convert" */ (maxFloat32 + delta32))
	_ = float32(-maxFloat32)
	_ = float32(maxFloat32)
	_ = float32(maxFloat32 /* ERROR "cannot convert" */ + delta32)

	_ = assert(float32(smallestFloat32) == smallestFloat32)
	_ = assert(float32(smallestFloat32/2) == 0)
	_ = assert(float32(smallestFloat64) == 0)
	_ = assert(float32(smallestFloat64/2) == 0)
)

const delta64 = maxFloat64/(1 << 52)

const (
	_ float64 = - /* ERROR "overflow" */ (maxFloat64 + delta64)
	_ float64 = -maxFloat64
	_ float64 = maxFloat64
	_ float64 = maxFloat64 /* ERROR "overflow" */ + delta64

	_ = float64(- /* ERROR "cannot convert" */ (maxFloat64 + delta64))
	_ = float64(-maxFloat64)
	_ = float64(maxFloat64)
	_ = float64(maxFloat64 /* ERROR "cannot convert" */ + delta64)

	_ = assert(float64(smallestFloat32) == smallestFloat32)
	_ = assert(float64(smallestFloat32/2) == smallestFloat32/2)
	_ = assert(float64(smallestFloat64) == smallestFloat64)
	// TODO(gri) With the change to the declaration of smallestFloat64
	//           this now fails to be true. See issue #44058.
	// _ = assert(float64(smallestFloat64/2) == 0)
)

const (
	_ complex64 = - /* ERROR "overflow" */ (maxFloat32 + delta32)
	_ complex64 = -maxFloat32
	_ complex64 = maxFloat32
	_ complex64 = maxFloat32 /* ERROR "overflow" */ + delta32

	_ = complex64(- /* ERROR "cannot convert" */ (maxFloat32 + delta32))
	_ = complex64(-maxFloat32)
	_ = complex64(maxFloat32)
	_ = complex64(maxFloat32 /* ERROR "cannot convert" */ + delta32)
)

const (
	_ complex128 = - /* ERROR "overflow" */ (maxFloat64 + delta64)
	_ complex128 = -maxFloat64
	_ complex128 = maxFloat64
	_ complex128 = maxFloat64 /* ERROR "overflow" */ + delta64

	_ = complex128(- /* ERROR "cannot convert" */ (maxFloat64 + delta64))
	_ = complex128(-maxFloat64)
	_ = complex128(maxFloat64)
	_ = complex128(maxFloat64 /* ERROR "cannot convert" */ + delta64)
)

// Initialization of typed constant and conversion are the same:
const (
	f32 = 1 + smallestFloat32
	x32 float32 = f32
	y32 = float32(f32)
	_ = assert(x32 - y32 == 0)
)

const (
	f64 = 1 + smallestFloat64
	x64 float64 = f64
	y64 = float64(f64)
	_ = assert(x64 - y64 == 0)
)

const (
	_ = int8(-1) << 7
	_ = int8 /* ERROR "overflows" */ (-1) << 8

	_ = uint32(1) << 31
	_ = uint32 /* ERROR "overflows" */ (1) << 32
)