Skip to content

Commit 6f8fb91

Browse files
authored
Rollup merge of #98582 - oli-obk:unconstrained_opaque_type, r=estebank
Allow destructuring opaque types in their defining scopes fixes #96572 Before this PR, the following code snippet failed with an incomprehensible error, and similar code just ICEd in mir borrowck. ```rust type T = impl Copy; let foo: T = (1u32, 2u32); let (a, b) = foo; ``` The problem was that the last line created MIR projections of the form `foo.0` and `foo.1`, but `foo`'s type is `T`, which doesn't have fields (only its hidden type does). But the pattern supplies enough type information (a tuple of two different inference types) to bind a hidden type.
2 parents 7210e46 + 57e9f7a commit 6f8fb91

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+396
-205
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
20932093
}
20942094
StorageDeadOrDrop::Destructor(_) => kind,
20952095
},
2096-
ProjectionElem::Field(..) | ProjectionElem::Downcast(..) => {
2096+
ProjectionElem::OpaqueCast { .. }
2097+
| ProjectionElem::Field(..)
2098+
| ProjectionElem::Downcast(..) => {
20972099
match place_ty.ty.kind() {
20982100
ty::Adt(def, _) if def.has_dtor(tcx) => {
20992101
// Report the outermost adt with a destructor

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
226226
}
227227
ProjectionElem::Downcast(..) if including_downcast.0 => return None,
228228
ProjectionElem::Downcast(..) => (),
229+
ProjectionElem::OpaqueCast(..) => (),
229230
ProjectionElem::Field(field, _ty) => {
230231
// FIXME(project-rfc_2229#36): print capture precisely here.
231232
if let Some(field) = self.is_upvar_field_projection(PlaceRef {
@@ -286,6 +287,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
286287
PlaceRef { local, projection: proj_base }.ty(self.body, self.infcx.tcx)
287288
}
288289
ProjectionElem::Downcast(..) => place.ty(self.body, self.infcx.tcx),
290+
ProjectionElem::OpaqueCast(ty) => PlaceTy::from_ty(*ty),
289291
ProjectionElem::Field(_, field_type) => PlaceTy::from_ty(*field_type),
290292
},
291293
};

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
169169
..,
170170
ProjectionElem::Index(_)
171171
| ProjectionElem::ConstantIndex { .. }
172+
| ProjectionElem::OpaqueCast { .. }
172173
| ProjectionElem::Subslice { .. }
173174
| ProjectionElem::Downcast(..),
174175
],

compiler/rustc_borrowck/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,6 +1788,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
17881788
for (place_base, elem) in place.iter_projections().rev() {
17891789
match elem {
17901790
ProjectionElem::Index(_/*operand*/) |
1791+
ProjectionElem::OpaqueCast(_) |
17911792
ProjectionElem::ConstantIndex { .. } |
17921793
// assigning to P[i] requires P to be valid.
17931794
ProjectionElem::Downcast(_/*adt_def*/, _/*variant_idx*/) =>
@@ -2179,6 +2180,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
21792180
| ProjectionElem::Index(..)
21802181
| ProjectionElem::ConstantIndex { .. }
21812182
| ProjectionElem::Subslice { .. }
2183+
| ProjectionElem::OpaqueCast { .. }
21822184
| ProjectionElem::Downcast(..) => {
21832185
let upvar_field_projection = self.is_upvar_field_projection(place);
21842186
if let Some(field) = upvar_field_projection {

compiler/rustc_borrowck/src/places_conflict.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ fn place_components_conflict<'tcx>(
255255
| (ProjectionElem::Index { .. }, _, _)
256256
| (ProjectionElem::ConstantIndex { .. }, _, _)
257257
| (ProjectionElem::Subslice { .. }, _, _)
258+
| (ProjectionElem::OpaqueCast { .. }, _, _)
258259
| (ProjectionElem::Downcast { .. }, _, _) => {
259260
// Recursive case. This can still be disjoint on a
260261
// further iteration if this a shallow access and
@@ -322,6 +323,17 @@ fn place_projection_conflict<'tcx>(
322323
debug!("place_element_conflict: DISJOINT-OR-EQ-DEREF");
323324
Overlap::EqualOrDisjoint
324325
}
326+
(ProjectionElem::OpaqueCast(v1), ProjectionElem::OpaqueCast(v2)) => {
327+
if v1 == v2 {
328+
// same type - recur.
329+
debug!("place_element_conflict: DISJOINT-OR-EQ-OPAQUE");
330+
Overlap::EqualOrDisjoint
331+
} else {
332+
// Different types. Disjoint!
333+
debug!("place_element_conflict: DISJOINT-OPAQUE");
334+
Overlap::Disjoint
335+
}
336+
}
325337
(ProjectionElem::Field(f1, _), ProjectionElem::Field(f2, _)) => {
326338
if f1 == f2 {
327339
// same field (e.g., `a.y` vs. `a.y`) - recur.
@@ -525,6 +537,7 @@ fn place_projection_conflict<'tcx>(
525537
| ProjectionElem::Field(..)
526538
| ProjectionElem::Index(..)
527539
| ProjectionElem::ConstantIndex { .. }
540+
| ProjectionElem::OpaqueCast { .. }
528541
| ProjectionElem::Subslice { .. }
529542
| ProjectionElem::Downcast(..),
530543
_,

compiler/rustc_borrowck/src/prefixes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ impl<'cx, 'tcx> Iterator for Prefixes<'cx, 'tcx> {
8181
}
8282
ProjectionElem::Downcast(..)
8383
| ProjectionElem::Subslice { .. }
84+
| ProjectionElem::OpaqueCast { .. }
8485
| ProjectionElem::ConstantIndex { .. }
8586
| ProjectionElem::Index(_) => {
8687
cursor = cursor_base;

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,19 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
790790
}
791791
PlaceTy::from_ty(fty)
792792
}
793+
ProjectionElem::OpaqueCast(ty) => {
794+
let ty = self.sanitize_type(place, ty);
795+
let ty = self.cx.normalize(ty, location);
796+
self.cx
797+
.eq_types(
798+
base.ty,
799+
ty,
800+
location.to_locations(),
801+
ConstraintCategory::TypeAnnotation,
802+
)
803+
.unwrap();
804+
PlaceTy::from_ty(ty)
805+
}
793806
}
794807
}
795808

@@ -1195,10 +1208,11 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
11951208
tcx,
11961209
self.param_env,
11971210
proj,
1198-
|this, field, ()| {
1211+
|this, field, _| {
11991212
let ty = this.field_ty(tcx, field);
12001213
self.normalize(ty, locations)
12011214
},
1215+
|_, _| unreachable!(),
12021216
);
12031217
curr_projected_ty = projected_ty;
12041218
}
@@ -2493,6 +2507,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
24932507
}
24942508
ProjectionElem::Field(..)
24952509
| ProjectionElem::Downcast(..)
2510+
| ProjectionElem::OpaqueCast(..)
24962511
| ProjectionElem::Index(..)
24972512
| ProjectionElem::ConstantIndex { .. }
24982513
| ProjectionElem::Subslice { .. } => {

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,7 @@ pub(crate) fn codegen_place<'tcx>(
825825
cplace = cplace.place_deref(fx);
826826
}
827827
}
828+
PlaceElem::OpaqueCast(ty) => cplace = cplace.place_opaque_cast(fx, ty),
828829
PlaceElem::Field(field, _ty) => {
829830
cplace = cplace.place_field(fx, field);
830831
}

compiler/rustc_codegen_cranelift/src/value_and_place.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,14 @@ impl<'tcx> CPlace<'tcx> {
615615
}
616616
}
617617

618+
pub(crate) fn place_opaque_cast(
619+
self,
620+
fx: &mut FunctionCx<'_, '_, 'tcx>,
621+
ty: Ty<'tcx>,
622+
) -> CPlace<'tcx> {
623+
CPlace { inner: self.inner, layout: fx.layout_of(ty) }
624+
}
625+
618626
pub(crate) fn place_field(
619627
self,
620628
fx: &mut FunctionCx<'_, '_, 'tcx>,

compiler/rustc_codegen_ssa/src/mir/place.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,21 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
411411
downcast
412412
}
413413

414+
pub fn project_type<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
415+
&self,
416+
bx: &mut Bx,
417+
ty: Ty<'tcx>,
418+
) -> Self {
419+
let mut downcast = *self;
420+
downcast.layout = bx.cx().layout_of(ty);
421+
422+
// Cast to the appropriate type.
423+
let variant_ty = bx.cx().backend_type(downcast.layout);
424+
downcast.llval = bx.pointercast(downcast.llval, bx.cx().type_ptr_to(variant_ty));
425+
426+
downcast
427+
}
428+
414429
pub fn storage_live<Bx: BuilderMethods<'a, 'tcx, Value = V>>(&self, bx: &mut Bx) {
415430
bx.lifetime_start(self.llval, self.layout.size);
416431
}
@@ -459,6 +474,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
459474
mir::ProjectionElem::Field(ref field, _) => {
460475
cg_base.project_field(bx, field.index())
461476
}
477+
mir::ProjectionElem::OpaqueCast(ty) => cg_base.project_type(bx, ty),
462478
mir::ProjectionElem::Index(index) => {
463479
let index = &mir::Operand::Copy(mir::Place::from(index));
464480
let index = self.codegen_operand(bx, index);

0 commit comments

Comments
 (0)