Skip to content

Commit eb0f3ed

Browse files
committed
Auto merge of #115025 - ouz-a:ouz_testing, r=lcnr
Make subtyping explicit in MIR This adds new mir-opt that pushes new `ProjectionElem` called `ProjectionElem::Subtype(T)` to `Rvalue` of a subtyped assignment so we can unsoundness issues like #107205 Addresses #112651 r? `@lcnr`
2 parents 9998f4a + 5d753ab commit eb0f3ed

File tree

34 files changed

+282
-65
lines changed

34 files changed

+282
-65
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2828,6 +2828,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
28282828
}
28292829
ProjectionElem::ConstantIndex { .. }
28302830
| ProjectionElem::Subslice { .. }
2831+
| ProjectionElem::Subtype(_)
28312832
| ProjectionElem::Index(_) => kind,
28322833
},
28332834
place_ty.projection_ty(tcx, elem),

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
242242
ProjectionElem::Downcast(..) if opt.including_downcast => return None,
243243
ProjectionElem::Downcast(..) => (),
244244
ProjectionElem::OpaqueCast(..) => (),
245+
ProjectionElem::Subtype(..) => (),
245246
ProjectionElem::Field(field, _ty) => {
246247
// FIXME(project-rfc_2229#36): print capture precisely here.
247248
if let Some(field) = self.is_upvar_field_projection(PlaceRef {
@@ -322,7 +323,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
322323
PlaceRef { local, projection: proj_base }.ty(self.body, self.infcx.tcx)
323324
}
324325
ProjectionElem::Downcast(..) => place.ty(self.body, self.infcx.tcx),
325-
ProjectionElem::OpaqueCast(ty) => PlaceTy::from_ty(*ty),
326+
ProjectionElem::Subtype(ty) | ProjectionElem::OpaqueCast(ty) => {
327+
PlaceTy::from_ty(*ty)
328+
}
326329
ProjectionElem::Field(_, field_type) => PlaceTy::from_ty(*field_type),
327330
},
328331
};

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
159159
[
160160
..,
161161
ProjectionElem::Index(_)
162+
| ProjectionElem::Subtype(_)
162163
| ProjectionElem::ConstantIndex { .. }
163164
| ProjectionElem::OpaqueCast { .. }
164165
| ProjectionElem::Subslice { .. }

compiler/rustc_borrowck/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,6 +1803,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
18031803
for (place_base, elem) in place.iter_projections().rev() {
18041804
match elem {
18051805
ProjectionElem::Index(_/*operand*/) |
1806+
ProjectionElem::Subtype(_) |
18061807
ProjectionElem::OpaqueCast(_) |
18071808
ProjectionElem::ConstantIndex { .. } |
18081809
// assigning to P[i] requires P to be valid.
@@ -2191,6 +2192,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
21912192
| ProjectionElem::Index(..)
21922193
| ProjectionElem::ConstantIndex { .. }
21932194
| ProjectionElem::Subslice { .. }
2195+
| ProjectionElem::Subtype(..)
21942196
| ProjectionElem::OpaqueCast { .. }
21952197
| ProjectionElem::Downcast(..) => {
21962198
let upvar_field_projection = self.is_upvar_field_projection(place);

compiler/rustc_borrowck/src/places_conflict.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ fn place_components_conflict<'tcx>(
249249
| (ProjectionElem::ConstantIndex { .. }, _, _)
250250
| (ProjectionElem::Subslice { .. }, _, _)
251251
| (ProjectionElem::OpaqueCast { .. }, _, _)
252+
| (ProjectionElem::Subtype(_), _, _)
252253
| (ProjectionElem::Downcast { .. }, _, _) => {
253254
// Recursive case. This can still be disjoint on a
254255
// further iteration if this a shallow access and
@@ -508,6 +509,7 @@ fn place_projection_conflict<'tcx>(
508509
| ProjectionElem::Field(..)
509510
| ProjectionElem::Index(..)
510511
| ProjectionElem::ConstantIndex { .. }
512+
| ProjectionElem::Subtype(_)
511513
| ProjectionElem::OpaqueCast { .. }
512514
| ProjectionElem::Subslice { .. }
513515
| ProjectionElem::Downcast(..),

compiler/rustc_borrowck/src/prefixes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ impl<'cx, 'tcx> Iterator for Prefixes<'cx, 'tcx> {
8989
cursor = cursor_base;
9090
continue 'cursor;
9191
}
92+
ProjectionElem::Subtype(..) => {
93+
panic!("Subtype projection is not allowed before borrow check")
94+
}
9295
ProjectionElem::Deref => {
9396
// (handled below)
9497
}

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,9 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
716716
}
717717
PlaceTy::from_ty(fty)
718718
}
719+
ProjectionElem::Subtype(_) => {
720+
bug!("ProjectionElem::Subtype shouldn't exist in borrowck")
721+
}
719722
ProjectionElem::OpaqueCast(ty) => {
720723
let ty = self.sanitize_type(place, ty);
721724
let ty = self.cx.normalize(ty, location);
@@ -2563,6 +2566,9 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
25632566
| ProjectionElem::Subslice { .. } => {
25642567
// other field access
25652568
}
2569+
ProjectionElem::Subtype(_) => {
2570+
bug!("ProjectionElem::Subtype shouldn't exist in borrowck")
2571+
}
25662572
}
25672573
}
25682574
}

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,7 @@ pub(crate) fn codegen_place<'tcx>(
876876
cplace = cplace.place_deref(fx);
877877
}
878878
PlaceElem::OpaqueCast(ty) => bug!("encountered OpaqueCast({ty}) in codegen"),
879+
PlaceElem::Subtype(ty) => cplace = cplace.place_transmute_type(fx, fx.monomorphize(ty)),
879880
PlaceElem::Field(field, _ty) => {
880881
cplace = cplace.place_field(fx, field);
881882
}

compiler/rustc_codegen_cranelift/src/value_and_place.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,16 @@ impl<'tcx> CPlace<'tcx> {
674674
}
675675
}
676676

677+
/// Used for `ProjectionElem::Subtype`, `ty` has to be monomorphized before
678+
/// passed on.
679+
pub(crate) fn place_transmute_type(
680+
self,
681+
fx: &mut FunctionCx<'_, '_, 'tcx>,
682+
ty: Ty<'tcx>,
683+
) -> CPlace<'tcx> {
684+
CPlace { inner: self.inner, layout: fx.layout_of(ty) }
685+
}
686+
677687
pub(crate) fn place_field(
678688
self,
679689
fx: &mut FunctionCx<'_, '_, 'tcx>,

compiler/rustc_codegen_ssa/src/mir/place.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
466466
mir::ProjectionElem::OpaqueCast(ty) => {
467467
bug!("encountered OpaqueCast({ty}) in codegen")
468468
}
469+
mir::ProjectionElem::Subtype(ty) => cg_base.project_type(bx, self.monomorphize(ty)),
469470
mir::ProjectionElem::Index(index) => {
470471
let index = &mir::Operand::Copy(mir::Place::from(index));
471472
let index = self.codegen_operand(bx, index);

0 commit comments

Comments
 (0)