Skip to content

Commit bed6132

Browse files
pnkfelixpietroalbini
authored andcommitted
If we encounter _ ascribed to structural pattern like (a, b), just skip relate_types.
1 parent 0db8330 commit bed6132

File tree

3 files changed

+62
-22
lines changed

3 files changed

+62
-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
@@ -991,20 +991,39 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
991991
let v1 = ty::Contravariant.xform(v);
992992

993993
let tcx = self.infcx.tcx;
994-
let mut projected_ty = PlaceTy::from_ty(ty);
994+
let ty = self.normalize(ty, locations);
995+
996+
// We need to follow any provided projetions into the type.
997+
//
998+
// if we hit a ty var as we descend, then just skip the
999+
// attempt to relate the mir local with any type.
1000+
#[derive(Debug)] struct HitTyVar;
1001+
let mut curr_projected_ty: Result<PlaceTy, HitTyVar>;
1002+
1003+
curr_projected_ty = Ok(PlaceTy::from_ty(ty));
9951004
for proj in &user_ty.projs {
996-
projected_ty = projected_ty.projection_ty_core(
1005+
let projected_ty = if let Ok(projected_ty) = curr_projected_ty {
1006+
projected_ty
1007+
} else {
1008+
break;
1009+
};
1010+
curr_projected_ty = projected_ty.projection_ty_core(
9971011
tcx, proj, |this, field, &()| {
998-
let ty = this.field_ty(tcx, field);
999-
self.normalize(ty, locations)
1012+
if this.to_ty(tcx).is_ty_var() {
1013+
Err(HitTyVar)
1014+
} else {
1015+
let ty = this.field_ty(tcx, field);
1016+
Ok(self.normalize(ty, locations))
1017+
}
10001018
});
10011019
}
10021020
debug!("user_ty base: {:?} freshened: {:?} projs: {:?} yields: {:?}",
1003-
user_ty.base, ty, user_ty.projs, projected_ty);
1021+
user_ty.base, ty, user_ty.projs, curr_projected_ty);
10041022

1005-
let ty = projected_ty.to_ty(tcx);
1006-
1007-
self.relate_types(ty, v1, a, locations, category)?;
1023+
if let Ok(projected_ty) = curr_projected_ty {
1024+
let ty = projected_ty.to_ty(tcx);
1025+
self.relate_types(ty, v1, a, locations, category)?;
1026+
}
10081027
}
10091028
UserTypeAnnotation::TypeOf(def_id, canonical_substs) => {
10101029
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,

0 commit comments

Comments
 (0)