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 four() int { return 4 }
17 // A non-constant case makes a switch "impure", but its pure
18 // cases form two separate switches.
19 func SwitchWithNonConstantCase(x int) {
21 // case t1: Call <()> print t1
22 // case t2: Call <()> print t4
23 // case t3: Call <()> print t4
24 // default: BinOp <bool> {==} t26 t27
28 // case t5: Call <()> print t5
29 // case t6: Call <()> print t6
30 // default: Call <()> print t7
47 // Switches may be found even where the source
48 // program doesn't have a switch statement.
50 func ImplicitSwitches(x, y int) {
52 // case t1: Call <()> print t4
53 // case t2: Call <()> print t4
54 // default: BinOp <bool> {<} t27 t3
56 if x == 1 || 2 == x || x < 5 {
61 // case t5: Call <()> print t7
62 // case t6: Call <()> print t7
63 // default: BinOp <bool> {==} t49 t50
65 if x == 3 || 4 == x || x == y {
69 // Not a switch: no consistent variable.
74 // Not a switch: only one constant comparison.
80 func IfElseBasedSwitch(x int) {
82 // case t1: Call <()> print t1
83 // case t2: Call <()> print t2
84 // default: Call <()> print t3
95 func GotoBasedSwitch(x int) {
97 // case t1: Call <()> print t1
98 // case t2: Call <()> print t2
99 // default: Call <()> print t3
116 func SwitchInAForLoop(x, y int) {
118 // case t2: Call <()> print t2
119 // case t3: Call <()> print t3
120 // default: BinOp <bool> {==} t29 t28
139 // This case is a switch in a for-loop, both constructed using goto.
140 // As before, the default case points back to the block containing the
141 // switch, but that's ok.
142 func SwitchInAForLoopUsingGoto(x int) {
144 // case t2: Call <()> print t2
145 // case t3: Call <()> print t3
146 // default: BinOp <bool> {==} t8 t2
165 func UnstructuredSwitchInAForLoop(x int) {
167 // case t1: Call <()> print t1
168 // case t2: BinOp <bool> {==} t8 t1
169 // default: Call <()> print t3
184 func CaseWithMultiplePreds(x int) {
191 // This block has multiple predecessors,
192 // so can't be treated as a switch case.
201 func DuplicateConstantsAreNotEliminated(x int) {
203 // case t1: Call <()> print t1
204 // case t1: Call <()> print t2
205 // case t3: Call <()> print t3
210 } else if x == 1 { // duplicate => unreachable
217 // Interface values (created by comparisons) are not constants,
218 // so ConstSwitch.X is never of interface type.
219 func MakeInterfaceIsNotAConstant(x interface{}) {
227 func ZeroInitializedVarsAreConstants(x int) {
229 // case t4: Call <()> print t1
230 // case t2: Call <()> print t2
231 // default: Call <()> print t3
233 var zero int // SSA construction replaces zero with 0
242 // -------- Type switches --------
244 // NB, potentially fragile reliance on register number.
245 func AdHocTypeSwitch(x interface{}) {
246 // switch t2.(type) {
247 // case t4 int: Call <()> println t8
248 // case t13 string: Call <()> println t16
249 // default: Call <()> print t1
251 if i, ok := x.(int); ok {
253 } else if s, ok := x.(string); ok {