Skip to content

Commit 1348290

Browse files
Filter out no-op reborrow adjustments in THIR building
1 parent 9d30bc0 commit 1348290

30 files changed

Lines changed: 104 additions & 95 deletions

File tree

‎compiler/rustc_mir_build/src/thir/cx/expr.rs‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,24 @@ impl<'tcx> ThirBuildCx<'tcx> {
249249
deref.span,
250250
)
251251
}
252-
Adjust::Borrow(AutoBorrow::Ref(m)) => ExprKind::Borrow {
253-
borrow_kind: m.to_borrow_kind(),
254-
arg: self.thir.exprs.push(expr),
255-
},
252+
Adjust::Borrow(AutoBorrow::Ref(m)) => {
253+
// Don't emit no-op &T -> &T reborrows
254+
if let AutoBorrowMutability::Not = m
255+
&& let ExprKind::Deref { arg: derefed_expr } = expr.kind
256+
&& let ty::Ref(_, _, ty::Mutability::Not) =
257+
self.thir.exprs[derefed_expr].ty.kind()
258+
// FIXME: removing these reborrows may prevent closure upvars from resolving
259+
&& !self.tcx.is_closure_like(self.body_owner)
260+
{
261+
assert_eq!(self.thir.exprs.last_index(), Some(derefed_expr));
262+
return self.thir.exprs.pop().unwrap();
263+
} else {
264+
ExprKind::Borrow {
265+
borrow_kind: m.to_borrow_kind(),
266+
arg: self.thir.exprs.push(expr),
267+
}
268+
}
269+
}
256270
Adjust::Borrow(AutoBorrow::RawPtr(mutability)) => {
257271
ExprKind::RawBorrow { mutability, arg: self.thir.exprs.push(expr) }
258272
}

‎src/tools/clippy/clippy_lints/src/redundant_clone.rs‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,18 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClone {
116116
continue;
117117
}
118118

119-
// `{ arg = &cloned; clone(move arg); }` or `{ arg = &cloned; to_path_buf(arg); }`
120-
let Some((cloned, cannot_move_out)) = find_stmt_assigns_to(cx, mir, arg, from_borrow, bb) else {
121-
continue;
122-
};
123-
124119
let loc = mir::Location {
125120
block: bb,
126121
statement_index: bbdata.statements.len(),
127122
};
128123

