Skip to content

Commit 4cd3294

Browse files
committed
Auto merge of #55637 - pnkfelix:issue-55552-dont-attempt-to-ascribe-projections-out-of-a-ty-var, r=nikomatsakis
Do not attempt to ascribe projections out of a ty var If we encounter `_` ascribed to structural pattern like `(a, b)`, just skip relate_types. Fix #55552
2 parents 0366cca + 1bbaa55 commit 4cd3294

File tree

4 files changed

+93
-22
lines changed

4 files changed

+93
-22
lines changed

src/librustc/mir/tcx.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,21 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
8080
elem: &PlaceElem<'tcx>)
8181
-> PlaceTy<'tcx>
8282
{
83-
self.projection_ty_core(tcx, elem, |_, _, ty| ty)
83+
self.projection_ty_core(tcx, elem, |_, _, ty| -> Result<Ty<'tcx>, ()> { Ok(ty) })
84+
.unwrap()
8485
}
8586

8687
/// `place_ty.projection_ty_core(tcx, elem, |...| { ... })`
8788
/// projects `place_ty` onto `elem`, returning the appropriate
8889
/// `Ty` or downcast variant corresponding to that projection.
8990
/// The `handle_field` callback must map a `Field` to its `Ty`,
9091
/// (which should be trivial when `T` = `Ty`).
91-
pub fn projection_ty_core<V, T>(self,
92-
tcx: TyCtxt<'a, 'gcx, 'tcx>,
93-
elem: &ProjectionElem<'tcx, V, T>,
94-
mut handle_field: impl FnMut(&Self, &Field, &T) -> Ty<'tcx>)
95-
-> PlaceTy<'tcx>
92+
pub fn projection_ty_core<V, T, E>(
93+
self,
94+
tcx: TyCtxt<'a, 'gcx, 'tcx>,
95+
elem: &ProjectionElem<'tcx, V, T>,
96+
mut handle_field: impl FnMut(&Self, &Field, &T) -> Result<Ty<'tcx>, E>)
97+
-> Result<PlaceTy<'tcx>, E>
9698
where
9799
V: ::std::fmt::Debug, T: ::std::fmt::Debug
98100
{
@@ -142,10 +144,11 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
142144
bug!("cannot downcast non-ADT type: `{:?}`", self)
143145
}
144146
},
145-
ProjectionElem::Field(ref f, ref fty) => PlaceTy::Ty { ty: handle_field(&self, f, fty) }
147+
ProjectionElem::Field(ref f, ref fty) =>
148+
PlaceTy::Ty { ty: handle_field(&self, f, fty)? },
146149
};
147150
debug!("projection_ty self: {:?} elem: {:?} yields: {:?}", self, elem, answer);
148-
answer
151+
Ok(answer)
149152
}
150153
}
151154

src/librustc_mir/borrow_check/nll/type_check/mod.rs

+27-8
Original file line numberDiff line numberDiff line change
@@ -1021,20 +1021,39 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
10211021
let v1 = ty::Contravariant.xform(v);
10221022

10231023
let tcx = self.infcx.tcx;
1024-
let mut projected_ty = PlaceTy::from_ty(ty);
1024+
let ty = self.normalize(ty, locations);
1025+
1026+
// We need to follow any provided projetions into the type.
1027+
//
1028+
// if we hit a ty var as we descend, then just skip the
1029+
// attempt to relate the mir local with any type.
1030+
#[derive(Debug)] struct HitTyVar;
1031+
let mut curr_projected_ty: Result<PlaceTy, HitTyVar>;
1032+
1033+
curr_projected_ty = Ok(PlaceTy::from_ty(ty));
10251034
for proj in &user_ty.projs {
1026-
projected_ty = projected_ty.projection_ty_core(
1035+
let projected_ty = if let Ok(projected_ty) = curr_projected_ty {
1036+
projected_ty
1037+
} else {
1038+
break;
1039+
};
1040+
curr_projected_ty = projected_ty.projection_ty_core(
10271041
tcx, proj, |this, field, &()| {
1028-
let ty = this.field_ty(tcx, field);
1029-
self.normalize(ty, locations)
1042+
if this.to_ty(tcx).is_ty_var() {
1043+
Err(HitTyVar)
1044+
} else {
1045+
let ty = this.field_ty(tcx, field);
1046+
Ok(self.normalize(ty, locations))
1047+
}
10301048
});
10311049
}
10321050
debug!("user_ty base: {:?} freshened: {:?} projs: {:?} yields: {:?}",
1033-
user_ty.base, ty, user_ty.projs, projected_ty);
1051+
user_ty.base, ty, user_ty.projs, curr_projected_ty);
10341052

