Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions compiler/rustc_mir_transform/src/remove_unneeded_drops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
//! useful because (unlike MIR building) it runs after type checking, so it can make use of
//! `TypingMode::PostAnalysis` to provide more precise type information, especially about opaque
//! types.
//!
//! When we're optimizing, we also remove calls to `drop_in_place<T>` when `T` isn't `needs_drop`,
//! as those are essentially equivalent to `Drop` terminators. While the compiler doesn't insert
//! them automatically, preferring the built-in instead, they're common in generic code (such as
//! `Vec::truncate`) so removing them from things like inlined `Vec<u8>` is helpful.

use rustc_hir::LangItem;
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
use tracing::{debug, trace};
Expand All @@ -27,19 +21,8 @@ impl<'tcx> crate::MirPass<'tcx> for RemoveUnneededDrops {
let mut should_simplify = false;
for block in body.basic_blocks.as_mut() {
let terminator = block.terminator_mut();
let (ty, target) = match terminator.kind {
TerminatorKind::Drop { place, target, .. } => {
(place.ty(&body.local_decls, tcx).ty, target)
}
TerminatorKind::Call { ref func, target: Some(target), .. }
if tcx.sess.mir_opt_level() > 0
&& let Some((def_id, generics)) = func.const_fn_def()
&& tcx.is_lang_item(def_id, LangItem::DropInPlace) =>
{
(generics.type_at(0), target)
}
_ => continue,
};
let TerminatorKind::Drop { place, target, .. } = terminator.kind else { continue };
let ty = place.ty(&body.local_decls, tcx).ty;

if ty.needs_drop(tcx, typing_env) {
continue;
Expand Down
12 changes: 7 additions & 5 deletions tests/mir-opt/remove_unneeded_drop_in_place.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
//@ test-mir-pass: RemoveUnneededDrops
//@ test-mir-pass: Inline
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: now that this isn't a different pass, maybe move it into https://github.com/rust-lang/rust/tree/main/tests/mir-opt/inline? Could rename it to inline_empty_drop_glue or something too, to make it clearer what the goal is.

//@ needs-unwind
//@ compile-flags: -Z mir-opt-level=1

// EMIT_MIR remove_unneeded_drop_in_place.slice_in_place.RemoveUnneededDrops.diff
// EMIT_MIR remove_unneeded_drop_in_place.slice_in_place.Inline.diff
unsafe fn slice_in_place(ptr: *mut [char]) {
// CHECK-LABEL: fn slice_in_place(_1: *mut [char])
// CHECK: bb0: {
// CHECK-NEXT: return;
// CHECK: bb0: {
// CHECK-NEXT: StorageLive(_2);
// CHECK-NEXT: _2 = copy _1;
// CHECK-NEXT: StorageDead(_2);
// CHECK-NEXT: return;
// CHECK-NEXT: }
std::ptr::drop_in_place(ptr)
}
Comment on lines 6 to 12
Copy link
Copy Markdown
Member Author

@WaffleLapkin WaffleLapkin Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View changes since the review

idk how this used to pass, given that the diff is unchanged...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You removed the compile-flags, so that probably impacted it? The filecheck validations are on final mir always...

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
- // MIR for `slice_in_place` before RemoveUnneededDrops
+ // MIR for `slice_in_place` after RemoveUnneededDrops
- // MIR for `slice_in_place` before Inline
+ // MIR for `slice_in_place` after Inline

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

bb0: {
StorageLive(_2);
Expand Down
Loading