Skip to content

Commit 42b384e

Browse files
committed
Auto merge of #147055 - beepster4096:subtype_is_not_a_projection, r=lcnr
Turn ProjectionElem::Subtype into CastKind::Subtype I noticed that drop elaboration can't, in general, handle `ProjectionElem::SubType`. It creates a disjoint move path that overlaps with other move paths. (`Subslice` does too, and I'm working on a different PR to make that special case less fragile.) If its skipped and treated as the same move path as its parent then `MovePath.place` has multiple possible projections. (It would probably make sense to remove all `Subtype` projections for the canonical place but it doesn't make sense to have this special case for a problem that doesn't actually occur in real MIR.) The only reason this doesn't break is that `Subtype` is always the sole projection of the local its applied to. For the same reason, it works fine as a `CastKind` so I figured that makes more sense than documenting and validating this hidden invariant. cc #112651, #133258 r? Icnr (bc you've been the main person dealing with `Subtype` it looks like)
2 parents 3369e82 + 413f095 commit 42b384e

File tree

34 files changed

+56
-121
lines changed

34 files changed

+56
-121
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3968,7 +3968,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
39683968
}
39693969
ProjectionElem::ConstantIndex { .. }
39703970
| ProjectionElem::Subslice { .. }
3971-
| ProjectionElem::Subtype(_)
39723971
| ProjectionElem::Index(_)
39733972
| ProjectionElem::UnwrapUnsafeBinder(_) => kind,
39743973
},

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
402402
ProjectionElem::Downcast(..) if opt.including_downcast => return None,
403403
ProjectionElem::Downcast(..) => (),
404404
ProjectionElem::OpaqueCast(..) => (),
405-
ProjectionElem::Subtype(..) => (),
406405
ProjectionElem::UnwrapUnsafeBinder(_) => (),
407406
ProjectionElem::Field(field, _ty) => {
408407
// FIXME(project-rfc_2229#36): print capture precisely here.
@@ -484,9 +483,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
484483
PlaceRef { local, projection: proj_base }.ty(self.body, self.infcx.tcx)
485484
}
486485
ProjectionElem::Downcast(..) => place.ty(self.body, self.infcx.tcx),
487-
ProjectionElem::Subtype(ty)
488-
| ProjectionElem::OpaqueCast(ty)
489-
| ProjectionElem::UnwrapUnsafeBinder(ty) => PlaceTy::from_ty(*ty),
486+
ProjectionElem::OpaqueCast(ty) | ProjectionElem::UnwrapUnsafeBinder(ty) => {
487+
PlaceTy::from_ty(*ty)
488+
}
490489
ProjectionElem::Field(_, field_type) => PlaceTy::from_ty(*field_type),
491490
},
492491
};

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
192192
[
193193
..,
194194
ProjectionElem::Index(_)
195-
| ProjectionElem::Subtype(_)
196195
| ProjectionElem::ConstantIndex { .. }
197196
| ProjectionElem::OpaqueCast { .. }
198197
| ProjectionElem::Subslice { .. }

compiler/rustc_borrowck/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,10 +1989,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
19891989
},
19901990
// `OpaqueCast`: only transmutes the type, so no moves there.
19911991
// `Downcast` : only changes information about a `Place` without moving.
1992-
// `Subtype` : only transmutes the type, so no moves.
19931992
// So it's safe to skip these.
19941993
ProjectionElem::OpaqueCast(_)
1995-
| ProjectionElem::Subtype(_)
19961994
| ProjectionElem::Downcast(_, _)
19971995
| ProjectionElem::UnwrapUnsafeBinder(_) => (),
19981996
}
@@ -2218,7 +2216,6 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
22182216
for (place_base, elem) in place.iter_projections().rev() {
22192217
match elem {
22202218
ProjectionElem::Index(_/*operand*/) |
2221-
ProjectionElem::Subtype(_) |
22222219
ProjectionElem::OpaqueCast(_) |
22232220
ProjectionElem::ConstantIndex { .. } |
22242221
// assigning to P[i] requires P to be valid.
@@ -2610,7 +2607,6 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
26102607
| ProjectionElem::Index(..)
26112608
| ProjectionElem::ConstantIndex { .. }
26122609
| ProjectionElem::Subslice { .. }
2613-
| ProjectionElem::Subtype(..)
26142610
| ProjectionElem::OpaqueCast { .. }
26152611
| ProjectionElem::Downcast(..)
26162612
| ProjectionElem::UnwrapUnsafeBinder(_) => {

compiler/rustc_borrowck/src/places_conflict.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ fn place_components_conflict<'tcx>(
249249
| (ProjectionElem::ConstantIndex { .. }, _, _)
250250
| (ProjectionElem::Subslice { .. }, _, _)
251251
| (ProjectionElem::OpaqueCast { .. }, _, _)
252-
| (ProjectionElem::Subtype(_), _, _)
253252
| (ProjectionElem::Downcast { .. }, _, _)
254253
| (ProjectionElem::UnwrapUnsafeBinder(_), _, _) => {
255254
// Recursive case. This can still be disjoint on a
@@ -510,7 +509,6 @@ fn place_projection_conflict<'tcx>(
510509
| ProjectionElem::Field(..)
511510
| ProjectionElem::Index(..)
512511
| ProjectionElem::ConstantIndex { .. }
513-
| ProjectionElem::Subtype(_)
514512
| ProjectionElem::OpaqueCast { .. }
515513
| ProjectionElem::Subslice { .. }
516514
| ProjectionElem::Downcast(..),

compiler/rustc_borrowck/src/prefixes.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ impl<'tcx> Iterator for Prefixes<'tcx> {
7777
| ProjectionElem::Index(_) => {
7878
cursor = cursor_base;
7979
}
80-
ProjectionElem::Subtype(..) => {
81-
panic!("Subtype projection is not allowed before borrow check")
82-
}
8380
ProjectionElem::Deref => {
8481
match self.kind {
8582
PrefixSet::Shallow => {

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,9 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
15581558
),
15591559
}
15601560
}
1561+
CastKind::Subtype => {
1562+
bug!("CastKind::Subtype shouldn't exist in borrowck")
1563+
}
15611564
}
15621565
}
15631566

@@ -1882,9 +1885,6 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
18821885
)
18831886
.unwrap();
18841887
}
1885-
ProjectionElem::Subtype(_) => {
1886-
bug!("ProjectionElem::Subtype shouldn't exist in borrowck")
1887-
}
18881888
}
18891889
}
18901890
}
@@ -2412,9 +2412,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
24122412
| ProjectionElem::UnwrapUnsafeBinder(_) => {
24132413
// other field access
24142414
}
2415-
ProjectionElem::Subtype(_) => {
2416-
bug!("ProjectionElem::Subtype shouldn't exist in borrowck")
2417-
}
24182415
}
24192416
}
24202417
}

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
789789
let operand = codegen_operand(fx, operand);
790790
crate::unsize::coerce_unsized_into(fx, operand, lval);
791791
}
792-
Rvalue::Cast(CastKind::Transmute, ref operand, _to_ty) => {
792+
Rvalue::Cast(CastKind::Transmute | CastKind::Subtype, ref operand, _to_ty) => {
793793
let operand = codegen_operand(fx, operand);
794794
lval.write_cvalue_transmute(fx, operand);
795795
}
@@ -996,7 +996,7 @@ pub(crate) fn codegen_place<'tcx>(
996996
cplace = cplace.place_deref(fx);
997997
}
998998
PlaceElem::OpaqueCast(ty) => bug!("encountered OpaqueCast({ty}) in codegen"),
999-
PlaceElem::Subtype(ty) | PlaceElem::UnwrapUnsafeBinder(ty) => {
999+
PlaceElem::UnwrapUnsafeBinder(ty) => {
10001000
cplace = cplace.place_transmute_type(fx, fx.monomorphize(ty));
10011001
}
10021002
PlaceElem::Field(field, _ty) => {

compiler/rustc_codegen_cranelift/src/value_and_place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ impl<'tcx> CPlace<'tcx> {
660660
}
661661
}
662662

663-
/// Used for `ProjectionElem::Subtype`, `ty` has to be monomorphized before
663+
/// Used for `ProjectionElem::UnwrapUnsafeBinder`, `ty` has to be monomorphized before
664664
/// passed on.
665665
pub(crate) fn place_transmute_type(
666666
self,

compiler/rustc_codegen_ssa/src/mir/analyze.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,6 @@ impl<'a, 'b, 'tcx, Bx: BuilderMethods<'b, 'tcx>> LocalAnalyzer<'a, 'b, 'tcx, Bx>
150150
{
151151
layout.for_variant(self.fx.cx, vidx)
152152
}
153-
mir::PlaceElem::Subtype(subtype_ty) => {
154-
let subtype_ty = self.fx.monomorphize(subtype_ty);
155-
self.fx.cx.layout_of(subtype_ty)
156-
}
157153
_ => {
158154
self.locals[place_ref.local] = LocalKind::Memory;
159155
return;

0 commit comments

Comments
 (0)