From 9e7067bdbe171d06675752603d0bc9d21430d977 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 30 Sep 2023 21:42:49 +0000 Subject: [PATCH] Do not create move paths that do not need dropping. --- compiler/rustc_borrowck/src/lib.rs | 2 +- .../src/elaborate_drops.rs | 28 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index a81c2f5bc512b..7ae7b4169011e 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -33,7 +33,7 @@ use rustc_middle::mir::tcx::PlaceTy; use rustc_middle::mir::*; use rustc_middle::query::Providers; use rustc_middle::traits::DefiningAnchor; -use rustc_middle::ty::{self, Ty, CapturedPlace, ParamEnv, RegionVid, TyCtxt}; +use rustc_middle::ty::{self, CapturedPlace, ParamEnv, RegionVid, Ty, TyCtxt}; use rustc_session::lint::builtin::UNUSED_MUT; use rustc_span::{Span, Symbol}; use rustc_target::abi::FieldIdx; diff --git a/compiler/rustc_mir_transform/src/elaborate_drops.rs b/compiler/rustc_mir_transform/src/elaborate_drops.rs index a4a2f650a0378..4eef41b3f31a0 100644 --- a/compiler/rustc_mir_transform/src/elaborate_drops.rs +++ b/compiler/rustc_mir_transform/src/elaborate_drops.rs @@ -55,6 +55,7 @@ impl<'tcx> MirPass<'tcx> for ElaborateDrops { let def_id = body.source.def_id(); let param_env = tcx.param_env_reveal_all_normalized(def_id); let move_data = MoveData::gather_moves(&body, tcx, param_env, |ty| match ty.kind() { + _ if !ty.needs_drop(tcx, param_env) => PathFilter::Skip, ty::Ref(..) | ty::RawPtr(..) | ty::Slice(_) => PathFilter::Leaf, ty::Adt(adt, _) => { if adt.has_dtor(tcx) && !adt.is_box() { @@ -371,7 +372,16 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> { let terminator = data.terminator(); match terminator.kind { - TerminatorKind::Drop { place, target, unwind, replace } => { + TerminatorKind::Drop { place, target, unwind, replace: _ } => { + if !place + .ty(&self.body.local_decls, self.tcx) + .ty + .needs_drop(self.tcx, self.env.param_env) + { + self.patch.patch_terminator(bb, TerminatorKind::Goto { target }); + continue; + } + self.init_data.seek_before(loc); match self.move_data().rev_lookup.find(place.as_ref()) { LookupResult::Exact(path) => { @@ -404,18 +414,10 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> { bb, ) } - LookupResult::Parent(..) => { - if !replace { - self.tcx.sess.delay_span_bug( - terminator.source_info.span, - format!("drop of untracked value {bb:?}"), - ); - } - // A drop and replace behind a pointer/array/whatever. - // The borrow checker requires that these locations are initialized before the assignment, - // so we just leave an unconditional drop. - assert!(!data.is_cleanup); - } + // A drop and replace behind a pointer/array/whatever. + // The borrow checker requires that these locations are initialized before the assignment, + // so we just leave an unconditional drop. + LookupResult::Parent(..) => {} } } _ => continue,