about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/librustc_middle/ty/mod.rs66
-rw-r--r--src/librustc_mir/monomorphize/polymorphize.rs15
-rw-r--r--src/librustc_trait_selection/traits/fulfill.rs2
-rw-r--r--src/librustc_typeck/check/method/suggest.rs7
-rw-r--r--src/librustc_typeck/collect.rs4
5 files changed, 35 insertions, 59 deletions
diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs
index 6f454e15aad..d1c6d3be5f4 100644
--- a/src/librustc_middle/ty/mod.rs
+++ b/src/librustc_middle/ty/mod.rs
@@ -1067,20 +1067,23 @@ impl<'tcx> Predicate<'tcx> {
 
     /// Returns the inner `PredicateAtom`.
     ///
-    /// Note that this method does not check if predicate has unbound variables,
-    /// rebinding the returned atom potentially causes the previously bound variables
+    /// Note that this method does not check if the predicate has unbound variables.
+    ///
+    /// Rebinding the returned atom can causes the previously bound variables
     /// to end up at the wrong binding level.
     pub fn skip_binders_unchecked(self) -> PredicateAtom<'tcx> {
         match self.kind() {
             &PredicateKind::ForAll(binder) => binder.skip_binder(),
-            &ty::PredicateKind::Atom(atom) => atom,
+            &PredicateKind::Atom(atom) => atom,
         }
     }
 
+    /// Allows using a `Binder<PredicateAtom<'tcx>>` even if the given predicate previously
+    /// contained unbound variables by shifting these variables outwards.
     pub fn bound_atom(self, tcx: TyCtxt<'tcx>) -> Binder<PredicateAtom<'tcx>> {
         match self.kind() {
             &PredicateKind::ForAll(binder) => binder,
-            &ty::PredicateKind::Atom(atom) => Binder::wrap_nonbinding(tcx, atom),
+            &PredicateKind::Atom(atom) => Binder::wrap_nonbinding(tcx, atom),
         }
     }
 }
@@ -1105,7 +1108,6 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Predicate<'tcx> {
 pub enum PredicateKind<'tcx> {
     /// `for<'a>: ...`
     ForAll(Binder<PredicateAtom<'tcx>>),
-
     Atom(PredicateAtom<'tcx>),
 }
 
@@ -1179,7 +1181,7 @@ pub struct CratePredicatesMap<'tcx> {
     /// For each struct with outlive bounds, maps to a vector of the
     /// predicate of its outlive bounds. If an item has no outlives
     /// bounds, it will have no entry.
-    pub predicates: FxHashMap<DefId, &'tcx [(ty::Predicate<'tcx>, Span)]>,
+    pub predicates: FxHashMap<DefId, &'tcx [(Predicate<'tcx>, Span)]>,
 }
 
 impl<'tcx> Predicate<'tcx> {
@@ -1192,7 +1194,7 @@ impl<'tcx> Predicate<'tcx> {
         self,
         tcx: TyCtxt<'tcx>,
         trait_ref: &ty::PolyTraitRef<'tcx>,
-    ) -> ty::Predicate<'tcx> {
+    ) -> Predicate<'tcx> {
         // The interaction between HRTB and supertraits is not entirely
         // obvious. Let me walk you (and myself) through an example.
         //
@@ -1384,13 +1386,13 @@ impl ToPredicate<'tcx> for PredicateAtom<'tcx> {
     #[inline(always)]
     fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
         debug_assert!(!self.has_escaping_bound_vars(), "escaping bound vars for {:?}", self);
-        tcx.mk_predicate(ty::PredicateKind::Atom(self))
+        tcx.mk_predicate(PredicateKind::Atom(self))
     }
 }
 
 impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<TraitRef<'tcx>> {
     fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
-        ty::PredicateAtom::Trait(ty::TraitPredicate { trait_ref: self.value }, self.constness)
+        PredicateAtom::Trait(ty::TraitPredicate { trait_ref: self.value }, self.constness)
             .to_predicate(tcx)
     }
 }
