5 // This file is the input to TestSwitches in switch_test.go.
6 // Each multiway conditional with constant or type cases (Switch)
7 // discovered by Switches is printed, and compared with the
10 // The body of each case is printed as the value of its first
13 // -------- Value switches --------
15 func SimpleSwitch(x, y int) {
17 // case 1:int: print(1:int)
18 // case 2:int: print(23:int)
19 // case 3:int: print(23:int)
20 // case 4:int: print(3:int)
39 func four() int { return 4 }
41 // A non-constant case makes a switch "impure", but its pure
42 // cases form two separate switches.
43 func SwitchWithNonConstantCase(x int) {
45 // case 1:int: print(1:int)
46 // case 2:int: print(23:int)
47 // case 3:int: print(23:int)
52 // case 5:int: print(5:int)
53 // case 6:int: print(6:int)
54 // default: print("done":string)
71 // Switches may be found even where the source
72 // program doesn't have a switch statement.
74 func ImplicitSwitches(x, y int) {
76 // case 1:int: print(12:int)
77 // case 2:int: print(12:int)
80 if x == 1 || 2 == x || x < 5 {
85 // case 3:int: print(34:int)
86 // case 4:int: print(34:int)
89 if x == 3 || 4 == x || x == y {
93 // Not a switch: no consistent variable.
98 // Not a switch: only one constant comparison.
104 func IfElseBasedSwitch(x int) {
106 // case 1:int: print(1:int)
107 // case 2:int: print(2:int)
108 // default: print("else":string)
119 func GotoBasedSwitch(x int) {
121 // case 1:int: print(1:int)
122 // case 2:int: print(2:int)
123 // default: print("else":string)
140 func SwitchInAForLoop(x int) {
142 // case 1:int: print(1:int)
143 // case 2:int: print(2:int)
144 // default: print("head":string)
160 // This case is a switch in a for-loop, both constructed using goto.
161 // As before, the default case points back to the block containing the
162 // switch, but that's ok.
163 func SwitchInAForLoopUsingGoto(x int) {
165 // case 1:int: print(1:int)
166 // case 2:int: print(2:int)
167 // default: print("head":string)
186 func UnstructuredSwitchInAForLoop(x int) {
188 // case 1:int: print(1:int)
189 // case 2:int: x == 1:int
190 // default: print("end":string)
205 func CaseWithMultiplePreds(x int) {
212 // This block has multiple predecessors,
213 // so can't be treated as a switch case.
222 func DuplicateConstantsAreNotEliminated(x int) {
224 // case 1:int: print(1:int)
225 // case 1:int: print("1a":string)
226 // case 2:int: print(2:int)
231 } else if x == 1 { // duplicate => unreachable
238 // Interface values (created by comparisons) are not constants,
239 // so ConstSwitch.X is never of interface type.
240 func MakeInterfaceIsNotAConstant(x interface{}) {
248 func ZeroInitializedVarsAreConstants(x int) {
250 // case 0:int: print(1:int)
251 // case 2:int: print(2:int)
252 // default: print("end":string)
254 var zero int // SSA construction replaces zero with 0
263 // -------- Select --------
265 // NB, potentially fragile reliance on register number.
266 func SelectDesugarsToSwitch(ch chan int) {
268 // case 0:int: extract t0 #2
269 // case 1:int: println(0:int)
270 // case 2:int: println(1:int)
271 // default: println("default":string)
285 // NB, potentially fragile reliance on register number.
286 func NonblockingSelectDefaultCasePanics(ch chan int) {
288 // case 0:int: extract t0 #2
289 // case 1:int: println(0:int)
290 // case 2:int: println(1:int)
291 // default: make interface{} <- string ("blocking select m...":string)
303 // -------- Type switches --------
305 // NB, reliance on fragile register numbering.
306 func SimpleTypeSwitch(x interface{}) {
308 // case t3 int: println(x)
309 // case t7 bool: println(x)
310 // case t10 string: println(t10)
311 // default: println(x)
313 switch y := x.(type) {
325 // NB, potentially fragile reliance on register number.
326 func DuplicateTypesAreNotEliminated(x interface{}) {
328 // case t1 string: println(1:int)
329 // case t5 interface{}: println(t5)
330 // case t9 int: println(3:int)
333 switch y := x.(type) {
339 println(3) // unreachable!
343 // NB, potentially fragile reliance on register number.
344 func AdHocTypeSwitch(x interface{}) {
346 // case t1 int: println(t1)
347 // case t5 string: println(t5)
348 // default: print("default":string)
350 if i, ok := x.(int); ok {
352 } else if s, ok := x.(string); ok {