]> git.lizzy.rs Git - rust.git/commitdiff
Avoid storing the ImplPolarity and Constness next to a TraitRef and use TraitPredicat...
authorOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Mon, 25 Oct 2021 14:29:54 +0000 (14:29 +0000)
committerDeadbeef <ent3rm4n@gmail.com>
Mon, 29 Nov 2021 13:19:49 +0000 (21:19 +0800)
compiler/rustc_infer/src/traits/util.rs
compiler/rustc_middle/src/traits/select.rs
compiler/rustc_middle/src/ty/mod.rs
compiler/rustc_trait_selection/src/traits/mod.rs
compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs
compiler/rustc_trait_selection/src/traits/select/confirmation.rs
compiler/rustc_trait_selection/src/traits/select/mod.rs
compiler/rustc_trait_selection/src/traits/specialize/mod.rs
compiler/rustc_trait_selection/src/traits/util.rs
compiler/rustc_trait_selection/src/traits/wf.rs
compiler/rustc_typeck/src/astconv/mod.rs

index 92f74af4eb3eb930959168337dd0911572a85803..7e3cfbe2eba2d59b97dc55f2fbbac19b7763bcab 100644 (file)
@@ -328,8 +328,8 @@ pub fn transitive_bounds_that_define_assoc_type<'tcx>(
                 ));
                 for (super_predicate, _) in super_predicates.predicates {
                     let subst_predicate = super_predicate.subst_supertrait(tcx, &trait_ref);
-                    if let Some(binder) = subst_predicate.to_opt_poly_trait_ref() {
-                        stack.push(binder.value);
+                    if let Some(binder) = subst_predicate.to_opt_poly_trait_pred() {
+                        stack.push(binder.map_bound(|t| t.trait_ref));
                     }
                 }
 
@@ -362,8 +362,8 @@ impl<'tcx, I: Iterator<Item = PredicateObligation<'tcx>>> Iterator for FilterToT
 
     fn next(&mut self) -> Option<ty::PolyTraitRef<'tcx>> {
         while let Some(obligation) = self.base_iterator.next() {
-            if let Some(data) = obligation.predicate.to_opt_poly_trait_ref() {
-                return Some(data.value);
+            if let Some(data) = obligation.predicate.to_opt_poly_trait_pred() {
+                return Some(data.map_bound(|t| t.trait_ref));
             }
         }
         None
index ff711bff86f6127279ff800d7fd670268023111a..71ee00c602a3d158a603809007463a960cd49b48 100644 (file)
@@ -101,7 +101,7 @@ pub enum SelectionCandidate<'tcx> {
         /// `false` if there are no *further* obligations.
         has_nested: bool,
     },
-    ParamCandidate((ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>, ty::ImplPolarity)),
+    ParamCandidate(ty::PolyTraitPredicate<'tcx>),
     ImplCandidate(DefId),
     AutoImplCandidate(DefId),
 
index 8e7eb46f260100e0766d703a6e2a93d766c66fd6..c5bbaf60069adb73500d419dd29b6126d5d6b23d 100644 (file)
@@ -885,12 +885,10 @@ fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
 }
 
 impl<'tcx> Predicate<'tcx> {
-    pub fn to_opt_poly_trait_ref(self) -> Option<ConstnessAnd<PolyTraitRef<'tcx>>> {
+    pub fn to_opt_poly_trait_pred(self) -> Option<PolyTraitPredicate<'tcx>> {
         let predicate = self.kind();
         match predicate.skip_binder() {
-            PredicateKind::Trait(t) => {
-                Some(ConstnessAnd { constness: t.constness, value: predicate.rebind(t.trait_ref) })
-            }
+            PredicateKind::Trait(t) => Some(predicate.rebind(t)),
             PredicateKind::Projection(..)
             | PredicateKind::Subtype(..)
             | PredicateKind::Coerce(..)
index 36305429877a8df64c247014aad868f5621884cc..ba527341acc0637c04ac1fe92e4d76c6f175b936 100644 (file)
@@ -574,14 +574,17 @@ fn prepare_vtable_segments<'tcx, T>(
                     .predicates
                     .into_iter()
                     .filter_map(move |(pred, _)| {
-                        pred.subst_supertrait(tcx, &inner_most_trait_ref).to_opt_poly_trait_ref()
+                        pred.subst_supertrait(tcx, &inner_most_trait_ref).to_opt_poly_trait_pred()
                     });
 
                 'diving_in_skip_visited_traits: loop {
                     if let Some(next_super_trait) = direct_super_traits_iter.next() {
                         if visited.insert(next_super_trait.to_predicate(tcx)) {
+                            // We're throwing away potential constness of super traits here.
+                            // FIXME: handle ~const super traits
+                            let next_super_trait = next_super_trait.map_bound(|t| t.trait_ref);
                             stack.push((
-                                next_super_trait.value,
+                                next_super_trait,
                                 emit_vptr_on_new_entry,
                                 Some(direct_super_traits_iter),
                             ));
@@ -613,7 +616,11 @@ fn prepare_vtable_segments<'tcx, T>(
                     if let Some(siblings) = siblings_opt {
                         if let Some(next_inner_most_trait_ref) = siblings.next() {
                             if visited.insert(next_inner_most_trait_ref.to_predicate(tcx)) {
-                                *inner_most_trait_ref = next_inner_most_trait_ref.value;
+                                // We're throwing away potential constness of super traits here.
+                                // FIXME: handle ~const super traits
+                                let next_inner_most_trait_ref =
+                                    next_inner_most_trait_ref.map_bound(|t| t.trait_ref);
+                                *inner_most_trait_ref = next_inner_most_trait_ref;
                                 *emit_vptr = emit_vptr_on_new_entry;
                                 break 'exiting_out;
                             } else {
index 5a857676a20c86105eccce4a6a92c2bc30bbe963..d78fa2482315191837949e0c605c8fa1042269a7 100644 (file)
@@ -383,17 +383,19 @@ fn assemble_candidates_from_caller_bounds<'o>(
             .param_env
             .caller_bounds()
             .iter()
-            .filter_map(|o| o.to_opt_poly_trait_ref());
+            .filter_map(|o| o.to_opt_poly_trait_pred());
 
         // Micro-optimization: filter out predicates relating to different traits.
         let matching_bounds =
-            all_bounds.filter(|p| p.value.def_id() == stack.obligation.predicate.def_id());
+            all_bounds.filter(|p| p.def_id() == stack.obligation.predicate.def_id());
 
         // Keep only those bounds which may apply, and propagate overflow if it occurs.
         for bound in matching_bounds {
-            let wc = self.evaluate_where_clause(stack, bound.value)?;
+            // FIXME(oli-obk): it is suspicious that we are dropping the constness and
+            // polarity here.
+            let wc = self.evaluate_where_clause(stack, bound.map_bound(|t| t.trait_ref))?;
             if wc.may_apply() {
-                candidates.vec.push(ParamCandidate((bound, stack.obligation.polarity())));
+                candidates.vec.push(ParamCandidate(bound));
             }
         }
 
index 2f1f7971a7926aa433352e92bfe306c48d26d8b0..ec2ea2b8c940fe69a90e8781ee64a427f11940e9 100644 (file)
@@ -58,8 +58,9 @@ pub(super) fn confirm_candidate(
             }
 
             ParamCandidate(param) => {
-                let obligations = self.confirm_param_candidate(obligation, param.0.value);
-                Ok(ImplSource::Param(obligations, param.0.constness))
+                let obligations =
+                    self.confirm_param_candidate(obligation, param.map_bound(|t| t.trait_ref));
+                Ok(ImplSource::Param(obligations, param.skip_binder().constness))
             }
 
             ImplCandidate(impl_def_id) => {
@@ -139,7 +140,7 @@ fn confirm_projection_candidate(
 
             let trait_predicate = self.infcx.shallow_resolve(obligation.predicate);
             let placeholder_trait_predicate =
-                self.infcx().replace_bound_vars_with_placeholders(trait_predicate);
+                self.infcx().replace_bound_vars_with_placeholders(trait_predicate).trait_ref;
             let placeholder_self_ty = placeholder_trait_predicate.self_ty();
             let placeholder_trait_predicate = ty::Binder::dummy(placeholder_trait_predicate);
             let (def_id, substs) = match *placeholder_self_ty.kind() {
@@ -150,8 +151,9 @@ fn confirm_projection_candidate(
 
             let candidate_predicate = tcx.item_bounds(def_id)[idx].subst(tcx, substs);
             let candidate = candidate_predicate
-                .to_opt_poly_trait_ref()
-                .expect("projection candidate is not a trait predicate");
+                .to_opt_poly_trait_pred()
+                .expect("projection candidate is not a trait predicate")
+                .map_bound(|t| t.trait_ref);
             let mut obligations = Vec::new();
             let candidate = normalize_with_depth_to(
                 self,
@@ -165,7 +167,7 @@ fn confirm_projection_candidate(
             obligations.extend(self.infcx.commit_if_ok(|_| {
                 self.infcx
                     .at(&obligation.cause, obligation.param_env)
-                    .sup(placeholder_trait_predicate.to_poly_trait_ref(), candidate.value)
+                    .sup(placeholder_trait_predicate, candidate)
                     .map(|InferOk { obligations, .. }| obligations)
                     .map_err(|_| Unimplemented)
             })?);
index 55fd38ac261dd12e4f362f88431bc6cb2189f4b2..e1ab9bba4598247b56ac1a6622d518bba59d460d 100644 (file)
@@ -1089,10 +1089,9 @@ fn filter_impls(
                     ImplCandidate(def_id)
                         if tcx.impl_constness(def_id) == hir::Constness::Const => {}
                     // const param
-                    ParamCandidate((
-                        ty::ConstnessAnd { constness: ty::BoundConstness::ConstIfConst, .. },
-                        _,
-                    )) => {}
+                    ParamCandidate(trait_pred)
+                        if trait_pred.skip_binder().constness
+                            == ty::BoundConstness::ConstIfConst => {}
                     // auto trait impl
                     AutoImplCandidate(..) => {}
                     // generator, this will raise error in other places
@@ -1474,7 +1473,7 @@ fn candidate_should_be_dropped_in_favor_of(
         // Check if a bound would previously have been removed when normalizing
         // the param_env so that it can be given the lowest priority. See
         // #50825 for the motivation for this.
-        let is_global = |cand: &ty::PolyTraitRef<'tcx>| {
+        let is_global = |cand: &ty::PolyTraitPredicate<'tcx>| {
             cand.is_global(self.infcx.tcx) && !cand.has_late_bound_regions()
         };
 
@@ -1507,25 +1506,22 @@ fn candidate_should_be_dropped_in_favor_of(
                 | ConstDropCandidate,
             ) => false,
 
-            (
-                ParamCandidate((other, other_polarity)),
-                ParamCandidate((victim, victim_polarity)),
-            ) => {
-                let same_except_bound_vars = other.value.skip_binder()
-                    == victim.value.skip_binder()
-                    && other.constness == victim.constness
-                    && other_polarity == victim_polarity
-                    && !other.value.skip_binder().has_escaping_bound_vars();
+            (ParamCandidate(other), ParamCandidate(victim)) => {
+                let same_except_bound_vars = other.skip_binder().trait_ref
+                    == victim.skip_binder().trait_ref
+                    && other.skip_binder().constness == victim.skip_binder().constness
+                    && other.skip_binder().polarity == victim.skip_binder().polarity
+                    && !other.skip_binder().trait_ref.has_escaping_bound_vars();
                 if same_except_bound_vars {
                     // See issue #84398. In short, we can generate multiple ParamCandidates which are
                     // the same except for unused bound vars. Just pick the one with the fewest bound vars
                     // or the current one if tied (they should both evaluate to the same answer). This is
                     // probably best characterized as a "hack", since we might prefer to just do our
                     // best to *not* create essentially duplicate candidates in the first place.
-                    other.value.bound_vars().len() <= victim.value.bound_vars().len()
-                } else if other.value == victim.value
-                    && victim.constness == ty::BoundConstness::NotConst
-                    && other_polarity == victim_polarity
+                    other.bound_vars().len() <= victim.bound_vars().len()
+                } else if other.skip_binder().trait_ref == victim.skip_binder().trait_ref
+                    && victim.skip_binder().constness == ty::BoundConstness::NotConst
+                    && other.skip_binder().polarity == victim.skip_binder().polarity
                 {
                     // Drop otherwise equivalent non-const candidates in favor of const candidates.
                     true
@@ -1555,11 +1551,11 @@ fn candidate_should_be_dropped_in_favor_of(
                 | TraitAliasCandidate(..)
                 | ObjectCandidate(_)
                 | ProjectionCandidate(_),
-            ) => !is_global(&cand.0.value),
+            ) => !is_global(cand),
             (ObjectCandidate(_) | ProjectionCandidate(_), ParamCandidate(ref cand)) => {
                 // Prefer these to a global where-clause bound
                 // (see issue #50825).
-                is_global(&cand.0.value)
+                is_global(cand)
             }
             (
                 ImplCandidate(_)
@@ -1575,7 +1571,7 @@ fn candidate_should_be_dropped_in_favor_of(
             ) => {
                 // Prefer these to a global where-clause bound
                 // (see issue #50825).
-                is_global(&cand.0.value) && other.evaluation.must_apply_modulo_regions()
+                is_global(cand) && other.evaluation.must_apply_modulo_regions()
             }
 
             (ProjectionCandidate(i), ProjectionCandidate(j))
index b64c55592272ed07f63b2813a3a748ff7e2d8778..ab732f510ff92c73d9c739485ac4f00d1388387b 100644 (file)
@@ -508,9 +508,9 @@ fn report_conflicting_impls(
         Vec::with_capacity(predicates.len() + types_without_default_bounds.len());
 
     for (p, _) in predicates {
-        if let Some(poly_trait_ref) = p.to_opt_poly_trait_ref() {
-            if Some(poly_trait_ref.value.def_id()) == sized_trait {
-                types_without_default_bounds.remove(poly_trait_ref.value.self_ty().skip_binder());
+        if let Some(poly_trait_ref) = p.to_opt_poly_trait_pred() {
+            if Some(poly_trait_ref.def_id()) == sized_trait {
+                types_without_default_bounds.remove(poly_trait_ref.self_ty().skip_binder());
                 continue;
             }
         }
index 6d2323abba465e152e76ed1c23538c725626fb4e..bc3da12b22e450f703bc758cdcbfee1aec70a65b 100644 (file)
@@ -126,8 +126,8 @@ fn expand(&mut self, item: &TraitAliasExpansionInfo<'tcx>) -> bool {
 
         let items = predicates.predicates.iter().rev().filter_map(|(pred, span)| {
             pred.subst_supertrait(tcx, &trait_ref)
-                .to_opt_poly_trait_ref()
-                .map(|trait_ref| item.clone_and_push(trait_ref.value, *span))
+                .to_opt_poly_trait_pred()
+                .map(|trait_ref| item.clone_and_push(trait_ref.map_bound(|t| t.trait_ref), *span))
         });
         debug!("expand_trait_aliases: items={:?}", items.clone());
 
@@ -183,8 +183,8 @@ fn next(&mut self) -> Option<DefId> {
             predicates
                 .predicates
                 .iter()
-                .filter_map(|(pred, _)| pred.to_opt_poly_trait_ref())
-                .map(|trait_ref| trait_ref.value.def_id())
+                .filter_map(|(pred, _)| pred.to_opt_poly_trait_pred())
+                .map(|trait_ref| trait_ref.def_id())
                 .filter(|&super_def_id| visited.insert(super_def_id)),
         );
         Some(def_id)
index 2a66684e2a2a0937a498866a831992877c24f02e..664dd861612b1f2e64241cb790ece775b1511cba 100644 (file)
@@ -298,9 +298,10 @@ fn compute_trait_ref(&mut self, trait_ref: &ty::TraitRef<'tcx>, elaborate: Elabo
 
         let extend = |obligation: traits::PredicateObligation<'tcx>| {
             let mut cause = cause.clone();
-            if let Some(parent_trait_ref) = obligation.predicate.to_opt_poly_trait_ref() {
+            if let Some(parent_trait_ref) = obligation.predicate.to_opt_poly_trait_pred() {
                 let derived_cause = traits::DerivedObligationCause {
-                    parent_trait_ref: parent_trait_ref.value,
+                    // TODO: sus
+                    parent_trait_ref: parent_trait_ref.map_bound(|t| t.trait_ref),
                     parent_code: Lrc::new(obligation.cause.code.clone()),
                 };
                 cause.make_mut().code =
index da751f2075399cef877e49ae73c661a34c2a03a6..b532a6b118f060e469882b9a45c1ad04441140e2 100644 (file)
@@ -1588,7 +1588,7 @@ fn find_bound_for_assoc_item(
                 traits::transitive_bounds_that_define_assoc_type(
                     tcx,
                     predicates.iter().filter_map(|(p, _)| {
-                        p.to_opt_poly_trait_ref().map(|trait_ref| trait_ref.value)
+                        Some(p.to_opt_poly_trait_pred()?.map_bound(|t| t.trait_ref))
                     }),
                     assoc_name,
                 )