129124
// `Local` to be cloned, and a local of `clone` call's destination
130125
let (local, ret_local) = if from_borrow {
126+
// `{ arg = &cloned; clone(move arg); }` or `{ arg = &cloned; to_path_buf(arg); }`
127+
let Some((cloned, cannot_move_out)) = find_stmt_assigns_to(cx, mir, arg, from_borrow, bb) else {
128+
continue;
129+
};
130+
131131
// `res = clone(arg)` can be turned into `res = move arg;`
132132
// if `arg` is the only borrow of `cloned` at this point.
133133

@@ -149,7 +149,6 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClone {
149149
// receiver of the `deref()` call
150150
let (pred_arg, deref_clone_ret) = if let Some((pred_fn_def_id, pred_arg, pred_arg_ty, res)) =
151151
is_call_with_ref_arg(cx, mir, &pred_terminator.kind)
152-
&& res == cloned
153152
&& cx.tcx.is_diagnostic_item(sym::deref_method, pred_fn_def_id)
154153
&& let ty::Adt(pred_arg_def, _) = pred_arg_ty.kind()
155154
&& let Some(pred_arg_name) = cx.tcx.get_diagnostic_name(pred_arg_def.did())
@@ -178,7 +177,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClone {
178177
// StorageDead(pred_arg);
179178
// res = to_path_buf(cloned);
180179
// ```
181-
if cannot_move_out || !possible_borrower.only_borrowers(&[arg, cloned], local, loc) {
180+
if cannot_move_out || !possible_borrower.only_borrowers(&[arg, deref_clone_ret], local, loc) {
182181
continue;
183182
}
184183

‎tests/mir-opt/dont_inline_type_id.call.Inline.diff‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
bb0: {
1010
StorageLive(_2);
11-
_2 = &(*_1);
11+
_2 = copy _1;
1212
_0 = <T as Any>::type_id(move _2) -> [return: bb1, unwind unreachable];
1313
}
1414

‎tests/mir-opt/gvn.constant_index_overflow.GVN.panic-abort.diff‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535
+ _5 = const usize::MAX;
3636
StorageLive(_6);
3737
StorageLive(_7);
38-
_7 = &(*_1);
39-
_6 = core::slice::<impl [T]>::len(move _7) -> [return: bb1, unwind unreachable];
38+
_7 = copy _1;
39+
- _6 = core::slice::<impl [T]>::len(move _7) -> [return: bb1, unwind unreachable];
40+
+ _6 = core::slice::<impl [T]>::len(copy _1) -> [return: bb1, unwind unreachable];
4041
}
4142

4243
bb1: {

‎tests/mir-opt/gvn.constant_index_overflow.GVN.panic-unwind.diff‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535
+ _5 = const usize::MAX;
3636
StorageLive(_6);
3737
StorageLive(_7);
38-
_7 = &(*_1);
39-
_6 = core::slice::<impl [T]>::len(move _7) -> [return: bb1, unwind continue];
38+
_7 = copy _1;
39+
- _6 = core::slice::<impl [T]>::len(move _7) -> [return: bb1, unwind continue];
40+
+ _6 = core::slice::<impl [T]>::len(copy _1) -> [return: bb1, unwind continue];
4041
}
4142

4243
bb1: {

‎tests/mir-opt/gvn.slice_const_length.GVN.panic-abort.diff‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
bb0: {
2020
StorageLive(_2);
2121
StorageLive(_3);
22-
_3 = &(*_1);
23-
_2 = core::slice::<impl [i32]>::as_ptr(move _3) -> [return: bb1, unwind unreachable];
22+
_3 = copy _1;
23+
- _2 = core::slice::<impl [i32]>::as_ptr(move _3) -> [return: bb1, unwind unreachable];
24+
+ _2 = core::slice::<impl [i32]>::as_ptr(copy _1) -> [return: bb1, unwind unreachable];
2425
}
2526

2627
bb1: {

‎tests/mir-opt/gvn.slice_const_length.GVN.panic-unwind.diff‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
bb0: {
2020
StorageLive(_2);
2121
StorageLive(_3);
22-
_3 = &(*_1);
23-
_2 = core::slice::<impl [i32]>::as_ptr(move _3) -> [return: bb1, unwind continue];
22+
_3 = copy _1;
23+
- _2 = core::slice::<impl [i32]>::as_ptr(move _3) -> [return: bb1, unwind continue];
24+
+ _2 = core::slice::<impl [i32]>::as_ptr(copy _1) -> [return: bb1, unwind continue];
2425
}
2526

2627
bb1: {

‎tests/mir-opt/gvn.slices.GVN.panic-abort.diff‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,10 @@
111111
+ nop;
112112
StorageLive(_10);
113113
StorageLive(_11);
114-
_11 = &(*_1);
115-
_10 = core::str::<impl str>::as_ptr(move _11) -> [return: bb3, unwind unreachable];
114+
- _11 = copy _1;
115+
- _10 = core::str::<impl str>::as_ptr(move _11) -> [return: bb3, unwind unreachable];
116+
+ _11 = const "my favourite slice";
117+
+ _10 = core::str::<impl str>::as_ptr(const "my favourite slice") -> [return: bb3, unwind unreachable];
116118
}
117119

118120
bb3: {
@@ -122,9 +124,10 @@
122124
+ nop;
123125
StorageLive(_13);
124126
StorageLive(_14);
125-
- _14 = &(*_4);
126-
+ _14 = &(*_1);
127-
_13 = core::str::<impl str>::as_ptr(move _14) -> [return: bb4, unwind unreachable];
127+
- _14 = copy _4;
128+
- _13 = core::str::<impl str>::as_ptr(move _14) -> [return: bb4, unwind unreachable];
129+
+ _14 = const "my favourite slice";
130+
+ _13 = core::str::<impl str>::as_ptr(const "my favourite slice") -> [return: bb4, unwind unreachable];
128131
}
129132

130133
bb4: {
@@ -211,8 +214,10 @@
211214
+ nop;
212215
StorageLive(_34);
213216
StorageLive(_35);
214-
_35 = &(*_1);
215-
_34 = core::str::<impl str>::as_ptr(move _35) -> [return: bb8, unwind unreachable];
217+
- _35 = copy _1;
218+
- _34 = core::str::<impl str>::as_ptr(move _35) -> [return: bb8, unwind unreachable];
219+
+ _35 = const "my favourite slice";
220+
+ _34 = core::str::<impl str>::as_ptr(const "my favourite slice") -> [return: bb8, unwind unreachable];
216221
}
217222

218223
bb8: {
@@ -222,8 +227,9 @@
222227
+ nop;
223228
StorageLive(_37);
224229
StorageLive(_38);
225-
_38 = &(*_27);
226-
_37 = core::slice::<impl [u8]>::as_ptr(move _38) -> [return: bb9, unwind unreachable];
230+
_38 = copy _27;
231+
- _37 = core::slice::<impl [u8]>::as_ptr(move _38) -> [return: bb9, unwind unreachable];
232+
+ _37 = core::slice::<impl [u8]>::as_ptr(copy _27) -> [return: bb9, unwind unreachable];
227233
}
228234

229235
bb9: {

‎tests/mir-opt/gvn.slices.GVN.panic-unwind.diff‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,10 @@
111111
+ nop;
112112
StorageLive(_10);
113113
StorageLive(_11);
114-
_11 = &(*_1);
115-
_10 = core::str::<impl str>::as_ptr(move _11) -> [return: bb3, unwind continue];
114+
- _11 = copy _1;
115+
- _10 = core::str::<impl str>::as_ptr(move _11) -> [return: bb3, unwind continue];
116+
+ _11 = const "my favourite slice";
117+
+ _10 = core::str::<impl str>::as_ptr(const "my favourite slice") -> [return: bb3, unwind continue];
116118
}
117119

118120
bb3: {
@@ -122,9 +124,10 @@
122124
+ nop;
123125
StorageLive(_13);
124126
StorageLive(_14);
125-
- _14 = &(*_4);
126-
+ _14 = &(*_1);
127-
_13 = core::str::<impl str>::as_ptr(move _14) -> [return: bb4, unwind continue];
127+
- _14 = copy _4;
128+
- _13 = core::str::<impl str>::as_ptr(move _14) -> [return: bb4, unwind continue];
129+
+ _14 = const "my favourite slice";
130+
+ _13 = core::str::<impl str>::as_ptr(const "my favourite slice") -> [return: bb4, unwind continue];
128131
}
129132

130133
bb4: {
@@ -211,8 +214,10 @@
211214
+ nop;
212215
StorageLive(_34);
213216
StorageLive(_35);
214-
_35 = &(*_1);
215-
_34 = core::str::<impl str>::as_ptr(move _35) -> [return: bb8, unwind continue];
217+
- _35 = copy _1;
218+
- _34 = core::str::<impl str>::as_ptr(move _35) -> [return: bb8, unwind continue];
219+
+ _35 = const "my favourite slice";
220+
+ _34 = core::str::<impl str>::as_ptr(const "my favourite slice") -> [return: bb8, unwind continue];
216221
}
217222

218223
bb8: {
@@ -222,8 +227,9 @@
222227
+ nop;
223228
StorageLive(_37);
224229
StorageLive(_38);
225-
_38 = &(*_27);
226-
_37 = core::slice::<impl [u8]>::as_ptr(move _38) -> [return: bb9, unwind continue];
230+
_38 = copy _27;
231+
- _37 = core::slice::<impl [u8]>::as_ptr(move _38) -> [return: bb9, unwind continue];
232+
+ _37 = core::slice::<impl [u8]>::as_ptr(copy _27) -> [return: bb9, unwind continue];
227233
}
228234

229235
bb9: {

‎tests/mir-opt/inline_generically_if_sized.call.Inline.diff‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
bb0: {
1212
StorageLive(_2);
13-
_2 = &(*_1);
13+
_2 = copy _1;
1414
- _0 = <T as Foo>::bar(move _2) -> [return: bb1, unwind unreachable];
1515
- }
1616
-

0 commit comments

Comments
 (0)