Skip to content

Commit 231bcd1

Browse files
committed
Auto merge of #105274 - saethlin:instcombine-mut-ref, r=cjgillot
Enable instcombine for mutable reborrows `instcombine` used to contain this comment, which is no longer accurate because there it is fine to copy `&mut _` in MIR: ```rust // The dereferenced place must have type `&_`, so that we don't copy `&mut _`. ``` So let's try replacing that check with something much more permissive...
2 parents 9aa5c24 + 1409cb5 commit 231bcd1

4 files changed

+9
-31
lines changed

compiler/rustc_mir_transform/src/instcombine.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,7 @@ impl<'tcx> InstCombineContext<'tcx, '_> {
110110
fn combine_ref_deref(&self, source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) {
111111
if let Rvalue::Ref(_, _, place) = rvalue {
112112
if let Some((base, ProjectionElem::Deref)) = place.as_ref().last_projection() {
113-
if let ty::Ref(_, _, Mutability::Not) =
114-
base.ty(self.local_decls, self.tcx).ty.kind()
115-
{
116-
// The dereferenced place must have type `&_`, so that we don't copy `&mut _`.
117-
} else {
113+
if rvalue.ty(self.local_decls, self.tcx) != base.ty(self.local_decls, self.tcx).ty {
118114
return;
119115
}
120116

tests/mir-opt/funky_arms.float_to_exponential_common.ConstProp.diff

+2-8
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,14 @@
8686

8787
bb6: {
8888
_10 = ((_7 as Some).0: usize); // scope 3 at $DIR/funky_arms.rs:+13:17: +13:26
89-
StorageLive(_11); // scope 3 at $DIR/funky_arms.rs:+15:43: +15:46
90-
_11 = &mut (*_1); // scope 3 at $DIR/funky_arms.rs:+15:43: +15:46
9189
StorageLive(_13); // scope 3 at $DIR/funky_arms.rs:+15:53: +15:57
9290
_13 = _6; // scope 3 at $DIR/funky_arms.rs:+15:53: +15:57
9391
StorageLive(_14); // scope 3 at $DIR/funky_arms.rs:+15:59: +15:79
9492
StorageLive(_15); // scope 3 at $DIR/funky_arms.rs:+15:59: +15:75
9593
_15 = _10 as u32 (IntToInt); // scope 3 at $DIR/funky_arms.rs:+15:59: +15:75
9694
_14 = Add(move _15, const 1_u32); // scope 3 at $DIR/funky_arms.rs:+15:59: +15:79
9795
StorageDead(_15); // scope 3 at $DIR/funky_arms.rs:+15:78: +15:79
98-
_0 = float_to_exponential_common_exact::<T>(move _11, _2, move _13, move _14, _3) -> bb7; // scope 3 at $DIR/funky_arms.rs:+15:9: +15:87
96+
_0 = float_to_exponential_common_exact::<T>(_1, _2, move _13, move _14, _3) -> bb7; // scope 3 at $DIR/funky_arms.rs:+15:9: +15:87
9997
// mir::Constant
10098
// + span: $DIR/funky_arms.rs:26:9: 26:42
10199
// + literal: Const { ty: for<'a, 'b, 'c> fn(&'a mut Formatter<'b>, &'c T, Sign, u32, bool) -> Result<(), std::fmt::Error> {float_to_exponential_common_exact::<T>}, val: Value(<ZST>) }
@@ -104,24 +102,20 @@
104102
bb7: {
105103
StorageDead(_14); // scope 3 at $DIR/funky_arms.rs:+15:86: +15:87
106104
StorageDead(_13); // scope 3 at $DIR/funky_arms.rs:+15:86: +15:87
107-
StorageDead(_11); // scope 3 at $DIR/funky_arms.rs:+15:86: +15:87
108105
goto -> bb10; // scope 2 at $DIR/funky_arms.rs:+13:5: +18:6
109106
}
110107

111108
bb8: {
112-
StorageLive(_18); // scope 2 at $DIR/funky_arms.rs:+17:46: +17:49
113-
_18 = &mut (*_1); // scope 2 at $DIR/funky_arms.rs:+17:46: +17:49
114109
StorageLive(_20); // scope 2 at $DIR/funky_arms.rs:+17:56: +17:60
115110
_20 = _6; // scope 2 at $DIR/funky_arms.rs:+17:56: +17:60
116-
_0 = float_to_exponential_common_shortest::<T>(move _18, _2, move _20, _3) -> bb9; // scope 2 at $DIR/funky_arms.rs:+17:9: +17:68
111+
_0 = float_to_exponential_common_shortest::<T>(_1, _2, move _20, _3) -> bb9; // scope 2 at $DIR/funky_arms.rs:+17:9: +17:68
117112
// mir::Constant
118113
// + span: $DIR/funky_arms.rs:28:9: 28:45
119114
// + literal: Const { ty: for<'a, 'b, 'c> fn(&'a mut Formatter<'b>, &'c T, Sign, bool) -> Result<(), std::fmt::Error> {float_to_exponential_common_shortest::<T>}, val: Value(<ZST>) }
120115
}
121116

122117
bb9: {
123118
StorageDead(_20); // scope 2 at $DIR/funky_arms.rs:+17:67: +17:68
124-
StorageDead(_18); // scope 2 at $DIR/funky_arms.rs:+17:67: +17:68
125119
goto -> bb10; // scope 2 at $DIR/funky_arms.rs:+13:5: +18:6
126120
}
127121

tests/mir-opt/inline/issue_58867_inline_as_ref_as_mut.a.Inline.after.mir

+1-5
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,14 @@ fn a(_1: &mut [T]) -> &mut [T] {
88
let mut _4: &mut [T]; // in scope 0 at $DIR/issue_58867_inline_as_ref_as_mut.rs:+1:5: +1:15
99
scope 1 (inlined <[T] as AsMut<[T]>>::as_mut) { // at $DIR/issue_58867_inline_as_ref_as_mut.rs:3:7: 3:15
1010
debug self => _4; // 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
1211
}
1312

1413
bb0: {
1514
StorageLive(_2); // scope 0 at $DIR/issue_58867_inline_as_ref_as_mut.rs:+1:5: +1:15
1615
StorageLive(_3); // scope 0 at $DIR/issue_58867_inline_as_ref_as_mut.rs:+1:5: +1:15
1716
StorageLive(_4); // scope 0 at $DIR/issue_58867_inline_as_ref_as_mut.rs:+1:5: +1:15
1817
_4 = &mut (*_1); // scope 0 at $DIR/issue_58867_inline_as_ref_as_mut.rs:+1:5: +1: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
18+
_3 = _4; // scope 1 at $SRC_DIR/core/src/convert/mod.rs:LL:COL
2319
_2 = &mut (*_3); // scope 0 at $DIR/issue_58867_inline_as_ref_as_mut.rs:+1:5: +1:15
2420
StorageDead(_4); // scope 0 at $DIR/issue_58867_inline_as_ref_as_mut.rs:+1:14: +1:15
2521
_0 = &mut (*_2); // scope 0 at $DIR/issue_58867_inline_as_ref_as_mut.rs:+1:5: +1:15

tests/mir-opt/inline/issue_58867_inline_as_ref_as_mut.b.Inline.after.mir

+5-13
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,18 @@ fn b(_1: &mut Box<T>) -> &mut T {
88
let mut _4: &mut std::boxed::Box<T>; // in scope 0 at $DIR/issue_58867_inline_as_ref_as_mut.rs:+1:5: +1:15
99
scope 1 (inlined <Box<T> as AsMut<T>>::as_mut) { // at $DIR/issue_58867_inline_as_ref_as_mut.rs:8:7: 8:15
1010
debug self => _4; // 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
14-
let mut _8: *const T; // in scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
11+
let mut _5: std::boxed::Box<T>; // in scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
12+
let mut _6: *const T; // in scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
1513
}
1614

1715
bb0: {
1816
StorageLive(_2); // scope 0 at $DIR/issue_58867_inline_as_ref_as_mut.rs:+1:5: +1:15
1917
StorageLive(_3); // scope 0 at $DIR/issue_58867_inline_as_ref_as_mut.rs:+1:5: +1:15
2018
StorageLive(_4); // scope 0 at $DIR/issue_58867_inline_as_ref_as_mut.rs:+1:5: +1:15
2119
_4 = &mut (*_1); // scope 0 at $DIR/issue_58867_inline_as_ref_as_mut.rs:+1:5: +1:15
22-
StorageLive(_5); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
23-
StorageLive(_6); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
24-
_7 = deref_copy (*_4); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
25-
_8 = (((_7.0: std::ptr::Unique<T>).0: std::ptr::NonNull<T>).0: *const T); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
26-
_6 = &mut (*_8); // 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
20+
_5 = deref_copy (*_4); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
21+
_6 = (((_5.0: std::ptr::Unique<T>).0: std::ptr::NonNull<T>).0: *const T); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
22+
_3 = &mut (*_6); // scope 1 at $SRC_DIR/alloc/src/boxed.rs:LL:COL
3123
_2 = &mut (*_3); // scope 0 at $DIR/issue_58867_inline_as_ref_as_mut.rs:+1:5: +1:15
3224
StorageDead(_4); // scope 0 at $DIR/issue_58867_inline_as_ref_as_mut.rs:+1:14: +1:15
3325
_0 = &mut (*_2); // scope 0 at $DIR/issue_58867_inline_as_ref_as_mut.rs:+1:5: +1:15

0 commit comments

Comments
 (0)