]> git.lizzy.rs Git - rust.git/blob - src/test/mir-opt/match-arm-scopes.rs
Rollup merge of #68978 - ecstatic-morse:const-int-pow, r=oli-obk
[rust.git] / src / test / mir-opt / match-arm-scopes.rs
1 // Test that StorageDead and Drops are generated properly for bindings in
2 // matches:
3 // * The MIR should only contain a single drop of `s` and `t`: at the end
4 //   of their respective arms.
5 // * StorageDead and StorageLive statements are correctly matched up on
6 //   non-unwind paths.
7 // * The visibility scopes of the match arms should be disjoint, and contain.
8 //   all of the bindings for that scope.
9 // * No drop flags are used.
10
11 fn complicated_match(cond: bool, items: (bool, bool, String)) -> i32 {
12     match items {
13         (false, a, s) | (a, false, s) if if cond { return 3 } else { a } => 1,
14         (true, b, t) | (false, b, t) => 2,
15     }
16 }
17
18 const CASES: &[(bool, bool, bool, i32)] = &[
19     (false, false, false, 2),
20     (false, false, true, 1),
21     (false, true, false, 1),
22     (false, true, true, 2),
23     (true, false, false, 3),
24     (true, false, true, 3),
25     (true, true, false, 3),
26     (true, true, true, 2),
27 ];
28
29 fn main() {
30     for &(cond, items_1, items_2, result) in CASES {
31         assert_eq!(complicated_match(cond, (items_1, items_2, String::new())), result,);
32     }
33 }
34
35 // END RUST SOURCE
36 // START rustc.complicated_match.SimplifyCfg-initial.after.mir
37 // let mut _0: i32;
38 // let mut _3: &bool;                   // Temp for fake borrow of `items.0`
39 // let mut _4: &bool;                   // Temp for fake borrow of `items.1`
40 // let _5: bool;                        // `a` in arm
41 // let _6: &bool;                       // `a` in guard
42 // let _7: std::string::String;         // `s` in arm
43 // let _8: &std::string::String;        // `s` in guard
44 // let mut _9: bool;                    // `if cond { return 3 } else { a }`
45 // let mut _10: bool;                   // `cond`
46 // let mut _11: !;                      // `return 3`
47 // let mut _12: bool;                   // `if cond { return 3 } else { a }`
48 // let mut _13: bool;                   // `cond`
49 // let mut _14: !;                      // `return 3`
50 // let _15: bool;                       // `b`
51 // let _16: std::string::String;        // `t`
52 // scope 1 {
53 //     debug a => _5;
54 //     debug a => _6;
55 //     debug s => _7;
56 //     debug s => _8;
57 // }
58 // scope 2 {
59 //     debug b => _15;
60 //     debug t => _16;
61 // }
62 // bb0: {
63 //     FakeRead(ForMatchedPlace, _2);
64 //     switchInt((_2.0: bool)) -> [false: bb2, otherwise: bb3];
65 // }
66 // bb1 (cleanup): {
67 //     resume;
68 // }
69 // bb2: {                               // pre-binding for arm 1 first pattern
70 //     falseEdges -> [real: bb9, imaginary: bb4];
71 // }
72 // bb3: {
73 //     switchInt((_2.1: bool)) -> [false: bb4, otherwise: bb5];
74 // }
75 // bb4: {                               // pre-binding for arm 1 second pattern
76 //     falseEdges -> [real: bb18, imaginary: bb6];
77 // }
78 // bb5: {
79 //     switchInt((_2.0: bool)) -> [false: bb7, otherwise: bb6];
80 // }
81 // bb6: {                               // pre-binding for arm 2 first pattern
82 //     falseEdges -> [real: bb26, imaginary: bb7];
83 // }
84 // bb7: {                               // bindings for arm 2 - second pattern
85 //     StorageLive(_15);
86 //     _15 = (_2.1: bool);
87 //     StorageLive(_16);
88 //     _16 = move (_2.2: std::string::String);
89 //     goto -> bb25;
90 // }
91 // bb8: {                               // arm 1
92 //     _0 = const 1i32;
93 //     drop(_7) -> [return: bb24, unwind: bb14];
94 // }
95 // bb9: {                               // guard - first time
96 //     StorageLive(_6);
97 //     _6 = &(_2.1: bool);
98 //     StorageLive(_8);
99 //     _8 = &(_2.2: std::string::String);
100 //     _3 = &shallow (_2.0: bool);
101 //     _4 = &shallow (_2.1: bool);
102 //     StorageLive(_9);
103 //     StorageLive(_10);
104 //     _10 = _1;
105 //     FakeRead(ForMatchedPlace, _10);
106 //     switchInt(_10) -> [false: bb11, otherwise: bb10];
107 // }
108 // bb10: {
109 //     falseEdges -> [real: bb12, imaginary: bb11];
110 // }
111 // bb11: {                              // `else` block - first time
112 //     _9 = (*_6);
113 //     StorageDead(_10);
114 //     switchInt(move _9) -> [false: bb17, otherwise: bb16];
115 // }
116 // bb12: {                              // `return 3` - first time
117 //     _0 = const 3i32;
118 //     StorageDead(_10);
119 //     StorageDead(_9);
120 //     StorageDead(_8);
121 //     StorageDead(_6);
122 //     goto -> bb15;
123 // }
124 // bb13: {
125 //     return;
126 // }
127 // bb14 (cleanup): {
128 //     drop(_2) -> bb1;
129 // }
130 // bb15: {
131 //     drop(_2) -> [return: bb13, unwind: bb1];
132 // }
133 // bb16: {
134 //     StorageDead(_9);
135 //     FakeRead(ForMatchGuard, _3);
136 //     FakeRead(ForMatchGuard, _4);
137 //     FakeRead(ForGuardBinding, _6);
138 //     FakeRead(ForGuardBinding, _8);
139 //     StorageLive(_5);
140 //     _5 = (_2.1: bool);
141 //     StorageLive(_7);
142 //     _7 = move (_2.2: std::string::String);
143 //     goto -> bb8;
144 // }
145 // bb17: {                              // guard otherwise case - first time
146 //     StorageDead(_9);
147 //     StorageDead(_8);
148 //     StorageDead(_6);
149 //     falseEdges -> [real: bb3, imaginary: bb4];
150 // }
151 // bb18: {                              // guard - second time
152 //     StorageLive(_6);
153 //     _6 = &(_2.0: bool);
154 //     StorageLive(_8);
155 //     _8 = &(_2.2: std::string::String);
156 //     _3 = &shallow (_2.0: bool);
157 //     _4 = &shallow (_2.1: bool);
158 //     StorageLive(_12);
159 //     StorageLive(_13);
160 //     _13 = _1;
161 //     FakeRead(ForMatchedPlace, _13);
162 //     switchInt(_13) -> [false: bb20, otherwise: bb19];
163 // }
164 // bb19: {
165 //     falseEdges -> [real: bb21, imaginary: bb20];
166 // }
167 // bb20: {                              // `else` block - second time
168 //     _12 = (*_6);
169 //     StorageDead(_13);
170 //     switchInt(move _12) -> [false: bb23, otherwise: bb22];
171 // }
172 // bb21: {
173 //     _0 = const 3i32;
174 //     StorageDead(_13);
175 //     StorageDead(_12);
176 //     StorageDead(_8);
177 //     StorageDead(_6);
178 //     goto -> bb15;
179 // }
180 // bb22: {                              // bindings for arm 1
181 //     StorageDead(_12);
182 //     FakeRead(ForMatchGuard, _3);
183 //     FakeRead(ForMatchGuard, _4);
184 //     FakeRead(ForGuardBinding, _6);
185 //     FakeRead(ForGuardBinding, _8);
186 //     StorageLive(_5);
187 //     _5 = (_2.0: bool);
188 //     StorageLive(_7);
189 //     _7 = move (_2.2: std::string::String);
190 //     goto -> bb8;
191 // }
192 // bb23: {                              // Guard otherwise case - second time
193 //     StorageDead(_12);
194 //     StorageDead(_8);
195 //     StorageDead(_6);
196 //     falseEdges -> [real: bb5, imaginary: bb6];
197 // }
198 // bb24: {                              // rest of arm 1
199 //     StorageDead(_7);
200 //     StorageDead(_5);
201 //     StorageDead(_8);
202 //     StorageDead(_6);
203 //     goto -> bb28;
204 // }
205 // bb25: {                              // arm 2
206 //     _0 = const 2i32;
207 //     drop(_16) -> [return: bb27, unwind: bb14];
208 // }
209 // bb26: {                              // bindings for arm 2 - first pattern
210 //     StorageLive(_15);
211 //     _15 = (_2.1: bool);
212 //     StorageLive(_16);
213 //     _16 = move (_2.2: std::string::String);
214 //     goto -> bb25;
215 // }
216
217 // bb27: {                              // rest of arm 2
218 //     StorageDead(_16);
219 //     StorageDead(_15);
220 //     goto -> bb28;
221 // }
222 // bb28: {
223 //     drop(_2) -> [return: bb13, unwind: bb1];
224 // }
225 // END rustc.complicated_match.SimplifyCfg-initial.after.mir
226 // START rustc.complicated_match.ElaborateDrops.after.mir
227 // let _16: std::string::String;      // No drop flags, which would come after this.
228 // scope 1 {
229 // END rustc.complicated_match.ElaborateDrops.after.mir