Skip to content

Commit 641adcb

Browse files
Auto merge of #144883 - scottmcm:remove-unneeded-drop_in_place, r=<try>
Remove unneeded `drop_in_place` calls
2 parents 07b7dc9 + 952f142 commit 641adcb

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

compiler/rustc_mir_transform/src/remove_unneeded_drops.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! `TypingMode::PostAnalysis` to provide more precise type information, especially about opaque
66
//! types.
77
8+
use rustc_hir::LangItem;
89
use rustc_middle::mir::*;
910
use rustc_middle::ty::TyCtxt;
1011
use tracing::{debug, trace};
@@ -21,15 +22,25 @@ impl<'tcx> crate::MirPass<'tcx> for RemoveUnneededDrops {
2122
let mut should_simplify = false;
2223
for block in body.basic_blocks.as_mut() {
2324
let terminator = block.terminator_mut();
24-
if let TerminatorKind::Drop { place, target, .. } = terminator.kind {
25-
let ty = place.ty(&body.local_decls, tcx);
26-
if ty.ty.needs_drop(tcx, typing_env) {
27-
continue;
25+
let (ty, target) = match terminator.kind {
26+
TerminatorKind::Drop { place, target, .. } => {
27+
(place.ty(&body.local_decls, tcx).ty, target)
2828
}
29-
debug!("SUCCESS: replacing `drop` with goto({:?})", target);
30-
terminator.kind = TerminatorKind::Goto { target };
31-
should_simplify = true;
29+
TerminatorKind::Call { ref func, target: Some(target), .. }
30+
if let Some((def_id, generics)) = func.const_fn_def()
31+
&& tcx.is_lang_item(def_id, LangItem::DropInPlace) =>
32+
{
33+
(generics.type_at(0), target)
34+
}
35+
_ => continue,
36+
};
37+
38+
if ty.needs_drop(tcx, typing_env) {
39+
continue;
3240
}
41+
debug!("SUCCESS: replacing `drop` with goto({:?})", target);
42+
terminator.kind = TerminatorKind::Goto { target };
43+
should_simplify = true;
3344
}
3445

3546
// if we applied optimizations, we potentially have some cfg to cleanup to
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ test-mir-pass: RemoveUnneededDrops
2+
//@ needs-unwind
3+
4+
// EMIT_MIR remove_unneeded_drop_in_place.slice_in_place.RemoveUnneededDrops.diff
5+
unsafe fn slice_in_place(ptr: *mut [char]) {
6+
std::ptr::drop_in_place(ptr)
7+
}
8+
9+
fn main() {
10+
// CHECK-LABEL: fn main(
11+
let mut a = ['o', 'k'];
12+
unsafe { slice_in_place(&raw mut a) };
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
- // MIR for `slice_in_place` before RemoveUnneededDrops
2+
+ // MIR for `slice_in_place` after RemoveUnneededDrops
3+
4+
fn slice_in_place(_1: *mut [char]) -> () {
5+
debug ptr => _1;
6+
let mut _0: ();
7+
let mut _2: *mut [char];
8+
9+
bb0: {
10+
StorageLive(_2);
11+
_2 = copy _1;
12+
- _0 = drop_in_place::<[char]>(move _2) -> [return: bb1, unwind continue];
13+
- }
14+
-
15+
- bb1: {
16+
StorageDead(_2);
17+
return;
18+
}
19+
}
20+

0 commit comments

Comments
 (0)