| 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 four() int { return 4 } |
| 19 |
|
| 20 |
// A non-constant case makes a switch "impure", but its pure |
| 21 |
// cases form two separate switches. |
| 22 |
func SwitchWithNonConstantCase(x int) { |
| 23 |
// switch x { |
| 24 |
// case 1:int: print(1:int) |
| 25 |
// case 2:int: print(23:int) |
| 26 |
// case 3:int: print(23:int) |
| 27 |
// default: four() |
| 28 |
// } |
| 29 |
|
| 30 |
// switch x { |
| 31 |
// case 5:int: print(5:int) |
| 32 |
// case 6:int: print(6:int) |
| 33 |
// default: print("done":string) |
| 34 |
// } |
| 35 |
switch x { |
| 36 |
case 1: |
| 37 |
print(1) |
| 38 |
case 2, 3: |
| 39 |
print(23) |
| 40 |
case four(): |
| 41 |
print(3) |
| 42 |
case 5: |
| 43 |
print(5) |
| 44 |
case 6: |
| 45 |
print(6) |
| 46 |
} |
| 47 |
print("done") |
| 48 |
} |
| 49 |
|
| 50 |
// Switches may be found even where the source |
| 51 |
// program doesn't have a switch statement. |
| 52 |
|
| 53 |
func ImplicitSwitches(x, y int) { |
| 54 |
// switch x { |
| 55 |
// case 1:int: print(12:int) |
| 56 |
// case 2:int: print(12:int) |
| 57 |
// default: x < 5:int |
| 58 |
// } |
| 59 |
if x == 1 || 2 == x || x < 5 { |
| 60 |
print(12) |
| 61 |
} |
| 62 |
|
| 63 |
// switch x { |
| 64 |
// case 3:int: print(34:int) |
| 65 |
// case 4:int: print(34:int) |
| 66 |
// default: x == y |
| 67 |
// } |
| 68 |
if x == 3 || 4 == x || x == y { |
| 69 |
print(34) |
| 70 |
} |
| 71 |
|
| 72 |
// Not a switch: no consistent variable. |
| 73 |
if x == 5 || y == 6 { |
| 74 |
print(56) |
| 75 |
} |
| 76 |
|
| 77 |
// Not a switch: only one constant comparison. |
| 78 |
if x == 7 || x == y { |
| 79 |
print(78) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func IfElseBasedSwitch(x int) { |
| 84 |
// switch x { |
| 85 |
// case 1:int: print(1:int) |
| 86 |
// case 2:int: print(2:int) |
| 87 |
// default: print("else":string) |
| 88 |
// } |
| 89 |
if x == 1 { |
| 90 |
print(1) |
| 91 |
} else if x == 2 { |
| 92 |
print(2) |
| 93 |
} else { |
| 94 |
print("else") |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
func GotoBasedSwitch(x int) { |
| 99 |
// switch x { |
| 100 |
// case 1:int: print(1:int) |
| 101 |
// case 2:int: print(2:int) |
| 102 |
// default: print("else":string) |
| 103 |
// } |
| 104 |
if x == 1 { |
| 105 |
goto L1 |
| 106 |
} |
| 107 |
if x == 2 { |
| 108 |
goto L2 |
| 109 |
} |
| 110 |
print("else") |
| 111 |
L1: |
| 112 |
print(1) |
| 113 |
goto end |
| 114 |
L2: |
| 115 |
print(2) |
| 116 |
end: |
| 117 |
} |
| 118 |
|
| 119 |
func SwitchInAForLoop(x, y int) { |
| 120 |
// switch x { |
| 121 |
// case 1:int: print(1:int) |
| 122 |
// case 2:int: print(2:int) |
| 123 |
// default: x == y |
| 124 |
// } |
| 125 |
loop: |
| 126 |
for { |
| 127 |
print("head") |
| 128 |
switch x { |
| 129 |
case 1: |
| 130 |
print(1) |
| 131 |
break loop |
| 132 |
case 2: |
| 133 |
print(2) |
| 134 |
break loop |
| 135 |
case y: |
| 136 |
print(3) |
| 137 |
break loop |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// This case is a switch in a for-loop, both constructed using goto. |
| 143 |
// As before, the default case points back to the block containing the |
| 144 |
// switch, but that's ok. |
| 145 |
func SwitchInAForLoopUsingGoto(x int) { |
| 146 |
// switch x { |
| 147 |
// case 1:int: print(1:int) |
| 148 |
// case 2:int: print(2:int) |
| 149 |
// default: print("head":string) |
| 150 |
// } |
| 151 |
loop: |
| 152 |
print("head") |
| 153 |
if x == 1 { |
| 154 |
goto L1 |
| 155 |
} |
| 156 |
if x == 2 { |
| 157 |
goto L2 |
| 158 |
} |
| 159 |
goto loop |
| 160 |
L1: |
| 161 |
print(1) |
| 162 |
goto end |
| 163 |
L2: |
| 164 |
print(2) |
| 165 |
end: |
| 166 |
} |
| 167 |
|
| 168 |
func UnstructuredSwitchInAForLoop(x int) { |
| 169 |
// switch x { |
| 170 |
// case 1:int: print(1:int) |
| 171 |
// case 2:int: x == 1:int |
| 172 |
// default: print("end":string) |
| 173 |
// } |
| 174 |
for { |
| 175 |
if x == 1 { |
| 176 |
print(1) |
| 177 |
return |
| 178 |
} |
| 179 |
if x == 2 { |
| 180 |
continue |
| 181 |
} |
| 182 |
break |
| 183 |
} |
| 184 |
print("end") |
| 185 |
} |
| 186 |
|
| 187 |
func CaseWithMultiplePreds(x int) { |
| 188 |
for { |
| 189 |
if x == 1 { |
| 190 |
print(1) |
| 191 |
return |
| 192 |
} |
| 193 |
loop: |
| 194 |
// This block has multiple predecessors, |
| 195 |
// so can't be treated as a switch case. |
| 196 |
if x == 2 { |
| 197 |
goto loop |
| 198 |
} |
| 199 |
break |
| 200 |
} |
| 201 |
print("end") |
| 202 |
} |
| 203 |
|
| 204 |
func DuplicateConstantsAreNotEliminated(x int) { |
| 205 |
// switch x { |
| 206 |
// case 1:int: print(1:int) |
| 207 |
// case 1:int: print("1a":string) |
| 208 |
// case 2:int: print(2:int) |
| 209 |
// default: return |
| 210 |
// } |
| 211 |
if x == 1 { |
| 212 |
print(1) |
| 213 |
} else if x == 1 { // duplicate => unreachable |
| 214 |
print("1a") |
| 215 |
} else if x == 2 { |
| 216 |
print(2) |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
// Interface values (created by comparisons) are not constants, |
| 221 |
// so ConstSwitch.X is never of interface type. |
| 222 |
func MakeInterfaceIsNotAConstant(x interface{}) { |
| 223 |
if x == "foo" { |
| 224 |
print("foo") |
| 225 |
} else if x == 1 { |
| 226 |
print(1) |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
func ZeroInitializedVarsAreConstants(x int) { |
| 231 |
// switch x { |
| 232 |
// case 0:int: print(1:int) |
| 233 |
// case 2:int: print(2:int) |
| 234 |
// default: print("end":string) |
| 235 |
// } |
| 236 |
var zero int // SSA construction replaces zero with 0 |
| 237 |
if x == zero { |
| 238 |
print(1) |
| 239 |
} else if x == 2 { |
| 240 |
print(2) |
| 241 |
} |
| 242 |
print("end") |
| 243 |
} |
| 244 |
|
| 245 |
// -------- Type switches -------- |
| 246 |
|
| 247 |
// NB, potentially fragile reliance on register number. |
| 248 |
func AdHocTypeSwitch(x interface{}) { |
| 249 |
// switch x.(type) { |
| 250 |
// case t2 int: println(t2) |
| 251 |
// case t9 string: println(t9) |
| 252 |
// default: print("default":string) |
| 253 |
// } |
| 254 |
if i, ok := x.(int); ok { |
| 255 |
println(i) |
| 256 |
} else if s, ok := x.(string); ok { |
| 257 |
println(s) |
| 258 |
} else { |
| 259 |
print("default") |
| 260 |
} |
| 261 |
} |