Skip to content

Commit 7bc7235

Browse files
committed
remove DerefTemp and CopyFromDeref from runtime mir
1 parent 8e62bfd commit 7bc7235

File tree

19 files changed

+122
-82
lines changed

19 files changed

+122
-82
lines changed

compiler/rustc_borrowck/src/lib.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,24 +1473,6 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
14731473
self.consume_operand(location, (operand, span), state)
14741474
}
14751475

1476-
&Rvalue::CopyForDeref(place) => {
1477-
self.access_place(
1478-
location,
1479-
(place, span),
1480-
(Deep, Read(ReadKind::Copy)),
1481-
LocalMutationIsAllowed::No,
1482-
state,
1483-
);
1484-
1485-
// Finally, check if path was already moved.
1486-
self.check_if_path_or_subpath_is_moved(
1487-
location,
1488-
InitializationRequiringAction::Use,
1489-
(place.as_ref(), span),
1490-
state,
1491-
);
1492-
}
1493-
14941476
&(Rvalue::Len(place) | Rvalue::Discriminant(place)) => {
14951477
let af = match *rvalue {
14961478
Rvalue::Len(..) => Some(ArtificialField::ArrayLength),
@@ -1553,6 +1535,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
15531535
Rvalue::WrapUnsafeBinder(op, _) => {
15541536
self.consume_operand(location, (op, span), state);
15551537
}
1538+
1539+
Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in borrowck"),
15561540
}
15571541
}
15581542

compiler/rustc_borrowck/src/polonius/legacy/loan_invalidations.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,6 @@ impl<'a, 'tcx> LoanInvalidationsGenerator<'a, 'tcx> {
301301
| Rvalue::Cast(_ /*cast_kind*/, operand, _ /*ty*/)
302302
| Rvalue::ShallowInitBox(operand, _ /*ty*/) => self.consume_operand(location, operand),
303303

304-
&Rvalue::CopyForDeref(place) => {
305-
let op = &Operand::Copy(place);
306-
self.consume_operand(location, op);
307-
}
308-
309304
&(Rvalue::Len(place) | Rvalue::Discriminant(place)) => {
310305
let af = match rvalue {
311306
Rvalue::Len(..) => Some(ArtificialField::ArrayLength),
@@ -336,6 +331,8 @@ impl<'a, 'tcx> LoanInvalidationsGenerator<'a, 'tcx> {
336331
Rvalue::WrapUnsafeBinder(op, _) => {
337332
self.consume_operand(location, op);
338333
}
334+
335+
Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in borrowck"),
339336
}
340337
}
341338

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -601,11 +601,6 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
601601
let val = codegen_operand(fx, operand);
602602
lval.write_cvalue(fx, val);
603603
}
604-
Rvalue::CopyForDeref(place) => {
605-
let cplace = codegen_place(fx, place);
606-
let val = cplace.to_cvalue(fx);
607-
lval.write_cvalue(fx, val)
608-
}
609604
Rvalue::Ref(_, _, place) | Rvalue::RawPtr(_, place) => {
610605
let place = codegen_place(fx, place);
611606
let ref_ = place.place_ref(fx, lval.layout());
@@ -935,6 +930,7 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
935930
let operand = codegen_operand(fx, operand);
936931
lval.write_cvalue_transmute(fx, operand);
937932
}
933+
Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in codegen"),
938934
}
939935
}
940936
StatementKind::StorageLive(_)

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,9 +494,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
494494
self.codegen_place_to_pointer(bx, place, mk_ref)
495495
}
496496

497-
mir::Rvalue::CopyForDeref(place) => {
498-
self.codegen_operand(bx, &mir::Operand::Copy(place))
499-
}
500497
mir::Rvalue::RawPtr(kind, place) => {
501498
let mk_ptr = move |tcx: TyCtxt<'tcx>, ty: Ty<'tcx>| {
502499
Ty::new_ptr(tcx, ty, kind.to_mutbl_lossy())
@@ -740,6 +737,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
740737
let layout = bx.cx().layout_of(binder_ty);
741738
OperandRef { val: operand.val, layout }
742739
}
740+
mir::Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in codegen"),
743741
}
744742
}
745743

compiler/rustc_const_eval/src/check_consts/qualifs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ where
236236
in_place::<Q, _>(cx, in_local, place.as_ref())
237237
}
238238

239-
Rvalue::CopyForDeref(place) => in_place::<Q, _>(cx, in_local, place.as_ref()),
239+
Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in runtime MIR"),
240240

241241
Rvalue::Use(operand)
242242
| Rvalue::Repeat(operand, _)

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
185185
self.copy_op(&op, &dest)?;
186186
}
187187

188-
CopyForDeref(place) => {
189-
let op = self.eval_place_to_op(place, Some(dest.layout))?;
190-
self.copy_op(&op, &dest)?;
191-
}
188+
CopyForDeref(_) => bug!("`CopyForDeref` in runtime MIR"),
192189

