blob: 04f5321b73f21d2894d01cc5ec77ce9e74569a91 [file] [log] [blame]
Russ Cox57eb06f2012-02-16 23:51:04 -05001// errorcheck
Robert Griesemer8c207872011-05-25 10:26:06 -07002
3// Copyright 2011 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
Rob Pike80a97832012-02-24 11:48:19 +11007// Test illegal shifts.
Robert Griesemer8c207872011-05-25 10:26:06 -07008// Issue 1708, illegal cases.
Rob Pike80a97832012-02-24 11:48:19 +11009// Does not compile.
Robert Griesemer8c207872011-05-25 10:26:06 -070010
11package p
12
13func f(x int) int { return 0 }
14func g(x interface{}) int { return 0 }
15func h(x float64) int { return 0 }
16
17// from the spec
18var (
19 s uint = 33
Ian Lance Taylor9169c272011-09-21 17:25:48 -070020 u = 1.0 << s // ERROR "invalid operation|shift of non-integer operand"
21 v float32 = 1 << s // ERROR "invalid" "as type float32"
Robert Griesemer8c207872011-05-25 10:26:06 -070022)
23
24// non-constant shift expressions
25var (
Ian Lance Taylor1761e252013-06-26 08:23:52 -070026 e1 = g(2.0 << s) // ERROR "invalid|shift of non-integer operand" "as type interface"
Ian Lance Taylor9169c272011-09-21 17:25:48 -070027 f1 = h(2 << s) // ERROR "invalid" "as type float64"
Robert Griesemer8c207872011-05-25 10:26:06 -070028 g1 int64 = 1.1 << s // ERROR "truncated"
29)
30
31// constant shift expressions
32const c uint = 65
33
34var (
35 a2 int = 1.0 << c // ERROR "overflow"
36 b2 = 1.0 << c // ERROR "overflow"
37 d2 = f(1.0 << c) // ERROR "overflow"
38)
Rémy Oudomphenga85fce22013-03-04 16:51:42 +010039
40var (
41 // issues 4882, 4936.
Ian Lance Taylor1761e252013-06-26 08:23:52 -070042 a3 = 1.0<<s + 0 // ERROR "invalid|shift of non-integer operand"
Rémy Oudomphenga85fce22013-03-04 16:51:42 +010043 // issue 4937
Ian Lance Taylor1761e252013-06-26 08:23:52 -070044 b3 = 1<<s + 1 + 1.0 // ERROR "invalid|shift of non-integer operand"
Rémy Oudompheng861aa462013-03-16 00:37:28 +010045 // issue 5014
Ian Lance Taylor1761e252013-06-26 08:23:52 -070046 c3 = complex(1<<s, 0) // ERROR "invalid|shift of type float64"
47 d3 int = complex(1<<s, 3) // ERROR "non-integer|cannot use.*as type int" "shift of type float64"
Rémy Oudompheng861aa462013-03-16 00:37:28 +010048 e3 = real(1 << s) // ERROR "invalid"
49 f3 = imag(1 << s) // ERROR "invalid"
50)
51
Robert Griesemerf8ff6892013-03-21 16:56:59 -070052// from the spec
53func _() {
54 var (
55 s uint = 33
56 i = 1 << s // 1 has type int
57 j int32 = 1 << s // 1 has type int32; j == 0
58 k = uint64(1 << s) // 1 has type uint64; k == 1<<33
59 m int = 1.0 << s // 1.0 has type int
60 n = 1.0<<s != i // 1.0 has type int; n == false if ints are 32bits in size
61 o = 1<<s == 2<<s // 1 and 2 have type int; o == true if ints are 32bits in size
62 // next test only fails on 32bit systems
63 // p = 1<<s == 1<<33 // illegal if ints are 32bits in size: 1 has type int, but 1<<33 overflows int
Ian Lance Taylor1761e252013-06-26 08:23:52 -070064 u = 1.0 << s // ERROR "non-integer|float64"
65 u1 = 1.0<<s != 0 // ERROR "non-integer|float64"
66 u2 = 1<<s != 1.0 // ERROR "non-integer|float64"
67 v float32 = 1 << s // ERROR "non-integer|float32"
Robert Griesemerf8ff6892013-03-21 16:56:59 -070068 w int64 = 1.0 << 33 // 1.0<<33 is a constant shift expression
Ian Lance Taylor1761e252013-06-26 08:23:52 -070069 _, _, _, _, _, _, _, _, _, _ = j, k, m, n, o, u, u1, u2, v, w
Robert Griesemerf8ff6892013-03-21 16:56:59 -070070 )
71}
72
73// shifts in comparisons w/ untyped operands
Rémy Oudompheng861aa462013-03-16 00:37:28 +010074var (
Robert Griesemerf8ff6892013-03-21 16:56:59 -070075 _ = 1<<s == 1
Ian Lance Taylor1761e252013-06-26 08:23:52 -070076 _ = 1<<s == 1. // ERROR "invalid|shift of type float64"
77 _ = 1.<<s == 1 // ERROR "invalid|shift of type float64"
78 _ = 1.<<s == 1. // ERROR "invalid|non-integer|shift of type float64"
Robert Griesemerf8ff6892013-03-21 16:56:59 -070079
80 _ = 1<<s+1 == 1
Ian Lance Taylor1761e252013-06-26 08:23:52 -070081 _ = 1<<s+1 == 1. // ERROR "invalid|shift of type float64"
82 _ = 1<<s+1. == 1 // ERROR "invalid|shift of type float64"
83 _ = 1<<s+1. == 1. // ERROR "invalid|shift of type float64"
84 _ = 1.<<s+1 == 1 // ERROR "invalid|shift of type float64"
85 _ = 1.<<s+1 == 1. // ERROR "invalid|shift of type float64"
86 _ = 1.<<s+1. == 1 // ERROR "invalid|shift of type float64"
87 _ = 1.<<s+1. == 1. // ERROR "invalid|non-integer|shift of type float64"
Robert Griesemerf8ff6892013-03-21 16:56:59 -070088
89 _ = 1<<s == 1<<s
Ian Lance Taylor1761e252013-06-26 08:23:52 -070090 _ = 1<<s == 1.<<s // ERROR "invalid|shift of type float64"
91 _ = 1.<<s == 1<<s // ERROR "invalid|shift of type float64"
92 _ = 1.<<s == 1.<<s // ERROR "invalid|non-integer|shift of type float64"
Robert Griesemerf8ff6892013-03-21 16:56:59 -070093
94 _ = 1<<s+1<<s == 1
Ian Lance Taylor1761e252013-06-26 08:23:52 -070095 _ = 1<<s+1<<s == 1. // ERROR "invalid|shift of type float64"
96 _ = 1<<s+1.<<s == 1 // ERROR "invalid|shift of type float64"
97 _ = 1<<s+1.<<s == 1. // ERROR "invalid|shift of type float64"
98 _ = 1.<<s+1<<s == 1 // ERROR "invalid|shift of type float64"
99 _ = 1.<<s+1<<s == 1. // ERROR "invalid|shift of type float64"
100 _ = 1.<<s+1.<<s == 1 // ERROR "invalid|shift of type float64"
101 _ = 1.<<s+1.<<s == 1. // ERROR "invalid|non-integer|shift of type float64"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700102
103 _ = 1<<s+1<<s == 1<<s+1<<s
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700104 _ = 1<<s+1<<s == 1<<s+1.<<s // ERROR "invalid|shift of type float64"
105 _ = 1<<s+1<<s == 1.<<s+1<<s // ERROR "invalid|shift of type float64"
106 _ = 1<<s+1<<s == 1.<<s+1.<<s // ERROR "invalid|shift of type float64"
107 _ = 1<<s+1.<<s == 1<<s+1<<s // ERROR "invalid|shift of type float64"
108 _ = 1<<s+1.<<s == 1<<s+1.<<s // ERROR "invalid|shift of type float64"
109 _ = 1<<s+1.<<s == 1.<<s+1<<s // ERROR "invalid|shift of type float64"
110 _ = 1<<s+1.<<s == 1.<<s+1.<<s // ERROR "invalid|non-integer|shift of type float64"
111 _ = 1.<<s+1<<s == 1<<s+1<<s // ERROR "invalid|shift of type float64"
112 _ = 1.<<s+1<<s == 1<<s+1.<<s // ERROR "invalid|shift of type float64"
113 _ = 1.<<s+1<<s == 1.<<s+1<<s // ERROR "invalid|shift of type float64"
114 _ = 1.<<s+1<<s == 1.<<s+1.<<s // ERROR "invalid|non-integer|shift of type float64"
115 _ = 1.<<s+1.<<s == 1<<s+1<<s // ERROR "invalid|shift of type float64"
116 _ = 1.<<s+1.<<s == 1<<s+1.<<s // ERROR "invalid|non-integer|shift of type float64"
117 _ = 1.<<s+1.<<s == 1.<<s+1<<s // ERROR "invalid|non-integer|shift of type float64"
118 _ = 1.<<s+1.<<s == 1.<<s+1.<<s // ERROR "invalid|non-integer|shift of type float64"
Rémy Oudomphenga85fce22013-03-04 16:51:42 +0100119)
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700120
121// shifts in comparisons w/ typed operands
122var (
123 x int
124 _ = 1<<s == x
125 _ = 1.<<s == x
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700126 _ = 1.1<<s == x // ERROR "truncated"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700127
128 _ = 1<<s+x == 1
129 _ = 1<<s+x == 1.
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700130 _ = 1<<s+x == 1.1 // ERROR "truncated"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700131 _ = 1.<<s+x == 1
132 _ = 1.<<s+x == 1.
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700133 _ = 1.<<s+x == 1.1 // ERROR "truncated"
134 _ = 1.1<<s+x == 1 // ERROR "truncated"
135 _ = 1.1<<s+x == 1. // ERROR "truncated"
136 _ = 1.1<<s+x == 1.1 // ERROR "truncated"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700137
138 _ = 1<<s == x<<s
139 _ = 1.<<s == x<<s
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700140 _ = 1.1<<s == x<<s // ERROR "truncated"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700141)
142
143// shifts as operands in non-arithmetic operations and as arguments
144func _() {
145 var s uint
146 var a []int
147 _ = a[1<<s]
148 _ = a[1.]
149 // For now, the spec disallows these. We may revisit past Go 1.1.
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700150 _ = a[1.<<s] // ERROR "integer|shift of type float64"
151 _ = a[1.1<<s] // ERROR "integer|shift of type float64"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700152
153 _ = make([]int, 1)
154 _ = make([]int, 1.)
155 _ = make([]int, 1.<<s)
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700156 _ = make([]int, 1.1<<s) // ERROR "non-integer|truncated"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700157
158 _ = float32(1)
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700159 _ = float32(1 << s) // ERROR "non-integer|shift of type float32"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700160 _ = float32(1.)
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700161 _ = float32(1. << s) // ERROR "non-integer|shift of type float32"
162 _ = float32(1.1 << s) // ERROR "non-integer|shift of type float32"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700163
164 _ = append(a, 1<<s)
165 _ = append(a, 1.<<s)
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700166 _ = append(a, 1.1<<s) // ERROR "truncated"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700167
168 var b []float32
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700169 _ = append(b, 1<<s) // ERROR "non-integer|type float32"
170 _ = append(b, 1.<<s) // ERROR "non-integer|type float32"
171 _ = append(b, 1.1<<s) // ERROR "non-integer|type float32"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700172
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700173 _ = complex(1.<<s, 0) // ERROR "non-integer|shift of type float64"
174 _ = complex(1.1<<s, 0) // ERROR "non-integer|shift of type float64"
175 _ = complex(0, 1.<<s) // ERROR "non-integer|shift of type float64"
176 _ = complex(0, 1.1<<s) // ERROR "non-integer|shift of type float64"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700177
178 var a4 float64
179 var b4 int
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700180 _ = complex(1<<s, a4) // ERROR "non-integer|shift of type float64"
181 _ = complex(1<<s, b4) // ERROR "invalid|non-integer|"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700182
183 var m1 map[int]string
184 delete(m1, 1<<s)
185 delete(m1, 1.<<s)
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700186 delete(m1, 1.1<<s) // ERROR "truncated|shift of type float64"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700187
188 var m2 map[float32]string
189 delete(m2, 1<<s) // ERROR "invalid|cannot use 1 << s as type float32"
190 delete(m2, 1.<<s) // ERROR "invalid|cannot use 1 << s as type float32"
191 delete(m2, 1.1<<s) // ERROR "invalid|cannot use 1.1 << s as type float32"
192}
193
194// shifts of shifts
195func _() {
196 var s uint
197 _ = 1 << (1 << s)
198 _ = 1 << (1. << s)
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700199 _ = 1 << (1.1 << s) // ERROR "non-integer|truncated"
200 _ = 1. << (1 << s) // ERROR "non-integer|shift of type float64"
201 _ = 1. << (1. << s) // ERROR "non-integer|shift of type float64"
202 _ = 1.1 << (1.1 << s) // ERROR "invalid|non-integer|truncated"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700203
204 _ = (1 << s) << (1 << s)
205 _ = (1 << s) << (1. << s)
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700206 _ = (1 << s) << (1.1 << s) // ERROR "truncated"
207 _ = (1. << s) << (1 << s) // ERROR "non-integer|shift of type float64"
208 _ = (1. << s) << (1. << s) // ERROR "non-integer|shift of type float64"
209 _ = (1.1 << s) << (1.1 << s) // ERROR "invalid|non-integer|truncated"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700210
211 var x int
212 x = 1 << (1 << s)
213 x = 1 << (1. << s)
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700214 x = 1 << (1.1 << s) // ERROR "truncated"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700215 x = 1. << (1 << s)
216 x = 1. << (1. << s)
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700217 x = 1.1 << (1.1 << s) // ERROR "truncated"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700218
219 x = (1 << s) << (1 << s)
220 x = (1 << s) << (1. << s)
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700221 x = (1 << s) << (1.1 << s) // ERROR "truncated"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700222 x = (1. << s) << (1 << s)
223 x = (1. << s) << (1. << s)
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700224 x = (1.1 << s) << (1.1 << s) // ERROR "truncated"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700225
226 var y float32
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700227 y = 1 << (1 << s) // ERROR "non-integer|type float32"
228 y = 1 << (1. << s) // ERROR "non-integer|type float32"
229 y = 1 << (1.1 << s) // ERROR "invalid|truncated|float32"
230 y = 1. << (1 << s) // ERROR "non-integer|type float32"
231 y = 1. << (1. << s) // ERROR "non-integer|type float32"
232 y = 1.1 << (1.1 << s) // ERROR "invalid|truncated|float32"
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700233
234 var z complex128
Ian Lance Taylor1761e252013-06-26 08:23:52 -0700235 z = (1 << s) << (1 << s) // ERROR "non-integer|type complex128"
236 z = (1 << s) << (1. << s) // ERROR "non-integer|type complex128"
237 z = (1 << s) << (1.1 << s) // ERROR "invalid|truncated|complex128"
238 z = (1. << s) << (1 << s) // ERROR "non-integer|type complex128"
239 z = (1. << s) << (1. << s) // ERROR "non-integer|type complex128"
240 z = (1.1 << s) << (1.1 << s) // ERROR "invalid|truncated|complex128"
Ian Lance Taylor8259b012014-07-20 12:25:24 -0700241
242 _, _, _ = x, y, z
Robert Griesemerf8ff6892013-03-21 16:56:59 -0700243}