Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / honnef.co / go / tools@v0.0.1-2020.1.5 / ir / irutil / testdata / switches.go
1 // +build ignore
2
3 package main
4
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
8 // comments.
9 //
10 // The body of each case is printed as the value of its first
11 // instruction.
12
13 // -------- Value switches --------
14
15 func four() int { return 4 }
16
17 // A non-constant case makes a switch "impure", but its pure
18 // cases form two separate switches.
19 func SwitchWithNonConstantCase(x int) {
20         // switch t8 {
21         // case t1: Call <()> print t1
22         // case t2: Call <()> print t4
23         // case t3: Call <()> print t4
24         // default: BinOp <bool> {==} t26 t27
25         // }
26
27         // switch t32 {
28         // case t5: Call <()> print t5
29         // case t6: Call <()> print t6
30         // default: Call <()> print t7
31         // }
32         switch x {
33         case 1:
34                 print(1)
35         case 2, 3:
36                 print(23)
37         case four():
38                 print(3)
39         case 5:
40                 print(5)
41         case 6:
42                 print(6)
43         }
44         print("done")
45 }
46
47 // Switches may be found even where the source
48 // program doesn't have a switch statement.
49
50 func ImplicitSwitches(x, y int) {
51         // switch t12 {
52         // case t1: Call <()> print t4
53         // case t2: Call <()> print t4
54         // default: BinOp <bool> {<} t27 t3
55         // }
56         if x == 1 || 2 == x || x < 5 {
57                 print(12)
58         }
59
60         // switch t24 {
61         // case t5: Call <()> print t7
62         // case t6: Call <()> print t7
63         // default: BinOp <bool> {==} t49 t50
64         // }
65         if x == 3 || 4 == x || x == y {
66                 print(34)
67         }
68
69         // Not a switch: no consistent variable.
70         if x == 5 || y == 6 {
71                 print(56)
72         }
73
74         // Not a switch: only one constant comparison.
75         if x == 7 || x == y {
76                 print(78)
77         }
78 }
79
80 func IfElseBasedSwitch(x int) {
81         // switch t4 {
82         // case t1: Call <()> print t1
83         // case t2: Call <()> print t2
84         // default: Call <()> print t3
85         // }
86         if x == 1 {
87                 print(1)
88         } else if x == 2 {
89                 print(2)
90         } else {
91                 print("else")
92         }
93 }
94
95 func GotoBasedSwitch(x int) {
96         // switch t4 {
97         // case t1: Call <()> print t1
98         // case t2: Call <()> print t2
99         // default: Call <()> print t3
100         // }
101         if x == 1 {
102                 goto L1
103         }
104         if x == 2 {
105                 goto L2
106         }
107         print("else")
108 L1:
109         print(1)
110         goto end
111 L2:
112         print(2)
113 end:
114 }
115
116 func SwitchInAForLoop(x, y int) {
117         // switch t11 {
118         // case t2: Call <()> print t2
119         // case t3: Call <()> print t3
120         // default: BinOp <bool> {==} t29 t28
121         // }
122 loop:
123         for {
124                 print("head")
125                 switch x {
126                 case 1:
127                         print(1)
128                         break loop
129                 case 2:
130                         print(2)
131                         break loop
132                 case y:
133                         print(3)
134                         break loop
135                 }
136         }
137 }
138
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) {
143         // switch t8 {
144         // case t2: Call <()> print t2
145         // case t3: Call <()> print t3
146         // default: BinOp <bool> {==} t8 t2
147         // }
148 loop:
149         print("head")
150         if x == 1 {
151                 goto L1
152         }
153         if x == 2 {
154                 goto L2
155         }
156         goto loop
157 L1:
158         print(1)
159         goto end
160 L2:
161         print(2)
162 end:
163 }
164
165 func UnstructuredSwitchInAForLoop(x int) {
166         // switch t8 {
167         // case t1: Call <()> print t1
168         // case t2: BinOp <bool> {==} t8 t1
169         // default: Call <()> print t3
170         // }
171         for {
172                 if x == 1 {
173                         print(1)
174                         return
175                 }
176                 if x == 2 {
177                         continue
178                 }
179                 break
180         }
181         print("end")
182 }
183
184 func CaseWithMultiplePreds(x int) {
185         for {
186                 if x == 1 {
187                         print(1)
188                         return
189                 }
190         loop:
191                 // This block has multiple predecessors,
192                 // so can't be treated as a switch case.
193                 if x == 2 {
194                         goto loop
195                 }
196                 break
197         }
198         print("end")
199 }
200
201 func DuplicateConstantsAreNotEliminated(x int) {
202         // switch t4 {
203         // case t1: Call <()> print t1
204         // case t1: Call <()> print t2
205         // case t3: Call <()> print t3
206         // default: Return
207         // }
208         if x == 1 {
209                 print(1)
210         } else if x == 1 { // duplicate => unreachable
211                 print("1a")
212         } else if x == 2 {
213                 print(2)
214         }
215 }
216
217 // Interface values (created by comparisons) are not constants,
218 // so ConstSwitch.X is never of interface type.
219 func MakeInterfaceIsNotAConstant(x interface{}) {
220         if x == "foo" {
221                 print("foo")
222         } else if x == 1 {
223                 print(1)
224         }
225 }
226
227 func ZeroInitializedVarsAreConstants(x int) {
228         // switch t5 {
229         // case t4: Call <()> print t1
230         // case t2: Call <()> print t2
231         // default: Call <()> print t3
232         // }
233         var zero int // SSA construction replaces zero with 0
234         if x == zero {
235                 print(1)
236         } else if x == 2 {
237                 print(2)
238         }
239         print("end")
240 }
241
242 // -------- Type switches --------
243
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
250         // }
251         if i, ok := x.(int); ok {
252                 println(i)
253         } else if s, ok := x.(string); ok {
254                 println(s)
255         } else {
256                 print("default")
257         }
258 }