Skip to content

Commit 258f063

Browse files
committed
Ensure StorageDead is created even if variable initialization fails
Fixes #49232
1 parent 3fd82a5 commit 258f063

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

src/librustc_mir/build/matches/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
188188
self.user_assert_ty(block, ty, var, irrefutable_pat.span);
189189
}
190190

191-
unpack!(block = self.into(&place, block, initializer));
192191
self.schedule_drop_for_binding(var, irrefutable_pat.span, OutsideGuard);
193-
block.unit()
192+
self.into(&place, block, initializer)
194193
}
195194
_ => {
196195
let place = unpack!(block = self.as_place(block, initializer));

src/test/mir-opt/issue-49232.rs

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z identify_regions -Z emit-end-regions
12+
// ignore-tidy-linelength
13+
14+
// We must mark a variable whose initialization fails due to an
15+
// abort statement as StorageDead.
16+
17+
fn main() {
18+
loop {
19+
let beacon = {
20+
match true {
21+
false => 4,
22+
true => break,
23+
}
24+
};
25+
drop(&beacon);
26+
}
27+
}
28+
29+
// END RUST SOURCE
30+
// START rustc.main.mir_map.0.mir
31+
// ...
32+
// scope 1 {
33+
// let _2: i32;
34+
// }
35+
// ...
36+
// bb3: {
37+
// StorageLive(_2);
38+
// StorageLive(_3);
39+
// _3 = const true;
40+
// EndRegion('3s);
41+
// _4 = discriminant(_3);
42+
// switchInt(_3) -> [false: bb11, otherwise: bb10];
43+
// }
44+
// ...
45+
// bb22: {
46+
// EndRegion('20_0rs);
47+
// StorageDead(_2);
48+
// goto -> bb23;
49+
// }
50+
// ...
51+
// bb28: {
52+
// EndRegion('18s);
53+
// StorageDead(_7);
54+
// EndRegion('19s);
55+
// EndRegion('19ds);
56+
// _1 = ();
57+
// EndRegion('20_0rs);
58+
// StorageDead(_2);
59+
// EndRegion('20s);
60+
// EndRegion('20ds);
61+
// goto -> bb1;
62+
// }
63+
// bb29: {
64+
// return;
65+
// }
66+
// END rustc.main.mir_map.0.mir

0 commit comments

Comments
 (0)