Skip to content

Commit

Permalink
Do not create move paths that do not need dropping.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Sep 30, 2023
1 parent 62a9d29 commit 9e7067b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/lib.rs
Expand Up @@ -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;
Expand Down
28 changes: 15 additions & 13 deletions compiler/rustc_mir_transform/src/elaborate_drops.rs
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9e7067b

Please sign in to comment.