Skip to content

Commit fc959e5

Browse files
committed
remove DerefTemp and CopyFromDeref from runtime mir
1 parent 8111a2d commit fc959e5

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
@@ -1539,24 +1539,6 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
15391539
self.consume_operand(location, (operand, span), state)
15401540
}
15411541

1542-
&Rvalue::CopyForDeref(place) => {
1543-
self.access_place(
1544-
location,
1545-
(place, span),
1546-
(Deep, Read(ReadKind::Copy)),
1547-
LocalMutationIsAllowed::No,
1548-
state,
1549-
);
1550-
1551-
// Finally, check if path was already moved.
1552-
self.check_if_path_or_subpath_is_moved(
1553-
location,
1554-
InitializationRequiringAction::Use,
1555-
(place.as_ref(), span),
1556-
state,
1557-
);
1558-
}
1559-
15601542
&Rvalue::Discriminant(place) => {
15611543
let af = match *rvalue {
15621544
Rvalue::Discriminant(..) => None,
@@ -1618,6 +1600,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
16181600
Rvalue::WrapUnsafeBinder(op, _) => {
16191601
self.consume_operand(location, (op, span), state);
16201602
}
1603+
1604+
Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in borrowck"),
16211605
}
16221606
}
16231607

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::Discriminant(place) => {
310305
self.access_place(
311306
location,
@@ -331,6 +326,8 @@ impl<'a, 'tcx> LoanInvalidationsGenerator<'a, 'tcx> {
331326
Rvalue::WrapUnsafeBinder(op, _) => {
332327
self.consume_operand(location, op);
333328
}
329+
330+
Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in borrowck"),
334331
}
335332
}
336333

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -600,11 +600,6 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
600600
let val = codegen_operand(fx, operand);
601601
lval.write_cvalue(fx, val);
602602
}
603-
Rvalue::CopyForDeref(place) => {
604-
let cplace = codegen_place(fx, place);
605-
let val = cplace.to_cvalue(fx);
606-
lval.write_cvalue(fx, val)
607-
}
608603
Rvalue::Ref(_, _, place) | Rvalue::RawPtr(_, place) => {
609604
let place = codegen_place(fx, place);
610605
let ref_ = place.place_ref(fx, lval.layout());
@@ -928,6 +923,7 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
928923
let operand = codegen_operand(fx, operand);
929924
lval.write_cvalue_transmute(fx, operand);
930925
}
926+
Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in codegen"),
931927
}
932928
}
933929
StatementKind::StorageLive(_)

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

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

507-
mir::Rvalue::CopyForDeref(place) => {
508-
self.codegen_operand(bx, &mir::Operand::Copy(place))
509-
}
510507
mir::Rvalue::RawPtr(kind, place) => {
511508
let mk_ptr = move |tcx: TyCtxt<'tcx>, ty: Ty<'tcx>| {
512509
Ty::new_ptr(tcx, ty, kind.to_mutbl_lossy())
@@ -742,6 +739,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
742739
let layout = bx.cx().layout_of(binder_ty);
743740
OperandRef { val: operand.val, layout }
744741
}
742+
mir::Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in codegen"),
745743
}
746744
}
747745

compiler/rustc_const_eval/src/check_consts/qualifs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ where
234234

235235
Rvalue::Discriminant(place) => in_place::<Q, _>(cx, in_local, place.as_ref()),
236236

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

239239
Rvalue::Use(operand)
240240
| 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
@@ -188,10 +188,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
188188
self.copy_op(&op, &dest)?;
189189
}
190190

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

196193
BinaryOp(bin_op, box (ref left, ref right)) => {
197194
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
@@ -1065,7 +1065,11 @@ pub enum LocalInfo<'tcx> {
10651065
/// A temporary created during evaluating `if` predicate, possibly for pattern matching for `let`s,
10661066
/// and subject to Edition 2024 temporary lifetime rules
10671067
IfThenRescopeTemp { if_then: HirId },
1068-
/// A temporary created during the pass `Derefer` to avoid it's retagging
1068+
/// A temporary created during the pass `Derefer` treated as a transparent alias
1069+
/// for the place its copied from by analysis passes such as `AddRetag` and `ElaborateDrops`.
1070+
///
1071+
/// It may only be written to by a `CopyForDeref` and otherwise only accessed through a deref.
1072+
/// In runtime MIR, it is replaced with a normal `Boring` local.
10691073
DerefTemp,
10701074
/// A temporary created for borrow checking.
10711075
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`]
@@ -1460,11 +1462,13 @@ pub enum Rvalue<'tcx> {
14601462
/// A CopyForDeref is equivalent to a read from a place at the
14611463
/// codegen level, but is treated specially by drop elaboration. When such a read happens, it
14621464
/// is guaranteed (via nature of the mir_opt `Derefer` in rustc_mir_transform/src/deref_separator)
1463-
/// that the only use of the returned value is a deref operation, immediately
1464-
/// followed by one or more projections. Drop elaboration treats this rvalue as if the
1465+
/// that the returned value is written into a `DerefTemp` local and that its only use is a deref operation,
1466+
/// immediately followed by one or more projections. Drop elaboration treats this rvalue as if the
14651467
/// read never happened and just projects further. This allows simplifying various MIR
14661468
/// optimizations and codegen backends that previously had to handle deref operations anywhere
14671469
/// in a place.
1470+
///
1471+
/// Disallowed in runtime MIR and is replaced by normal copies.
14681472
CopyForDeref(Place<'tcx>),
14691473

14701474
/// 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(true);

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`.
@@ -488,9 +483,7 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
488483
}
489484
Rvalue::Discriminant(place) => state.get_discr(place.as_ref(), &self.map),
490485
Rvalue::Use(operand) => return self.handle_operand(operand, state),
491-
Rvalue::CopyForDeref(place) => {
492-
return self.handle_operand(&Operand::Copy(*place), state);
493-
}
486+
Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in runtime MIR"),
494487
Rvalue::Ref(..) | Rvalue::RawPtr(..) => {
495488
// We don't track such places.
496489
return ValueOrPlace::TOP;

0 commit comments

Comments
 (0)