Skip to content

Commit 904d6c8

Browse files
committed
destroy temp at the end and avoid ICE
1 parent 105e90f commit 904d6c8

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

compiler/rustc_mir_transform/src/deref_separator.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
88
let mut patch = MirPatch::new(body);
99
let (basic_blocks, local_decl) = body.basic_blocks_and_local_decls_mut();
1010
for (block, data) in basic_blocks.iter_enumerated_mut() {
11+
let statement_len = data.statements.len();
1112
for (i, stmt) in data.statements.iter_mut().enumerate() {
1213
match stmt.kind {
1314
StatementKind::Assign(box (og_place, Rvalue::Ref(region, borrow_knd, place))) => {
1415
for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() {
15-
if p_elem == ProjectionElem::Deref && !p_ref.projection.is_empty() {
16+
if p_elem == ProjectionElem::Deref
17+
&& !p_ref.projection.is_empty()
18+
&& region.is_erased()
19+
{
1620
// The type that we are derefing
1721
let ty = p_ref.ty(local_decl, tcx).ty;
1822
let temp = patch.new_temp(ty, stmt.source_info.span);
@@ -36,21 +40,20 @@ pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
3640
// and copying derefed values which we need to create new statement
3741
let temp_place =
3842
Place::from(temp).project_deeper(&place.projection[idx..], tcx);
39-
patch.add_assign(
40-
loc,
41-
og_place,
42-
Rvalue::Ref(region, borrow_knd, temp_place),
43-
);
44-
4543
let new_stmt = Statement {
4644
source_info: stmt.source_info,
4745
kind: StatementKind::Assign(Box::new((
4846
og_place,
4947
Rvalue::Ref(region, borrow_knd, temp_place),
5048
))),
5149
};
50+
5251
// Replace current statement with newly created one
5352
*stmt = new_stmt;
53+
54+
// Since our job with the temp is done it should be gone
55+
let loc = Location { block: block, statement_index: statement_len };
56+
patch.add_statement(loc, StatementKind::StorageDead(temp));
5457
}
5558
}
5659
}

src/test/mir-opt/derefer_test.main.Derefer.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@
3737
- _4 = &mut ((*(_2.1: &mut (i32, i32))).0: i32); // scope 2 at $DIR/derefer_test.rs:5:13: 5:26
3838
+ StorageLive(_6); // scope 2 at $DIR/derefer_test.rs:5:13: 5:26
3939
+ _6 = move (_2.1: &mut (i32, i32)); // scope 2 at $DIR/derefer_test.rs:5:13: 5:26
40-
+ _4 = &mut ((*_6).0: i32); // scope 2 at $DIR/derefer_test.rs:5:13: 5:26
4140
+ _4 = &mut ((*_6).0: i32); // scope 2 at $DIR/derefer_test.rs:5:13: 5:26
4241
StorageLive(_5); // scope 3 at $DIR/derefer_test.rs:6:9: 6:10
4342
- _5 = &mut ((*(_2.1: &mut (i32, i32))).1: i32); // scope 3 at $DIR/derefer_test.rs:6:13: 6:26
4443
+ StorageLive(_7); // scope 3 at $DIR/derefer_test.rs:6:13: 6:26
4544
+ _7 = move (_2.1: &mut (i32, i32)); // scope 3 at $DIR/derefer_test.rs:6:13: 6:26
46-
+ _5 = &mut ((*_7).1: i32); // scope 3 at $DIR/derefer_test.rs:6:13: 6:26
4745
+ _5 = &mut ((*_7).1: i32); // scope 3 at $DIR/derefer_test.rs:6:13: 6:26
4846
_0 = const (); // scope 0 at $DIR/derefer_test.rs:2:11: 7:2
4947
StorageDead(_5); // scope 3 at $DIR/derefer_test.rs:7:1: 7:2
5048
StorageDead(_4); // scope 2 at $DIR/derefer_test.rs:7:1: 7:2
5149
StorageDead(_2); // scope 1 at $DIR/derefer_test.rs:7:1: 7:2
5250
StorageDead(_1); // scope 0 at $DIR/derefer_test.rs:7:1: 7:2
51+
+ StorageDead(_6); // scope 0 at $DIR/derefer_test.rs:7:2: 7:2
52+
+ StorageDead(_7); // scope 0 at $DIR/derefer_test.rs:7:2: 7:2
5353
return; // scope 0 at $DIR/derefer_test.rs:7:2: 7:2
5454
+ }
5555
+

src/test/mir-opt/inline/issue_58867_inline_as_ref_as_mut.b.Inline.after.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ fn b(_1: &mut Box<T>) -> &mut T {
2323
StorageLive(_7); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
2424
_7 = move (*_4); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
2525
_6 = &mut (*_7); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
26-
_6 = &mut (*_7); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
2726
_5 = &mut (*_6); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
2827
_3 = &mut (*_5); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
2928
StorageDead(_6); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
3029
StorageDead(_5); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
30+
StorageDead(_7); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
3131
_2 = &mut (*_3); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
3232
StorageDead(_4); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:14: 8:15
3333
_0 = &mut (*_2); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15

src/test/mir-opt/inline/issue_58867_inline_as_ref_as_mut.d.Inline.after.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn d(_1: &Box<T>) -> &T {
1717
StorageLive(_4); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
1818
_4 = move (*_3); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
1919
_2 = &(*_4); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
20-
_2 = &(*_4); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
20+
StorageDead(_4); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
2121
_0 = &(*_2); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:18:5: 18:15
2222
StorageDead(_3); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:18:14: 18:15
2323
StorageDead(_2); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:19:1: 19:2

0 commit comments

Comments
 (0)