Debian Codegraph @

go/ssa/ssautil/testdata/switches.txtar

raw
1 -- go.mod --
2 module example.com
3 go 1.22
4
5 -- switches.go --
6 package main
7
8 // This file is the input to TestSwitches in switch_test.go.
9 // Each multiway conditional with constant or type cases (Switch)
10 // discovered by Switches is printed, and compared with the
11 // comments.
12 //
13 // The body of each case is printed as the value of its first
14 // instruction.
15
16 // -------- Value switches --------
17
18 func SimpleSwitch(x, y int) {
19 // switch x {
20 // case 1:int: print(1:int)
21 // case 2:int: print(23:int)
22 // case 3:int: print(23:int)
23 // case 4:int: print(3:int)
24 // default: x == y
25 // }
26 switch x {
27 case 1:
28 print(1)
29 case 2, 3:
30 print(23)
31 fallthrough
32 case 4:
33 print(3)
34 default:
35 print(4)
36 case y:
37 print(5)
38 }
39 print(6)
40 }
41
42 func four() int { return 4 }
43
44 // A non-constant case makes a switch "impure", but its pure
45 // cases form two separate switches.
46 func SwitchWithNonConstantCase(x int) {
47 // switch x {
48 // case 1:int: print(1:int)
49 // case 2:int: print(23:int)
50 // case 3:int: print(23:int)
51 // default: four()
52 // }
53
54 // switch x {
55 // case 5:int: print(5:int)
56 // case 6:int: print(6:int)
57 // default: print("done":string)
58 // }
59 switch x {
60 case 1:
61 print(1)
62 case 2, 3:
63 print(23)
64 case four():
65 print(3)
66 case 5:
67 print(5)
68 case 6:
69 print(6)
70 }
71 print("done")
72 }
73
74 // Switches may be found even where the source
75 // program doesn't have a switch statement.
76
77 func ImplicitSwitches(x, y int) {
78 // switch x {
79 // case 1:int: print(12:int)
80 // case 2:int: print(12:int)
81 // default: x < 5:int
82 // }
83 if x == 1 || 2 == x || x < 5 {
84 print(12)
85 }
86
87 // switch x {
88 // case 3:int: print(34:int)
89 // case 4:int: print(34:int)
90 // default: x == y
91 // }
92 if x == 3 || 4 == x || x == y {
93 print(34)
94 }
95
96 // Not a switch: no consistent variable.
97 if x == 5 || y == 6 {
98 print(56)
99 }
100
101 // Not a switch: only one constant comparison.
102 if x == 7 || x == y {
103 print(78)
104 }
105 }
106
107 func IfElseBasedSwitch(x int) {
108 // switch x {
109 // case 1:int: print(1:int)
110 // case 2:int: print(2:int)
111 // default: print("else":string)
112 // }
113 if x == 1 {
114 print(1)
115 } else if x == 2 {
116 print(2)
117 } else {
118 print("else")
119 }
120 }
121
122 func GotoBasedSwitch(x int) {
123 // switch x {
124 // case 1:int: print(1:int)
125 // case 2:int: print(2:int)
126 // default: print("else":string)
127 // }
128 if x == 1 {
129 goto L1
130 }
131 if x == 2 {
132 goto L2
133 }
134 print("else")
135 L1:
136 print(1)
137 goto end
138 L2:
139 print(2)
140 end:
141 }
142
143 func SwitchInAForLoop(x int) {
144 // switch x {
145 // case 1:int: print(1:int)
146 // case 2:int: print(2:int)
147 // default: print("head":string)
148 // }
149 loop:
150 for {
151 print("head")
152 switch x {
153 case 1:
154 print(1)
155 break loop
156 case 2:
157 print(2)
158 break loop
159 }
160 }
161 }
162
163 // This case is a switch in a for-loop, both constructed using goto.
164 // As before, the default case points back to the block containing the
165 // switch, but that's ok.
166 func SwitchInAForLoopUsingGoto(x int) {
167 // switch x {
168 // case 1:int: print(1:int)
169 // case 2:int: print(2:int)
170 // default: print("head":string)
171 // }
172 loop:
173 print("head")
174 if x == 1 {
175 goto L1
176 }
177 if x == 2 {
178 goto L2
179 }
180 goto loop
181 L1:
182 print(1)
183 goto end
184 L2:
185 print(2)
186 end:
187 }
188
189 func UnstructuredSwitchInAForLoop(x int) {
190 // switch x {
191 // case 1:int: print(1:int)
192 // case 2:int: x == 1:int
193 // default: print("end":string)
194 // }
195 for {
196 if x == 1 {
197 print(1)
198 return
199 }
200 if x == 2 {
201 continue
202 }
203 break
204 }
205 print("end")
206 }
207
208 func CaseWithMultiplePreds(x int) {
209 for {
210 if x == 1 {
211 print(1)
212 return
213 }
214 loop:
215 // This block has multiple predecessors,
216 // so can't be treated as a switch case.
217 if x == 2 {
218 goto loop
219 }
220 break
221 }
222 print("end")
223 }
224
225 func DuplicateConstantsAreNotEliminated(x int) {
226 // switch x {
227 // case 1:int: print(1:int)
228 // case 1:int: print("1a":string)
229 // case 2:int: print(2:int)
230 // default: return
231 // }
232 if x == 1 {
233 print(1)
234 } else if x == 1 { // duplicate => unreachable
235 print("1a")
236 } else if x == 2 {
237 print(2)
238 }
239 }
240
241 // Interface values (created by comparisons) are not constants,
242 // so ConstSwitch.X is never of interface type.
243 func MakeInterfaceIsNotAConstant(x interface{}) {
244 if x == "foo" {
245 print("foo")
246 } else if x == 1 {
247 print(1)
248 }
249 }
250
251 func ZeroInitializedVarsAreConstants(x int) {
252 // switch x {
253 // case 0:int: print(1:int)
254 // case 2:int: print(2:int)
255 // default: print("end":string)
256 // }
257 var zero int // SSA construction replaces zero with 0
258 if x == zero {
259 print(1)
260 } else if x == 2 {
261 print(2)
262 }
263 print("end")
264 }
265
266 // -------- Select --------
267
268 // NB, potentially fragile reliance on register number.
269 func SelectDesugarsToSwitch(ch chan int) {
270 // switch t1 {
271 // case 0:int: extract t0 #2
272 // case 1:int: println(0:int)
273 // case 2:int: println(1:int)
274 // default: println("default":string)
275 // }
276 select {
277 case x := <-ch:
278 println(x)
279 case <-ch:
280 println(0)
281 case ch <- 1:
282 println(1)
283 default:
284 println("default")
285 }
286 }
287
288 // NB, potentially fragile reliance on register number.
289 func NonblockingSelectDefaultCasePanics(ch chan int) {
290 // switch t1 {
291 // case 0:int: extract t0 #2
292 // case 1:int: println(0:int)
293 // case 2:int: println(1:int)
294 // default: make interface{} <- string ("blocking select m...":string)
295 // }
296 select {
297 case x := <-ch:
298 println(x)
299 case <-ch:
300 println(0)
301 case ch <- 1:
302 println(1)
303 }
304 }
305
306 // -------- Type switches --------
307
308 // NB, reliance on fragile register numbering.
309 func SimpleTypeSwitch(x interface{}) {
310 // switch x.(type) {
311 // case t3 int: println(x)
312 // case t7 bool: println(x)
313 // case t10 string: println(t10)
314 // default: println(x)
315 // }
316 switch y := x.(type) {
317 case nil:
318 println(y)
319 case int, bool:
320 println(y)
321 case string:
322 println(y)
323 default:
324 println(y)
325 }
326 }
327
328 // NB, potentially fragile reliance on register number.
329 func DuplicateTypesAreNotEliminated(x interface{}) {
330 // switch x.(type) {
331 // case t1 string: println(1:int)
332 // case t5 interface{}: println(t5)
333 // case t9 int: println(3:int)
334 // default: return
335 // }
336 switch y := x.(type) {
337 case string:
338 println(1)
339 case interface{}:
340 println(y)
341 case int:
342 println(3) // unreachable!
343 }
344 }
345
346 // NB, potentially fragile reliance on register number.
347 func AdHocTypeSwitch(x interface{}) {
348 // switch x.(type) {
349 // case t1 int: println(t1)
350 // case t5 string: println(t5)
351 // default: print("default":string)
352 // }
353 if i, ok := x.(int); ok {
354 println(i)
355 } else if s, ok := x.(string); ok {
356 println(s)
357 } else {
358 print("default")
359 }
360 }