Skip to content

Remove unneeded drop_in_place calls #144883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
25 changes: 18 additions & 7 deletions compiler/rustc_mir_transform/src/remove_unneeded_drops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! `TypingMode::PostAnalysis` to provide more precise type information, especially about opaque
//! types.

use rustc_hir::LangItem;
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
use tracing::{debug, trace};
Expand All @@ -21,15 +22,25 @@ 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();
if let TerminatorKind::Drop { place, target, .. } = terminator.kind {
let ty = place.ty(&body.local_decls, tcx);
if ty.ty.needs_drop(tcx, typing_env) {
continue;
let (ty, target) = match terminator.kind {
TerminatorKind::Drop { place, target, .. } => {
(place.ty(&body.local_decls, tcx).ty, target)
}
debug!("SUCCESS: replacing `drop` with goto({:?})", target);
terminator.kind = TerminatorKind::Goto { target };
should_simplify = true;
TerminatorKind::Call { ref func, target: Some(target), .. }
if let Some((def_id, generics)) = func.const_fn_def()
&& tcx.is_lang_item(def_id, LangItem::DropInPlace) =>
{
(generics.type_at(0), target)
}
_ => continue,
};

if ty.needs_drop(tcx, typing_env) {
continue;
}
debug!("SUCCESS: replacing `drop` with goto({:?})", target);
terminator.kind = TerminatorKind::Goto { target };
should_simplify = true;
}

// if we applied optimizations, we potentially have some cfg to cleanup to
Expand Down
13 changes: 13 additions & 0 deletions tests/mir-opt/remove_unneeded_drop_in_place.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@ test-mir-pass: RemoveUnneededDrops
//@ needs-unwind

// EMIT_MIR remove_unneeded_drop_in_place.slice_in_place.RemoveUnneededDrops.diff
unsafe fn slice_in_place(ptr: *mut [char]) {
std::ptr::drop_in_place(ptr)
}

fn main() {
// CHECK-LABEL: fn main(
let mut a = ['o', 'k'];
unsafe { slice_in_place(&raw mut a) };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
- // MIR for `slice_in_place` before RemoveUnneededDrops
+ // MIR for `slice_in_place` after RemoveUnneededDrops

fn slice_in_place(_1: *mut [char]) -> () {
debug ptr => _1;
let mut _0: ();
let mut _2: *mut [char];

bb0: {
StorageLive(_2);
_2 = copy _1;
- _0 = drop_in_place::<[char]>(move _2) -> [return: bb1, unwind continue];
- }
-
- bb1: {
StorageDead(_2);
return;
}
}

Loading