Skip to content

Commit f0bd991

Browse files
committed
make MoveDataBuilder a bit less strict so it can run in copy_prop, fix copy_prop's taut remover
1 parent 536d81e commit f0bd991

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

compiler/rustc_mir_dataflow/src/move_paths/builder.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::mem;
22

33
use rustc_index::IndexVec;
4+
use rustc_middle::bug;
45
use rustc_middle::mir::*;
56
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
6-
use rustc_middle::{bug, span_bug};
77
use smallvec::{SmallVec, smallvec};
88
use tracing::debug;
99

@@ -344,13 +344,14 @@ impl<'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> MoveDataBuilder<'a, 'tcx, F> {
344344
match &stmt.kind {
345345
StatementKind::Assign(box (place, Rvalue::CopyForDeref(reffed))) => {
346346
let local = place.as_local().unwrap();
347-
assert!(self.body.local_decls[local].is_deref_temp());
347+
if self.body.local_decls[local].is_deref_temp() {
348+
let rev_lookup = &mut self.data.rev_lookup;
348349

349-
let rev_lookup = &mut self.data.rev_lookup;
350-
351-
rev_lookup.un_derefer.insert(local, reffed.as_ref());
352-
let base_local = rev_lookup.un_derefer.deref_chain(local).first().unwrap().local;
353-
rev_lookup.locals[local] = rev_lookup.locals[base_local];
350+
rev_lookup.un_derefer.insert(local, reffed.as_ref());
351+
let base_local =
352+
rev_lookup.un_derefer.deref_chain(local).first().unwrap().local;
353+
rev_lookup.locals[local] = rev_lookup.locals[base_local];
354+
}
354355
}
355356
StatementKind::Assign(box (place, rval)) => {
356357
self.create_move_path(*place);
@@ -375,13 +376,9 @@ impl<'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> MoveDataBuilder<'a, 'tcx, F> {
375376
self.gather_move(Place::from(*local));
376377
}
377378
}
378-
StatementKind::SetDiscriminant { .. } | StatementKind::Deinit(..) => {
379-
span_bug!(
380-
stmt.source_info.span,
381-
"SetDiscriminant/Deinit should not exist during borrowck"
382-
);
383-
}
384-
StatementKind::Retag { .. }
379+
StatementKind::SetDiscriminant { .. }
380+
| StatementKind::Deinit(..)
381+
| StatementKind::Retag { .. }
385382
| StatementKind::AscribeUserType(..)
386383
| StatementKind::PlaceMention(..)
387384
| StatementKind::Coverage(..)

compiler/rustc_mir_transform/src/copy_prop.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ impl<'tcx> crate::MirPass<'tcx> for CopyProp {
5252
let storage_to_remove = if any_replacement {
5353
// The replacer will remove tautological moves from the head to the local,
5454
// so we remove them before running `MaybeUninitializedPlaces`.
55-
TautologicalMoveAssignmentRemover { tcx, copy_classes: ssa.copy_classes() }
56-
.visit_body_preserves_cfg(body);
55+
TautologicalMoveAssignmentRemover {
56+
tcx,
57+
copy_classes: ssa.copy_classes(),
58+
borrowed_locals: ssa.borrowed_locals(),
59+
}
60+
.visit_body_preserves_cfg(body);
5761

5862
let move_data = MoveData::gather_moves(body, tcx, |_| true);
5963

@@ -134,6 +138,7 @@ fn fully_moved_locals(ssa: &SsaLocals, body: &Body<'_>) -> DenseBitSet<Local> {
134138
struct TautologicalMoveAssignmentRemover<'a, 'tcx> {
135139
tcx: TyCtxt<'tcx>,
136140
copy_classes: &'a IndexSlice<Local, Local>,
141+
borrowed_locals: &'a DenseBitSet<Local>,
137142
}
138143

139144
impl<'tcx> MutVisitor<'tcx> for TautologicalMoveAssignmentRemover<'_, 'tcx> {
@@ -142,10 +147,17 @@ impl<'tcx> MutVisitor<'tcx> for TautologicalMoveAssignmentRemover<'_, 'tcx> {
142147
}
143148

144149
fn visit_statement(&mut self, stmt: &mut Statement<'tcx>, loc: Location) {
150+
self.super_statement(stmt, loc);
151+
145152
// Similar to `Replacer::visit_statement`, but only handle moves.
146153
if let StatementKind::Assign(box (lhs, ref rhs)) = stmt.kind
147154
&& let Rvalue::Use(Operand::Move(rhs)) = *rhs
148155
{
156+
if self.borrowed_locals.contains(lhs.local) || self.borrowed_locals.contains(rhs.local)
157+
{
158+
return;
159+
}
160+
149161
let new_lhs_local = self.copy_classes[lhs.local];
150162
let new_rhs_local = self.copy_classes[rhs.local];
151163

0 commit comments

Comments
 (0)