Skip to content

Commit 7c3a7ce

Browse files
committed
Auto merge of #155044 - WaffleLapkin:dont-remove-unneeded-drops, r=<try>
Don't try to remove `drop_in_place` calls in `RemoveUnneededDrops`
2 parents 25a54d4 + fa73f03 commit 7c3a7ce

3 files changed

Lines changed: 11 additions & 26 deletions

File tree

compiler/rustc_mir_transform/src/remove_unneeded_drops.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@
44
//! useful because (unlike MIR building) it runs after type checking, so it can make use of
55
//! `TypingMode::PostAnalysis` to provide more precise type information, especially about opaque
66
//! types.
7-
//!
8-
//! When we're optimizing, we also remove calls to `drop_in_place<T>` when `T` isn't `needs_drop`,
9-
//! as those are essentially equivalent to `Drop` terminators. While the compiler doesn't insert
10-
//! them automatically, preferring the built-in instead, they're common in generic code (such as
11-
//! `Vec::truncate`) so removing them from things like inlined `Vec<u8>` is helpful.
127
13-
use rustc_hir::LangItem;
148
use rustc_middle::mir::*;
159
use rustc_middle::ty::TyCtxt;
1610
use tracing::{debug, trace};
@@ -27,19 +21,8 @@ impl<'tcx> crate::MirPass<'tcx> for RemoveUnneededDrops {
2721
let mut should_simplify = false;
2822
for block in body.basic_blocks.as_mut() {
2923
let terminator = block.terminator_mut();
30-
let (ty, target) = match terminator.kind {
31-
TerminatorKind::Drop { place, target, .. } => {
32-
(place.ty(&body.local_decls, tcx).ty, target)
33-
}
34-
TerminatorKind::Call { ref func, target: Some(target), .. }
35-
if tcx.sess.mir_opt_level() > 0
36-
&& let Some((def_id, generics)) = func.const_fn_def()
37-
&& tcx.is_lang_item(def_id, LangItem::DropInPlace) =>
38-
{
39-
(generics.type_at(0), target)
40-
}
41-
_ => continue,
42-
};
24+
let TerminatorKind::Drop { place, target, .. } = terminator.kind else { continue };
25+
let ty = place.ty(&body.local_decls, tcx).ty;
4326

4427
if ty.needs_drop(tcx, typing_env) {
4528
continue;

tests/mir-opt/remove_unneeded_drop_in_place.rs renamed to tests/mir-opt/inline/inline_empty_drop_glue.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
//@ test-mir-pass: RemoveUnneededDrops
1+
//@ test-mir-pass: Inline
22
//@ needs-unwind
3-
//@ compile-flags: -Z mir-opt-level=1
3+
//@ compile-flags: -Zmir-opt-level=1
44

5-
// EMIT_MIR remove_unneeded_drop_in_place.slice_in_place.RemoveUnneededDrops.diff
5+
// EMIT_MIR inline_empty_drop_glue.slice_in_place.Inline.diff
66
unsafe fn slice_in_place(ptr: *mut [char]) {
77
// CHECK-LABEL: fn slice_in_place(_1: *mut [char])
8-
// CHECK: bb0: {
9-
// CHECK-NEXT: return;
8+
// CHECK: bb0: {
9+
// CHECK-NEXT: return;
1010
// CHECK-NEXT: }
1111
std::ptr::drop_in_place(ptr)
1212
}

tests/mir-opt/remove_unneeded_drop_in_place.slice_in_place.RemoveUnneededDrops.diff renamed to tests/mir-opt/inline/inline_empty_drop_glue.slice_in_place.Inline.diff

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
- // MIR for `slice_in_place` before RemoveUnneededDrops
2-
+ // MIR for `slice_in_place` after RemoveUnneededDrops
1+
- // MIR for `slice_in_place` before Inline
2+
+ // MIR for `slice_in_place` after Inline
33

44
fn slice_in_place(_1: *mut [char]) -> () {
55
debug ptr => _1;
66
let mut _0: ();
77
let mut _2: *mut [char];
8+
+ scope 1 (inlined drop_in_place::<[char]> - shim(None)) {
9+
+ }
810

911
bb0: {
1012
StorageLive(_2);

0 commit comments

Comments
 (0)