Skip to content

Commit f6a30bd

Browse files
committed
Auto merge of #43859 - arielb1:nonfree-block-live, r=nagisa
emit StorageLive for box temporaries We started emitting StorageDead, so we better emit the corrseponding StorageLive to avoid problems. cc #43772 rust-lang/miri#303
2 parents df80627 + e3495b2 commit f6a30bd

File tree

2 files changed

+97
-3
lines changed

2 files changed

+97
-3
lines changed

src/librustc_mir/build/expr/as_rvalue.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,19 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
9797
ExprKind::Box { value } => {
9898
let value = this.hir.mirror(value);
9999
let result = this.temp(expr.ty, expr_span);
100-
// to start, malloc some memory of suitable type (thus far, uninitialized):
101-
let box_ = Rvalue::NullaryOp(NullOp::Box, value.ty);
102-
this.cfg.push_assign(block, source_info, &result, box_);
103100
if let Some(scope) = scope {
104101
// schedule a shallow free of that memory, lest we unwind:
102+
this.cfg.push(block, Statement {
103+
source_info: source_info,
104+
kind: StatementKind::StorageLive(result.clone())
105+
});
105106
this.schedule_drop(expr_span, scope, &result, value.ty);
106107
}
108+
109+
// malloc some memory of suitable type (thus far, uninitialized):
110+
let box_ = Rvalue::NullaryOp(NullOp::Box, value.ty);
111+
this.cfg.push_assign(block, source_info, &result, box_);
112+
107113
// initialize the box contents:
108114
unpack!(block = this.into(&result.clone().deref(), block, value));
109115
block.and(Rvalue::Use(Operand::Consume(result)))

src/test/mir-opt/box_expr.rs

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
#![feature(box_syntax)]
12+
13+
fn main() {
14+
let x = box S::new();
15+
drop(x);
16+
}
17+
18+
struct S;
19+
20+
impl S {
21+
fn new() -> Self { S }
22+
}
23+
24+
impl Drop for S {
25+
fn drop(&mut self) {
26+
println!("splat!");
27+
}
28+
}
29+
30+
// END RUST SOURCE
31+
// START rustc.node4.ElaborateDrops.before.mir
32+
// let mut _0: ();
33+
// let _1: std::boxed::Box<S>;
34+
// let mut _2: std::boxed::Box<S>;
35+
// let mut _3: ();
36+
// let mut _4: std::boxed::Box<S>;
37+
//
38+
// bb0: {
39+
// StorageLive(_1);
40+
// StorageLive(_2);
41+
// _2 = Box(S);
42+
// (*_2) = const S::new() -> [return: bb1, unwind: bb3];
43+
// }
44+
//
45+
// bb1: {
46+
// _1 = _2;
47+
// drop(_2) -> bb4;
48+
// }
49+
//
50+
// bb2: {
51+
// resume;
52+
// }
53+
//
54+
// bb3: {
55+
// drop(_2) -> bb2;
56+
// }
57+
//
58+
// bb4: {
59+
// StorageDead(_2);
60+
// StorageLive(_4);
61+
// _4 = _1;
62+
// _3 = const std::mem::drop(_4) -> [return: bb5, unwind: bb7];
63+
// }
64+
//
65+
// bb5: {
66+
// drop(_4) -> [return: bb8, unwind: bb6];
67+
// }
68+
//
69+
// bb6: {
70+
// drop(_1) -> bb2;
71+
// }
72+
//
73+
// bb7: {
74+
// drop(_4) -> bb6;
75+
// }
76+
//
77+
// bb8: {
78+
// StorageDead(_4);
79+
// _0 = ();
80+
// drop(_1) -> bb9;
81+
// }
82+
//
83+
// bb9: {
84+
// StorageDead(_1);
85+
// return;
86+
// }
87+
// }
88+
// END rustc.node4.ElaborateDrops.before.mir

0 commit comments

Comments
 (0)