OLD | NEW |
1 // Copyright 2012 The Go Authors. All rights reserved. | 1 // Copyright 2012 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // various expressions | 5 // various expressions |
6 | 6 |
7 package expr3 | 7 package expr3 |
8 | 8 |
9 // TODO(gri) Move the code below into function "shifts" once we check | 9 func shifts1() { |
10 // declarations with initilizations inside functions. | 10 » var ( |
11 var ( | 11 » » i0 int |
12 » i0 int | 12 » » u0 uint |
13 » u0 uint | 13 » ) |
14 ) | |
15 | 14 |
16 var ( | 15 » var ( |
17 » v0 = 1<<0 | 16 » » v0 = 1<<0 |
18 » v1 = 1<<i0 /* ERROR "must be unsigned" */ | 17 » » v1 = 1<<i0 /* ERROR "must be unsigned" */ |
19 » v2 = 1<<u0 | 18 » » v2 = 1<<u0 |
20 » v3 = 1<<"foo" /* ERROR "must be unsigned" */ | 19 » » v3 = 1<<"foo" /* ERROR "must be unsigned" */ |
21 » v4 = 1<<- /* ERROR "stupid shift" */ 1 | 20 » » v4 = 1<<- /* ERROR "stupid shift" */ 1 |
22 » v5 = 1<<1025 /* ERROR "stupid shift" */ | 21 » » v5 = 1<<1025 /* ERROR "stupid shift" */ |
23 » v6 = 1 /* ERROR "overflows" */ <<100 | 22 » » v6 = 1 /* ERROR "overflows" */ <<100 |
24 | 23 |
25 » v10 uint = 1 << 0 | 24 » » v10 uint = 1 << 0 |
26 » v11 uint = 1 << u0 | 25 » » v11 uint = 1 << u0 |
27 » v12 float32 = 1 /* ERROR "must be integer" */ << u0 | 26 » » v12 float32 = 1 /* ERROR "must be integer" */ << u0 |
28 ) | 27 » ) |
| 28 } |
29 | 29 |
30 // TODO(gri) enable commented out tests below. | 30 func shifts2() { |
31 | 31 » // TODO(gri) enable commented out tests below. |
32 // from the spec | 32 » var ( |
33 var ( | 33 » » s uint = 33 |
34 » s uint = 33 | 34 » » i = 1<<s // 1 has type int |
35 » i = 1<<s // 1 has type int | 35 » » j int32 = 1<<s // 1 has type int32; j == 0 |
36 » j int32 = 1<<s // 1 has type int32; j == 0 | 36 » » k = uint64(1<<s) // 1 has type uint64; k == 1<<33 |
37 » k = uint64(1<<s) // 1 has type uint64; k == 1<<33 | 37 » » m int = 1.0<<s // 1.0 has type int |
38 » m int = 1.0<<s // 1.0 has type int | 38 » //» n = 1.0<<s != 0 // 1.0 has type int; n == false if ints are 3
2bits in size |
39 //» n = 1.0<<s != 0 // 1.0 has type int; n == false if ints are 32bits in
size | 39 » » o = 1<<s == 2<<s // 1 and 2 have type int; o == true if ints a
re 32bits in size |
40 » o = 1<<s == 2<<s // 1 and 2 have type int; o == true if ints are 32bit
s in size | 40 » //» p = 1<<s == 1 /* ERROR "overflows" */ <<33 // illegal if ints a
re 32bits in size: 1 has type int, but 1<<33 overflows int |
41 //» p = 1<<s == 1 /* ERROR "overflows" */ <<33 // illegal if ints are 32bit
s in size: 1 has type int, but 1<<33 overflows int | 41 » » u = 1.0 /* ERROR "must be integer" */ <<s // illegal: 1.
0 has type float64, cannot shift |
42 » u = 1.0 /* ERROR "must be integer" */ <<s // illegal: 1.0 has ty
pe float64, cannot shift | 42 » » v float32 = 1 /* ERROR "must be integer" */ <<s // illegal: 1
has type float32, cannot shift |
43 » v float32 = 1 /* ERROR "must be integer" */ <<s // illegal: 1 has type
float32, cannot shift | 43 » » w int64 = 1.0<<33 // 1.0<<33 is a constant shift expression |
44 » w int64 = 1.0<<33 // 1.0<<33 is a constant shift expression | 44 » ) |
45 ) | 45 } |
46 | 46 |
47 // TODO(gri) The error messages below depond on adjusting the spec | 47 // TODO(gri) The error messages below depond on adjusting the spec |
48 // to reflect what gc is doing at the moment (the spec | 48 // to reflect what gc is doing at the moment (the spec |
49 // asks for run-time errors at the moment - see issue 4231). | 49 // asks for run-time errors at the moment - see issue 4231). |
50 // | 50 // |
51 func indexes() { | 51 func indexes() { |
52 _ = 1 /* ERROR "cannot index" */ [0] | 52 _ = 1 /* ERROR "cannot index" */ [0] |
53 _ = indexes /* ERROR "cannot index" */ [0] | 53 _ = indexes /* ERROR "cannot index" */ [0] |
54 _ = ( /* ERROR "cannot slice" */ 12 + 3)[1:2] | 54 _ = ( /* ERROR "cannot slice" */ 12 + 3)[1:2] |
55 | 55 |
56 var a [10]int | 56 var a [10]int |
57 _ = a[true /* ERROR "must be integer" */ ] | 57 _ = a[true /* ERROR "must be integer" */ ] |
58 _ = a["foo" /* ERROR "must be integer" */ ] | 58 _ = a["foo" /* ERROR "must be integer" */ ] |
59 _ = a[1.1 /* ERROR "must be integer" */ ] | 59 _ = a[1.1 /* ERROR "must be integer" */ ] |
60 _ = a[1.0] | 60 _ = a[1.0] |
61 _ = a[- /* ERROR "index .* negative" */ 1] | 61 _ = a[- /* ERROR "index .* negative" */ 1] |
62 _ = a[- /* ERROR "index .* negative" */ 1 :] | 62 _ = a[- /* ERROR "index .* negative" */ 1 :] |
63 _ = a[: - /* ERROR "index .* negative" */ 1] | 63 _ = a[: - /* ERROR "index .* negative" */ 1] |
64 var a0 int | 64 var a0 int |
65 a0 = a[0] | 65 a0 = a[0] |
66 var a1 int32 | 66 var a1 int32 |
67 a1 = a /* ERROR "cannot assign" */ [1]· | 67 a1 = a /* ERROR "cannot assign" */ [1]· |
68 _ = a[9] | 68 _ = a[9] |
69 _ = a[10 /* ERROR "index .* out of bounds" */ ] | 69 _ = a[10 /* ERROR "index .* out of bounds" */ ] |
| 70 _ = a[1 /* ERROR "stupid index" */ <<100] |
70 _ = a[10:] | 71 _ = a[10:] |
71 _ = a[:10] | 72 _ = a[:10] |
72 _ = a[10:10] | 73 _ = a[10:10] |
73 _ = a[11 /* ERROR "index .* out of bounds" */ :] | 74 _ = a[11 /* ERROR "index .* out of bounds" */ :] |
74 _ = a[: 11 /* ERROR "index .* out of bounds" */ ] | 75 _ = a[: 11 /* ERROR "index .* out of bounds" */ ] |
| 76 _ = a[: 1 /* ERROR "stupid index" */ <<100] |
75 | 77 |
76 var b [0]int | 78 var b [0]int |
77 _ = b[0 /* ERROR "index .* out of bounds" */ ] | 79 _ = b[0 /* ERROR "index .* out of bounds" */ ] |
78 _ = b[:] | 80 _ = b[:] |
79 _ = b[0:] | 81 _ = b[0:] |
80 _ = b[:0] | 82 _ = b[:0] |
81 _ = b[0:0] | 83 _ = b[0:0] |
82 | 84 |
83 var s []int | 85 var s []int |
84 _ = s[- /* ERROR "index .* negative" */ 1] | 86 _ = s[- /* ERROR "index .* negative" */ 1] |
85 _ = s[- /* ERROR "index .* negative" */ 1 :] | 87 _ = s[- /* ERROR "index .* negative" */ 1 :] |
86 _ = s[: - /* ERROR "index .* negative" */ 1] | 88 _ = s[: - /* ERROR "index .* negative" */ 1] |
87 _ = s[0] | 89 _ = s[0] |
88 _ = s[1 : 2] | 90 _ = s[1 : 2] |
89 _ = s[2 /* ERROR "inverted slice range" */ : 1] | 91 _ = s[2 /* ERROR "inverted slice range" */ : 1] |
90 _ = s[2 :] | 92 _ = s[2 :] |
91 » _ = s[: 1<<100] | 93 » _ = s[: 1 /* ERROR "stupid index" */ <<100] |
92 » _ = s[1<<100 :] | 94 » _ = s[1 /* ERROR "stupid index" */ <<100 :] |
93 » _ = s[1<<100 : 1<<100] | 95 » _ = s[1 /* ERROR "stupid index" */ <<100 : 1 /* ERROR "stupid index" */
<<100] |
94 » _ = s[1 /* ERROR "inverted slice range" */ <<100+1 : 1<<100] | |
95 » _ = s[1 /* ERROR "inverted slice range" */ <<100+1 : 10] | |
96 | 96 |
97 var t string | 97 var t string |
98 _ = t[- /* ERROR "index .* negative" */ 1] | 98 _ = t[- /* ERROR "index .* negative" */ 1] |
99 _ = t[- /* ERROR "index .* negative" */ 1 :] | 99 _ = t[- /* ERROR "index .* negative" */ 1 :] |
100 _ = t[: - /* ERROR "index .* negative" */ 1] | 100 _ = t[: - /* ERROR "index .* negative" */ 1] |
101 var t0 byte | 101 var t0 byte |
102 t0 = t[0] | 102 t0 = t[0] |
103 var t1 rune | 103 var t1 rune |
104 t1 = t /* ERROR "cannot assign" */ [2] | 104 t1 = t /* ERROR "cannot assign" */ [2] |
105 _ = ("foo" + "bar")[5] | 105 _ = ("foo" + "bar")[5] |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 | 159 |
160 // unkeyed elements | 160 // unkeyed elements |
161 _ = T0{1, 2, 3} | 161 _ = T0{1, 2, 3} |
162 _ = T0{1, b /* ERROR "mixture" */ : 2, 3} | 162 _ = T0{1, b /* ERROR "mixture" */ : 2, 3} |
163 _ = T0{1, 2} /* ERROR "too few values" */ | 163 _ = T0{1, 2} /* ERROR "too few values" */ |
164 _ = T0{1, 2, 3, 4 /* ERROR "too many values" */ } | 164 _ = T0{1, 2, 3, 4 /* ERROR "too many values" */ } |
165 _ = T0{1, "foo" /* ERROR "cannot use" */, 3.4 /* ERROR "cannot use" */} | 165 _ = T0{1, "foo" /* ERROR "cannot use" */, 3.4 /* ERROR "cannot use" */} |
166 } | 166 } |
167 | 167 |
168 func array_literals() { | 168 func array_literals() { |
169 » // TODO(gri) | 169 » type A0 [0]int |
| 170 » _ = A0{} |
| 171 » _ = A0{0 /* ERROR "index .* out of bounds" */} |
| 172 » _ = A0{0 /* ERROR "index .* out of bounds" */ : 0} |
| 173 |
| 174 » type A1 [10]int |
| 175 » _ = A1{} |
| 176 » _ = A1{0, 1, 2} |
| 177 » _ = A1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} |
| 178 » _ = A1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 /* ERROR "index .* out of bounds
" */ } |
| 179 » _ = A1{- /* ERROR "index .* negative" */ 1: 0} |
| 180 » _ = A1{8: 8, 9} |
| 181 » _ = A1{8: 8, 9, 10 /* ERROR "index .* out of bounds" */ } |
| 182 » _ = A1{0, 1, 2, 0 /* ERROR "duplicate index" */ : 0, 3: 3, 4} |
| 183 » _ = A1{5: 5, 6, 7, 3: 3, 4} |
| 184 » _ = A1{5: 5, 6, 7, 3: 3, 4, 5 /* ERROR "duplicate index" */ } |
| 185 » _ = A1{10 /* ERROR "index .* out of bounds" */ : 10, 10 /* ERROR "index
.* out of bounds" */ : 10} |
| 186 » _ = A1{5: 5, 6, 7, 3: 3, 1 /* ERROR "stupid index" */ <<100: 4, 5 /* ERR
OR "duplicate index" */ } |
| 187 » _ = A1{5: 5, 6, 7, 4: 4, 1 /* ERROR "stupid index" */ <<100: 4} |
| 188 » _ = A1{2.0} |
| 189 » _ = A1{2.1 /* ERROR "cannot use" */ } |
| 190 » _ = A1{"foo" /* ERROR "cannot use" */ } |
| 191 |
| 192 » a0 := [...]int{} |
| 193 » assert(len(a0) == 0) |
| 194 »······· |
| 195 » a1 := [...]int{0, 1, 2} |
| 196 » assert(len(a1) == 3) |
| 197 » var a13 [3]int |
| 198 » var a14 [4]int |
| 199 » a13 = a1 |
| 200 » a14 = a1 /* ERROR "cannot assign" */ |
| 201 »······· |
| 202 » a2 := [...]int{- /* ERROR "index .* negative" */ 1: 0} |
| 203 |
| 204 » a3 := [...]int{0, 1, 2, 0 /* ERROR "duplicate index" */ : 0, 3: 3, 4} |
| 205 » assert(len(a3) == 5) // somewhat arbitrary |
| 206 |
| 207 » a4 := [...]complex128{0, 1, 2, 1<<10-2: -1i, 1i, 400: 10, 12, 14} |
| 208 » assert(len(a4) == 1024) |
170 } | 209 } |
171 | 210 |
172 func slice_literals() { | 211 func slice_literals() { |
173 » // TODO(gri) | 212 » type S0 []int |
| 213 » _ = S0{} |
| 214 » _ = S0{0, 1, 2} |
| 215 » _ = S0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} |
| 216 » _ = S0{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} |
| 217 » _ = S0{- /* ERROR "index .* negative" */ 1: 0} |
| 218 » _ = S0{8: 8, 9} |
| 219 » _ = S0{8: 8, 9, 10} |
| 220 » _ = S0{0, 1, 2, 0 /* ERROR "duplicate index" */ : 0, 3: 3, 4} |
| 221 » _ = S0{5: 5, 6, 7, 3: 3, 4} |
| 222 » _ = S0{5: 5, 6, 7, 3: 3, 4, 5 /* ERROR "duplicate index" */ } |
| 223 » _ = S0{10: 10, 10 /* ERROR "duplicate index" */ : 10} |
| 224 » _ = S0{5: 5, 6, 7, 3: 3, 1 /* ERROR "stupid index" */ <<100: 4, 5 /* ERR
OR "duplicate index" */ } |
| 225 » _ = S0{5: 5, 6, 7, 4: 4, 1 /* ERROR "stupid index" */ <<100: 4} |
| 226 » _ = S0{2.0} |
| 227 » _ = S0{2.1 /* ERROR "cannot use" */ } |
| 228 » _ = S0{"foo" /* ERROR "cannot use" */ } |
174 } | 229 } |
175 | 230 |
176 func map_literals() { | 231 func map_literals() { |
177 type M0 map[string]int | 232 type M0 map[string]int |
178 | 233 |
179 _ = M0{} | 234 _ = M0{} |
180 _ = M0{1 /* ERROR "missing key" */ } | 235 _ = M0{1 /* ERROR "missing key" */ } |
181 _ = M0{1 /* ERROR "cannot use .* as string key" */ : 2} | 236 _ = M0{1 /* ERROR "cannot use .* as string key" */ : 2} |
182 _ = M0{"foo": "bar" /* ERROR "cannot use .* as int value" */ } | 237 _ = M0{"foo": "bar" /* ERROR "cannot use .* as int value" */ } |
183 _ = M0{"foo": 1, "bar": 2, "foo" /* ERROR "duplicate key" */ : 3 } | 238 _ = M0{"foo": 1, "bar": 2, "foo" /* ERROR "duplicate key" */ : 3 } |
184 } | 239 } |
OLD | NEW |