193190
BinaryOp(bin_op, box (ref left, ref right)) => {
194191
let layout = util::binop_left_homogeneous(bin_op).then_some(dest.layout);

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,11 @@ pub enum LocalInfo<'tcx> {
11091109
/// A temporary created during evaluating `if` predicate, possibly for pattern matching for `let`s,
11101110
/// and subject to Edition 2024 temporary lifetime rules
11111111
IfThenRescopeTemp { if_then: HirId },
1112-
/// A temporary created during the pass `Derefer` to avoid it's retagging
1112+
/// A temporary created during the pass `Derefer` treated as a transparent alias
1113+
/// for the place its copied from by analysis passes such as `AddRetag` and `ElaborateDrops`.
1114+
///
1115+
/// It may only be written to by a `CopyForDeref` and otherwise only accessed through a deref.
1116+
/// In runtime MIR, it is replaced with a normal `Boring` local.
11131117
DerefTemp,
11141118
/// A temporary created for borrow checking.
11151119
FakeBorrow,

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ pub enum RuntimePhase {
130130
/// * [`TerminatorKind::Yield`]
131131
/// * [`TerminatorKind::CoroutineDrop`]
132132
/// * [`Rvalue::Aggregate`] for any `AggregateKind` except `Array`
133+
/// * [`Rvalue::CopyForDeref`]
133134
/// * [`PlaceElem::OpaqueCast`]
135+
/// * [`LocalInfo::DerefTemp`](super::LocalInfo::DerefTemp)
134136
///
135137
/// And the following variants are allowed:
136138
/// * [`StatementKind::Retag`]
@@ -1485,11 +1487,13 @@ pub enum Rvalue<'tcx> {
14851487
/// A CopyForDeref is equivalent to a read from a place at the
14861488
/// codegen level, but is treated specially by drop elaboration. When such a read happens, it
14871489
/// is guaranteed (via nature of the mir_opt `Derefer` in rustc_mir_transform/src/deref_separator)
1488-
/// that the only use of the returned value is a deref operation, immediately
1489-
/// followed by one or more projections. Drop elaboration treats this rvalue as if the
1490+
/// that the returned value is written into a `DerefTemp` local and that its only use is a deref operation,
1491+
/// immediately followed by one or more projections. Drop elaboration treats this rvalue as if the
14901492
/// read never happened and just projects further. This allows simplifying various MIR
14911493
/// optimizations and codegen backends that previously had to handle deref operations anywhere
14921494
/// in a place.
1495+
///
1496+
/// Disallowed in runtime MIR and is replaced by normal copies.
14931497
CopyForDeref(Place<'tcx>),
14941498

14951499
/// Wraps a value in an unsafe binder.

compiler/rustc_mir_transform/src/copy_prop.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ fn fully_moved_locals(ssa: &SsaLocals, body: &Body<'_>) -> DenseBitSet<Local> {
7474
let mut fully_moved = DenseBitSet::new_filled(body.local_decls.len());
7575

7676
for (_, rvalue, _) in ssa.assignments(body) {
77-
let (Rvalue::Use(Operand::Copy(place) | Operand::Move(place))
78-
| Rvalue::CopyForDeref(place)) = rvalue
79-
else {
77+
let Rvalue::Use(Operand::Copy(place) | Operand::Move(place)) = rvalue else {
8078
continue;
8179
};
8280

@@ -85,7 +83,7 @@ fn fully_moved_locals(ssa: &SsaLocals, body: &Body<'_>) -> DenseBitSet<Local> {
8583
continue;
8684
}
8785

88-
if let Rvalue::Use(Operand::Copy(_)) | Rvalue::CopyForDeref(_) = rvalue {
86+
if let Rvalue::Use(Operand::Copy(_)) = rvalue {
8987
fully_moved.remove(rhs);
9088
}
9189
}
@@ -146,8 +144,7 @@ impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> {
146144

147145
// Do not leave tautological assignments around.
148146
if let StatementKind::Assign(box (lhs, ref rhs)) = stmt.kind
149-
&& let Rvalue::Use(Operand::Copy(rhs) | Operand::Move(rhs)) | Rvalue::CopyForDeref(rhs) =
150-
*rhs
147+
&& let Rvalue::Use(Operand::Copy(rhs) | Operand::Move(rhs)) = *rhs
151148
&& lhs == rhs
152149
{
153150
stmt.make_nop();

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,7 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
309309
self.assign_operand(state, target, operand);
310310
}
311311
}
312-
Rvalue::CopyForDeref(rhs) => {
313-
state.flood(target.as_ref(), &self.map);
314-
if let Some(target) = self.map.find(target.as_ref()) {
315-
self.assign_operand(state, target, &Operand::Copy(*rhs));
316-
}
317-
}
312+
Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in runtime MIR"),
318313
Rvalue::Aggregate(kind, operands) => {
319314
// If we assign `target = Enum::Variant#0(operand)`,
320315
// we must make sure that all `target as Variant#i` are `Top`.
@@ -492,9 +487,7 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
492487
}
493488
Rvalue::Discriminant(place) => state.get_discr(place.as_ref(), &self.map),
494489
Rvalue::Use(operand) => return self.handle_operand(operand, state),
495-
Rvalue::CopyForDeref(place) => {
496-
return self.handle_operand(&Operand::Copy(*place), state);
497-
}
490+
Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in runtime MIR"),
498491
Rvalue::Ref(..) | Rvalue::RawPtr(..) => {
499492
// We don't track such places.
500493
return ValueOrPlace::TOP;

0 commit comments

Comments
 (0)