Skip to content

Commit 105e90f

Browse files
committed
fixed error, made function leaner and tighter
1 parent 4332b5f commit 105e90f

8 files changed

+69
-89
lines changed

compiler/rustc_mir_transform/src/deref_separator.rs

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,46 @@ pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
1111
for (i, stmt) in data.statements.iter_mut().enumerate() {
1212
match stmt.kind {
1313
StatementKind::Assign(box (og_place, Rvalue::Ref(region, borrow_knd, place))) => {
14-
if borrow_knd == (BorrowKind::Mut { allow_two_phase_borrow: false }) {
15-
for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() {
16-
if p_elem == ProjectionElem::Deref {
17-
// The type that we are derefing
18-
let ty = p_ref.ty(local_decl, tcx).ty;
19-
let temp = patch.new_temp(ty, stmt.source_info.span);
14+
for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() {
15+
if p_elem == ProjectionElem::Deref && !p_ref.projection.is_empty() {
16+
// The type that we are derefing
17+
let ty = p_ref.ty(local_decl, tcx).ty;
18+
let temp = patch.new_temp(ty, stmt.source_info.span);
2019

21-
// Because we are assigning this right before original statement
22-
// we are using index i of statement
23-
let loc = Location { block: block, statement_index: i };
24-
patch.add_statement(loc, StatementKind::StorageLive(temp));
20+
// Because we are assigning this right before original statement
21+
// we are using index i of statement
22+
let loc = Location { block: block, statement_index: i };
23+
patch.add_statement(loc, StatementKind::StorageLive(temp));
2524

26-
// We are adding current p_ref's projections to our
27-
// temp value
28-
let deref_place =
29-
Place::from(p_ref.local).project_deeper(p_ref.projection, tcx);
30-
patch.add_assign(
31-
loc,
32-
Place::from(temp),
33-
Rvalue::Use(Operand::Move(deref_place)),
34-
);
25+
// We are adding current p_ref's projections to our
26+
// temp value
27+
let deref_place =
28+
Place::from(p_ref.local).project_deeper(p_ref.projection, tcx);
29+
patch.add_assign(
30+
loc,
31+
Place::from(temp),
32+
Rvalue::Use(Operand::Move(deref_place)),
33+
);
3534

36-
// We are creating a place by using our temp value's location
37-
// and copying derefed values we need to it
38-
let temp_place =
39-
Place::from(temp).project_deeper(&place.projection[idx..], tcx);
40-
patch.add_assign(
41-
loc,
35+
// We are creating a place by using our temp value's location
36+
// and copying derefed values which we need to create new statement
37+
let temp_place =
38+
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+
45+
let new_stmt = Statement {
46+
source_info: stmt.source_info,
47+
kind: StatementKind::Assign(Box::new((
4248
og_place,
4349
Rvalue::Ref(region, borrow_knd, temp_place),
44-
);
45-
// We have to delete the original statement since we just
46-
// replaced it
47-
stmt.make_nop();
48-
}
50+
))),
51+
};
52+
// Replace current statement with newly created one
53+
*stmt = new_stmt;
4954
}
5055
}
5156
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@
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
4040
+ _4 = &mut ((*_6).0: i32); // scope 2 at $DIR/derefer_test.rs:5:13: 5:26
41-
+ nop; // scope 2 at $DIR/derefer_test.rs:5:13: 5:26
41+
+ _4 = &mut ((*_6).0: i32); // scope 2 at $DIR/derefer_test.rs:5:13: 5:26
4242
StorageLive(_5); // scope 3 at $DIR/derefer_test.rs:6:9: 6:10
4343
- _5 = &mut ((*(_2.1: &mut (i32, i32))).1: i32); // scope 3 at $DIR/derefer_test.rs:6:13: 6:26
4444
+ StorageLive(_7); // scope 3 at $DIR/derefer_test.rs:6:13: 6:26
4545
+ _7 = move (_2.1: &mut (i32, i32)); // scope 3 at $DIR/derefer_test.rs:6:13: 6:26
4646
+ _5 = &mut ((*_7).1: i32); // scope 3 at $DIR/derefer_test.rs:6:13: 6:26
47-
+ nop; // scope 3 at $DIR/derefer_test.rs:6:13: 6:26
47+
+ _5 = &mut ((*_7).1: i32); // scope 3 at $DIR/derefer_test.rs:6:13: 6:26
4848
_0 = const (); // scope 0 at $DIR/derefer_test.rs:2:11: 7:2
4949
StorageDead(_5); // scope 3 at $DIR/derefer_test.rs:7:1: 7:2
5050
StorageDead(_4); // scope 2 at $DIR/derefer_test.rs:7:1: 7:2

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

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,23 @@ fn a(_1: &mut [T]) -> &mut [T] {
66
let mut _2: &mut [T]; // in scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
77
let mut _3: &mut [T]; // in scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
88
let mut _4: &mut [T]; // in scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
9-
let mut _5: &mut [T]; // in scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
10-
let mut _6: &mut [T]; // in scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
119
scope 1 (inlined <[T] as AsMut<[T]>>::as_mut) { // at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
1210
debug self => _4; // in scope 1 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
13-
let mut _7: &mut [T]; // in scope 1 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
14-
let mut _8: &mut [T]; // in scope 1 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
15-
let mut _9: &mut [T]; // in scope 1 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
11+
let mut _5: &mut [T]; // in scope 1 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
1612
}
1713

1814
bb0: {
1915
StorageLive(_2); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
2016
StorageLive(_3); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
2117
StorageLive(_4); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
2218
_4 = &mut (*_1); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
23-
StorageLive(_7); // scope 1 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
24-
StorageLive(_8); // scope 1 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
25-
_8 = move _4; // scope 1 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
26-
_7 = &mut (*_8); // scope 1 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
27-
StorageLive(_9); // scope 1 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
28-
_9 = move _7; // scope 1 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
29-
_3 = &mut (*_9); // scope 1 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
30-
StorageDead(_7); // scope 1 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
31-
StorageLive(_5); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
32-
_5 = move _3; // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
33-
_2 = &mut (*_5); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
19+
StorageLive(_5); // scope 1 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
20+
_5 = &mut (*_4); // scope 1 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
21+
_3 = &mut (*_5); // scope 1 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
22+
StorageDead(_5); // scope 1 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
23+
_2 = &mut (*_3); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
3424
StorageDead(_4); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:14: 3:15
35-
StorageLive(_6); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
36-
_6 = move _2; // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
37-
_0 = &mut (*_6); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
25+
_0 = &mut (*_2); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
3826
StorageDead(_3); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:4:1: 4:2
3927
StorageDead(_2); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:4:1: 4:2
4028
return; // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:4:2: 4:2

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

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,31 @@ fn b(_1: &mut Box<T>) -> &mut T {
66
let mut _2: &mut T; // in scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
77
let mut _3: &mut T; // in scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
88
let mut _4: &mut std::boxed::Box<T>; // in scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
9-
let mut _5: &mut T; // in scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
10-
let mut _6: &mut T; // in scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
119
scope 1 (inlined <Box<T> as AsMut<T>>::as_mut) { // at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
1210
debug self => _4; // in scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
13-
let mut _7: &mut T; // in scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
14-
let mut _8: &mut T; // in scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
15-
let mut _9: &mut std::boxed::Box<T>; // in scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
16-
let mut _10: std::boxed::Box<T>; // in scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
17-
let mut _11: &mut T; // in scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
18-
let mut _12: &mut T; // in scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
11+
let mut _5: &mut T; // in scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
12+
let mut _6: &mut T; // in scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
13+
let mut _7: std::boxed::Box<T>; // in scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
1914
}
2015

2116
bb0: {
2217
StorageLive(_2); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
2318
StorageLive(_3); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
2419
StorageLive(_4); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
2520
_4 = &mut (*_1); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
21+
StorageLive(_5); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
22+
StorageLive(_6); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
2623
StorageLive(_7); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
27-
StorageLive(_8); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
28-
StorageLive(_9); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
29-
_9 = move _4; // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
30-
_8 = &mut (*(*_9)); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
31-
StorageLive(_10); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
32-
_10 = move (*_4); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
33-
_8 = &mut (*_10); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
34-
StorageLive(_11); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
35-
_11 = move _8; // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
36-
_7 = &mut (*_11); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
37-
StorageLive(_12); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
38-
_12 = move _7; // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
39-
_3 = &mut (*_12); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
40-
StorageDead(_8); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
41-
StorageDead(_7); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
42-
StorageLive(_5); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
43-
_5 = move _3; // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
44-
_2 = &mut (*_5); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
24+
_7 = move (*_4); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
25+
_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
27+
_5 = &mut (*_6); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
28+
_3 = &mut (*_5); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
29+
StorageDead(_6); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
30+
StorageDead(_5); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
31+
_2 = &mut (*_3); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
4532
StorageDead(_4); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:14: 8:15
46-
StorageLive(_6); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
47-
_6 = move _2; // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
48-
_0 = &mut (*_6); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
33+
_0 = &mut (*_2); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
4934
StorageDead(_3); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:9:1: 9:2
5035
StorageDead(_2); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:9:1: 9:2
5136
return; // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:9:2: 9:2

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ fn d(_1: &Box<T>) -> &T {
77
let mut _3: &std::boxed::Box<T>; // in scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:18:5: 18:15
88
scope 1 (inlined <Box<T> as AsRef<T>>::as_ref) { // at $DIR/issue-58867-inline-as-ref-as-mut.rs:18:5: 18:15
99
debug self => _3; // in scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
10+
let mut _4: std::boxed::Box<T>; // in scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
1011
}
1112

1213
bb0: {
1314
StorageLive(_2); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:18:5: 18:15
1415
StorageLive(_3); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:18:5: 18:15
1516
_3 = &(*_1); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:18:5: 18:15
16-
_2 = &(*(*_3)); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
17+
StorageLive(_4); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
18+
_4 = move (*_3); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
19+
_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
1721
_0 = &(*_2); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:18:5: 18:15
1822
StorageDead(_3); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:18:14: 18:15
1923
StorageDead(_2); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:19:1: 19:2

src/test/mir-opt/simplify_locals.t2.SimplifyLocals.diff

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@
55
let mut _0: (); // return place in scope 0 at $DIR/simplify-locals.rs:48:9: 48:9
66
- let _1: &mut u32; // in scope 0 at $DIR/simplify-locals.rs:50:14: 50:20
77
- let mut _2: *mut u32; // in scope 0 at $DIR/simplify-locals.rs:50:19: 50:20
8-
- let mut _3: *mut u32; // in scope 0 at $DIR/simplify-locals.rs:50:14: 50:20
98
scope 1 {
109
}
1110

1211
bb0: {
1312
- StorageLive(_1); // scope 0 at $DIR/simplify-locals.rs:50:5: 50:22
1413
- StorageLive(_2); // scope 1 at $DIR/simplify-locals.rs:50:19: 50:20
1514
- _2 = &/*tls*/ mut X; // scope 1 at $DIR/simplify-locals.rs:50:19: 50:20
16-
- StorageLive(_3); // scope 1 at $DIR/simplify-locals.rs:50:14: 50:20
17-
- _3 = move _2; // scope 1 at $DIR/simplify-locals.rs:50:14: 50:20
18-
- _1 = &mut (*_3); // scope 1 at $DIR/simplify-locals.rs:50:14: 50:20
15+
- _1 = &mut (*_2); // scope 1 at $DIR/simplify-locals.rs:50:14: 50:20
1916
- StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:50:22: 50:23
2017
- StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:50:22: 50:23
2118
return; // scope 0 at $DIR/simplify-locals.rs:51:2: 51:2

src/test/mir-opt/simplify_locals.t3.SimplifyLocals.diff

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
- let _1: u32; // in scope 0 at $DIR/simplify-locals.rs:56:14: 56:21
77
- let mut _2: &mut u32; // in scope 0 at $DIR/simplify-locals.rs:56:15: 56:21
88
- let mut _3: *mut u32; // in scope 0 at $DIR/simplify-locals.rs:56:20: 56:21
9-
- let mut _4: *mut u32; // in scope 0 at $DIR/simplify-locals.rs:56:15: 56:21
109
scope 1 {
1110
}
1211

@@ -15,9 +14,7 @@
1514
- StorageLive(_2); // scope 1 at $DIR/simplify-locals.rs:56:15: 56:21
1615
- StorageLive(_3); // scope 1 at $DIR/simplify-locals.rs:56:20: 56:21
1716
- _3 = &/*tls*/ mut X; // scope 1 at $DIR/simplify-locals.rs:56:20: 56:21
18-
- StorageLive(_4); // scope 1 at $DIR/simplify-locals.rs:56:15: 56:21
19-
- _4 = move _3; // scope 1 at $DIR/simplify-locals.rs:56:15: 56:21
20-
- _2 = &mut (*_4); // scope 1 at $DIR/simplify-locals.rs:56:15: 56:21
17+
- _2 = &mut (*_3); // scope 1 at $DIR/simplify-locals.rs:56:15: 56:21
2118
- _1 = (*_2); // scope 1 at $DIR/simplify-locals.rs:56:14: 56:21
2219
- StorageDead(_3); // scope 0 at $DIR/simplify-locals.rs:56:23: 56:24
2320
- StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:56:23: 56:24

0 commit comments

Comments
 (0)