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
|
// +build ignore
package main
// This file is the input to TestSwitches in switch_test.go.
// Each multiway conditional with constant or type cases (Switch)
// discovered by Switches is printed, and compared with the
// comments.
//
// The body of each case is printed as the value of its first
// instruction.
// -------- Value switches --------
func four() int { return 4 }
// A non-constant case makes a switch "impure", but its pure
// cases form two separate switches.
func SwitchWithNonConstantCase(x int) {
// switch t8 {
// case t1: Call <()> print t1
// case t2: Call <()> print t4
// case t3: Call <()> print t4
// default: BinOp <bool> {==} t26 t27
// }
// switch t32 {
// case t5: Call <()> print t5
// case t6: Call <()> print t6
// default: Call <()> print t7
// }
switch x {
case 1:
print(1)
case 2, 3:
print(23)
case four():
print(3)
case 5:
print(5)
case 6:
print(6)
}
print("done")
}
// Switches may be found even where the source
// program doesn't have a switch statement.
func ImplicitSwitches(x, y int) {
// switch t12 {
// case t1: Call <()> print t4
// case t2: Call <()> print t4
// default: BinOp <bool> {<} t27 t3
// }
if x == 1 || 2 == x || x < 5 {
print(12)
}
// switch t24 {
// case t5: Call <()> print t7
// case t6: Call <()> print t7
// default: BinOp <bool> {==} t49 t50
// }
if x == 3 || 4 == x || x == y {
print(34)
}
// Not a switch: no consistent variable.
if x == 5 || y == 6 {
print(56)
}
// Not a switch: only one constant comparison.
if x == 7 || x == y {
print(78)
}
}
func IfElseBasedSwitch(x int) {
// switch t4 {
// case t1: Call <()> print t1
// case t2: Call <()> print t2
// default: Call <()> print t3
// }
if x == 1 {
print(1)
} else if x == 2 {
print(2)
} else {
print("else")
}
}
func GotoBasedSwitch(x int) {
// switch t4 {
// case t1: Call <()> print t1
// case t2: Call <()> print t2
// default: Call <()> print t3
// }
if x == 1 {
goto L1
}
if x == 2 {
goto L2
}
print("else")
L1:
print(1)
goto end
L2:
print(2)
end:
}
func SwitchInAForLoop(x, y int) {
// switch t11 {
// case t2: Call <()> print t2
// case t3: Call <()> print t3
// default: BinOp <bool> {==} t29 t28
// }
loop:
for {
print("head")
switch x {
case 1:
print(1)
break loop
case 2:
print(2)
break loop
case y:
print(3)
break loop
}
}
}
// This case is a switch in a for-loop, both constructed using goto.
// As before, the default case points back to the block containing the
// switch, but that's ok.
func SwitchInAForLoopUsingGoto(x int) {
// switch t8 {
// case t2: Call <()> print t2
// case t3: Call <()> print t3
// default: BinOp <bool> {==} t8 t2
// }
loop:
print("head")
if x == 1 {
goto L1
}
if x == 2 {
goto L2
}
goto loop
L1:
print(1)
goto end
L2:
print(2)
end:
}
func UnstructuredSwitchInAForLoop(x int) {
// switch t8 {
// case t1: Call <()> print t1
// case t2: BinOp <bool> {==} t8 t1
// default: Call <()> print t3
// }
for {
if x == 1 {
print(1)
return
}
if x == 2 {
continue
}
break
}
print("end")
}
func CaseWithMultiplePreds(x int) {
for {
if x == 1 {
print(1)
return
}
loop:
// This block has multiple predecessors,
// so can't be treated as a switch case.
if x == 2 {
goto loop
}
break
}
print("end")
}
func DuplicateConstantsAreNotEliminated(x int) {
// switch t4 {
// case t1: Call <()> print t1
// case t1: Call <()> print t2
// case t3: Call <()> print t3
// default: Return
// }
if x == 1 {
print(1)
} else if x == 1 { // duplicate => unreachable
print("1a")
} else if x == 2 {
print(2)
}
}
// Interface values (created by comparisons) are not constants,
// so ConstSwitch.X is never of interface type.
func MakeInterfaceIsNotAConstant(x interface{}) {
if x == "foo" {
print("foo")
} else if x == 1 {
print(1)
}
}
func ZeroInitializedVarsAreConstants(x int) {
// switch t5 {
// case t4: Call <()> print t1
// case t2: Call <()> print t2
// default: Call <()> print t3
// }
var zero int // SSA construction replaces zero with 0
if x == zero {
print(1)
} else if x == 2 {
print(2)
}
print("end")
}
// -------- Type switches --------
// NB, potentially fragile reliance on register number.
func AdHocTypeSwitch(x interface{}) {
// switch t2.(type) {
// case t4 int: Call <()> println t8
// case t13 string: Call <()> println t16
// default: Call <()> print t1
// }
if i, ok := x.(int); ok {
println(i)
} else if s, ok := x.(string); ok {
println(s)
} else {
print("default")
}
}
|