summaryrefslogtreecommitdiff
path: root/test/shift1.go
blob: 04f5321b73f21d2894d01cc5ec77ce9e74569a91 (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
// errorcheck

// Copyright 2011 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.

// Test illegal shifts.
// Issue 1708, illegal cases.
// Does not compile.

package p

func f(x int) int         { return 0 }
func g(x interface{}) int { return 0 }
func h(x float64) int     { return 0 }

// from the spec
var (
	s uint    = 33
	u         = 1.0 << s // ERROR "invalid operation|shift of non-integer operand"
	v float32 = 1 << s   // ERROR "invalid" "as type float32"
)

// non-constant shift expressions
var (
	e1       = g(2.0 << s) // ERROR "invalid|shift of non-integer operand" "as type interface"
	f1       = h(2 << s)   // ERROR "invalid" "as type float64"
	g1 int64 = 1.1 << s    // ERROR "truncated"
)

// constant shift expressions
const c uint = 65

var (
	a2 int = 1.0 << c    // ERROR "overflow"
	b2     = 1.0 << c    // ERROR "overflow"
	d2     = f(1.0 << c) // ERROR "overflow"
)

var (
	// issues 4882, 4936.
	a3 = 1.0<<s + 0 // ERROR "invalid|shift of non-integer operand"
	// issue 4937
	b3 = 1<<s + 1 + 1.0 // ERROR "invalid|shift of non-integer operand"
	// issue 5014
	c3     = complex(1<<s, 0) // ERROR "invalid|shift of type float64"
	d3 int = complex(1<<s, 3) // ERROR "non-integer|cannot use.*as type int" "shift of type float64"
	e3     = real(1 << s)     // ERROR "invalid"
	f3     = imag(1 << s)     // ERROR "invalid"
)

// from the spec
func _() {
	var (
		s uint  = 33
		i       = 1 << s         // 1 has type int
		j int32 = 1 << s         // 1 has type int32; j == 0
		k       = uint64(1 << s) // 1 has type uint64; k == 1<<33
		m int   = 1.0 << s       // 1.0 has type int
		n       = 1.0<<s != i    // 1.0 has type int; n == false if ints are 32bits in size
		o       = 1<<s == 2<<s   // 1 and 2 have type int; o == true if ints are 32bits in size
		// next test only fails on 32bit systems
		// p = 1<<s == 1<<33  // illegal if ints are 32bits in size: 1 has type int, but 1<<33 overflows int
		u          = 1.0 << s    // ERROR "non-integer|float64"
		u1         = 1.0<<s != 0 // ERROR "non-integer|float64"
		u2         = 1<<s != 1.0 // ERROR "non-integer|float64"
		v  float32 = 1 << s      // ERROR "non-integer|float32"
		w  int64   = 1.0 << 33   // 1.0<<33 is a constant shift expression
		_, _, _, _, _, _, _, _, _, _ = j, k, m, n, o, u, u1, u2, v, w
	)
}

// shifts in comparisons w/ untyped operands
var (
	_ = 1<<s == 1
	_ = 1<<s == 1.  // ERROR "invalid|shift of type float64"
	_ = 1.<<s == 1  // ERROR "invalid|shift of type float64"
	_ = 1.<<s == 1. // ERROR "invalid|non-integer|shift of type float64"

	_ = 1<<s+1 == 1
	_ = 1<<s+1 == 1.   // ERROR "invalid|shift of type float64"
	_ = 1<<s+1. == 1   // ERROR "invalid|shift of type float64"
	_ = 1<<s+1. == 1.  // ERROR "invalid|shift of type float64"
	_ = 1.<<s+1 == 1   // ERROR "invalid|shift of type float64"
	_ = 1.<<s+1 == 1.  // ERROR "invalid|shift of type float64"
	_ = 1.<<s+1. == 1  // ERROR "invalid|shift of type float64"
	_ = 1.<<s+1. == 1. // ERROR "invalid|non-integer|shift of type float64"

	_ = 1<<s == 1<<s
	_ = 1<<s == 1.<<s  // ERROR "invalid|shift of type float64"
	_ = 1.<<s == 1<<s  // ERROR "invalid|shift of type float64"
	_ = 1.<<s == 1.<<s // ERROR "invalid|non-integer|shift of type float64"

	_ = 1<<s+1<<s == 1
	_ = 1<<s+1<<s == 1.   // ERROR "invalid|shift of type float64"
	_ = 1<<s+1.<<s == 1   // ERROR "invalid|shift of type float64"
	_ = 1<<s+1.<<s == 1.  // ERROR "invalid|shift of type float64"
	_ = 1.<<s+1<<s == 1   // ERROR "invalid|shift of type float64"
	_ = 1.<<s+1<<s == 1.  // ERROR "invalid|shift of type float64"
	_ = 1.<<s+1.<<s == 1  // ERROR "invalid|shift of type float64"
	_ = 1.<<s+1.<<s == 1. // ERROR "invalid|non-integer|shift of type float64"

	_ = 1<<s+1<<s == 1<<s+1<<s
	_ = 1<<s+1<<s == 1<<s+1.<<s    // ERROR "invalid|shift of type float64"
	_ = 1<<s+1<<s == 1.<<s+1<<s    // ERROR "invalid|shift of type float64"
	_ = 1<<s+1<<s == 1.<<s+1.<<s   // ERROR "invalid|shift of type float64"
	_ = 1<<s+1.<<s == 1<<s+1<<s    // ERROR "invalid|shift of type float64"
	_ = 1<<s+1.<<s == 1<<s+1.<<s   // ERROR "invalid|shift of type float64"
	_ = 1<<s+1.<<s == 1.<<s+1<<s   // ERROR "invalid|shift of type float64"
	_ = 1<<s+1.<<s == 1.<<s+1.<<s  // ERROR "invalid|non-integer|shift of type float64"
	_ = 1.<<s+1<<s == 1<<s+1<<s    // ERROR "invalid|shift of type float64"
	_ = 1.<<s+1<<s == 1<<s+1.<<s   // ERROR "invalid|shift of type float64"
	_ = 1.<<s+1<<s == 1.<<s+1<<s   // ERROR "invalid|shift of type float64"
	_ = 1.<<s+1<<s == 1.<<s+1.<<s  // ERROR "invalid|non-integer|shift of type float64"
	_ = 1.<<s+1.<<s == 1<<s+1<<s   // ERROR "invalid|shift of type float64"
	_ = 1.<<s+1.<<s == 1<<s+1.<<s  // ERROR "invalid|non-integer|shift of type float64"
	_ = 1.<<s+1.<<s == 1.<<s+1<<s  // ERROR "invalid|non-integer|shift of type float64"
	_ = 1.<<s+1.<<s == 1.<<s+1.<<s // ERROR "invalid|non-integer|shift of type float64"
)

// shifts in comparisons w/ typed operands
var (
	x int
	_ = 1<<s == x
	_ = 1.<<s == x
	_ = 1.1<<s == x // ERROR "truncated"

	_ = 1<<s+x == 1
	_ = 1<<s+x == 1.
	_ = 1<<s+x == 1.1 // ERROR "truncated"
	_ = 1.<<s+x == 1
	_ = 1.<<s+x == 1.
	_ = 1.<<s+x == 1.1  // ERROR "truncated"
	_ = 1.1<<s+x == 1   // ERROR "truncated"
	_ = 1.1<<s+x == 1.  // ERROR "truncated"
	_ = 1.1<<s+x == 1.1 // ERROR "truncated"

	_ = 1<<s == x<<s
	_ = 1.<<s == x<<s
	_ = 1.1<<s == x<<s // ERROR "truncated"
)

// shifts as operands in non-arithmetic operations and as arguments
func _() {
	var s uint
	var a []int
	_ = a[1<<s]
	_ = a[1.]
	// For now, the spec disallows these. We may revisit past Go 1.1.
	_ = a[1.<<s]  // ERROR "integer|shift of type float64"
	_ = a[1.1<<s] // ERROR "integer|shift of type float64"

	_ = make([]int, 1)
	_ = make([]int, 1.)
	_ = make([]int, 1.<<s)
	_ = make([]int, 1.1<<s) // ERROR "non-integer|truncated"

	_ = float32(1)
	_ = float32(1 << s) // ERROR "non-integer|shift of type float32"
	_ = float32(1.)
	_ = float32(1. << s)  // ERROR "non-integer|shift of type float32"
	_ = float32(1.1 << s) // ERROR "non-integer|shift of type float32"

	_ = append(a, 1<<s)
	_ = append(a, 1.<<s)
	_ = append(a, 1.1<<s) // ERROR "truncated"

	var b []float32
	_ = append(b, 1<<s)   // ERROR "non-integer|type float32"
	_ = append(b, 1.<<s)  // ERROR "non-integer|type float32"
	_ = append(b, 1.1<<s) // ERROR "non-integer|type float32"

	_ = complex(1.<<s, 0)  // ERROR "non-integer|shift of type float64"
	_ = complex(1.1<<s, 0) // ERROR "non-integer|shift of type float64"
	_ = complex(0, 1.<<s)  // ERROR "non-integer|shift of type float64"
	_ = complex(0, 1.1<<s) // ERROR "non-integer|shift of type float64"

	var a4 float64
	var b4 int
	_ = complex(1<<s, a4) // ERROR "non-integer|shift of type float64"
	_ = complex(1<<s, b4) // ERROR "invalid|non-integer|"

	var m1 map[int]string
	delete(m1, 1<<s)
	delete(m1, 1.<<s)
	delete(m1, 1.1<<s) // ERROR "truncated|shift of type float64"

	var m2 map[float32]string
	delete(m2, 1<<s)   // ERROR "invalid|cannot use 1 << s as type float32"
	delete(m2, 1.<<s)  // ERROR "invalid|cannot use 1 << s as type float32"
	delete(m2, 1.1<<s) // ERROR "invalid|cannot use 1.1 << s as type float32"
}

// shifts of shifts
func _() {
	var s uint
	_ = 1 << (1 << s)
	_ = 1 << (1. << s)
	_ = 1 << (1.1 << s)   // ERROR "non-integer|truncated"
	_ = 1. << (1 << s)    // ERROR "non-integer|shift of type float64"
	_ = 1. << (1. << s)   // ERROR "non-integer|shift of type float64"
	_ = 1.1 << (1.1 << s) // ERROR "invalid|non-integer|truncated"

	_ = (1 << s) << (1 << s)
	_ = (1 << s) << (1. << s)
	_ = (1 << s) << (1.1 << s)   // ERROR "truncated"
	_ = (1. << s) << (1 << s)    // ERROR "non-integer|shift of type float64"
	_ = (1. << s) << (1. << s)   // ERROR "non-integer|shift of type float64"
	_ = (1.1 << s) << (1.1 << s) // ERROR "invalid|non-integer|truncated"

	var x int
	x = 1 << (1 << s)
	x = 1 << (1. << s)
	x = 1 << (1.1 << s) // ERROR "truncated"
	x = 1. << (1 << s)
	x = 1. << (1. << s)
	x = 1.1 << (1.1 << s) // ERROR "truncated"

	x = (1 << s) << (1 << s)
	x = (1 << s) << (1. << s)
	x = (1 << s) << (1.1 << s) // ERROR "truncated"
	x = (1. << s) << (1 << s)
	x = (1. << s) << (1. << s)
	x = (1.1 << s) << (1.1 << s) // ERROR "truncated"

	var y float32
	y = 1 << (1 << s)     // ERROR "non-integer|type float32"
	y = 1 << (1. << s)    // ERROR "non-integer|type float32"
	y = 1 << (1.1 << s)   // ERROR "invalid|truncated|float32"
	y = 1. << (1 << s)    // ERROR "non-integer|type float32"
	y = 1. << (1. << s)   // ERROR "non-integer|type float32"
	y = 1.1 << (1.1 << s) // ERROR "invalid|truncated|float32"

	var z complex128
	z = (1 << s) << (1 << s)     // ERROR "non-integer|type complex128"
	z = (1 << s) << (1. << s)    // ERROR "non-integer|type complex128"
	z = (1 << s) << (1.1 << s)   // ERROR "invalid|truncated|complex128"
	z = (1. << s) << (1 << s)    // ERROR "non-integer|type complex128"
	z = (1. << s) << (1. << s)   // ERROR "non-integer|type complex128"
	z = (1.1 << s) << (1.1 << s) // ERROR "invalid|truncated|complex128"

	_, _, _ = x, y, z
}