@@ -1407,51 +1409,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> {
-        if let Some(pred) = self.value.no_bound_vars() {
-            ty::PredicateAtom::Trait(pred, self.constness).to_predicate(tcx)
-        } else {
-            ty::PredicateKind::ForAll(
-                self.value.map_bound(|pred| ty::PredicateAtom::Trait(pred, self.constness)),
-            )
-            .to_predicate(tcx)
-        }
+        PredicateAtom::Trait(self.value.skip_binder(), self.constness)
+            .potentially_quantified(tcx, PredicateKind::ForAll)
     }
 }
 
 impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate<'tcx> {
     fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
-        if let Some(outlives) = self.no_bound_vars() {
-            PredicateAtom::RegionOutlives(outlives).to_predicate(tcx)
-        } else {
-            ty::PredicateKind::ForAll(
-                self.map_bound(|outlives| PredicateAtom::RegionOutlives(outlives)),
-            )
-            .to_predicate(tcx)
-        }
+        PredicateAtom::RegionOutlives(self.skip_binder())
+            .potentially_quantified(tcx, PredicateKind::ForAll)
     }
 }
 
 impl<'tcx> ToPredicate<'tcx> for PolyTypeOutlivesPredicate<'tcx> {
     fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
-        if let Some(outlives) = self.no_bound_vars() {
-            PredicateAtom::TypeOutlives(outlives).to_predicate(tcx)
-        } else {
-            ty::PredicateKind::ForAll(
-                self.map_bound(|outlives| PredicateAtom::TypeOutlives(outlives)),
-            )
-            .to_predicate(tcx)
-        }
+        PredicateAtom::TypeOutlives(self.skip_binder())
+            .potentially_quantified(tcx, PredicateKind::ForAll)
     }
 }
 
 impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
     fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
-        if let Some(proj) = self.no_bound_vars() {
-            PredicateAtom::Projection(proj).to_predicate(tcx)
-        } else {
-            ty::PredicateKind::ForAll(self.map_bound(|proj| PredicateAtom::Projection(proj)))
-                .to_predicate(tcx)
-        }
+        PredicateAtom::Projection(self.skip_binder())
+            .potentially_quantified(tcx, PredicateKind::ForAll)
     }
 }
 
@@ -1746,7 +1726,7 @@ pub struct ParamEnv<'tcx> {
     // Specifically, the low bit represents Reveal, with 0 meaning `UserFacing`
     // and 1 meaning `All`. The rest is the pointer.
     //
-    // This relies on the List<ty::Predicate<'tcx>> type having at least 2-byte
+    // This relies on the List<Predicate<'tcx>> type having at least 2-byte
     // alignment. Lists start with a usize and are repr(C) so this should be
     // fine; there is a debug_assert in the constructor as well.
     //
@@ -1760,7 +1740,7 @@ pub struct ParamEnv<'tcx> {
     ///
     /// Note: This is packed into the `packed_data` usize above, use the
     /// `caller_bounds()` method to access it.
-    caller_bounds: PhantomData<&'tcx List<ty::Predicate<'tcx>>>,
+    caller_bounds: PhantomData<&'tcx List<Predicate<'tcx>>>,
 
     /// Typically, this is `Reveal::UserFacing`, but during codegen we
     /// want `Reveal::All`.
@@ -1838,7 +1818,7 @@ impl<'tcx> ParamEnv<'tcx> {
     }
 
     #[inline]
