From af3b1cb0b55ed28c0f4a500e42699603edf199e7 Mon Sep 17 00:00:00 2001 From: Jack Huey Date: Wed, 16 Dec 2020 21:13:29 -0500 Subject: [PATCH] Change potentially_qualified to be defined on Binder --- .../src/infer/canonical/query_response.rs | 11 +++++---- compiler/rustc_middle/src/ty/mod.rs | 24 +++++++++++-------- compiler/rustc_typeck/src/collect.rs | 23 +++++++++++------- compiler/rustc_typeck/src/outlives/mod.rs | 10 ++++---- 4 files changed, 41 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_infer/src/infer/canonical/query_response.rs b/compiler/rustc_infer/src/infer/canonical/query_response.rs index c8d66cbb695..71ce50f7453 100644 --- a/compiler/rustc_infer/src/infer/canonical/query_response.rs +++ b/compiler/rustc_infer/src/infer/canonical/query_response.rs @@ -525,10 +525,10 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> { result_subst: &'a CanonicalVarValues<'tcx>, ) -> impl Iterator> + '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) }) diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 7428f34153c..b0f02dee59b 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1149,17 +1149,16 @@ pub enum PredicateAtom<'tcx> { TypeWellFormedFromEnv(Ty<'tcx>), } -impl<'tcx> PredicateAtom<'tcx> { +impl<'tcx> Binder> { /// 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>) -> 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> { impl<'tcx> ToPredicate<'tcx> for ConstnessAnd> { 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) } } diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index c70554cc627..3b49c73c7f2 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -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)] } } diff --git a/compiler/rustc_typeck/src/outlives/mod.rs b/compiler/rustc_typeck/src/outlives/mod.rs index 94926f480e2..d7c084acf50 100644 --- a/compiler/rustc_typeck/src/outlives/mod.rs +++ b/compiler/rustc_typeck/src/outlives/mod.rs @@ -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,