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 shifts
6
7 func shifts0() {
8 // basic constant shifts
9 const (
10 s = 10
11 _ = 0<<0
12 _ = 1<<s
13 _ = 1<<- /* ERROR "negative shift count" */ 1
14 // For the test below we may decide to convert to int
15 // rather than uint and then report a negative shift
16 // count instead, which might be a better error. The
17 // (minor) difference is that this would restrict the
18 // shift count range by half (from all uint values to
19 // the positive int values).
20 // This depends on the exact spec wording which is not
21 // done yet.
22 // TODO(gri) revisit and adjust when spec change is done
23 _ = 1<<- /* ERROR "negative shift count" */ 1.0
24 _ = 1<<1075 /* ERROR "invalid shift" */
25 _ = 2.0<<1
26 _ = 1<<1.0
27 _ = 1<<(1+0i)
28
29 _ int = 2<<s
30 _ float32 = 2<<s
31 _ complex64 = 2<<s
32
33 _ int = 2.0<<s
34 _ float32 = 2.0<<s
35 _ complex64 = 2.0<<s
36
37 _ int = 'a'<<s
38 _ float32 = 'a'<<s
39 _ complex64 = 'a'<<s
40 )
41 }
42
43 func shifts1() {
44 // basic non-constant shifts
45 var (
46 i int
47 u uint
48
49 _ = 1<<0
50 _ = 1<<i
51 _ = 1<<u
52 _ = 1<<"foo" /* ERROR "cannot convert" */
53 _ = i<<0
54 _ = i<<- /* ERROR "negative shift count" */ 1
55 _ = i<<1.0
56 _ = 1<<(1+0i)
57 _ = 1 /* ERROR "overflows" */ <<100
58
59 _ uint = 1 << 0
60 _ uint = 1 << u
61 _ float32 = 1 /* ERROR "must be integer" */ << u
62
63 // issue #14822
64 _ = 1<<( /* ERROR "overflows uint" */ 1<<64)
65 _ = 1<<( /* ERROR "invalid shift count" */ 1<<64-1)
66
67 // issue #43697
68 _ = u<<( /* ERROR "overflows uint" */ 1<<64)
69 _ = u<<(1<<64-1)
70 )
71 }
72
73 func shifts2() {
74 // from the spec
75 var (
76 s uint = 33
77 i = 1<<s // 1 has type int
78 j int32 = 1<<s // 1 has type int32; j == 0
79 k = uint64(1<<s) // 1 has type uint64; k == 1<<33
80 m int = 1.0<<s // 1.0 has type int
81 n = 1.0<<s != i // 1.0 has type int; n == false if ints are 32bits in size
82 o = 1<<s == 2<<s // 1 and 2 have type int; o == true if ints are 32bits in size
83 p = 1<<s == 1<<33 // illegal if ints are 32bits in size: 1 has type int, but 1<<33 overflows int
84 u = 1.0 /* ERROR "must be integer" */ <<s // illegal: 1.0 has type float64, cannot shift
85 u1 = 1.0 /* ERROR "must be integer" */ <<s != 0 // illegal: 1.0 has type float64, cannot shift
86 u2 = 1 /* ERROR "must be integer" */ <<s != 1.0 // illegal: 1 has type float64, cannot shift
87 v float32 = 1 /* ERROR "must be integer" */ <<s // illegal: 1 has type float32, cannot shift
88 w int64 = 1.0<<33 // 1.0<<33 is a constant shift expression
89 )
90 _, _, _, _, _, _, _, _, _, _, _, _ = i, j, k, m, n, o, p, u, u1, u2, v, w
91 }
92
93 func shifts3(a int16, b float32) {
94 // random tests
95 var (
96 s uint = 11
97 u = 1 /* ERROR "must be integer" */ <<s + 1.0
98 v complex128 = 1 /* ERROR "must be integer" */ << s + 1.0 /* ERROR "must be integer" */ << s + 1
99 )
100 x := 1.0 /* ERROR "must be integer" */ <<s + 1
101 shifts3(1.0 << s, 1 /* ERROR "must be integer" */ >> s)
102 _, _, _ = u, v, x
103 }
104
105 func shifts4() {
106 // shifts in comparisons w/ untyped operands
107 var s uint
108
109 _ = 1<<s == 1
110 _ = 1 /* ERROR "integer" */ <<s == 1.
111 _ = 1. /* ERROR "integer" */ <<s == 1
112 _ = 1. /* ERROR "integer" */ <<s == 1.
113
114 _ = 1<<s + 1 == 1
115 _ = 1 /* ERROR "integer" */ <<s + 1 == 1.
116 _ = 1 /* ERROR "integer" */ <<s + 1. == 1
117 _ = 1 /* ERROR "integer" */ <<s + 1. == 1.
118 _ = 1. /* ERROR "integer" */ <<s + 1 == 1
119 _ = 1. /* ERROR "integer" */ <<s + 1 == 1.
120 _ = 1. /* ERROR "integer" */ <<s + 1. == 1
121 _ = 1. /* ERROR "integer" */ <<s + 1. == 1.
122
123 _ = 1<<s == 1<<s
124 _ = 1 /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s
125 _ = 1. /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s
126 _ = 1. /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s
127
128 _ = 1<<s + 1<<s == 1
129 _ = 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1.
130 _ = 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1
131 _ = 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1.
132 _ = 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1
133 _ = 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1.
134 _ = 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1
135 _ = 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1.
136
137 _ = 1<<s + 1<<s == 1<<s + 1<<s
138 _ = 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
139 _ = 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
140 _ = 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
141 _ = 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
142 _ = 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
143 _ = 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
144 _ = 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
145 _ = 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
146 _ = 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
147 _ = 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
148 _ = 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
149 _ = 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
150 _ = 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
151 _ = 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
152 _ = 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
153 }
154
155 func shifts5() {
156 // shifts in comparisons w/ typed operands
157 var s uint
158 var x int
159
160 _ = 1<<s == x
161 _ = 1.<<s == x
162 _ = 1.1 /* ERROR "int" */ <<s == x
163
164 _ = 1<<s + x == 1
165 _ = 1<<s + x == 1.
166 _ = 1<<s + x == 1.1 /* ERROR "int" */
167 _ = 1.<<s + x == 1
168 _ = 1.<<s + x == 1.
169 _ = 1.<<s + x == 1.1 /* ERROR "int" */
170 _ = 1.1 /* ERROR "int" */ <<s + x == 1
171 _ = 1.1 /* ERROR "int" */ <<s + x == 1.
172 _ = 1.1 /* ERROR "int" */ <<s + x == 1.1
173
174 _ = 1<<s == x<<s
175 _ = 1.<<s == x<<s
176 _ = 1.1 /* ERROR "int" */ <<s == x<<s
177 }
178
179 func shifts6() {
180 // shifts as operands in non-arithmetic operations and as arguments
181 var a [10]int
182 var s uint
183
184 _ = a[1<<s]
185 _ = a[1.0]
186 _ = a[1.0<<s]
187
188 _ = make([]int, 1.0)
189 _ = make([]int, 1.0<<s)
190 _ = make([]int, 1.1 /* ERROR "must be integer" */ <<s)
191
192 _ = float32(1)
193 _ = float32(1 /* ERROR "must be integer" */ <<s)
194 _ = float32(1.0)
195 _ = float32(1.0 /* ERROR "must be integer" */ <<s)
196 _ = float32(1.1 /* ERROR "must be integer" */ <<s)
197
198 _ = int32(0x80000000 /* ERROR "overflows int32" */ << s)
199 // TODO(rfindley) Eliminate the redundant error here.
200 _ = int32(( /* ERROR "truncated to int32" */ 0x80000000 /* ERROR "truncated to int32" */ + 0i) << s)
201
202 _ = int(1+0i<<0)
203 _ = int((1+0i)<<s)
204 _ = int(1.0<<s)
205 _ = int(complex(1, 0)<<s)
206 _ = int(float32/* ERROR "must be integer" */(1.0) <<s)
207 _ = int(1.1 /* ERROR must be integer */ <<s)
208 _ = int(( /* ERROR "must be integer" */ 1+1i) <<s)
209
210 _ = complex(1 /* ERROR "must be integer" */ <<s, 0)
211
212 var b []int
213 _ = append(b, 1<<s)
214 _ = append(b, 1.0<<s)
215 _ = append(b, (1+0i)<<s)
216 _ = append(b, 1.1 /* ERROR "must be integer" */ <<s)
217 _ = append(b, (1 + 0i) <<s)
218 _ = append(b, ( /* ERROR "must be integer" */ 1 + 1i) <<s)
219
220 _ = complex(1.0 /* ERROR "must be integer" */ <<s, 0)
221 _ = complex(1.1 /* ERROR "must be integer" */ <<s, 0)
222 _ = complex(0, 1.0 /* ERROR "must be integer" */ <<s)
223 _ = complex(0, 1.1 /* ERROR "must be integer" */ <<s)
224
225 // TODO(gri) The delete below is not type-checked correctly yet.
226 // var m1 map[int]string
227 // delete(m1, 1<<s)
228 }
229
230 func shifts7() {
231 // shifts of shifts
232 var s uint
233 var x int
234 _ = x
235
236 _ = 1<<(1<<s)
237 _ = 1<<(1.<<s)
238 _ = 1. /* ERROR "integer" */ <<(1<<s)
239 _ = 1. /* ERROR "integer" */ <<(1.<<s)
240
241 x = 1<<(1<<s)
242 x = 1<<(1.<<s)
243 x = 1.<<(1<<s)
244 x = 1.<<(1.<<s)
245
246 _ = (1<<s)<<(1<<s)
247 _ = (1<<s)<<(1.<<s)
248 _ = ( /* ERROR "integer" */ 1.<<s)<<(1<<s)
249 _ = ( /* ERROR "integer" */ 1.<<s)<<(1.<<s)
250
251 x = (1<<s)<<(1<<s)
252 x = (1<<s)<<(1.<<s)
253 x = ( /* ERROR "integer" */ 1.<<s)<<(1<<s)
254 x = ( /* ERROR "integer" */ 1.<<s)<<(1.<<s)
255 }
256
257 func shifts8() {
258 // shift examples from shift discussion: better error messages
259 var s uint
260 _ = 1.0 /* ERROR "shifted operand 1.0 \(type float64\) must be integer" */ <<s == 1
261 _ = 1.0 /* ERROR "shifted operand 1.0 \(type float64\) must be integer" */ <<s == 1.0
262 _ = 1 /* ERROR "shifted operand 1 \(type float64\) must be integer" */ <<s == 1.0
263 _ = 1 /* ERROR "shifted operand 1 \(type float64\) must be integer" */ <<s + 1.0 == 1
264 _ = 1 /* ERROR "shifted operand 1 \(type float64\) must be integer" */ <<s + 1.1 == 1
265 _ = 1 /* ERROR "shifted operand 1 \(type float64\) must be integer" */ <<s + 1 == 1.0
266
267 // additional cases
268 _ = complex(1.0 /* ERROR "shifted operand 1.0 \(type float64\) must be integer" */ <<s, 1)
269 _ = complex(1.0, 1 /* ERROR "shifted operand 1 \(type float64\) must be integer" */ <<s)
270
271 _ = int(1.<<s)
272 _ = int(1.1 /* ERROR "shifted operand .* must be integer" */ <<s)
273 _ = float32(1 /* ERROR "shifted operand .* must be integer" */ <<s)
274 _ = float32(1. /* ERROR "shifted operand .* must be integer" */ <<s)
275 _ = float32(1.1 /* ERROR "shifted operand .* must be integer" */ <<s)
276 // TODO(gri) the error messages for these two are incorrect - disabled for now
277 // _ = complex64(1<<s)
278 // _ = complex64(1.<<s)
279 _ = complex64(1.1 /* ERROR "shifted operand .* must be integer" */ <<s)
280 }
281
282 func shifts9() {
283 // various originally failing snippets of code from the std library
284 // from src/compress/lzw/reader.go:90
285 {
286 var d struct {
287 bits uint32
288 width uint
289 }
290 _ = uint16(d.bits & (1<<d.width - 1))
291 }
292
293 // from src/debug/dwarf/buf.go:116
294 {
295 var ux uint64
296 var bits uint
297 x := int64(ux)
298 if x&(1<<(bits-1)) != 0 {}
299 }
300
301 // from src/encoding/asn1/asn1.go:160
302 {
303 var bytes []byte
304 if bytes[len(bytes)-1]&((1<<bytes[0])-1) != 0 {}
305 }
306
307 // from src/math/big/rat.go:140
308 {
309 var exp int
310 var mantissa uint64
311 shift := uint64(-1022 - (exp - 1)) // [1..53)
312 _ = mantissa & (1<<shift - 1)
313 }
314
315 // from src/net/interface.go:51
316 {
317 type Flags uint
318 var f Flags
319 var i int
320 if f&(1<<uint(i)) != 0 {}
321 }
322
323 // from src/runtime/softfloat64.go:234
324 {
325 var gm uint64
326 var shift uint
327 _ = gm & (1<<shift - 1)
328 }
329
330 // from src/strconv/atof.go:326
331 {
332 var mant uint64
333 var mantbits uint
334 if mant == 2<<mantbits {}
335 }
336
337 // from src/route_bsd.go:82
338 {
339 var Addrs int32
340 const rtaRtMask = 1
341 var i uint
342 if Addrs&rtaRtMask&(1<<i) == 0 {}
343 }
344
345 // from src/text/scanner/scanner.go:540
346 {
347 var s struct { Whitespace uint64 }
348 var ch rune
349 for s.Whitespace&(1<<uint(ch)) != 0 {}
350 }
351 }
352
353 func issue5895() {
354 var x = 'a' << 1 // type of x must be rune
355 var _ rune = x
356 }
357
358 func issue11325() {
359 var _ = 0 >> 1.1 /* ERROR "truncated to uint" */ // example from issue 11325
360 _ = 0 >> 1.1 /* ERROR "truncated to uint" */
361 _ = 0 << 1.1 /* ERROR "truncated to uint" */
362 _ = 0 >> 1.
363 _ = 1 >> 1.1 /* ERROR "truncated to uint" */
364 _ = 1 >> 1.
365 _ = 1. >> 1
366 _ = 1. >> 1.
367 _ = 1.1 /* ERROR "must be integer" */ >> 1
368 }
369
370 func issue11594() {
371 var _ = complex64 /* ERROR "must be integer" */ (1) << 2 // example from issue 11594
372 _ = float32 /* ERROR "must be integer" */ (0) << 1
373 _ = float64 /* ERROR "must be integer" */ (0) >> 2
374 _ = complex64 /* ERROR "must be integer" */ (0) << 3
375 _ = complex64 /* ERROR "must be integer" */ (0) >> 4
376 }
377
378 func issue21727() {
379 var s uint
380 var a = make([]int, 1<<s + 1.2 /* ERROR "truncated to int" */ )
381 var _ = a[1<<s - 2.3 /* ERROR "truncated to int" */ ]
382 var _ int = 1<<s + 3.4 /* ERROR "truncated to int" */
383 var _ = string(1 /* ERROR shifted operand 1 .* must be integer */ << s)
384 var _ = string(1.0 /* ERROR "cannot convert" */ << s)
385 }
386
387 func issue22969() {
388 var s uint
389 var a []byte
390 _ = a[0xffffffffffffffff /* ERROR "overflows int" */ <<s] // example from issue 22969
391 _ = make([]int, 0xffffffffffffffff /* ERROR "overflows int" */ << s)
392 _ = make([]int, 0, 0xffffffffffffffff /* ERROR "overflows int" */ << s)
393 var _ byte = 0x100 /* ERROR "overflows byte" */ << s
394 var _ int8 = 0xff /* ERROR "overflows int8" */ << s
395 var _ int16 = 0xffff /* ERROR "overflows int16" */ << s
396 var _ int32 = 0x80000000 /* ERROR "overflows int32" */ << s
397 }
398
View as plain text