]> Cypherpunks.ru repositories - gostls13.git/blob - test/shift1.go
test: more systematic shift tests
[gostls13.git] / test / shift1.go
1 // errorcheck
2
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
7 // Test illegal shifts.
8 // Issue 1708, illegal cases.
9 // Does not compile.
10
11 package p
12
13 func f(x int) int         { return 0 }
14 func g(x interface{}) int { return 0 }
15 func h(x float64) int     { return 0 }
16
17 // from the spec
18 var (
19         s uint    = 33
20         u         = 1.0 << s // ERROR "invalid operation|shift of non-integer operand"
21         v float32 = 1 << s   // ERROR "invalid" "as type float32"
22 )
23
24 // non-constant shift expressions
25 var (
26         e1       = g(2.0 << s) // ERROR "invalid" "as type interface"
27         f1       = h(2 << s)   // ERROR "invalid" "as type float64"
28         g1 int64 = 1.1 << s    // ERROR "truncated"
29 )
30
31 // constant shift expressions
32 const c uint = 65
33
34 var (
35         a2 int = 1.0 << c    // ERROR "overflow"
36         b2     = 1.0 << c    // ERROR "overflow"
37         d2     = f(1.0 << c) // ERROR "overflow"
38 )
39
40 var (
41         // issues 4882, 4936.
42         a3 = 1.0<<s + 0 // ERROR "invalid operation|shift of non-integer operand"
43         // issue 4937
44         b3 = 1<<s + 1 + 1.0 // ERROR "invalid operation|shift of non-integer operand"
45         // issue 5014
46         c3     = complex(1<<s, 0) // ERROR "shift of type float64"
47         d3 int = complex(1<<s, 3) // ERROR "cannot use.*as type int" "shift of type float64"
48         e3     = real(1 << s)     // ERROR "invalid"
49         f3     = imag(1 << s)     // ERROR "invalid"
50 )
51
52 // from the spec
53 func _() {
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
64                 u          = 1.0 << s    // ERROR "float64"
65                 u1         = 1.0<<s != 0 // ERROR "float64"
66                 u2         = 1<<s != 1.0 // ERROR "float64"
67                 v  float32 = 1 << s      // ERROR "float32"
68                 w  int64   = 1.0 << 33   // 1.0<<33 is a constant shift expression
69         )
70 }
71
72 // shifts in comparisons w/ untyped operands
73 var (
74         _ = 1<<s == 1
75         _ = 1<<s == 1.  // ERROR "shift of type float64"
76         _ = 1.<<s == 1  // ERROR "shift of type float64"
77         _ = 1.<<s == 1. // ERROR "shift of type float64"
78
79         _ = 1<<s+1 == 1
80         _ = 1<<s+1 == 1.   // ERROR "shift of type float64"
81         _ = 1<<s+1. == 1   // ERROR "shift of type float64"
82         _ = 1<<s+1. == 1.  // ERROR "shift of type float64"
83         _ = 1.<<s+1 == 1   // ERROR "shift of type float64"
84         _ = 1.<<s+1 == 1.  // ERROR "shift of type float64"
85         _ = 1.<<s+1. == 1  // ERROR "shift of type float64"
86         _ = 1.<<s+1. == 1. // ERROR "shift of type float64"
87
88         _ = 1<<s == 1<<s
89         _ = 1<<s == 1.<<s  // ERROR "shift of type float64"
90         _ = 1.<<s == 1<<s  // ERROR "shift of type float64"
91         _ = 1.<<s == 1.<<s // ERROR "shift of type float64"
92
93         _ = 1<<s+1<<s == 1
94         _ = 1<<s+1<<s == 1.   // ERROR "shift of type float64"
95         _ = 1<<s+1.<<s == 1   // ERROR "shift of type float64"
96         _ = 1<<s+1.<<s == 1.  // ERROR "shift of type float64"
97         _ = 1.<<s+1<<s == 1   // ERROR "shift of type float64"
98         _ = 1.<<s+1<<s == 1.  // ERROR "shift of type float64"
99         _ = 1.<<s+1.<<s == 1  // ERROR "shift of type float64"
100         _ = 1.<<s+1.<<s == 1. // ERROR "shift of type float64"
101
102         _ = 1<<s+1<<s == 1<<s+1<<s
103         _ = 1<<s+1<<s == 1<<s+1.<<s    // ERROR "shift of type float64"
104         _ = 1<<s+1<<s == 1.<<s+1<<s    // ERROR "shift of type float64"
105         _ = 1<<s+1<<s == 1.<<s+1.<<s   // ERROR "shift of type float64"
106         _ = 1<<s+1.<<s == 1<<s+1<<s    // ERROR "shift of type float64"
107         _ = 1<<s+1.<<s == 1<<s+1.<<s   // ERROR "shift of type float64"
108         _ = 1<<s+1.<<s == 1.<<s+1<<s   // ERROR "shift of type float64"
109         _ = 1<<s+1.<<s == 1.<<s+1.<<s  // ERROR "shift of type float64"
110         _ = 1.<<s+1<<s == 1<<s+1<<s    // ERROR "shift of type float64"
111         _ = 1.<<s+1<<s == 1<<s+1.<<s   // ERROR "shift of type float64"
112         _ = 1.<<s+1<<s == 1.<<s+1<<s   // ERROR "shift of type float64"
113         _ = 1.<<s+1<<s == 1.<<s+1.<<s  // ERROR "shift of type float64"
114         _ = 1.<<s+1.<<s == 1<<s+1<<s   // ERROR "shift of type float64"
115         _ = 1.<<s+1.<<s == 1<<s+1.<<s  // ERROR "shift of type float64"
116         _ = 1.<<s+1.<<s == 1.<<s+1<<s  // ERROR "shift of type float64"
117         _ = 1.<<s+1.<<s == 1.<<s+1.<<s // ERROR "shift of type float64"
118 )
119
120 // shifts in comparisons w/ typed operands
121 var (
122         x int
123         _ = 1<<s == x
124         _ = 1.<<s == x
125         _ = 1.1<<s == x // ERROR "1.1 truncated"
126
127         _ = 1<<s+x == 1
128         _ = 1<<s+x == 1.
129         _ = 1<<s+x == 1.1 // ERROR "1.1 truncated"
130         _ = 1.<<s+x == 1
131         _ = 1.<<s+x == 1.
132         _ = 1.<<s+x == 1.1  // ERROR "1.1 truncated"
133         _ = 1.1<<s+x == 1   // ERROR "1.1 truncated"
134         _ = 1.1<<s+x == 1.  // ERROR "1.1 truncated"
135         _ = 1.1<<s+x == 1.1 // ERROR "1.1 truncated"
136
137         _ = 1<<s == x<<s
138         _ = 1.<<s == x<<s
139         _ = 1.1<<s == x<<s // ERROR "1.1 truncated"
140 )
141
142 // shifts as operands in non-arithmetic operations and as arguments
143 func _() {
144         var s uint
145         var a []int
146         _ = a[1<<s]
147         _ = a[1.]
148         // For now, the spec disallows these. We may revisit past Go 1.1.
149         _ = a[1.<<s]  // ERROR "shift of type float64"
150         _ = a[1.1<<s] // ERROR "shift of type float64"
151
152         _ = make([]int, 1)
153         _ = make([]int, 1.)
154         _ = make([]int, 1.<<s)
155         _ = make([]int, 1.1<<s) // ERROR "1.1 truncated"
156
157         _ = float32(1)
158         _ = float32(1 << s) // ERROR "shift of type float32"
159         _ = float32(1.)
160         _ = float32(1. << s)  // ERROR "shift of type float32"
161         _ = float32(1.1 << s) // ERROR "shift of type float32"
162
163         _ = append(a, 1<<s)
164         _ = append(a, 1.<<s)
165         _ = append(a, 1.1<<s) // ERROR "1.1 truncated"
166
167         var b []float32
168         _ = append(b, 1<<s)   // ERROR "type float32"
169         _ = append(b, 1.<<s)  // ERROR "type float32"
170         _ = append(b, 1.1<<s) // ERROR "type float32"
171
172         _ = complex(1.<<s, 0)  // ERROR "shift of type float64"
173         _ = complex(1.1<<s, 0) // ERROR "shift of type float64"
174         _ = complex(0, 1.<<s)  // ERROR "shift of type float64"
175         _ = complex(0, 1.1<<s) // ERROR "shift of type float64"
176
177         var a4 float64
178         var b4 int
179         _ = complex(1<<s, a4) // ERROR "shift of type float64"
180         _ = complex(1<<s, b4) // ERROR "invalid"
181
182         var m1 map[int]string
183         delete(m1, 1<<s)
184         delete(m1, 1.<<s)
185         delete(m1, 1.1<<s) // ERROR "1.1 truncated|shift of type float64"
186
187         var m2 map[float32]string
188         delete(m2, 1<<s)   // ERROR "invalid|cannot use 1 << s as type float32"
189         delete(m2, 1.<<s)  // ERROR "invalid|cannot use 1 << s as type float32"
190         delete(m2, 1.1<<s) // ERROR "invalid|cannot use 1.1 << s as type float32"
191 }
192
193 // shifts of shifts
194 func _() {
195         var s uint
196         _ = 1 << (1 << s)
197         _ = 1 << (1. << s)
198         _ = 1 << (1.1 << s)   // ERROR "1.1 truncated"
199         _ = 1. << (1 << s)    // ERROR "shift of type float64"
200         _ = 1. << (1. << s)   // ERROR "shift of type float64"
201         _ = 1.1 << (1.1 << s) // ERROR "invalid|1.1 truncated"
202
203         _ = (1 << s) << (1 << s)
204         _ = (1 << s) << (1. << s)
205         _ = (1 << s) << (1.1 << s)   // ERROR "1.1 truncated"
206         _ = (1. << s) << (1 << s)    // ERROR "shift of type float64"
207         _ = (1. << s) << (1. << s)   // ERROR "shift of type float64"
208         _ = (1.1 << s) << (1.1 << s) // ERROR "invalid|1.1 truncated"
209
210         var x int
211         x = 1 << (1 << s)
212         x = 1 << (1. << s)
213         x = 1 << (1.1 << s) // ERROR "1.1 truncated"
214         x = 1. << (1 << s)
215         x = 1. << (1. << s)
216         x = 1.1 << (1.1 << s) // ERROR "1.1 truncated"
217
218         x = (1 << s) << (1 << s)
219         x = (1 << s) << (1. << s)
220         x = (1 << s) << (1.1 << s) // ERROR "1.1 truncated"
221         x = (1. << s) << (1 << s)
222         x = (1. << s) << (1. << s)
223         x = (1.1 << s) << (1.1 << s) // ERROR "1.1 truncated"
224
225         var y float32
226         y = 1 << (1 << s)     // ERROR "type float32"
227         y = 1 << (1. << s)    // ERROR "type float32"
228         y = 1 << (1.1 << s)   // ERROR "invalid|1.1 truncated|float32"
229         y = 1. << (1 << s)    // ERROR "type float32"
230         y = 1. << (1. << s)   // ERROR "type float32"
231         y = 1.1 << (1.1 << s) // ERROR "invalid|1.1 truncated|float32"
232
233         var z complex128
234         z = (1 << s) << (1 << s)     // ERROR "type complex128"
235         z = (1 << s) << (1. << s)    // ERROR "type complex128"
236         z = (1 << s) << (1.1 << s)   // ERROR "invalid|1.1 truncated|complex128"
237         z = (1. << s) << (1 << s)    // ERROR "type complex128"
238         z = (1. << s) << (1. << s)   // ERROR "type complex128"
239         z = (1.1 << s) << (1.1 << s) // ERROR "invalid|1.1 truncated|complex128"
240 }