1035-
let ty = projected_ty.to_ty(tcx);
1036-
1037-
self.relate_types(ty, v1, a, locations, category)?;
1053+
if let Ok(projected_ty) = curr_projected_ty {
1054+
let ty = projected_ty.to_ty(tcx);
1055+
self.relate_types(ty, v1, a, locations, category)?;
1056+
}
10381057
}
10391058
UserTypeAnnotation::TypeOf(def_id, canonical_substs) => {
10401059
let (

src/librustc_traits/type_op.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,35 @@ impl AscribeUserTypeCx<'me, 'gcx, 'tcx> {
151151
debug!("relate_type_and_user_type: ty of def-id is {:?}", ty);
152152
let ty = self.normalize(ty);
153153

154-
let mut projected_ty = PlaceTy::from_ty(ty);
154+
// We need to follow any provided projetions into the type.
155+
//
156+
// if we hit a ty var as we descend, then just skip the
157+
// attempt to relate the mir local with any type.
158+
159+
struct HitTyVar;
160+
let mut curr_projected_ty: Result<PlaceTy, HitTyVar>;
161+
curr_projected_ty = Ok(PlaceTy::from_ty(ty));
155162
for proj in projs {
156-
projected_ty = projected_ty.projection_ty_core(
163+
let projected_ty = if let Ok(projected_ty) = curr_projected_ty {
164+
projected_ty
165+
} else {
166+
break;
167+
};
168+
curr_projected_ty = projected_ty.projection_ty_core(
157169
tcx, proj, |this, field, &()| {
158-
let ty = this.field_ty(tcx, field);
159-
self.normalize(ty)
170+
if this.to_ty(tcx).is_ty_var() {
171+
Err(HitTyVar)
172+
} else {
173+
let ty = this.field_ty(tcx, field);
174+
Ok(self.normalize(ty))
175+
}
160176
});
161177
}
162-
let ty = projected_ty.to_ty(tcx);
163178

164-
self.relate(mir_ty, variance, ty)?;
179+
if let Ok(projected_ty) = curr_projected_ty {
180+
let ty = projected_ty.to_ty(tcx);
181+
self.relate(mir_ty, variance, ty)?;
182+
}
165183

166184
if let Some(UserSelfTy {
167185
impl_def_id,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// compile-pass
2+
3+
// rust-lang/rust#55552: The strategy pnkfelix landed in PR #55274
4+
// (for ensuring that NLL respects user-provided lifetime annotations)
5+
// did not handle the case where the ascribed type has some expliit
6+
// wildcards (`_`) mixed in, and it caused an internal compiler error
7+
// (ICE).
8+
//
9+
// This test is just checking that we do not ICE when such things
10+
// occur.
11+
12+
struct X;
13+
struct Y;
14+
struct Z;
15+
16+
struct Pair { x: X, y: Y }
17+
18+
pub fn join<A, B, RA, RB>(oper_a: A, oper_b: B) -> (RA, RB)
19+
where A: FnOnce() -> RA + Send,
20+
B: FnOnce() -> RB + Send,
21+
RA: Send,
22+
RB: Send
23+
{
24+
(oper_a(), oper_b())
25+
}
26+
27+
fn main() {
28+
let ((_x, _y), _z): (_, Z) = join(|| (X, Y), || Z);
29+
30+
let (Pair { x: _x, y: _y }, Z): (_, Z) = join(|| Pair { x: X, y: Y }, || Z);
31+
}

0 commit comments

Comments
 (0)