Skip to content

Commit 4844bb0

Browse files
committed
Let RemoveUnneededDrops also remove drop_in_place
1 parent 401f19f commit 4844bb0

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
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

tests/mir-opt/remove_unneeded_drops.slice_in_place.RemoveUnneededDrops.panic-abort.diff

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
bb0: {
1010
StorageLive(_2);
1111
_2 = copy _1;
12-
_0 = drop_in_place::<[char]>(move _2) -> [return: bb1, unwind unreachable];
13-
}
14-
15-
bb1: {
12+
- _0 = drop_in_place::<[char]>(move _2) -> [return: bb1, unwind unreachable];
13+
- }
14+
-
15+
- bb1: {
1616
StorageDead(_2);
1717
return;
1818
}

tests/mir-opt/remove_unneeded_drops.slice_in_place.RemoveUnneededDrops.panic-unwind.diff

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
bb0: {
1010
StorageLive(_2);
1111
_2 = copy _1;
12-
_0 = drop_in_place::<[char]>(move _2) -> [return: bb1, unwind continue];
13-
}
14-
15-
bb1: {
12+
- _0 = drop_in_place::<[char]>(move _2) -> [return: bb1, unwind continue];
13+
- }
14+
-
15+
- bb1: {
1616
StorageDead(_2);
1717
return;
1818
}

0 commit comments

Comments
 (0)