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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
// run
// Copyright 2009 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 switch statements.
package main
import "os"
func assert(cond bool, msg string) {
if !cond {
print("assertion fail: ", msg, "\n")
panic(1)
}
}
func main() {
i5 := 5
i7 := 7
hello := "hello"
switch true {
case i5 < 5:
assert(false, "<")
case i5 == 5:
assert(true, "!")
case i5 > 5:
assert(false, ">")
}
switch {
case i5 < 5:
assert(false, "<")
case i5 == 5:
assert(true, "!")
case i5 > 5:
assert(false, ">")
}
switch x := 5; true {
case i5 < x:
assert(false, "<")
case i5 == x:
assert(true, "!")
case i5 > x:
assert(false, ">")
}
switch x := 5; true {
case i5 < x:
assert(false, "<")
case i5 == x:
assert(true, "!")
case i5 > x:
assert(false, ">")
}
switch i5 {
case 0:
assert(false, "0")
case 1:
assert(false, "1")
case 2:
assert(false, "2")
case 3:
assert(false, "3")
case 4:
assert(false, "4")
case 5:
assert(true, "5")
case 6:
assert(false, "6")
case 7:
assert(false, "7")
case 8:
assert(false, "8")
case 9:
assert(false, "9")
default:
assert(false, "default")
}
switch i5 {
case 0, 1, 2, 3, 4:
assert(false, "4")
case 5:
assert(true, "5")
case 6, 7, 8, 9:
assert(false, "9")
default:
assert(false, "default")
}
switch i5 {
case 0:
case 1:
case 2:
case 3:
case 4:
assert(false, "4")
case 5:
assert(true, "5")
case 6:
case 7:
case 8:
case 9:
default:
assert(i5 == 5, "good")
}
switch i5 {
case 0:
dummy := 0
_ = dummy
fallthrough
case 1:
dummy := 0
_ = dummy
fallthrough
case 2:
dummy := 0
_ = dummy
fallthrough
case 3:
dummy := 0
_ = dummy
fallthrough
case 4:
dummy := 0
_ = dummy
assert(false, "4")
case 5:
dummy := 0
_ = dummy
fallthrough
case 6:
dummy := 0
_ = dummy
fallthrough
case 7:
dummy := 0
_ = dummy
fallthrough
case 8:
dummy := 0
_ = dummy
fallthrough
case 9:
dummy := 0
_ = dummy
fallthrough
default:
dummy := 0
_ = dummy
assert(i5 == 5, "good")
}
fired := false
switch i5 {
case 0:
dummy := 0
_ = dummy
fallthrough // tests scoping of cases
case 1:
dummy := 0
_ = dummy
fallthrough
case 2:
dummy := 0
_ = dummy
fallthrough
case 3:
dummy := 0
_ = dummy
fallthrough
case 4:
dummy := 0
_ = dummy
assert(false, "4")
case 5:
dummy := 0
_ = dummy
fallthrough
case 6:
dummy := 0
_ = dummy
fallthrough
case 7:
dummy := 0
_ = dummy
fallthrough
case 8:
dummy := 0
_ = dummy
fallthrough
case 9:
dummy := 0
_ = dummy
fallthrough
default:
dummy := 0
_ = dummy
fired = !fired
assert(i5 == 5, "good")
}
assert(fired, "fired")
count := 0
switch i5 {
case 0:
count = count + 1
fallthrough
case 1:
count = count + 1
fallthrough
case 2:
count = count + 1
fallthrough
case 3:
count = count + 1
fallthrough
case 4:
count = count + 1
assert(false, "4")
case 5:
count = count + 1
fallthrough
case 6:
count = count + 1
fallthrough
case 7:
count = count + 1
fallthrough
case 8:
count = count + 1
fallthrough
case 9:
count = count + 1
fallthrough
default:
assert(i5 == count, "good")
}
assert(fired, "fired")
switch hello {
case "wowie":
assert(false, "wowie")
case "hello":
assert(true, "hello")
case "jumpn":
assert(false, "jumpn")
default:
assert(false, "default")
}
fired = false
switch i := i5 + 2; i {
case i7:
fired = true
default:
assert(false, "fail")
}
assert(fired, "var")
// switch on nil-only comparison types
switch f := func() {}; f {
case nil:
assert(false, "f should not be nil")
default:
}
switch m := make(map[int]int); m {
case nil:
assert(false, "m should not be nil")
default:
}
switch a := make([]int, 1); a {
case nil:
assert(false, "m should not be nil")
default:
}
// switch on interface.
switch i := interface{}("hello"); i {
case 42:
assert(false, `i should be "hello"`)
case "hello":
assert(true, "hello")
default:
assert(false, `i should be "hello"`)
}
// switch on implicit bool converted to interface
// was broken: see issue 3980
switch i := interface{}(true); {
case i:
assert(true, "true")
case false:
assert(false, "i should be true")
default:
assert(false, "i should be true")
}
// switch on interface with constant cases differing by type.
// was rejected by compiler: see issue 4781
type T int
type B bool
type F float64
type S string
switch i := interface{}(float64(1.0)); i {
case nil:
assert(false, "i should be float64(1.0)")
case (*int)(nil):
assert(false, "i should be float64(1.0)")
case 1:
assert(false, "i should be float64(1.0)")
case T(1):
assert(false, "i should be float64(1.0)")
case F(1.0):
assert(false, "i should be float64(1.0)")
case 1.0:
assert(true, "true")
case "hello":
assert(false, "i should be float64(1.0)")
case S("hello"):
assert(false, "i should be float64(1.0)")
case true, B(false):
assert(false, "i should be float64(1.0)")
case false, B(true):
assert(false, "i should be float64(1.0)")
}
// switch on array.
switch ar := [3]int{1, 2, 3}; ar {
case [3]int{1, 2, 3}:
assert(true, "[1 2 3]")
case [3]int{4, 5, 6}:
assert(false, "ar should be [1 2 3]")
default:
assert(false, "ar should be [1 2 3]")
}
// switch on channel
switch c1, c2 := make(chan int), make(chan int); c1 {
case nil:
assert(false, "c1 did not match itself")
case c2:
assert(false, "c1 did not match itself")
case c1:
assert(true, "chan")
default:
assert(false, "c1 did not match itself")
}
// empty switch
switch {
}
// empty switch with default case.
fired = false
switch {
default:
fired = true
}
assert(fired, "fail")
// Default and fallthrough.
count = 0
switch {
default:
count++
fallthrough
case false:
count++
}
assert(count == 2, "fail")
// fallthrough to default, which is not at end.
count = 0
switch i5 {
case 5:
count++
fallthrough
default:
count++
case 6:
count++
}
assert(count == 2, "fail")
i := 0
switch x := 5; {
case i < x:
os.Exit(0)
case i == x:
case i > x:
os.Exit(1)
}
// Unified IR converts the tag and all case values to empty
// interface, when any of the case values aren't assignable to the
// tag value's type. Make sure that `case nil:` compares against the
// tag type's nil value (i.e., `(*int)(nil)`), not nil interface
// (i.e., `any(nil)`).
switch (*int)(nil) {
case nil:
// ok
case any(nil):
assert(false, "case any(nil) matched")
default:
assert(false, "default matched")
}
}
|