Change potentially_qualified to be defined on Binder<PredicateAtom>

This commit is contained in:
Jack Huey 2020-12-16 21:13:29 -05:00
parent e4297ba39c
commit af3b1cb0b5
4 changed files with 41 additions and 27 deletions

View file

@ -525,10 +525,10 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
result_subst: &'a CanonicalVarValues<'tcx>,
) -> impl Iterator<Item = PredicateObligation<'tcx>> + 'a + Captures<'tcx> {
unsubstituted_region_constraints.iter().map(move |&constraint| {
let ty::OutlivesPredicate(k1, r2) =
substitute_value(self.tcx, result_subst, constraint).skip_binder();
let predicate = substitute_value(self.tcx, result_subst, constraint);
let ty::OutlivesPredicate(k1, r2) = predicate.skip_binder();
let predicate = match k1.unpack() {
let atom = match k1.unpack() {
GenericArgKind::Lifetime(r1) => {
ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(r1, r2))
}
@ -540,8 +540,9 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
// encounter this branch.
span_bug!(cause.span, "unexpected const outlives {:?}", constraint);
}
}
.potentially_quantified(self.tcx, ty::PredicateKind::ForAll);
};
let predicate =
predicate.rebind(atom).potentially_quantified(self.tcx, ty::PredicateKind::ForAll);
Obligation::new(cause.clone(), param_env, predicate)
})

View file

@ -1149,17 +1149,16 @@ pub enum PredicateAtom<'tcx> {
TypeWellFormedFromEnv(Ty<'tcx>),
}
impl<'tcx> PredicateAtom<'tcx> {
impl<'tcx> Binder<PredicateAtom<'tcx>> {
/// Wraps `self` with the given qualifier if this predicate has any unbound variables.
pub fn potentially_quantified(
self,
tcx: TyCtxt<'tcx>,
qualifier: impl FnOnce(Binder<PredicateAtom<'tcx>>) -> PredicateKind<'tcx>,
) -> Predicate<'tcx> {
if self.has_escaping_bound_vars() {
qualifier(Binder::bind(self))
} else {
PredicateKind::Atom(self)
match self.no_bound_vars() {
Some(atom) => PredicateKind::Atom(atom),
None => qualifier(self),
}
.to_predicate(tcx)
}
@ -1252,7 +1251,11 @@ impl<'tcx> Predicate<'tcx> {
let substs = trait_ref.skip_binder().substs;
let pred = self.skip_binders();
let new = pred.subst(tcx, substs);
if new != pred { new.potentially_quantified(tcx, PredicateKind::ForAll) } else { self }
if new != pred {
trait_ref.rebind(new).potentially_quantified(tcx, PredicateKind::ForAll)
} else {
self
}
}
}
@ -1403,28 +1406,29 @@ impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<PolyTraitRef<'tcx>> {
impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<PolyTraitPredicate<'tcx>> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
PredicateAtom::Trait(self.value.skip_binder(), self.constness)
self.value
.map_bound(|value| PredicateAtom::Trait(value, self.constness))
.potentially_quantified(tcx, PredicateKind::ForAll)
}
}
impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
PredicateAtom::RegionOutlives(self.skip_binder())
self.map_bound(|value| PredicateAtom::RegionOutlives(value))
.potentially_quantified(tcx, PredicateKind::ForAll)
}
}
impl<'tcx> ToPredicate<'tcx> for PolyTypeOutlivesPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
PredicateAtom::TypeOutlives(self.skip_binder())
self.map_bound(|value| PredicateAtom::TypeOutlives(value))
.potentially_quantified(tcx, PredicateKind::ForAll)
}
}
impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
PredicateAtom::Projection(self.skip_binder())
self.map_bound(|value| PredicateAtom::Projection(value))
.potentially_quantified(tcx, PredicateKind::ForAll)
}
}

View file

@ -1919,10 +1919,11 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
} else {
let span = bound_pred.bounded_ty.span;
let re_root_empty = tcx.lifetimes.re_root_empty;
let predicate = ty::OutlivesPredicate(ty, re_root_empty);
let predicate = ty::Binder::bind(ty::PredicateAtom::TypeOutlives(
ty::OutlivesPredicate(ty, re_root_empty),
));
predicates.insert((
ty::PredicateAtom::TypeOutlives(predicate)
.potentially_quantified(tcx, ty::PredicateKind::ForAll),
predicate.potentially_quantified(tcx, ty::PredicateKind::ForAll),
span,
));
}
@ -1965,8 +1966,10 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
&hir::GenericBound::Outlives(ref lifetime) => {
let region = AstConv::ast_region_to_region(&icx, lifetime, None);
predicates.insert((
ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty, region))
.potentially_quantified(tcx, ty::PredicateKind::ForAll),
ty::Binder::bind(ty::PredicateAtom::TypeOutlives(
ty::OutlivesPredicate(ty, region),
))
.potentially_quantified(tcx, ty::PredicateKind::ForAll),
lifetime.span,
));
}
@ -1983,7 +1986,9 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
}
_ => bug!(),
};
let pred = ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(r1, r2));
let pred = ty::Binder::dummy(ty::PredicateAtom::RegionOutlives(
ty::OutlivesPredicate(r1, r2),
));
(pred.potentially_quantified(icx.tcx, ty::PredicateKind::ForAll), span)
}))
@ -2233,8 +2238,10 @@ fn predicates_from_bound<'tcx>(
}
hir::GenericBound::Outlives(ref lifetime) => {
let region = astconv.ast_region_to_region(lifetime, None);
let pred = ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(param_ty, region))
.potentially_quantified(astconv.tcx(), ty::PredicateKind::ForAll);
let pred = ty::Binder::dummy(ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(
param_ty, region,
)))
.potentially_quantified(astconv.tcx(), ty::PredicateKind::ForAll);
vec![(pred, lifetime.span)]
}
}

View file

@ -89,13 +89,15 @@ fn inferred_outlives_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> CratePredica
|(ty::OutlivesPredicate(kind1, region2), &span)| {
match kind1.unpack() {
GenericArgKind::Type(ty1) => Some((
ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty1, region2))
.potentially_quantified(tcx, ty::PredicateKind::ForAll),
ty::Binder::dummy(ty::PredicateAtom::TypeOutlives(
ty::OutlivesPredicate(ty1, region2),
))
.potentially_quantified(tcx, ty::PredicateKind::ForAll),
span,
)),
GenericArgKind::Lifetime(region1) => Some((
ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(
region1, region2,
ty::Binder::dummy(ty::PredicateAtom::RegionOutlives(
ty::OutlivesPredicate(region1, region2),
))
.potentially_quantified(tcx, ty::PredicateKind::ForAll),
span,