-    pub fn caller_bounds(self) -> &'tcx List<ty::Predicate<'tcx>> {
+    pub fn caller_bounds(self) -> &'tcx List<Predicate<'tcx>> {
         // mask out bottom bit
         unsafe { &*((self.packed_data & (!1)) as *const _) }
     }
@@ -1863,7 +1843,7 @@ impl<'tcx> ParamEnv<'tcx> {
     /// Construct a trait environment with the given set of predicates.
     #[inline]
     pub fn new(
-        caller_bounds: &'tcx List<ty::Predicate<'tcx>>,
+        caller_bounds: &'tcx List<Predicate<'tcx>>,
         reveal: Reveal,
         def_id: Option<DefId>,
     ) -> Self {
diff --git a/src/librustc_mir/monomorphize/polymorphize.rs b/src/librustc_mir/monomorphize/polymorphize.rs
index 071b9bb9711..562f512c5da 100644
--- a/src/librustc_mir/monomorphize/polymorphize.rs
+++ b/src/librustc_mir/monomorphize/polymorphize.rs
@@ -131,9 +131,9 @@ fn mark_used_by_predicates<'tcx>(
     let predicates = tcx.explicit_predicates_of(def_id);
     debug!("mark_parameters_used_in_predicates: predicates_of={:?}", predicates);
     for (predicate, _) in predicates.predicates {
-        match predicate.kind() {
-            ty::PredicateKind::Trait(predicate, ..) => {
-                let trait_ref = predicate.skip_binder().trait_ref;
+        match predicate.skip_binders() {
+            ty::PredicateAtom::Trait(predicate, ..) => {
+                let trait_ref = predicate.trait_ref;
                 if is_self_ty_used(unused_parameters, trait_ref.self_ty()) {
                     for ty in trait_ref.substs.types() {
                         debug!("unused_generic_params: (trait) ty={:?}", ty);
@@ -141,12 +141,11 @@ fn mark_used_by_predicates<'tcx>(
                     }
                 }
             }
-            ty::PredicateKind::Projection(predicate, ..) => {
-                let self_ty = predicate.skip_binder().projection_ty.self_ty();
+            ty::PredicateAtom::Projection(proj, ..) => {
+                let self_ty = proj.projection_ty.self_ty();
                 if is_self_ty_used(unused_parameters, self_ty) {
-                    let ty = predicate.ty();
-                    debug!("unused_generic_params: (projection) ty={:?}", ty);
-                    mark_ty(unused_parameters, ty.skip_binder());
+                    debug!("unused_generic_params: (projection ty={:?}", proj.ty);
+                    mark_ty(unused_parameters, proj.ty);
                 }
             }
             _ => (),
diff --git a/src/librustc_trait_selection/traits/fulfill.rs b/src/librustc_trait_selection/traits/fulfill.rs
index 2b9621c9271..2eda1ce595a 100644
--- a/src/librustc_trait_selection/traits/fulfill.rs
+++ b/src/librustc_trait_selection/traits/fulfill.rs
@@ -3,7 +3,7 @@ use rustc_data_structures::obligation_forest::ProcessResult;
 use rustc_data_structures::obligation_forest::{DoCompleted, Error, ForestObligation};
 use rustc_data_structures::obligation_forest::{ObligationForest, ObligationProcessor};
 use rustc_errors::ErrorReported;
-use rustc_infer::traits::{TraitObligation, TraitEngine, TraitEngineExt as _};
+use rustc_infer::traits::{TraitEngine, TraitEngineExt as _, TraitObligation};
 use rustc_middle::mir::interpret::ErrorHandled;
 use rustc_middle::ty::error::ExpectedFound;
 use rustc_middle::ty::ToPredicate;
diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs
index 07cc8332b84..ae2cf6daf53 100644
--- a/src/librustc_typeck/check/method/suggest.rs
+++ b/src/librustc_typeck/check/method/suggest.rs
@@ -571,9 +571,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                     let mut type_params = FxHashMap::default();
                     let mut bound_spans = vec![];
 
-                    let mut collect_type_param_suggestions = {
-                        // We need to move `tcx` while only borrowing the rest,
-                        // this is kind of ugly.
+                    let mut collect_type_param_suggestions =
                         |self_ty: Ty<'tcx>, parent_pred: &ty::Predicate<'tcx>, obligation: &str| {
                             // We don't care about regions here, so it's fine to skip the binder here.
                             if let (ty::Param(_), ty::PredicateAtom::Trait(p, _)) =
@@ -601,8 +599,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                                     }
                                 }
                             }
-                        }
-                    };
+                        };
                     let mut bound_span_label = |self_ty: Ty<'_>, obligation: &str, quiet: &str| {
                         let msg = format!(
                             "doesn't satisfy `{}`",
diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs
index a733ad4fccd..76439af79f3 100644
--- a/src/librustc_typeck/collect.rs
+++ b/src/librustc_typeck/collect.rs
@@ -1930,8 +1930,8 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat
                         let re_root_empty = tcx.lifetimes.re_root_empty;
                         let predicate = ty::OutlivesPredicate(ty, re_root_empty);
                         predicates.push((
-                            ty::PredicateKind::TypeOutlives(ty::Binder::bind(predicate))
-                                .to_predicate(tcx),
+                            ty::PredicateAtom::TypeOutlives(predicate)
+                                .potentially_quantified(tcx, ty::PredicateKind::ForAll),
                             span,
                         ));
                     }