Skip to content

Commit 9e7067b

Browse files
committed
Do not create move paths that do not need dropping.
1 parent 62a9d29 commit 9e7067b

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

compiler/rustc_borrowck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use rustc_middle::mir::tcx::PlaceTy;
3333
use rustc_middle::mir::*;
3434
use rustc_middle::query::Providers;
3535
use rustc_middle::traits::DefiningAnchor;
36-
use rustc_middle::ty::{self, Ty, CapturedPlace, ParamEnv, RegionVid, TyCtxt};
36+
use rustc_middle::ty::{self, CapturedPlace, ParamEnv, RegionVid, Ty, TyCtxt};
3737
use rustc_session::lint::builtin::UNUSED_MUT;
3838
use rustc_span::{Span, Symbol};
3939
use rustc_target::abi::FieldIdx;

compiler/rustc_mir_transform/src/elaborate_drops.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ impl<'tcx> MirPass<'tcx> for ElaborateDrops {
5555
let def_id = body.source.def_id();
5656
let param_env = tcx.param_env_reveal_all_normalized(def_id);
5757
let move_data = MoveData::gather_moves(&body, tcx, param_env, |ty| match ty.kind() {
58+
_ if !ty.needs_drop(tcx, param_env) => PathFilter::Skip,
5859
ty::Ref(..) | ty::RawPtr(..) | ty::Slice(_) => PathFilter::Leaf,
5960
ty::Adt(adt, _) => {
6061
if adt.has_dtor(tcx) && !adt.is_box() {
@@ -371,7 +372,16 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
371372
let terminator = data.terminator();
372373

373374
match terminator.kind {
374-
TerminatorKind::Drop { place, target, unwind, replace } => {
375+
TerminatorKind::Drop { place, target, unwind, replace: _ } => {
376+
if !place
377+
.ty(&self.body.local_decls, self.tcx)
378+
.ty
379+
.needs_drop(self.tcx, self.env.param_env)
380+
{
381+
self.patch.patch_terminator(bb, TerminatorKind::Goto { target });
382+
continue;
383+
}
384+
375385
self.init_data.seek_before(loc);
376386
match self.move_data().rev_lookup.find(place.as_ref()) {
377387
LookupResult::Exact(path) => {
@@ -404,18 +414,10 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
404414
bb,
405415
)
406416
}
407-
LookupResult::Parent(..) => {
408-
if !replace {
409-
self.tcx.sess.delay_span_bug(
410-
terminator.source_info.span,
411-
format!("drop of untracked value {bb:?}"),
412-
);
413-
}
414-
// A drop and replace behind a pointer/array/whatever.
415-
// The borrow checker requires that these locations are initialized before the assignment,
416-
// so we just leave an unconditional drop.
417-
assert!(!data.is_cleanup);
418-
}
417+
// A drop and replace behind a pointer/array/whatever.
418+
// The borrow checker requires that these locations are initialized before the assignment,
419+
// so we just leave an unconditional drop.
420+
LookupResult::Parent(..) => {}
419421
}
420422
}
421423
_ => continue,

0 commit comments

Comments
 (0)