From 104cb878e34da2f66c0d0b7e5ed388a2c5b96b58 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Thu, 11 Jun 2020 21:42:39 +0200 Subject: [PATCH 01/28] add `PredicateKint`, because who doesn't like bodging --- src/librustc_middle/ty/context.rs | 15 +- src/librustc_middle/ty/mod.rs | 177 +++++++++++++++++++++ src/librustc_middle/ty/structural_impls.rs | 11 ++ 3 files changed, 200 insertions(+), 3 deletions(-) diff --git a/src/librustc_middle/ty/context.rs b/src/librustc_middle/ty/context.rs index 3dd57eea2348f..08620f82536e2 100644 --- a/src/librustc_middle/ty/context.rs +++ b/src/librustc_middle/ty/context.rs @@ -19,7 +19,7 @@ use crate::ty::TyKind::*; use crate::ty::{ self, query, AdtDef, AdtKind, BindingMode, BoundVar, CanonicalPolyFnSig, Const, ConstVid, DefIdTree, ExistentialPredicate, FloatVar, FloatVid, GenericParamDefKind, InferConst, InferTy, - IntVar, IntVid, List, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKind, + IntVar, IntVid, List, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKint, ProjectionTy, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, }; @@ -79,6 +79,7 @@ pub struct CtxtInterners<'tcx> { region: InternedSet<'tcx, RegionKind>, existential_predicates: InternedSet<'tcx, List>>, predicate: InternedSet<'tcx, PredicateInner<'tcx>>, + predicate_kint: InternedSet<'tcx, PredicateKint<'tcx>>, predicates: InternedSet<'tcx, List>>, projs: InternedSet<'tcx, List>, place_elems: InternedSet<'tcx, List>>, @@ -98,11 +99,11 @@ impl<'tcx> CtxtInterners<'tcx> { existential_predicates: Default::default(), canonical_var_infos: Default::default(), predicate: Default::default(), + predicate_kint: Default::default(), predicates: Default::default(), projs: Default::default(), place_elems: Default::default(), const_: Default::default(), - chalk_environment_clause_list: Default::default(), } } @@ -1616,6 +1617,7 @@ nop_lift! {type_; Ty<'a> => Ty<'tcx>} nop_lift! {region; Region<'a> => Region<'tcx>} nop_lift! {const_; &'a Const<'a> => &'tcx Const<'tcx>} nop_lift! {predicate; &'a PredicateInner<'a> => &'tcx PredicateInner<'tcx>} +nop_lift! {predicate_kint; &'a PredicateKint<'a> => &'tcx PredicateKint<'tcx>} nop_list_lift! {type_list; Ty<'a> => Ty<'tcx>} nop_list_lift! {existential_predicates; ExistentialPredicate<'a> => ExistentialPredicate<'tcx>} @@ -2034,6 +2036,12 @@ impl<'tcx> Borrow> for Interned<'tcx, PredicateKind<'tcx>> { } } +impl<'tcx> Borrow> for Interned<'tcx, PredicateKint<'tcx>> { + fn borrow<'a>(&'a self) -> &'a PredicateKint<'tcx> { + &self.0 + } +} + macro_rules! direct_interners { ($($name:ident: $method:ident($ty:ty),)+) => { $(impl<'tcx> PartialEq for Interned<'tcx, $ty> { @@ -2063,6 +2071,7 @@ macro_rules! direct_interners { direct_interners! { region: mk_region(RegionKind), const_: mk_const(Const<'tcx>), + predicate_kint: intern_predicate_kint(PredicateKint<'tcx>), } macro_rules! slice_interners { @@ -2128,7 +2137,7 @@ impl<'tcx> TyCtxt<'tcx> { #[allow(rustc::usage_of_ty_tykind)] #[inline] - pub fn mk_ty(&self, st: TyKind<'tcx>) -> Ty<'tcx> { + pub fn mk_ty(self, st: TyKind<'tcx>) -> Ty<'tcx> { self.interners.intern_ty(st) } diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index a5ddcdfdb0408..36796e94973fa 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -1063,6 +1063,113 @@ impl<'a, 'tcx> HashStable> for Predicate<'tcx> { kind.hash_stable(hcx, hasher); } + + pub fn kint(self, tcx: TyCtxt<'tcx>) -> &'tcx PredicateKint<'tcx> { + // I am efficient + tcx.intern_predicate_kint(match *self.kind() { + PredicateKind::Trait(binder, data) => { + if let Some(simpl) = binder.no_bound_vars() { + PredicateKint::Trait(simpl, data) + } else { + let inner = tcx + .intern_predicate_kint(PredicateKint::Trait(*binder.skip_binder(), data)); + PredicateKint::ForAll(Binder::bind(inner)) + } + } + PredicateKind::RegionOutlives(binder) => { + if let Some(simpl) = binder.no_bound_vars() { + PredicateKint::RegionOutlives(simpl) + } else { + let inner = tcx.intern_predicate_kint(PredicateKint::RegionOutlives( + *binder.skip_binder(), + )); + PredicateKint::ForAll(Binder::bind(inner)) + } + } + PredicateKind::TypeOutlives(binder) => { + if let Some(simpl) = binder.no_bound_vars() { + PredicateKint::TypeOutlives(simpl) + } else { + let inner = tcx + .intern_predicate_kint(PredicateKint::TypeOutlives(*binder.skip_binder())); + PredicateKint::ForAll(Binder::bind(inner)) + } + } + PredicateKind::Projection(binder) => { + if let Some(simpl) = binder.no_bound_vars() { + PredicateKint::Projection(simpl) + } else { + let inner = + tcx.intern_predicate_kint(PredicateKint::Projection(*binder.skip_binder())); + PredicateKint::ForAll(Binder::bind(inner)) + } + } + PredicateKind::WellFormed(arg) => PredicateKint::WellFormed(arg), + PredicateKind::ObjectSafe(def_id) => PredicateKint::ObjectSafe(def_id), + PredicateKind::ClosureKind(def_id, substs, kind) => { + PredicateKint::ClosureKind(def_id, substs, kind) + } + PredicateKind::Subtype(binder) => { + if let Some(simpl) = binder.no_bound_vars() { + PredicateKint::Subtype(simpl) + } else { + let inner = + tcx.intern_predicate_kint(PredicateKint::Subtype(*binder.skip_binder())); + PredicateKint::ForAll(Binder::bind(inner)) + } + } + PredicateKind::ConstEvaluatable(def, substs) => { + PredicateKint::ConstEvaluatable(def, substs) + } + PredicateKind::ConstEquate(l, r) => PredicateKint::ConstEquate(l, r), + }) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(TypeFoldable)] +pub enum PredicateKint<'tcx> { + /// Corresponds to `where Foo: Bar`. `Foo` here would be + /// the `Self` type of the trait reference and `A`, `B`, and `C` + /// would be the type parameters. + /// + /// A trait predicate will have `Constness::Const` if it originates + /// from a bound on a `const fn` without the `?const` opt-out (e.g., + /// `const fn foobar() {}`). + Trait(TraitPredicate<'tcx>, Constness), + + /// `where 'a: 'b` + RegionOutlives(RegionOutlivesPredicate<'tcx>), + + /// `where T: 'a` + TypeOutlives(TypeOutlivesPredicate<'tcx>), + + /// `where ::Name == X`, approximately. + /// See the `ProjectionPredicate` struct for details. + Projection(ProjectionPredicate<'tcx>), + + /// No syntax: `T` well-formed. + WellFormed(GenericArg<'tcx>), + + /// Trait must be object-safe. + ObjectSafe(DefId), + + /// No direct syntax. May be thought of as `where T: FnFoo<...>` + /// for some substitutions `...` and `T` being a closure type. + /// Satisfied (or refuted) once we know the closure's kind. + ClosureKind(DefId, SubstsRef<'tcx>, ClosureKind), + + /// `T1 <: T2` + Subtype(SubtypePredicate<'tcx>), + + /// Constant initializer must evaluate successfully. + ConstEvaluatable(DefId, SubstsRef<'tcx>), + + /// Constants must be equal. The first component is the const that is expected. + ConstEquate(&'tcx Const<'tcx>, &'tcx Const<'tcx>), + + /// `for<'a>: ...` + ForAll(Binder<&'tcx PredicateKint<'tcx>>), } #[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)] @@ -1349,6 +1456,76 @@ impl ToPredicate<'tcx> for PredicateKind<'tcx> { } } +impl ToPredicate<'tcx> for PredicateKint<'tcx> { + #[inline(always)] + fn to_predicate(&self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { + let (predicate, in_binder) = if let PredicateKint::ForAll(binder) = self { + (*binder.skip_binder(), true) + } else { + (self, false) + }; + + macro_rules! bind { + ($expr:expr) => { + match $expr { + expr => { + if in_binder { + Binder::bind(expr) + } else { + Binder::dummy(expr) + } + } + } + }; + } + + match *predicate { + PredicateKint::ForAll(_) => bug!("unexpected PredicateKint: {:?}", self), + PredicateKint::Trait(data, ct) => PredicateKind::Trait(bind!(data), ct), + PredicateKint::RegionOutlives(data) => PredicateKind::RegionOutlives(bind!(data)), + PredicateKint::TypeOutlives(data) => PredicateKind::TypeOutlives(bind!(data)), + PredicateKint::Projection(data) => PredicateKind::Projection(bind!(data)), + PredicateKint::WellFormed(arg) => { + if in_binder { + bug!("unexpected ForAll: {:?}", self) + } else { + PredicateKind::WellFormed(arg) + } + } + PredicateKint::ObjectSafe(def_id) => { + if in_binder { + bug!("unexpected ForAll: {:?}", self) + } else { + PredicateKind::ObjectSafe(def_id) + } + } + PredicateKint::ClosureKind(def_id, substs, kind) => { + if in_binder { + bug!("unexpected ForAll: {:?}", self) + } else { + PredicateKind::ClosureKind(def_id, substs, kind) + } + } + PredicateKint::Subtype(data) => PredicateKind::Subtype(bind!(data)), + PredicateKint::ConstEvaluatable(def_id, substs) => { + if in_binder { + bug!("unexpected ForAll: {:?}", self) + } else { + PredicateKind::ConstEvaluatable(def_id, substs) + } + } + PredicateKint::ConstEquate(l, r) => { + if in_binder { + bug!("unexpected ForAll: {:?}", self) + } else { + PredicateKind::ConstEquate(l, r) + } + } + } + .to_predicate(tcx) + } +} + impl<'tcx> ToPredicate<'tcx> for ConstnessAnd> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { ty::PredicateKind::Trait( diff --git a/src/librustc_middle/ty/structural_impls.rs b/src/librustc_middle/ty/structural_impls.rs index f04bfe648fb78..31d221288bbd6 100644 --- a/src/librustc_middle/ty/structural_impls.rs +++ b/src/librustc_middle/ty/structural_impls.rs @@ -1028,6 +1028,17 @@ impl> PredicateVisitor<'tcx> for T { } } +impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::PredicateKint<'tcx> { + fn super_fold_with>(&self, folder: &mut F) -> Self { + let new = ty::PredicateKint::super_fold_with(self, folder); + folder.tcx().intern_predicate_kint(new) + } + + fn super_visit_with>(&self, visitor: &mut V) -> bool { + ty::PredicateKint::super_visit_with(self, visitor) + } +} + impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List> { fn super_fold_with>(&self, folder: &mut F) -> Self { fold_list(*self, folder, |tcx, v| tcx.intern_predicates(v)) From 9a33b5915467259b4487adbc451422134003f223 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sat, 13 Jun 2020 15:04:28 +0200 Subject: [PATCH 02/28] minimal --- src/librustc_middle/ty/context.rs | 12 +- src/librustc_middle/ty/mod.rs | 2 + .../traits/fulfill.rs | 108 +++++++----------- 3 files changed, 44 insertions(+), 78 deletions(-) diff --git a/src/librustc_middle/ty/context.rs b/src/librustc_middle/ty/context.rs index 08620f82536e2..6f22a0f8ce31e 100644 --- a/src/librustc_middle/ty/context.rs +++ b/src/librustc_middle/ty/context.rs @@ -19,9 +19,9 @@ use crate::ty::TyKind::*; use crate::ty::{ self, query, AdtDef, AdtKind, BindingMode, BoundVar, CanonicalPolyFnSig, Const, ConstVid, DefIdTree, ExistentialPredicate, FloatVar, FloatVid, GenericParamDefKind, InferConst, InferTy, - IntVar, IntVid, List, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKint, - ProjectionTy, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, - TyVid, TypeAndMut, + IntVar, IntVid, List, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKind, + PredicateKint, ProjectionTy, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, + TyS, TyVar, TyVid, TypeAndMut, }; use rustc_ast::ast; use rustc_ast::expand::allocator::AllocatorKind; @@ -2030,12 +2030,6 @@ impl<'tcx> Borrow> for Interned<'tcx, Const<'tcx>> { } } -impl<'tcx> Borrow> for Interned<'tcx, PredicateKind<'tcx>> { - fn borrow<'a>(&'a self) -> &'a PredicateKind<'tcx> { - &self.0 - } -} - impl<'tcx> Borrow> for Interned<'tcx, PredicateKint<'tcx>> { fn borrow<'a>(&'a self) -> &'a PredicateKint<'tcx> { &self.0 diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index 36796e94973fa..1afdc153fe551 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -1063,7 +1063,9 @@ impl<'a, 'tcx> HashStable> for Predicate<'tcx> { kind.hash_stable(hcx, hasher); } +} +impl<'tcx> Predicate<'tcx> { pub fn kint(self, tcx: TyCtxt<'tcx>) -> &'tcx PredicateKint<'tcx> { // I am efficient tcx.intern_predicate_kint(match *self.kind() { diff --git a/src/librustc_trait_selection/traits/fulfill.rs b/src/librustc_trait_selection/traits/fulfill.rs index c6c76028f857c..c09eebc22c09d 100644 --- a/src/librustc_trait_selection/traits/fulfill.rs +++ b/src/librustc_trait_selection/traits/fulfill.rs @@ -6,7 +6,7 @@ use rustc_errors::ErrorReported; use rustc_infer::traits::{TraitEngine, TraitEngineExt as _}; use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::ty::error::ExpectedFound; -use rustc_middle::ty::{self, Const, ToPolyTraitRef, Ty, TypeFoldable}; +use rustc_middle::ty::{self, Binder, Const, ToPredicate, Ty, TypeFoldable}; use std::marker::PhantomData; use super::project; @@ -317,9 +317,15 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { let infcx = self.selcx.infcx(); - match obligation.predicate.kind() { - ty::PredicateKind::Trait(ref data, _) => { - let trait_obligation = obligation.with(*data); + match obligation.predicate.kint(infcx.tcx) { + ty::PredicateKint::ForAll(binder) => { + let (pred, _) = infcx.replace_bound_vars_with_placeholders(binder); + ProcessResult::Changed(mk_pending(vec![ + obligation.with(pred.to_predicate(infcx.tcx)), + ])) + } + ty::PredicateKint::Trait(ref data, _) => { + let trait_obligation = obligation.with(Binder::dummy(*data)); if obligation.predicate.is_global() { // no type variables present, can use evaluation for better caching. @@ -352,7 +358,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { // trait selection is because we don't have enough // information about the types in the trait. pending_obligation.stalled_on = - trait_ref_infer_vars(self.selcx, data.to_poly_trait_ref()); + trait_ref_infer_vars(self.selcx, data.trait_ref); debug!( "process_predicate: pending obligation {:?} now stalled on {:?}", @@ -373,64 +379,32 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { } } - &ty::PredicateKind::RegionOutlives(binder) => { - match infcx.region_outlives_predicate(&obligation.cause, binder) { + &ty::PredicateKint::RegionOutlives(data) => { + match infcx.region_outlives_predicate(&obligation.cause, Binder::dummy(data)) { Ok(()) => ProcessResult::Changed(vec![]), Err(_) => ProcessResult::Error(CodeSelectionError(Unimplemented)), } } - ty::PredicateKind::TypeOutlives(ref binder) => { - // Check if there are higher-ranked vars. - match binder.no_bound_vars() { - // If there are, inspect the underlying type further. - None => { - // Convert from `Binder>` to `Binder`. - let binder = binder.map_bound_ref(|pred| pred.0); - - // Check if the type has any bound vars. - match binder.no_bound_vars() { - // If so, this obligation is an error (for now). Eventually we should be - // able to support additional cases here, like `for<'a> &'a str: 'a`. - // NOTE: this is duplicate-implemented between here and fulfillment. - None => ProcessResult::Error(CodeSelectionError(Unimplemented)), - // Otherwise, we have something of the form - // `for<'a> T: 'a where 'a not in T`, which we can treat as - // `T: 'static`. - Some(t_a) => { - let r_static = self.selcx.tcx().lifetimes.re_static; - if self.register_region_obligations { - self.selcx.infcx().register_region_obligation_with_cause( - t_a, - r_static, - &obligation.cause, - ); - } - ProcessResult::Changed(vec![]) - } - } - } - // If there aren't, register the obligation. - Some(ty::OutlivesPredicate(t_a, r_b)) => { - if self.register_region_obligations { - self.selcx.infcx().register_region_obligation_with_cause( - t_a, - r_b, - &obligation.cause, - ); - } - ProcessResult::Changed(vec![]) - } + ty::PredicateKint::TypeOutlives(ty::OutlivesPredicate(t_a, r_b)) => { + if self.register_region_obligations { + self.selcx.infcx().register_region_obligation_with_cause( + t_a, + r_b, + &obligation.cause, + ); } + ProcessResult::Changed(vec![]) } - ty::PredicateKind::Projection(ref data) => { - let project_obligation = obligation.with(*data); + ty::PredicateKint::Projection(ref data) => { + let project_obligation = obligation.with(Binder::dummy(*data)); match project::poly_project_and_unify_type(self.selcx, &project_obligation) { Ok(None) => { - let tcx = self.selcx.tcx(); - pending_obligation.stalled_on = - trait_ref_infer_vars(self.selcx, data.to_poly_trait_ref(tcx)); + pending_obligation.stalled_on = trait_ref_infer_vars( + self.selcx, + data.projection_ty.trait_ref(infcx.tcx), + ); ProcessResult::Unchanged } Ok(Some(os)) => ProcessResult::Changed(mk_pending(os)), @@ -438,7 +412,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { } } - &ty::PredicateKind::ObjectSafe(trait_def_id) => { + &ty::PredicateKint::ObjectSafe(trait_def_id) => { if !self.selcx.tcx().is_object_safe(trait_def_id) { ProcessResult::Error(CodeSelectionError(Unimplemented)) } else { @@ -446,7 +420,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { } } - &ty::PredicateKind::ClosureKind(_, closure_substs, kind) => { + &ty::PredicateKint::ClosureKind(_, closure_substs, kind) => { match self.selcx.infcx().closure_kind(closure_substs) { Some(closure_kind) => { if closure_kind.extends(kind) { @@ -459,7 +433,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { } } - &ty::PredicateKind::WellFormed(arg) => { + &ty::PredicateKint::WellFormed(arg) => { match wf::obligations( self.selcx.infcx(), obligation.param_env, @@ -476,27 +450,24 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { } } - &ty::PredicateKind::Subtype(subtype) => { + &ty::PredicateKint::Subtype(subtype) => { match self.selcx.infcx().subtype_predicate( &obligation.cause, obligation.param_env, - subtype, + Binder::dummy(subtype), ) { None => { // None means that both are unresolved. pending_obligation.stalled_on = vec![ - TyOrConstInferVar::maybe_from_ty(subtype.skip_binder().a).unwrap(), - TyOrConstInferVar::maybe_from_ty(subtype.skip_binder().b).unwrap(), + TyOrConstInferVar::maybe_from_ty(subtype.a).unwrap(), + TyOrConstInferVar::maybe_from_ty(subtype.b).unwrap(), ]; ProcessResult::Unchanged } Some(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)), Some(Err(err)) => { - let expected_found = ExpectedFound::new( - subtype.skip_binder().a_is_expected, - subtype.skip_binder().a, - subtype.skip_binder().b, - ); + let expected_found = + ExpectedFound::new(subtype.a_is_expected, subtype.a, subtype.b); ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError( expected_found, err, @@ -505,7 +476,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { } } - &ty::PredicateKind::ConstEvaluatable(def_id, substs) => { + &ty::PredicateKint::ConstEvaluatable(def_id, substs) => { match self.selcx.infcx().const_eval_resolve( obligation.param_env, def_id, @@ -518,7 +489,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { } } - ty::PredicateKind::ConstEquate(c1, c2) => { + ty::PredicateKint::ConstEquate(c1, c2) => { debug!("equating consts: c1={:?} c2={:?}", c1, c2); let stalled_on = &mut pending_obligation.stalled_on; @@ -601,12 +572,11 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { /// Returns the set of inference variables contained in a trait ref. fn trait_ref_infer_vars<'a, 'tcx>( selcx: &mut SelectionContext<'a, 'tcx>, - trait_ref: ty::PolyTraitRef<'tcx>, + trait_ref: ty::TraitRef<'tcx>, ) -> Vec> { selcx .infcx() .resolve_vars_if_possible(&trait_ref) - .skip_binder() // ok b/c this check doesn't care about regions .substs .iter() // FIXME(eddyb) try using `skip_current_subtree` to skip everything that From 1b33f39126c56192e7e35bc943bee9cca6960abd Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Sun, 14 Jun 2020 13:22:51 +0100 Subject: [PATCH 03/28] Handle trait/projection predicates with bound regions correctly --- .../traits/fulfill.rs | 200 ++++++++++++------ src/test/ui/issues/issue-26217.stderr | 2 +- 2 files changed, 132 insertions(+), 70 deletions(-) diff --git a/src/librustc_trait_selection/traits/fulfill.rs b/src/librustc_trait_selection/traits/fulfill.rs index c09eebc22c09d..a6f2fe78d28cc 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::{TraitEngine, TraitEngineExt as _}; +use rustc_infer::traits::{PolyTraitObligation, TraitEngine, TraitEngineExt as _}; use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::ty::error::ExpectedFound; use rustc_middle::ty::{self, Binder, Const, ToPredicate, Ty, TypeFoldable}; @@ -20,6 +20,7 @@ use super::{FulfillmentError, FulfillmentErrorCode}; use super::{ObligationCause, PredicateObligation}; use crate::traits::error_reporting::InferCtxtExt as _; +use crate::traits::project::PolyProjectionObligation; use crate::traits::query::evaluate_obligation::InferCtxtExt as _; impl<'tcx> ForestObligation for PendingPredicateObligation<'tcx> { @@ -318,65 +319,50 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { let infcx = self.selcx.infcx(); match obligation.predicate.kint(infcx.tcx) { - ty::PredicateKint::ForAll(binder) => { - let (pred, _) = infcx.replace_bound_vars_with_placeholders(binder); - ProcessResult::Changed(mk_pending(vec![ - obligation.with(pred.to_predicate(infcx.tcx)), - ])) - } - ty::PredicateKint::Trait(ref data, _) => { - let trait_obligation = obligation.with(Binder::dummy(*data)); - - if obligation.predicate.is_global() { - // no type variables present, can use evaluation for better caching. - // FIXME: consider caching errors too. - if infcx.predicate_must_hold_considering_regions(&obligation) { - debug!( - "selecting trait `{:?}` at depth {} evaluated to holds", - data, obligation.recursion_depth - ); - return ProcessResult::Changed(vec![]); - } + ty::PredicateKint::ForAll(binder) => match binder.skip_binder() { + // Evaluation will discard candidates using the leak check. + // This means we need to pass it the bound version of our + // predicate. + rustc_middle::ty::PredicateKint::Trait(trait_ref, _constness) => { + let trait_obligation = obligation.with(Binder::bind(*trait_ref)); + + self.process_trait_obligation( + obligation, + trait_obligation, + &mut pending_obligation.stalled_on, + ) } + rustc_middle::ty::PredicateKint::Projection(projection) => { + let project_obligation = obligation.with(Binder::bind(*projection)); - match self.selcx.select(&trait_obligation) { - Ok(Some(impl_source)) => { - debug!( - "selecting trait `{:?}` at depth {} yielded Ok(Some)", - data, obligation.recursion_depth - ); - ProcessResult::Changed(mk_pending(impl_source.nested_obligations())) - } - Ok(None) => { - debug!( - "selecting trait `{:?}` at depth {} yielded Ok(None)", - data, obligation.recursion_depth - ); - - // This is a bit subtle: for the most part, the - // only reason we can fail to make progress on - // trait selection is because we don't have enough - // information about the types in the trait. - pending_obligation.stalled_on = - trait_ref_infer_vars(self.selcx, data.trait_ref); - - debug!( - "process_predicate: pending obligation {:?} now stalled on {:?}", - infcx.resolve_vars_if_possible(obligation), - pending_obligation.stalled_on - ); - - ProcessResult::Unchanged - } - Err(selection_err) => { - info!( - "selecting trait `{:?}` at depth {} yielded Err", - data, obligation.recursion_depth - ); - - ProcessResult::Error(CodeSelectionError(selection_err)) - } + self.process_projection_obligation( + project_obligation, + &mut pending_obligation.stalled_on, + ) } + rustc_middle::ty::PredicateKint::RegionOutlives(_) + | rustc_middle::ty::PredicateKint::TypeOutlives(_) + | rustc_middle::ty::PredicateKint::WellFormed(_) + | rustc_middle::ty::PredicateKint::ObjectSafe(_) + | rustc_middle::ty::PredicateKint::ClosureKind(..) + | rustc_middle::ty::PredicateKint::Subtype(_) + | rustc_middle::ty::PredicateKint::ConstEvaluatable(..) + | rustc_middle::ty::PredicateKint::ConstEquate(..) + | rustc_middle::ty::PredicateKint::ForAll(_) => { + let (pred, _) = infcx.replace_bound_vars_with_placeholders(binder); + ProcessResult::Changed(mk_pending(vec![ + obligation.with(pred.to_predicate(infcx.tcx)), + ])) + } + }, + ty::PredicateKint::Trait(ref data, _) => { + let trait_obligation = obligation.with(Binder::dummy(*data)); + + self.process_trait_obligation( + obligation, + trait_obligation, + &mut pending_obligation.stalled_on, + ) } &ty::PredicateKint::RegionOutlives(data) => { @@ -399,17 +385,11 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { ty::PredicateKint::Projection(ref data) => { let project_obligation = obligation.with(Binder::dummy(*data)); - match project::poly_project_and_unify_type(self.selcx, &project_obligation) { - Ok(None) => { - pending_obligation.stalled_on = trait_ref_infer_vars( - self.selcx, - data.projection_ty.trait_ref(infcx.tcx), - ); - ProcessResult::Unchanged - } - Ok(Some(os)) => ProcessResult::Changed(mk_pending(os)), - Err(e) => ProcessResult::Error(CodeProjectionError(e)), - } + + self.process_projection_obligation( + project_obligation, + &mut pending_obligation.stalled_on, + ) } &ty::PredicateKint::ObjectSafe(trait_def_id) => { @@ -569,14 +549,96 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { } } +impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> { + fn process_trait_obligation( + &mut self, + obligation: &PredicateObligation<'tcx>, + trait_obligation: PolyTraitObligation<'tcx>, + stalled_on: &mut Vec>, + ) -> ProcessResult, FulfillmentErrorCode<'tcx>> { + let infcx = self.selcx.infcx(); + if obligation.predicate.is_global() { + // no type variables present, can use evaluation for better caching. + // FIXME: consider caching errors too. + if infcx.predicate_must_hold_considering_regions(obligation) { + debug!( + "selecting trait `{:?}` at depth {} evaluated to holds", + obligation.predicate, obligation.recursion_depth + ); + return ProcessResult::Changed(vec![]); + } + } + + match self.selcx.select(&trait_obligation) { + Ok(Some(impl_source)) => { + debug!( + "selecting trait `{:?}` at depth {} yielded Ok(Some)", + trait_obligation.predicate, obligation.recursion_depth + ); + ProcessResult::Changed(mk_pending(impl_source.nested_obligations())) + } + Ok(None) => { + debug!( + "selecting trait `{:?}` at depth {} yielded Ok(None)", + trait_obligation.predicate, obligation.recursion_depth + ); + + // This is a bit subtle: for the most part, the + // only reason we can fail to make progress on + // trait selection is because we don't have enough + // information about the types in the trait. + *stalled_on = trait_ref_infer_vars( + self.selcx, + trait_obligation.predicate.map_bound(|pred| pred.trait_ref), + ); + + debug!( + "process_predicate: pending obligation {:?} now stalled on {:?}", + infcx.resolve_vars_if_possible(obligation), + stalled_on + ); + + ProcessResult::Unchanged + } + Err(selection_err) => { + info!( + "selecting trait `{:?}` at depth {} yielded Err", + trait_obligation.predicate, obligation.recursion_depth + ); + + ProcessResult::Error(CodeSelectionError(selection_err)) + } + } + } + + fn process_projection_obligation( + &mut self, + project_obligation: PolyProjectionObligation<'tcx>, + stalled_on: &mut Vec>, + ) -> ProcessResult, FulfillmentErrorCode<'tcx>> { + match project::poly_project_and_unify_type(self.selcx, &project_obligation) { + Ok(None) => { + *stalled_on = trait_ref_infer_vars( + self.selcx, + project_obligation.predicate.to_poly_trait_ref(self.selcx.tcx()), + ); + ProcessResult::Unchanged + } + Ok(Some(os)) => ProcessResult::Changed(mk_pending(os)), + Err(e) => ProcessResult::Error(CodeProjectionError(e)), + } + } +} + /// Returns the set of inference variables contained in a trait ref. fn trait_ref_infer_vars<'a, 'tcx>( selcx: &mut SelectionContext<'a, 'tcx>, - trait_ref: ty::TraitRef<'tcx>, + trait_ref: ty::PolyTraitRef<'tcx>, ) -> Vec> { selcx .infcx() .resolve_vars_if_possible(&trait_ref) + .skip_binder() .substs .iter() // FIXME(eddyb) try using `skip_current_subtree` to skip everything that diff --git a/src/test/ui/issues/issue-26217.stderr b/src/test/ui/issues/issue-26217.stderr index be9da569f8be1..b1625536d4202 100644 --- a/src/test/ui/issues/issue-26217.stderr +++ b/src/test/ui/issues/issue-26217.stderr @@ -4,7 +4,7 @@ error[E0477]: the type `&'a i32` does not fulfill the required lifetime LL | foo::<&'a i32>(); | ^^^^^^^^^^^^^^ | - = note: type must satisfy the static lifetime + = note: type must outlive any other region error: aborting due to previous error From cd30894c2f7113bf79c7f66d30a774f11b315d3f Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Wed, 17 Jun 2020 00:13:00 +0200 Subject: [PATCH 04/28] anonymize_predicate --- src/librustc_infer/traits/util.rs | 37 +++++++++++++------------------ 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/src/librustc_infer/traits/util.rs b/src/librustc_infer/traits/util.rs index 4ae7e417a8f67..957c993c4bf32 100644 --- a/src/librustc_infer/traits/util.rs +++ b/src/librustc_infer/traits/util.rs @@ -10,41 +10,34 @@ pub fn anonymize_predicate<'tcx>( tcx: TyCtxt<'tcx>, pred: ty::Predicate<'tcx>, ) -> ty::Predicate<'tcx> { - let kind = pred.kind(); + let kind = pred.kint(tcx); let new = match kind { - &ty::PredicateKind::Trait(ref data, constness) => { - ty::PredicateKind::Trait(tcx.anonymize_late_bound_regions(data), constness) + ty::PredicateKint::ForAll(binder) => { + ty::PredicateKint::ForAll(tcx.anonymize_late_bound_regions(binder)) } + &ty::PredicateKint::Trait(data, constness) => ty::PredicateKint::Trait(data, constness), - ty::PredicateKind::RegionOutlives(data) => { - ty::PredicateKind::RegionOutlives(tcx.anonymize_late_bound_regions(data)) - } + &ty::PredicateKint::RegionOutlives(data) => ty::PredicateKint::RegionOutlives(data), - ty::PredicateKind::TypeOutlives(data) => { - ty::PredicateKind::TypeOutlives(tcx.anonymize_late_bound_regions(data)) - } + &ty::PredicateKint::TypeOutlives(data) => ty::PredicateKint::TypeOutlives(data), - ty::PredicateKind::Projection(data) => { - ty::PredicateKind::Projection(tcx.anonymize_late_bound_regions(data)) - } + &ty::PredicateKint::Projection(data) => ty::PredicateKint::Projection(data), - &ty::PredicateKind::WellFormed(data) => ty::PredicateKind::WellFormed(data), + &ty::PredicateKint::WellFormed(data) => ty::PredicateKint::WellFormed(data), - &ty::PredicateKind::ObjectSafe(data) => ty::PredicateKind::ObjectSafe(data), + &ty::PredicateKint::ObjectSafe(data) => ty::PredicateKint::ObjectSafe(data), - &ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => { - ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) + &ty::PredicateKint::ClosureKind(closure_def_id, closure_substs, kind) => { + ty::PredicateKint::ClosureKind(closure_def_id, closure_substs, kind) } - ty::PredicateKind::Subtype(data) => { - ty::PredicateKind::Subtype(tcx.anonymize_late_bound_regions(data)) - } + &ty::PredicateKint::Subtype(data) => ty::PredicateKint::Subtype(data), - &ty::PredicateKind::ConstEvaluatable(def_id, substs) => { - ty::PredicateKind::ConstEvaluatable(def_id, substs) + &ty::PredicateKint::ConstEvaluatable(def_id, substs) => { + ty::PredicateKint::ConstEvaluatable(def_id, substs) } - ty::PredicateKind::ConstEquate(c1, c2) => ty::PredicateKind::ConstEquate(c1, c2), + &ty::PredicateKint::ConstEquate(c1, c2) => ty::PredicateKint::ConstEquate(c1, c2), }; if new != *kind { new.to_predicate(tcx) } else { pred } From fb36c8bc80b4d8f071317adbb05337c9a0a2dbae Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Wed, 17 Jun 2020 10:46:52 +0200 Subject: [PATCH 05/28] query_outlives_constraints_into_obligations --- .../infer/canonical/query_response.rs | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/librustc_infer/infer/canonical/query_response.rs b/src/librustc_infer/infer/canonical/query_response.rs index 5cba581b9dffb..89add21a7cd3d 100644 --- a/src/librustc_infer/infer/canonical/query_response.rs +++ b/src/librustc_infer/infer/canonical/query_response.rs @@ -526,27 +526,31 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> { ) -> impl Iterator> + 'a + Captures<'tcx> { unsubstituted_region_constraints.iter().map(move |constraint| { let constraint = substitute_value(self.tcx, result_subst, constraint); - let ty::OutlivesPredicate(k1, r2) = constraint.skip_binder(); // restored below - Obligation::new( - cause.clone(), - param_env, - match k1.unpack() { - GenericArgKind::Lifetime(r1) => ty::PredicateKind::RegionOutlives( - ty::Binder::bind(ty::OutlivesPredicate(r1, r2)), - ) - .to_predicate(self.tcx), - GenericArgKind::Type(t1) => ty::PredicateKind::TypeOutlives(ty::Binder::bind( - ty::OutlivesPredicate(t1, r2), - )) - .to_predicate(self.tcx), - GenericArgKind::Const(..) => { - // Consts cannot outlive one another, so we don't expect to - // ecounter this branch. - span_bug!(cause.span, "unexpected const outlives {:?}", constraint); - } - }, - ) + let to_predicate = |ty::OutlivesPredicate(k1, r2): ty::OutlivesPredicate< + GenericArg<'tcx>, + ty::Region<'tcx>, + >| match k1.unpack() { + GenericArgKind::Lifetime(r1) => self.tcx.intern_predicate_kint( + ty::PredicateKint::RegionOutlives(ty::OutlivesPredicate(r1, r2)), + ), + GenericArgKind::Type(t1) => self.tcx.intern_predicate_kint( + ty::PredicateKint::TypeOutlives(ty::OutlivesPredicate(t1, r2)), + ), + GenericArgKind::Const(..) => { + // Consts cannot outlive one another, so we don't expect to + // ecounter this branch. + span_bug!(cause.span, "unexpected const outlives {:?}", constraint); + } + }; + + let predicate = if let Some(constraint) = constraint.no_bound_vars() { + to_predicate(constraint).to_predicate(self.tcx) + } else { + ty::PredicateKint::ForAll(constraint.map_bound(to_predicate)).to_predicate(self.tcx) + }; + + Obligation::new(cause.clone(), param_env, predicate) }) } From c1d244ffd881f59f1c8e5b7bdce38a1a80a0f7d6 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Wed, 17 Jun 2020 11:30:18 +0200 Subject: [PATCH 06/28] convert trivial predicates --- .../infer/canonical/query_response.rs | 6 ++---- src/librustc_infer/infer/combine.rs | 6 +++--- src/librustc_infer/infer/sub.rs | 4 ++-- src/librustc_infer/traits/util.rs | 8 ++++---- src/librustc_middle/ty/codec.rs | 4 ++-- src/librustc_mir/borrow_check/type_check/mod.rs | 14 +++++++------- .../traits/select/confirmation.rs | 2 +- src/librustc_trait_selection/traits/wf.rs | 2 +- src/librustc_traits/type_op.rs | 4 ++-- src/librustc_typeck/check/method/mod.rs | 2 +- src/librustc_typeck/check/mod.rs | 2 +- 11 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/librustc_infer/infer/canonical/query_response.rs b/src/librustc_infer/infer/canonical/query_response.rs index 89add21a7cd3d..c01759393dfa5 100644 --- a/src/librustc_infer/infer/canonical/query_response.rs +++ b/src/librustc_infer/infer/canonical/query_response.rs @@ -670,10 +670,8 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for QueryTypeRelatingDelegate<'_, 'tcx> { self.obligations.push(Obligation { cause: self.cause.clone(), param_env: self.param_env, - predicate: ty::PredicateKind::RegionOutlives(ty::Binder::dummy(ty::OutlivesPredicate( - sup, sub, - ))) - .to_predicate(self.infcx.tcx), + predicate: ty::PredicateKint::RegionOutlives(ty::OutlivesPredicate(sup, sub)) + .to_predicate(self.infcx.tcx), recursion_depth: 0, }); } diff --git a/src/librustc_infer/infer/combine.rs b/src/librustc_infer/infer/combine.rs index c63464e5baec9..e8ec5e651bb5d 100644 --- a/src/librustc_infer/infer/combine.rs +++ b/src/librustc_infer/infer/combine.rs @@ -308,7 +308,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> { self.obligations.push(Obligation::new( self.trace.cause.clone(), self.param_env, - ty::PredicateKind::WellFormed(b_ty.into()).to_predicate(self.infcx.tcx), + ty::PredicateKint::WellFormed(b_ty.into()).to_predicate(self.infcx.tcx), )); } @@ -400,9 +400,9 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> { b: &'tcx ty::Const<'tcx>, ) { let predicate = if a_is_expected { - ty::PredicateKind::ConstEquate(a, b) + ty::PredicateKint::ConstEquate(a, b) } else { - ty::PredicateKind::ConstEquate(b, a) + ty::PredicateKint::ConstEquate(b, a) }; self.obligations.push(Obligation::new( self.trace.cause.clone(), diff --git a/src/librustc_infer/infer/sub.rs b/src/librustc_infer/infer/sub.rs index d190f7e434298..50c1f491557fd 100644 --- a/src/librustc_infer/infer/sub.rs +++ b/src/librustc_infer/infer/sub.rs @@ -100,11 +100,11 @@ impl TypeRelation<'tcx> for Sub<'combine, 'infcx, 'tcx> { self.fields.obligations.push(Obligation::new( self.fields.trace.cause.clone(), self.fields.param_env, - ty::PredicateKind::Subtype(ty::Binder::dummy(ty::SubtypePredicate { + ty::PredicateKint::Subtype(ty::SubtypePredicate { a_is_expected: self.a_is_expected, a, b, - })) + }) .to_predicate(self.tcx()), )); diff --git a/src/librustc_infer/traits/util.rs b/src/librustc_infer/traits/util.rs index 957c993c4bf32..ddb7e7adbc6c5 100644 --- a/src/librustc_infer/traits/util.rs +++ b/src/librustc_infer/traits/util.rs @@ -234,16 +234,16 @@ impl Elaborator<'tcx> { if r.is_late_bound() { None } else { - Some(ty::PredicateKind::RegionOutlives(ty::Binder::dummy( - ty::OutlivesPredicate(r, r_min), + Some(ty::PredicateKint::RegionOutlives(ty::OutlivesPredicate( + r, r_min, ))) } } Component::Param(p) => { let ty = tcx.mk_ty_param(p.index, p.name); - Some(ty::PredicateKind::TypeOutlives(ty::Binder::dummy( - ty::OutlivesPredicate(ty, r_min), + Some(ty::PredicateKint::TypeOutlives(ty::OutlivesPredicate( + ty, r_min, ))) } diff --git a/src/librustc_middle/ty/codec.rs b/src/librustc_middle/ty/codec.rs index a7c7b16048039..b8e041a00ee17 100644 --- a/src/librustc_middle/ty/codec.rs +++ b/src/librustc_middle/ty/codec.rs @@ -39,7 +39,7 @@ impl<'tcx> EncodableWithShorthand for Ty<'tcx> { } impl<'tcx> EncodableWithShorthand for ty::Predicate<'tcx> { - type Variant = ty::PredicateKind<'tcx>; + type Variant = ty::PredicateKynd<'tcx>; fn variant(&self) -> &Self::Variant { self.kind() } @@ -195,7 +195,7 @@ where }) } else { let tcx = decoder.tcx(); - Ok(tcx.mk_predicate(ty::PredicateKind::decode(decoder)?)) + Ok(tcx.mk_predicate(ty::PredicateKynd::decode(decoder)?)) } } diff --git a/src/librustc_mir/borrow_check/type_check/mod.rs b/src/librustc_mir/borrow_check/type_check/mod.rs index bede9b22bbb0a..5491c3fb24867 100644 --- a/src/librustc_mir/borrow_check/type_check/mod.rs +++ b/src/librustc_mir/borrow_check/type_check/mod.rs @@ -27,8 +27,8 @@ use rustc_middle::ty::cast::CastTy; use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::subst::{GenericArgKind, Subst, SubstsRef, UserSubsts}; use rustc_middle::ty::{ - self, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, RegionVid, ToPolyTraitRef, - ToPredicate, Ty, TyCtxt, UserType, UserTypeAnnotationIndex, + self, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, RegionVid, ToPredicate, Ty, + TyCtxt, UserType, UserTypeAnnotationIndex, }; use rustc_span::{Span, DUMMY_SP}; use rustc_target::abi::VariantIdx; @@ -1021,7 +1021,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { } self.prove_predicate( - ty::PredicateKind::WellFormed(inferred_ty.into()).to_predicate(self.tcx()), + ty::PredicateKint::WellFormed(inferred_ty.into()).to_predicate(self.tcx()), Locations::All(span), ConstraintCategory::TypeAnnotation, ); @@ -1273,7 +1273,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { obligations.obligations.push(traits::Obligation::new( ObligationCause::dummy(), param_env, - ty::PredicateKind::WellFormed(revealed_ty.into()).to_predicate(infcx.tcx), + ty::PredicateKint::WellFormed(revealed_ty.into()).to_predicate(infcx.tcx), )); obligations.add( infcx @@ -1617,7 +1617,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { self.check_call_dest(body, term, &sig, destination, term_location); self.prove_predicates( - sig.inputs_and_output.iter().map(|ty| ty::PredicateKind::WellFormed(ty.into())), + sig.inputs_and_output.iter().map(|ty| ty::PredicateKint::WellFormed(ty.into())), term_location.to_locations(), ConstraintCategory::Boring, ); @@ -2706,8 +2706,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { category: ConstraintCategory, ) { self.prove_predicates( - Some(ty::PredicateKind::Trait( - trait_ref.to_poly_trait_ref().to_poly_trait_predicate(), + Some(ty::PredicateKint::Trait( + ty::TraitPredicate { trait_ref }, hir::Constness::NotConst, )), locations, diff --git a/src/librustc_trait_selection/traits/select/confirmation.rs b/src/librustc_trait_selection/traits/select/confirmation.rs index fa970589bbbf6..b9f39fd23cbb6 100644 --- a/src/librustc_trait_selection/traits/select/confirmation.rs +++ b/src/librustc_trait_selection/traits/select/confirmation.rs @@ -532,7 +532,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { obligations.push(Obligation::new( obligation.cause.clone(), obligation.param_env, - ty::PredicateKind::ClosureKind(closure_def_id, substs, kind) + ty::PredicateKint::ClosureKind(closure_def_id, substs, kind) .to_predicate(self.tcx()), )); } diff --git a/src/librustc_trait_selection/traits/wf.rs b/src/librustc_trait_selection/traits/wf.rs index 0e445e1e53bba..4eb1d1856fbd7 100644 --- a/src/librustc_trait_selection/traits/wf.rs +++ b/src/librustc_trait_selection/traits/wf.rs @@ -316,7 +316,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { traits::Obligation::new( new_cause, param_env, - ty::PredicateKind::WellFormed(arg).to_predicate(tcx), + ty::PredicateKint::WellFormed(arg).to_predicate(tcx), ) }), ); diff --git a/src/librustc_traits/type_op.rs b/src/librustc_traits/type_op.rs index 9cc9a35b38b8a..309de9bddb71e 100644 --- a/src/librustc_traits/type_op.rs +++ b/src/librustc_traits/type_op.rs @@ -140,7 +140,7 @@ impl AscribeUserTypeCx<'me, 'tcx> { self.relate(self_ty, Variance::Invariant, impl_self_ty)?; self.prove_predicate( - ty::PredicateKind::WellFormed(impl_self_ty.into()).to_predicate(self.tcx()), + ty::PredicateKint::WellFormed(impl_self_ty.into()).to_predicate(self.tcx()), ); } @@ -155,7 +155,7 @@ impl AscribeUserTypeCx<'me, 'tcx> { // them? This would only be relevant if some input // type were ill-formed but did not appear in `ty`, // which...could happen with normalization... - self.prove_predicate(ty::PredicateKind::WellFormed(ty.into()).to_predicate(self.tcx())); + self.prove_predicate(ty::PredicateKint::WellFormed(ty.into()).to_predicate(self.tcx())); Ok(()) } } diff --git a/src/librustc_typeck/check/method/mod.rs b/src/librustc_typeck/check/method/mod.rs index 64dce3e1738e3..ee27b581311ea 100644 --- a/src/librustc_typeck/check/method/mod.rs +++ b/src/librustc_typeck/check/method/mod.rs @@ -399,7 +399,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { obligations.push(traits::Obligation::new( cause, self.param_env, - ty::PredicateKind::WellFormed(method_ty.into()).to_predicate(tcx), + ty::PredicateKint::WellFormed(method_ty.into()).to_predicate(tcx), )); let callee = MethodCallee { def_id, substs: trait_ref.substs, sig: fn_sig }; diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 6e77aba30506a..7a96d6c678ad2 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3612,7 +3612,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.register_predicate(traits::Obligation::new( cause, self.param_env, - ty::PredicateKind::WellFormed(arg).to_predicate(self.tcx), + ty::PredicateKint::WellFormed(arg).to_predicate(self.tcx), )); } From 653f56af53c01c38f34f0926dd13ee391a290fa0 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Wed, 17 Jun 2020 22:50:28 +0200 Subject: [PATCH 07/28] wf --- src/librustc_trait_selection/traits/wf.rs | 36 ++++++++++++----------- src/librustc_typeck/check/wfcheck.rs | 2 +- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/librustc_trait_selection/traits/wf.rs b/src/librustc_trait_selection/traits/wf.rs index 4eb1d1856fbd7..333f90d78a79e 100644 --- a/src/librustc_trait_selection/traits/wf.rs +++ b/src/librustc_trait_selection/traits/wf.rs @@ -88,33 +88,35 @@ pub fn predicate_obligations<'a, 'tcx>( infcx: &InferCtxt<'a, 'tcx>, param_env: ty::ParamEnv<'tcx>, body_id: hir::HirId, - predicate: ty::Predicate<'tcx>, + predicate: &'tcx ty::PredicateKint<'tcx>, span: Span, ) -> Vec> { let mut wf = WfPredicates { infcx, param_env, body_id, span, out: vec![], item: None }; - // (*) ok to skip binders, because wf code is prepared for it - match predicate.kind() { - ty::PredicateKind::Trait(t, _) => { - wf.compute_trait_ref(&t.skip_binder().trait_ref, Elaborate::None); // (*) + match predicate { + ty::PredicateKint::ForAll(binder) => { + // It's ok to skip the binder here because wf code is prepared for it + return predicate_obligations(infcx, param_env, body_id, *binder.skip_binder(), span); } - ty::PredicateKind::RegionOutlives(..) => {} - ty::PredicateKind::TypeOutlives(t) => { - wf.compute(t.skip_binder().0.into()); + ty::PredicateKint::Trait(t, _) => { + wf.compute_trait_ref(&t.trait_ref, Elaborate::None); } - ty::PredicateKind::Projection(t) => { - let t = t.skip_binder(); // (*) + ty::PredicateKint::RegionOutlives(..) => {} + &ty::PredicateKint::TypeOutlives(ty::OutlivesPredicate(ty, _reg)) => { + wf.compute(ty.into()); + } + ty::PredicateKint::Projection(t) => { wf.compute_projection(t.projection_ty); wf.compute(t.ty.into()); } - &ty::PredicateKind::WellFormed(arg) => { + &ty::PredicateKint::WellFormed(arg) => { wf.compute(arg); } - ty::PredicateKind::ObjectSafe(_) => {} - ty::PredicateKind::ClosureKind(..) => {} - ty::PredicateKind::Subtype(data) => { - wf.compute(data.skip_binder().a.into()); // (*) - wf.compute(data.skip_binder().b.into()); // (*) + ty::PredicateKint::ObjectSafe(_) => {} + ty::PredicateKint::ClosureKind(..) => {} + &ty::PredicateKint::Subtype(ty::SubtypePredicate { a, b, a_is_expected: _ }) => { + wf.compute(a.into()); + wf.compute(b.into()); } &ty::PredicateKind::ConstEvaluatable(def, substs) => { let obligations = wf.nominal_obligations(def.did, substs); @@ -124,7 +126,7 @@ pub fn predicate_obligations<'a, 'tcx>( wf.compute(arg); } } - &ty::PredicateKind::ConstEquate(c1, c2) => { + &ty::PredicateKint::ConstEquate(c1, c2) => { wf.compute(c1.into()); wf.compute(c2.into()); } diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index dabae6cbc4137..aa40d7de6e005 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -829,7 +829,7 @@ fn check_where_clauses<'tcx, 'fcx>( assert_eq!(predicates.predicates.len(), predicates.spans.len()); let wf_obligations = predicates.predicates.iter().zip(predicates.spans.iter()).flat_map(|(&p, &sp)| { - traits::wf::predicate_obligations(fcx, fcx.param_env, fcx.body_id, p, sp) + traits::wf::predicate_obligations(fcx, fcx.param_env, fcx.body_id, p.kint(tcx), sp) }); for obligation in wf_obligations.chain(default_obligations) { From 1fda8c207e083acabace5a66d16824a52b43061d Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Wed, 17 Jun 2020 23:20:17 +0200 Subject: [PATCH 08/28] somewhat related cleanup --- src/librustc_middle/ty/mod.rs | 9 +-------- src/librustc_trait_selection/traits/util.rs | 6 +++--- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index 1afdc153fe551..b72dd6382199a 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -1302,7 +1302,7 @@ impl<'tcx> Predicate<'tcx> { // from the substitution and the value being substituted into, and // this trick achieves that). - let substs = &trait_ref.skip_binder().substs; + let substs = trait_ref.skip_binder().substs; let kind = self.kind(); let new = match kind { &PredicateKind::Trait(ref binder, constness) => { @@ -1555,13 +1555,6 @@ impl<'tcx> ToPredicate<'tcx> for ConstnessAnd> { } } -impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<&PolyTraitRef<'tcx>> { - fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { - ty::PredicateKind::Trait(self.value.to_poly_trait_predicate(), self.constness) - .to_predicate(tcx) - } -} - impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { PredicateKind::RegionOutlives(self).to_predicate(tcx) diff --git a/src/librustc_trait_selection/traits/util.rs b/src/librustc_trait_selection/traits/util.rs index d3484b8af89fd..cc8997078e0f0 100644 --- a/src/librustc_trait_selection/traits/util.rs +++ b/src/librustc_trait_selection/traits/util.rs @@ -59,8 +59,8 @@ impl<'tcx> TraitAliasExpansionInfo<'tcx> { ); } - pub fn trait_ref(&self) -> &ty::PolyTraitRef<'tcx> { - &self.top().0 + pub fn trait_ref(&self) -> ty::PolyTraitRef<'tcx> { + self.top().0 } pub fn top(&self) -> &(ty::PolyTraitRef<'tcx>, Span) { @@ -109,7 +109,7 @@ impl<'tcx> TraitAliasExpander<'tcx> { // Don't recurse if this trait alias is already on the stack for the DFS search. let anon_pred = anonymize_predicate(tcx, pred); - if item.path.iter().rev().skip(1).any(|(tr, _)| { + if item.path.iter().rev().skip(1).any(|&(tr, _)| { anonymize_predicate(tcx, tr.without_const().to_predicate(tcx)) == anon_pred }) { return false; From 7f39b0c9ab7c2c3beeb9dd3a54f51b3a5f7e21a0 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Thu, 18 Jun 2020 00:18:58 +0200 Subject: [PATCH 09/28] subst_supertrait --- src/librustc_infer/traits/util.rs | 4 +-- src/librustc_middle/ty/mod.rs | 41 +++++++++---------------------- 2 files changed, 14 insertions(+), 31 deletions(-) diff --git a/src/librustc_infer/traits/util.rs b/src/librustc_infer/traits/util.rs index ddb7e7adbc6c5..9dfe23b5aad8d 100644 --- a/src/librustc_infer/traits/util.rs +++ b/src/librustc_infer/traits/util.rs @@ -156,10 +156,10 @@ impl Elaborator<'tcx> { // Get predicates declared on the trait. let predicates = tcx.super_predicates_of(data.def_id()); - let obligations = predicates.predicates.iter().map(|(pred, span)| { + let obligations = predicates.predicates.iter().map(|&(pred, span)| { predicate_obligation( pred.subst_supertrait(tcx, &data.to_poly_trait_ref()), - Some(*span), + Some(span), ) }); debug!("super_predicates: data={:?}", data); diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index b72dd6382199a..ac1642ec86611 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -1303,37 +1303,20 @@ impl<'tcx> Predicate<'tcx> { // this trick achieves that). let substs = trait_ref.skip_binder().substs; - let kind = self.kind(); - let new = match kind { - &PredicateKind::Trait(ref binder, constness) => { - PredicateKind::Trait(binder.map_bound(|data| data.subst(tcx, substs)), constness) - } - PredicateKind::Subtype(binder) => { - PredicateKind::Subtype(binder.map_bound(|data| data.subst(tcx, substs))) - } - PredicateKind::RegionOutlives(binder) => { - PredicateKind::RegionOutlives(binder.map_bound(|data| data.subst(tcx, substs))) - } - PredicateKind::TypeOutlives(binder) => { - PredicateKind::TypeOutlives(binder.map_bound(|data| data.subst(tcx, substs))) - } - PredicateKind::Projection(binder) => { - PredicateKind::Projection(binder.map_bound(|data| data.subst(tcx, substs))) - } - &PredicateKind::WellFormed(data) => PredicateKind::WellFormed(data.subst(tcx, substs)), - &PredicateKind::ObjectSafe(trait_def_id) => PredicateKind::ObjectSafe(trait_def_id), - &PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => { - PredicateKind::ClosureKind(closure_def_id, closure_substs.subst(tcx, substs), kind) - } - &PredicateKind::ConstEvaluatable(def_id, const_substs) => { - PredicateKind::ConstEvaluatable(def_id, const_substs.subst(tcx, substs)) - } - PredicateKind::ConstEquate(c1, c2) => { - PredicateKind::ConstEquate(c1.subst(tcx, substs), c2.subst(tcx, substs)) - } + let kind = match self.kint(tcx) { + PredicateKint::ForAll(binder) => *binder.skip_binder(), + kind => kind, + }; + + let new = kind.subst(tcx, substs); + + let rebound = if new.has_escaping_bound_vars() { + PredicateKint::ForAll(Binder::bind(tcx.intern_predicate_kint(new))) + } else { + new }; - if new != *kind { new.to_predicate(tcx) } else { self } + if rebound != *kind { rebound.to_predicate(tcx) } else { self } } } From 4c3b1e89cff55b1f337c5c95c4988ea5a82e2441 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Thu, 18 Jun 2020 17:43:26 +0200 Subject: [PATCH 10/28] elaborate --- src/librustc_infer/traits/util.rs | 34 +++++++++++++---------- src/librustc_trait_selection/traits/wf.rs | 8 ++---- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/librustc_infer/traits/util.rs b/src/librustc_infer/traits/util.rs index 9dfe23b5aad8d..02a30195cc8e8 100644 --- a/src/librustc_infer/traits/util.rs +++ b/src/librustc_infer/traits/util.rs @@ -151,14 +151,22 @@ impl Elaborator<'tcx> { fn elaborate(&mut self, obligation: &PredicateObligation<'tcx>) { let tcx = self.visited.tcx; - match obligation.predicate.kind() { - ty::PredicateKind::Trait(ref data, _) => { + let pred = match obligation.predicate.kint(tcx) { + // We have to be careful and rebind this whenever + // dealing with a predicate further down. + ty::PredicateKint::ForAll(binder) => binder.skip_binder(), + pred => pred, + }; + + match pred { + ty::PredicateKint::ForAll(_) => bug!("unexpected predicate: {:?}", pred), + ty::PredicateKint::Trait(ref data, _) => { // Get predicates declared on the trait. let predicates = tcx.super_predicates_of(data.def_id()); let obligations = predicates.predicates.iter().map(|&(pred, span)| { predicate_obligation( - pred.subst_supertrait(tcx, &data.to_poly_trait_ref()), + pred.subst_supertrait(tcx, &ty::Binder::bind(data.trait_ref)), Some(span), ) }); @@ -173,36 +181,36 @@ impl Elaborator<'tcx> { self.stack.extend(obligations); } - ty::PredicateKind::WellFormed(..) => { + ty::PredicateKint::WellFormed(..) => { // Currently, we do not elaborate WF predicates, // although we easily could. } - ty::PredicateKind::ObjectSafe(..) => { + ty::PredicateKint::ObjectSafe(..) => { // Currently, we do not elaborate object-safe // predicates. } - ty::PredicateKind::Subtype(..) => { + ty::PredicateKint::Subtype(..) => { // Currently, we do not "elaborate" predicates like `X <: Y`, // though conceivably we might. } - ty::PredicateKind::Projection(..) => { + ty::PredicateKint::Projection(..) => { // Nothing to elaborate in a projection predicate. } - ty::PredicateKind::ClosureKind(..) => { + ty::PredicateKint::ClosureKind(..) => { // Nothing to elaborate when waiting for a closure's kind to be inferred. } - ty::PredicateKind::ConstEvaluatable(..) => { + ty::PredicateKint::ConstEvaluatable(..) => { // Currently, we do not elaborate const-evaluatable // predicates. } - ty::PredicateKind::ConstEquate(..) => { + ty::PredicateKint::ConstEquate(..) => { // Currently, we do not elaborate const-equate // predicates. } - ty::PredicateKind::RegionOutlives(..) => { + ty::PredicateKint::RegionOutlives(..) => { // Nothing to elaborate from `'a: 'b`. } - ty::PredicateKind::TypeOutlives(ref data) => { + ty::PredicateKint::TypeOutlives(ty::OutlivesPredicate(ty_max, r_min)) => { // We know that `T: 'a` for some type `T`. We can // often elaborate this. For example, if we know that // `[U]: 'a`, that implies that `U: 'a`. Similarly, if @@ -217,8 +225,6 @@ impl Elaborator<'tcx> { // consider this as evidence that `T: 'static`, but // I'm a bit wary of such constructions and so for now // I want to be conservative. --nmatsakis - let ty_max = data.skip_binder().0; - let r_min = data.skip_binder().1; if r_min.is_late_bound() { return; } diff --git a/src/librustc_trait_selection/traits/wf.rs b/src/librustc_trait_selection/traits/wf.rs index 333f90d78a79e..b72359129e9b1 100644 --- a/src/librustc_trait_selection/traits/wf.rs +++ b/src/librustc_trait_selection/traits/wf.rs @@ -397,7 +397,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { self.out.push(traits::Obligation::new( cause, self.param_env, - ty::PredicateKind::WellFormed(resolved_constant.into()) + ty::PredicateKint::WellFormed(resolved_constant.into()) .to_predicate(self.tcx()), )); } @@ -483,10 +483,8 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { self.out.push(traits::Obligation::new( cause, param_env, - ty::PredicateKind::TypeOutlives(ty::Binder::dummy( - ty::OutlivesPredicate(rty, r), - )) - .to_predicate(self.tcx()), + ty::PredicateKint::TypeOutlives(ty::OutlivesPredicate(rty, r)) + .to_predicate(self.tcx()), )); } } From 506f4308b757e64ceb9e894e27265c103593cd9e Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Thu, 18 Jun 2020 20:33:52 +0200 Subject: [PATCH 11/28] progress --- src/librustc_infer/traits/util.rs | 6 +-- src/librustc_lint/builtin.rs | 7 ++- src/librustc_lint/unused.rs | 8 +-- src/librustc_middle/ty/mod.rs | 49 ++++++++++++------- src/librustc_middle/ty/sty.rs | 3 +- .../borrow_check/type_check/mod.rs | 22 ++++----- src/librustc_privacy/lib.rs | 5 +- 7 files changed, 54 insertions(+), 46 deletions(-) diff --git a/src/librustc_infer/traits/util.rs b/src/librustc_infer/traits/util.rs index 02a30195cc8e8..2a09d9dbee399 100644 --- a/src/librustc_infer/traits/util.rs +++ b/src/librustc_infer/traits/util.rs @@ -3,7 +3,7 @@ use smallvec::smallvec; use crate::traits::{Obligation, ObligationCause, PredicateObligation}; use rustc_data_structures::fx::FxHashSet; use rustc_middle::ty::outlives::Component; -use rustc_middle::ty::{self, ToPolyTraitRef, ToPredicate, TyCtxt, WithConstness}; +use rustc_middle::ty::{self, ToPredicate, TyCtxt, WithConstness}; use rustc_span::Span; pub fn anonymize_predicate<'tcx>( @@ -330,8 +330,8 @@ impl<'tcx, I: Iterator>> Iterator for FilterToT fn next(&mut self) -> Option> { while let Some(obligation) = self.base_iterator.next() { - if let ty::PredicateKind::Trait(data, _) = obligation.predicate.kind() { - return Some(data.to_poly_trait_ref()); + if let Some(data) = obligation.predicate.to_opt_poly_trait_ref() { + return Some(data); } } None diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 5c4be715add79..a9490c954b55e 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -1202,13 +1202,15 @@ declare_lint_pass!( impl<'tcx> LateLintPass<'tcx> for TrivialConstraints { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) { use rustc_middle::ty::fold::TypeFoldable; - use rustc_middle::ty::PredicateKind::*; + use rustc_middle::ty::PredicateKint::*; if cx.tcx.features().trivial_bounds { let def_id = cx.tcx.hir().local_def_id(item.hir_id); let predicates = cx.tcx.predicates_of(def_id); for &(predicate, span) in predicates.predicates { - let predicate_kind_name = match predicate.kind() { + // We don't actually look inside of the predicate, + // so it is safe to skip this binder here. + let predicate_kind_name = match predicate.kint(cx.tcx).ignore_qualifiers().skip_binder() { Trait(..) => "Trait", TypeOutlives(..) | RegionOutlives(..) => "Lifetime", @@ -1223,6 +1225,7 @@ impl<'tcx> LateLintPass<'tcx> for TrivialConstraints { Subtype(..) | ConstEvaluatable(..) | ConstEquate(..) => continue, + ForAll(_) => bug!("unexpected predicate: {:?}", predicate) }; if predicate.is_global() { cx.struct_span_lint(TRIVIAL_BOUNDS, span, |lint| { diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index 6d6c7b24101ca..62127f3e12ca1 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -146,11 +146,11 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { ty::Opaque(def, _) => { let mut has_emitted = false; for (predicate, _) in cx.tcx.predicates_of(def).predicates { - if let ty::PredicateKind::Trait(ref poly_trait_predicate, _) = - predicate.kind() + // We only look at the `DefId`, so it is safe to skip the binder here. + if let ty::PredicateKint::Trait(ref poly_trait_predicate, _) = + predicate.kint(cx.tcx).ignore_qualifiers().skip_binder() { - let trait_ref = poly_trait_predicate.skip_binder().trait_ref; - let def_id = trait_ref.def_id; + let def_id = poly_trait_predicate.trait_ref.def_id; let descr_pre = &format!("{}implementer{} of ", descr_pre, plural_suffix,); if check_must_use_def(cx, def_id, span, descr_pre, descr_post) { diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index ac1642ec86611..b8f9550126247 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -1217,6 +1217,16 @@ pub enum PredicateKind<'tcx> { ConstEquate(&'tcx Const<'tcx>, &'tcx Const<'tcx>), } +impl<'tcx> PredicateKint<'tcx> { + /// Skips `PredicateKint::ForAll`. + pub fn ignore_qualifiers(&'tcx self) -> Binder<&'tcx PredicateKint<'tcx>> { + match self { + &PredicateKint::ForAll(binder) => binder, + pred => Binder::dummy(pred), + } + } +} + /// The crate outlives map is computed during typeck and contains the /// outlives of every item in the local crate. You should not use it /// directly, because to do so will make your pass dependent on the @@ -1513,34 +1523,37 @@ impl ToPredicate<'tcx> for PredicateKint<'tcx> { impl<'tcx> ToPredicate<'tcx> for ConstnessAnd> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { - ty::PredicateKind::Trait( - ty::Binder::dummy(ty::TraitPredicate { trait_ref: self.value }), - self.constness, - ) - .to_predicate(tcx) - } -} - -impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<&TraitRef<'tcx>> { - fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { - ty::PredicateKind::Trait( - ty::Binder::dummy(ty::TraitPredicate { trait_ref: *self.value }), - self.constness, - ) - .to_predicate(tcx) + ty::PredicateKint::Trait(ty::TraitPredicate { trait_ref: self.value }, self.constness) + .to_predicate(tcx) } } impl<'tcx> ToPredicate<'tcx> for ConstnessAnd> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { - ty::PredicateKind::Trait(self.value.to_poly_trait_predicate(), self.constness) - .to_predicate(tcx) + if let Some(trait_ref) = self.value.no_bound_vars() { + ty::PredicateKint::Trait(ty::TraitPredicate { trait_ref }, self.constness) + } else { + ty::PredicateKint::ForAll(self.value.map_bound(|trait_ref| { + tcx.intern_predicate_kint(ty::PredicateKint::Trait( + ty::TraitPredicate { trait_ref }, + self.constness, + )) + })) + } + .to_predicate(tcx) } } impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { - PredicateKind::RegionOutlives(self).to_predicate(tcx) + if let Some(outlives) = self.no_bound_vars() { + PredicateKint::RegionOutlives(outlives) + } else { + ty::PredicateKint::ForAll(self.map_bound(|outlives| { + tcx.intern_predicate_kint(PredicateKint::RegionOutlives(outlives)) + })) + } + .to_predicate(tcx) } } diff --git a/src/librustc_middle/ty/sty.rs b/src/librustc_middle/ty/sty.rs index 03bf51c95c5a3..cf9d40a6dc6c5 100644 --- a/src/librustc_middle/ty/sty.rs +++ b/src/librustc_middle/ty/sty.rs @@ -652,8 +652,7 @@ impl<'tcx> Binder> { Binder(tr).with_self_ty(tcx, self_ty).without_const().to_predicate(tcx) } ExistentialPredicate::Projection(p) => { - ty::PredicateKind::Projection(Binder(p.with_self_ty(tcx, self_ty))) - .to_predicate(tcx) + Binder(p.with_self_ty(tcx, self_ty)).to_predicate(tcx) } ExistentialPredicate::AutoTrait(did) => { let trait_ref = diff --git a/src/librustc_mir/borrow_check/type_check/mod.rs b/src/librustc_mir/borrow_check/type_check/mod.rs index 5491c3fb24867..9fc5dc0d131c6 100644 --- a/src/librustc_mir/borrow_check/type_check/mod.rs +++ b/src/librustc_mir/borrow_check/type_check/mod.rs @@ -28,7 +28,7 @@ use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::subst::{GenericArgKind, Subst, SubstsRef, UserSubsts}; use rustc_middle::ty::{ self, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, RegionVid, ToPredicate, Ty, - TyCtxt, UserType, UserTypeAnnotationIndex, + TyCtxt, UserType, UserTypeAnnotationIndex, WithConstness, }; use rustc_span::{Span, DUMMY_SP}; use rustc_target::abi::VariantIdx; @@ -2022,18 +2022,14 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { traits::ObligationCauseCode::RepeatVec(should_suggest), ), self.param_env, - ty::PredicateKind::Trait( - ty::Binder::bind(ty::TraitPredicate { - trait_ref: ty::TraitRef::new( - self.tcx().require_lang_item( - CopyTraitLangItem, - Some(self.last_span), - ), - tcx.mk_substs_trait(ty, &[]), - ), - }), - hir::Constness::NotConst, - ) + ty::Binder::bind(ty::TraitRef::new( + self.tcx().require_lang_item( + CopyTraitLangItem, + Some(self.last_span), + ), + tcx.mk_substs_trait(ty, &[]), + )) + .without_const() .to_predicate(self.tcx()), ), &traits::SelectionError::Unimplemented, diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index 1ecb0320744ff..0b97695041be7 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -68,10 +68,7 @@ trait DefIdVisitor<'tcx> { } } -struct DefIdVisitorSkeleton<'v, 'tcx, V> -where - V: DefIdVisitor<'tcx> + ?Sized, -{ +struct DefIdVisitorSkeleton<'v, 'tcx, V: ?Sized> { def_id_visitor: &'v mut V, visited_opaque_tys: FxHashSet, dummy: PhantomData>, From 9852b42b581bbc4843460f4654a2dbd859bd9034 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Thu, 18 Jun 2020 20:41:43 +0200 Subject: [PATCH 12/28] `PredicateKint` -> `PredicateKind`, the beginning of the end --- .../infer/canonical/query_response.rs | 20 +- src/librustc_infer/infer/combine.rs | 6 +- src/librustc_infer/infer/outlives/env.rs | 6 +- src/librustc_infer/infer/outlives/mod.rs | 3 +- src/librustc_infer/infer/outlives/verify.rs | 3 +- src/librustc_infer/infer/sub.rs | 2 +- src/librustc_infer/traits/util.rs | 81 ++-- src/librustc_lint/builtin.rs | 42 ++- src/librustc_lint/unused.rs | 4 +- src/librustc_middle/ty/codec.rs | 4 +- src/librustc_middle/ty/context.rs | 12 +- src/librustc_middle/ty/flags.rs | 57 ++- src/librustc_middle/ty/mod.rs | 354 ++++++------------ src/librustc_middle/ty/print/pretty.rs | 5 +- src/librustc_middle/ty/structural_impls.rs | 35 +- src/librustc_middle/ty/sty.rs | 22 ++ .../borrow_check/diagnostics/region_errors.rs | 8 +- .../type_check/free_region_relations.rs | 2 +- .../borrow_check/type_check/mod.rs | 8 +- .../transform/qualify_min_const_fn.rs | 6 +- src/librustc_privacy/lib.rs | 13 +- src/librustc_trait_selection/opaque_types.rs | 12 +- .../traits/auto_trait.rs | 37 +- .../traits/error_reporting/mod.rs | 76 ++-- .../traits/error_reporting/suggestions.rs | 11 +- .../traits/fulfill.rs | 52 ++- src/librustc_trait_selection/traits/mod.rs | 4 +- .../traits/object_safety.rs | 26 +- .../traits/project.rs | 56 +-- .../traits/query/type_op/prove_predicate.rs | 6 +- .../traits/select/candidate_assembly.rs | 3 +- .../traits/select/confirmation.rs | 2 +- .../traits/select/mod.rs | 21 +- .../traits/specialize/mod.rs | 2 +- src/librustc_trait_selection/traits/util.rs | 5 +- src/librustc_trait_selection/traits/wf.rs | 49 ++- src/librustc_traits/chalk/lowering.rs | 37 +- .../implied_outlives_bounds.rs | 26 +- .../normalize_erasing_regions.rs | 10 +- src/librustc_traits/type_op.rs | 4 +- src/librustc_typeck/astconv.rs | 10 +- src/librustc_typeck/check/closure.rs | 17 +- src/librustc_typeck/check/coercion.rs | 33 +- src/librustc_typeck/check/dropck.rs | 11 +- src/librustc_typeck/check/method/confirm.rs | 32 +- src/librustc_typeck/check/method/mod.rs | 2 +- src/librustc_typeck/check/method/probe.rs | 1 + src/librustc_typeck/check/method/suggest.rs | 45 ++- src/librustc_typeck/check/mod.rs | 112 +++--- src/librustc_typeck/check/regionck.rs | 2 +- src/librustc_typeck/check/wfcheck.rs | 4 +- src/librustc_typeck/coherence/builtin.rs | 4 +- src/librustc_typeck/collect.rs | 25 +- .../constrained_generic_params.rs | 10 +- .../impl_wf_check/min_specialization.rs | 15 +- src/librustc_typeck/outlives/explicit.rs | 9 +- src/librustc_typeck/outlives/mod.rs | 14 +- src/librustdoc/clean/auto_trait.rs | 2 +- .../repeated_projection_type.stderr | 2 +- 59 files changed, 742 insertions(+), 740 deletions(-) diff --git a/src/librustc_infer/infer/canonical/query_response.rs b/src/librustc_infer/infer/canonical/query_response.rs index c01759393dfa5..3541bf3b80938 100644 --- a/src/librustc_infer/infer/canonical/query_response.rs +++ b/src/librustc_infer/infer/canonical/query_response.rs @@ -531,12 +531,14 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> { GenericArg<'tcx>, ty::Region<'tcx>, >| match k1.unpack() { - GenericArgKind::Lifetime(r1) => self.tcx.intern_predicate_kint( - ty::PredicateKint::RegionOutlives(ty::OutlivesPredicate(r1, r2)), - ), - GenericArgKind::Type(t1) => self.tcx.intern_predicate_kint( - ty::PredicateKint::TypeOutlives(ty::OutlivesPredicate(t1, r2)), - ), + GenericArgKind::Lifetime(r1) => { + ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(r1, r2)) + .to_predicate(self.tcx) + } + GenericArgKind::Type(t1) => { + ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(t1, r2)) + .to_predicate(self.tcx) + } GenericArgKind::Const(..) => { // Consts cannot outlive one another, so we don't expect to // ecounter this branch. @@ -545,9 +547,9 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> { }; let predicate = if let Some(constraint) = constraint.no_bound_vars() { - to_predicate(constraint).to_predicate(self.tcx) + to_predicate(constraint) } else { - ty::PredicateKint::ForAll(constraint.map_bound(to_predicate)).to_predicate(self.tcx) + ty::PredicateKind::ForAll(constraint.map_bound(to_predicate)).to_predicate(self.tcx) }; Obligation::new(cause.clone(), param_env, predicate) @@ -670,7 +672,7 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for QueryTypeRelatingDelegate<'_, 'tcx> { self.obligations.push(Obligation { cause: self.cause.clone(), param_env: self.param_env, - predicate: ty::PredicateKint::RegionOutlives(ty::OutlivesPredicate(sup, sub)) + predicate: ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(sup, sub)) .to_predicate(self.infcx.tcx), recursion_depth: 0, }); diff --git a/src/librustc_infer/infer/combine.rs b/src/librustc_infer/infer/combine.rs index e8ec5e651bb5d..c63464e5baec9 100644 --- a/src/librustc_infer/infer/combine.rs +++ b/src/librustc_infer/infer/combine.rs @@ -308,7 +308,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> { self.obligations.push(Obligation::new( self.trace.cause.clone(), self.param_env, - ty::PredicateKint::WellFormed(b_ty.into()).to_predicate(self.infcx.tcx), + ty::PredicateKind::WellFormed(b_ty.into()).to_predicate(self.infcx.tcx), )); } @@ -400,9 +400,9 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> { b: &'tcx ty::Const<'tcx>, ) { let predicate = if a_is_expected { - ty::PredicateKint::ConstEquate(a, b) + ty::PredicateKind::ConstEquate(a, b) } else { - ty::PredicateKint::ConstEquate(b, a) + ty::PredicateKind::ConstEquate(b, a) }; self.obligations.push(Obligation::new( self.trace.cause.clone(), diff --git a/src/librustc_infer/infer/outlives/env.rs b/src/librustc_infer/infer/outlives/env.rs index 1a9e20e79fe1e..59f3d01777c7b 100644 --- a/src/librustc_infer/infer/outlives/env.rs +++ b/src/librustc_infer/infer/outlives/env.rs @@ -3,7 +3,7 @@ use crate::infer::{GenericKind, InferCtxt}; use crate::traits::query::OutlivesBound; use rustc_data_structures::fx::FxHashMap; use rustc_hir as hir; -use rustc_middle::ty; +use rustc_middle::ty::{self, TyCtxt}; use super::explicit_outlives_bounds; @@ -69,7 +69,7 @@ pub struct OutlivesEnvironment<'tcx> { pub type RegionBoundPairs<'tcx> = Vec<(ty::Region<'tcx>, GenericKind<'tcx>)>; impl<'a, 'tcx> OutlivesEnvironment<'tcx> { - pub fn new(param_env: ty::ParamEnv<'tcx>) -> Self { + pub fn new(tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self { let mut env = OutlivesEnvironment { param_env, free_region_map: Default::default(), @@ -77,7 +77,7 @@ impl<'a, 'tcx> OutlivesEnvironment<'tcx> { region_bound_pairs_accum: vec![], }; - env.add_outlives_bounds(None, explicit_outlives_bounds(param_env)); + env.add_outlives_bounds(None, explicit_outlives_bounds(tcx, param_env)); env } diff --git a/src/librustc_infer/infer/outlives/mod.rs b/src/librustc_infer/infer/outlives/mod.rs index 541a2f18045c4..ad1579083b646 100644 --- a/src/librustc_infer/infer/outlives/mod.rs +++ b/src/librustc_infer/infer/outlives/mod.rs @@ -5,9 +5,10 @@ pub mod obligations; pub mod verify; use rustc_middle::traits::query::OutlivesBound; -use rustc_middle::ty; +use rustc_middle::ty::{self, TyCtxt}; pub fn explicit_outlives_bounds<'tcx>( + tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, ) -> impl Iterator> + 'tcx { debug!("explicit_outlives_bounds()"); diff --git a/src/librustc_infer/infer/outlives/verify.rs b/src/librustc_infer/infer/outlives/verify.rs index 8f20b5743df4f..27d21dd0b70ef 100644 --- a/src/librustc_infer/infer/outlives/verify.rs +++ b/src/librustc_infer/infer/outlives/verify.rs @@ -331,8 +331,9 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> { compare_ty: impl Fn(Ty<'tcx>) -> bool, predicates: impl Iterator>, ) -> impl Iterator, ty::Region<'tcx>>> { + let tcx = self.tcx; predicates - .filter_map(|p| p.to_opt_type_outlives()) + .filter_map(move |p| p.to_opt_type_outlives(tcx)) .filter_map(|p| p.no_bound_vars()) .filter(move |p| compare_ty(p.0)) } diff --git a/src/librustc_infer/infer/sub.rs b/src/librustc_infer/infer/sub.rs index 50c1f491557fd..5ad08f0b8952a 100644 --- a/src/librustc_infer/infer/sub.rs +++ b/src/librustc_infer/infer/sub.rs @@ -100,7 +100,7 @@ impl TypeRelation<'tcx> for Sub<'combine, 'infcx, 'tcx> { self.fields.obligations.push(Obligation::new( self.fields.trace.cause.clone(), self.fields.param_env, - ty::PredicateKint::Subtype(ty::SubtypePredicate { + ty::PredicateKind::Subtype(ty::SubtypePredicate { a_is_expected: self.a_is_expected, a, b, diff --git a/src/librustc_infer/traits/util.rs b/src/librustc_infer/traits/util.rs index 2a09d9dbee399..1bee16f7556a1 100644 --- a/src/librustc_infer/traits/util.rs +++ b/src/librustc_infer/traits/util.rs @@ -10,34 +10,34 @@ pub fn anonymize_predicate<'tcx>( tcx: TyCtxt<'tcx>, pred: ty::Predicate<'tcx>, ) -> ty::Predicate<'tcx> { - let kind = pred.kint(tcx); + let kind = pred.kind(); let new = match kind { - ty::PredicateKint::ForAll(binder) => { - ty::PredicateKint::ForAll(tcx.anonymize_late_bound_regions(binder)) + ty::PredicateKind::ForAll(binder) => { + ty::PredicateKind::ForAll(tcx.anonymize_late_bound_regions(binder)) } - &ty::PredicateKint::Trait(data, constness) => ty::PredicateKint::Trait(data, constness), + &ty::PredicateKind::Trait(data, constness) => ty::PredicateKind::Trait(data, constness), - &ty::PredicateKint::RegionOutlives(data) => ty::PredicateKint::RegionOutlives(data), + &ty::PredicateKind::RegionOutlives(data) => ty::PredicateKind::RegionOutlives(data), - &ty::PredicateKint::TypeOutlives(data) => ty::PredicateKint::TypeOutlives(data), + &ty::PredicateKind::TypeOutlives(data) => ty::PredicateKind::TypeOutlives(data), - &ty::PredicateKint::Projection(data) => ty::PredicateKint::Projection(data), + &ty::PredicateKind::Projection(data) => ty::PredicateKind::Projection(data), - &ty::PredicateKint::WellFormed(data) => ty::PredicateKint::WellFormed(data), + &ty::PredicateKind::WellFormed(data) => ty::PredicateKind::WellFormed(data), - &ty::PredicateKint::ObjectSafe(data) => ty::PredicateKint::ObjectSafe(data), + &ty::PredicateKind::ObjectSafe(data) => ty::PredicateKind::ObjectSafe(data), - &ty::PredicateKint::ClosureKind(closure_def_id, closure_substs, kind) => { - ty::PredicateKint::ClosureKind(closure_def_id, closure_substs, kind) + &ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => { + ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) } - &ty::PredicateKint::Subtype(data) => ty::PredicateKint::Subtype(data), + &ty::PredicateKind::Subtype(data) => ty::PredicateKind::Subtype(data), - &ty::PredicateKint::ConstEvaluatable(def_id, substs) => { - ty::PredicateKint::ConstEvaluatable(def_id, substs) + &ty::PredicateKind::ConstEvaluatable(def_id, substs) => { + ty::PredicateKind::ConstEvaluatable(def_id, substs) } - &ty::PredicateKint::ConstEquate(c1, c2) => ty::PredicateKint::ConstEquate(c1, c2), + &ty::PredicateKind::ConstEquate(c1, c2) => ty::PredicateKind::ConstEquate(c1, c2), }; if new != *kind { new.to_predicate(tcx) } else { pred } @@ -145,22 +145,22 @@ fn predicate_obligation<'tcx>( } impl Elaborator<'tcx> { - pub fn filter_to_traits(self) -> FilterToTraits { - FilterToTraits::new(self) + pub fn filter_to_traits(self) -> FilterToTraits<'tcx, Self> { + FilterToTraits::new(self.visited.tcx, self) } fn elaborate(&mut self, obligation: &PredicateObligation<'tcx>) { let tcx = self.visited.tcx; - let pred = match obligation.predicate.kint(tcx) { - // We have to be careful and rebind this whenever + let pred = match obligation.predicate.kind() { + // We have to be careful and rebind this when // dealing with a predicate further down. - ty::PredicateKint::ForAll(binder) => binder.skip_binder(), + ty::PredicateKind::ForAll(binder) => binder.skip_binder().kind(), pred => pred, }; match pred { - ty::PredicateKint::ForAll(_) => bug!("unexpected predicate: {:?}", pred), - ty::PredicateKint::Trait(ref data, _) => { + ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", pred), + ty::PredicateKind::Trait(data, _) => { // Get predicates declared on the trait. let predicates = tcx.super_predicates_of(data.def_id()); @@ -181,36 +181,36 @@ impl Elaborator<'tcx> { self.stack.extend(obligations); } - ty::PredicateKint::WellFormed(..) => { + ty::PredicateKind::WellFormed(..) => { // Currently, we do not elaborate WF predicates, // although we easily could. } - ty::PredicateKint::ObjectSafe(..) => { + ty::PredicateKind::ObjectSafe(..) => { // Currently, we do not elaborate object-safe // predicates. } - ty::PredicateKint::Subtype(..) => { + ty::PredicateKind::Subtype(..) => { // Currently, we do not "elaborate" predicates like `X <: Y`, // though conceivably we might. } - ty::PredicateKint::Projection(..) => { + ty::PredicateKind::Projection(..) => { // Nothing to elaborate in a projection predicate. } - ty::PredicateKint::ClosureKind(..) => { + ty::PredicateKind::ClosureKind(..) => { // Nothing to elaborate when waiting for a closure's kind to be inferred. } - ty::PredicateKint::ConstEvaluatable(..) => { + ty::PredicateKind::ConstEvaluatable(..) => { // Currently, we do not elaborate const-evaluatable // predicates. } - ty::PredicateKint::ConstEquate(..) => { + ty::PredicateKind::ConstEquate(..) => { // Currently, we do not elaborate const-equate // predicates. } - ty::PredicateKint::RegionOutlives(..) => { + ty::PredicateKind::RegionOutlives(..) => { // Nothing to elaborate from `'a: 'b`. } - ty::PredicateKint::TypeOutlives(ty::OutlivesPredicate(ty_max, r_min)) => { + ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty_max, r_min)) => { // We know that `T: 'a` for some type `T`. We can // often elaborate this. For example, if we know that // `[U]: 'a`, that implies that `U: 'a`. Similarly, if @@ -240,7 +240,7 @@ impl Elaborator<'tcx> { if r.is_late_bound() { None } else { - Some(ty::PredicateKint::RegionOutlives(ty::OutlivesPredicate( + Some(ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate( r, r_min, ))) } @@ -248,7 +248,7 @@ impl Elaborator<'tcx> { Component::Param(p) => { let ty = tcx.mk_ty_param(p.index, p.name); - Some(ty::PredicateKint::TypeOutlives(ty::OutlivesPredicate( + Some(ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate( ty, r_min, ))) } @@ -293,7 +293,7 @@ impl Iterator for Elaborator<'tcx> { // Supertrait iterator /////////////////////////////////////////////////////////////////////////// -pub type Supertraits<'tcx> = FilterToTraits>; +pub type Supertraits<'tcx> = FilterToTraits<'tcx, Elaborator<'tcx>>; pub fn supertraits<'tcx>( tcx: TyCtxt<'tcx>, @@ -315,22 +315,23 @@ pub fn transitive_bounds<'tcx>( /// A filter around an iterator of predicates that makes it yield up /// just trait references. -pub struct FilterToTraits { +pub struct FilterToTraits<'tcx, I> { + tcx: TyCtxt<'tcx>, base_iterator: I, } -impl FilterToTraits { - fn new(base: I) -> FilterToTraits { - FilterToTraits { base_iterator: base } +impl<'tcx, I> FilterToTraits<'tcx, I> { + fn new(tcx: TyCtxt<'tcx>, base: I) -> FilterToTraits<'tcx, I> { + FilterToTraits { tcx, base_iterator: base } } } -impl<'tcx, I: Iterator>> Iterator for FilterToTraits { +impl<'tcx, I: Iterator>> Iterator for FilterToTraits<'tcx, I> { type Item = ty::PolyTraitRef<'tcx>; fn next(&mut self) -> Option> { while let Some(obligation) = self.base_iterator.next() { - if let Some(data) = obligation.predicate.to_opt_poly_trait_ref() { + if let Some(data) = obligation.predicate.to_opt_poly_trait_ref(self.tcx) { return Some(data); } } diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index a9490c954b55e..cedf742b9a949 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -1202,7 +1202,7 @@ declare_lint_pass!( impl<'tcx> LateLintPass<'tcx> for TrivialConstraints { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) { use rustc_middle::ty::fold::TypeFoldable; - use rustc_middle::ty::PredicateKint::*; + use rustc_middle::ty::PredicateKind::*; if cx.tcx.features().trivial_bounds { let def_id = cx.tcx.hir().local_def_id(item.hir_id); @@ -1210,7 +1210,7 @@ impl<'tcx> LateLintPass<'tcx> for TrivialConstraints { for &(predicate, span) in predicates.predicates { // We don't actually look inside of the predicate, // so it is safe to skip this binder here. - let predicate_kind_name = match predicate.kint(cx.tcx).ignore_qualifiers().skip_binder() { + let predicate_kind_name = match predicate.ignore_qualifiers(cx.tcx).skip_binder().kind() { Trait(..) => "Trait", TypeOutlives(..) | RegionOutlives(..) => "Lifetime", @@ -1495,34 +1495,32 @@ declare_lint_pass!(ExplicitOutlivesRequirements => [EXPLICIT_OUTLIVES_REQUIREMEN impl ExplicitOutlivesRequirements { fn lifetimes_outliving_lifetime<'tcx>( + tcx: TyCtxt<'tcx>, inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)], index: u32, ) -> Vec> { inferred_outlives .iter() - .filter_map(|(pred, _)| match pred.kind() { - ty::PredicateKind::RegionOutlives(outlives) => { - let outlives = outlives.skip_binder(); - match outlives.0 { - ty::ReEarlyBound(ebr) if ebr.index == index => Some(outlives.1), - _ => None, - } - } + .filter_map(|(pred, _)| match pred.ignore_qualifiers(tcx).skip_binder().kind() { + &ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => match a { + ty::ReEarlyBound(ebr) if ebr.index == index => Some(b), + _ => None, + }, _ => None, }) .collect() } fn lifetimes_outliving_type<'tcx>( + tcx: TyCtxt<'tcx>, inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)], index: u32, ) -> Vec> { inferred_outlives .iter() - .filter_map(|(pred, _)| match pred.kind() { - ty::PredicateKind::TypeOutlives(outlives) => { - let outlives = outlives.skip_binder(); - outlives.0.is_param(index).then_some(outlives.1) + .filter_map(|(pred, _)| match pred.ignore_qualifiers(tcx).skip_binder().kind() { + &ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(a, b)) => { + a.is_param(index).then_some(b) } _ => None, }) @@ -1541,10 +1539,10 @@ impl ExplicitOutlivesRequirements { match param.kind { hir::GenericParamKind::Lifetime { .. } => { - Self::lifetimes_outliving_lifetime(inferred_outlives, index) + Self::lifetimes_outliving_lifetime(tcx, inferred_outlives, index) } hir::GenericParamKind::Type { .. } => { - Self::lifetimes_outliving_type(inferred_outlives, index) + Self::lifetimes_outliving_type(tcx, inferred_outlives, index) } hir::GenericParamKind::Const { .. } => Vec::new(), } @@ -1696,7 +1694,11 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements { cx.tcx.named_region(predicate.lifetime.hir_id) { ( - Self::lifetimes_outliving_lifetime(inferred_outlives, index), + Self::lifetimes_outliving_lifetime( + cx.tcx, + inferred_outlives, + index, + ), &predicate.bounds, predicate.span, ) @@ -1712,7 +1714,11 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements { if let Res::Def(DefKind::TyParam, def_id) = path.res { let index = ty_generics.param_def_id_to_index[&def_id]; ( - Self::lifetimes_outliving_type(inferred_outlives, index), + Self::lifetimes_outliving_type( + cx.tcx, + inferred_outlives, + index, + ), &predicate.bounds, predicate.span, ) diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index 62127f3e12ca1..1430b799afce2 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -147,8 +147,8 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { let mut has_emitted = false; for (predicate, _) in cx.tcx.predicates_of(def).predicates { // We only look at the `DefId`, so it is safe to skip the binder here. - if let ty::PredicateKint::Trait(ref poly_trait_predicate, _) = - predicate.kint(cx.tcx).ignore_qualifiers().skip_binder() + if let ty::PredicateKind::Trait(ref poly_trait_predicate, _) = + predicate.ignore_qualifiers(cx.tcx).skip_binder().kind() { let def_id = poly_trait_predicate.trait_ref.def_id; let descr_pre = diff --git a/src/librustc_middle/ty/codec.rs b/src/librustc_middle/ty/codec.rs index b8e041a00ee17..a7c7b16048039 100644 --- a/src/librustc_middle/ty/codec.rs +++ b/src/librustc_middle/ty/codec.rs @@ -39,7 +39,7 @@ impl<'tcx> EncodableWithShorthand for Ty<'tcx> { } impl<'tcx> EncodableWithShorthand for ty::Predicate<'tcx> { - type Variant = ty::PredicateKynd<'tcx>; + type Variant = ty::PredicateKind<'tcx>; fn variant(&self) -> &Self::Variant { self.kind() } @@ -195,7 +195,7 @@ where }) } else { let tcx = decoder.tcx(); - Ok(tcx.mk_predicate(ty::PredicateKynd::decode(decoder)?)) + Ok(tcx.mk_predicate(ty::PredicateKind::decode(decoder)?)) } } diff --git a/src/librustc_middle/ty/context.rs b/src/librustc_middle/ty/context.rs index 6f22a0f8ce31e..1e7fceb8e22e1 100644 --- a/src/librustc_middle/ty/context.rs +++ b/src/librustc_middle/ty/context.rs @@ -20,8 +20,8 @@ use crate::ty::{ self, query, AdtDef, AdtKind, BindingMode, BoundVar, CanonicalPolyFnSig, Const, ConstVid, DefIdTree, ExistentialPredicate, FloatVar, FloatVid, GenericParamDefKind, InferConst, InferTy, IntVar, IntVid, List, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKind, - PredicateKint, ProjectionTy, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, - TyS, TyVar, TyVid, TypeAndMut, + ProjectionTy, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, + TyVid, TypeAndMut, }; use rustc_ast::ast; use rustc_ast::expand::allocator::AllocatorKind; @@ -79,7 +79,6 @@ pub struct CtxtInterners<'tcx> { region: InternedSet<'tcx, RegionKind>, existential_predicates: InternedSet<'tcx, List>>, predicate: InternedSet<'tcx, PredicateInner<'tcx>>, - predicate_kint: InternedSet<'tcx, PredicateKint<'tcx>>, predicates: InternedSet<'tcx, List>>, projs: InternedSet<'tcx, List>, place_elems: InternedSet<'tcx, List>>, @@ -99,7 +98,6 @@ impl<'tcx> CtxtInterners<'tcx> { existential_predicates: Default::default(), canonical_var_infos: Default::default(), predicate: Default::default(), - predicate_kint: Default::default(), predicates: Default::default(), projs: Default::default(), place_elems: Default::default(), @@ -1617,7 +1615,6 @@ nop_lift! {type_; Ty<'a> => Ty<'tcx>} nop_lift! {region; Region<'a> => Region<'tcx>} nop_lift! {const_; &'a Const<'a> => &'tcx Const<'tcx>} nop_lift! {predicate; &'a PredicateInner<'a> => &'tcx PredicateInner<'tcx>} -nop_lift! {predicate_kint; &'a PredicateKint<'a> => &'tcx PredicateKint<'tcx>} nop_list_lift! {type_list; Ty<'a> => Ty<'tcx>} nop_list_lift! {existential_predicates; ExistentialPredicate<'a> => ExistentialPredicate<'tcx>} @@ -2030,8 +2027,8 @@ impl<'tcx> Borrow> for Interned<'tcx, Const<'tcx>> { } } -impl<'tcx> Borrow> for Interned<'tcx, PredicateKint<'tcx>> { - fn borrow<'a>(&'a self) -> &'a PredicateKint<'tcx> { +impl<'tcx> Borrow> for Interned<'tcx, PredicateKind<'tcx>> { + fn borrow<'a>(&'a self) -> &'a PredicateKind<'tcx> { &self.0 } } @@ -2065,7 +2062,6 @@ macro_rules! direct_interners { direct_interners! { region: mk_region(RegionKind), const_: mk_const(Const<'tcx>), - predicate_kint: intern_predicate_kint(PredicateKint<'tcx>), } macro_rules! slice_interners { diff --git a/src/librustc_middle/ty/flags.rs b/src/librustc_middle/ty/flags.rs index 11a8bedb6605b..c8c8475b056da 100644 --- a/src/librustc_middle/ty/flags.rs +++ b/src/librustc_middle/ty/flags.rs @@ -201,45 +201,31 @@ impl FlagComputation { } } + fn add_predicate(&mut self, pred: &ty::Predicate<'_>) { + self.add_flags(pred.inner.flags); + self.add_exclusive_binder(pred.inner.outer_exclusive_binder); + } + fn add_predicate_kind(&mut self, kind: &ty::PredicateKind<'_>) { match kind { ty::PredicateKind::Trait(trait_pred, _constness) => { - let mut computation = FlagComputation::new(); - computation.add_substs(trait_pred.skip_binder().trait_ref.substs); - - self.add_bound_computation(computation); + self.add_substs(trait_pred.trait_ref.substs); } - ty::PredicateKind::RegionOutlives(poly_outlives) => { - let mut computation = FlagComputation::new(); - let ty::OutlivesPredicate(a, b) = poly_outlives.skip_binder(); - computation.add_region(a); - computation.add_region(b); - - self.add_bound_computation(computation); + ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => { + self.add_region(a); + self.add_region(b); } - ty::PredicateKind::TypeOutlives(poly_outlives) => { - let mut computation = FlagComputation::new(); - let ty::OutlivesPredicate(ty, region) = poly_outlives.skip_binder(); - computation.add_ty(ty); - computation.add_region(region); - - self.add_bound_computation(computation); + ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, region)) => { + self.add_ty(ty); + self.add_region(region); } - ty::PredicateKind::Subtype(poly_subtype) => { - let mut computation = FlagComputation::new(); - let ty::SubtypePredicate { a_is_expected: _, a, b } = poly_subtype.skip_binder(); - computation.add_ty(a); - computation.add_ty(b); - - self.add_bound_computation(computation); + ty::PredicateKind::Subtype(ty::SubtypePredicate { a_is_expected: _, a, b }) => { + self.add_ty(a); + self.add_ty(b); } - &ty::PredicateKind::Projection(projection) => { - let mut computation = FlagComputation::new(); - let ty::ProjectionPredicate { projection_ty, ty } = projection.skip_binder(); - computation.add_projection_ty(projection_ty); - computation.add_ty(ty); - - self.add_bound_computation(computation); + ty::PredicateKind::Projection(ty::ProjectionPredicate { projection_ty, ty }) => { + self.add_projection_ty(projection_ty); + self.add_ty(ty); } ty::PredicateKind::WellFormed(arg) => { self.add_substs(slice::from_ref(arg)); @@ -255,6 +241,13 @@ impl FlagComputation { self.add_const(expected); self.add_const(found); } + ty::PredicateKind::ForAll(binder) => { + let mut computation = FlagComputation::new(); + + computation.add_predicate(binder.skip_binder()); + + self.add_bound_computation(computation); + } } } diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index b8f9550126247..3b154bf1518d6 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -1048,6 +1048,36 @@ impl<'tcx> Predicate<'tcx> { pub fn kind(self) -> &'tcx PredicateKind<'tcx> { &self.inner.kind } + + /// Skips `PredicateKind::ForAll`. + pub fn ignore_qualifiers(self, tcx: TyCtxt<'tcx>) -> Binder> { + match self.kind() { + &PredicateKind::ForAll(binder) => binder, + ty::PredicateKind::Projection(..) + | ty::PredicateKind::Trait(..) + | ty::PredicateKind::Subtype(..) + | ty::PredicateKind::WellFormed(..) + | ty::PredicateKind::ObjectSafe(..) + | ty::PredicateKind::ClosureKind(..) + | ty::PredicateKind::TypeOutlives(..) + | ty::PredicateKind::ConstEvaluatable(..) + | ty::PredicateKind::ConstEquate(..) + | ty::PredicateKind::RegionOutlives(..) => Binder::wrap_nonbinding(tcx, self), + } + } + + /// Wraps `self` with the given qualifier if this predicate has any unbound variables. + pub fn potentially_qualified( + self, + tcx: TyCtxt<'tcx>, + qualifier: impl FnOnce(Binder>) -> PredicateKind<'tcx>, + ) -> Predicate<'tcx> { + if self.has_escaping_bound_vars() { + qualifier(Binder::bind(self)).to_predicate(tcx) + } else { + self + } + } } impl<'a, 'tcx> HashStable> for Predicate<'tcx> { @@ -1065,72 +1095,9 @@ impl<'a, 'tcx> HashStable> for Predicate<'tcx> { } } -impl<'tcx> Predicate<'tcx> { - pub fn kint(self, tcx: TyCtxt<'tcx>) -> &'tcx PredicateKint<'tcx> { - // I am efficient - tcx.intern_predicate_kint(match *self.kind() { - PredicateKind::Trait(binder, data) => { - if let Some(simpl) = binder.no_bound_vars() { - PredicateKint::Trait(simpl, data) - } else { - let inner = tcx - .intern_predicate_kint(PredicateKint::Trait(*binder.skip_binder(), data)); - PredicateKint::ForAll(Binder::bind(inner)) - } - } - PredicateKind::RegionOutlives(binder) => { - if let Some(simpl) = binder.no_bound_vars() { - PredicateKint::RegionOutlives(simpl) - } else { - let inner = tcx.intern_predicate_kint(PredicateKint::RegionOutlives( - *binder.skip_binder(), - )); - PredicateKint::ForAll(Binder::bind(inner)) - } - } - PredicateKind::TypeOutlives(binder) => { - if let Some(simpl) = binder.no_bound_vars() { - PredicateKint::TypeOutlives(simpl) - } else { - let inner = tcx - .intern_predicate_kint(PredicateKint::TypeOutlives(*binder.skip_binder())); - PredicateKint::ForAll(Binder::bind(inner)) - } - } - PredicateKind::Projection(binder) => { - if let Some(simpl) = binder.no_bound_vars() { - PredicateKint::Projection(simpl) - } else { - let inner = - tcx.intern_predicate_kint(PredicateKint::Projection(*binder.skip_binder())); - PredicateKint::ForAll(Binder::bind(inner)) - } - } - PredicateKind::WellFormed(arg) => PredicateKint::WellFormed(arg), - PredicateKind::ObjectSafe(def_id) => PredicateKint::ObjectSafe(def_id), - PredicateKind::ClosureKind(def_id, substs, kind) => { - PredicateKint::ClosureKind(def_id, substs, kind) - } - PredicateKind::Subtype(binder) => { - if let Some(simpl) = binder.no_bound_vars() { - PredicateKint::Subtype(simpl) - } else { - let inner = - tcx.intern_predicate_kint(PredicateKint::Subtype(*binder.skip_binder())); - PredicateKint::ForAll(Binder::bind(inner)) - } - } - PredicateKind::ConstEvaluatable(def, substs) => { - PredicateKint::ConstEvaluatable(def, substs) - } - PredicateKind::ConstEquate(l, r) => PredicateKint::ConstEquate(l, r), - }) - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -#[derive(TypeFoldable)] -pub enum PredicateKint<'tcx> { +#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)] +#[derive(HashStable, TypeFoldable)] +pub enum PredicateKind<'tcx> { /// Corresponds to `where Foo: Bar`. `Foo` here would be /// the `Self` type of the trait reference and `A`, `B`, and `C` /// would be the type parameters. @@ -1164,67 +1131,14 @@ pub enum PredicateKint<'tcx> { /// `T1 <: T2` Subtype(SubtypePredicate<'tcx>), - /// Constant initializer must evaluate successfully. - ConstEvaluatable(DefId, SubstsRef<'tcx>), - - /// Constants must be equal. The first component is the const that is expected. - ConstEquate(&'tcx Const<'tcx>, &'tcx Const<'tcx>), - - /// `for<'a>: ...` - ForAll(Binder<&'tcx PredicateKint<'tcx>>), -} - -#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)] -#[derive(HashStable, TypeFoldable)] -pub enum PredicateKind<'tcx> { - /// Corresponds to `where Foo: Bar`. `Foo` here would be - /// the `Self` type of the trait reference and `A`, `B`, and `C` - /// would be the type parameters. - /// - /// A trait predicate will have `Constness::Const` if it originates - /// from a bound on a `const fn` without the `?const` opt-out (e.g., - /// `const fn foobar() {}`). - Trait(PolyTraitPredicate<'tcx>, Constness), - - /// `where 'a: 'b` - RegionOutlives(PolyRegionOutlivesPredicate<'tcx>), - - /// `where T: 'a` - TypeOutlives(PolyTypeOutlivesPredicate<'tcx>), - - /// `where ::Name == X`, approximately. - /// See the `ProjectionPredicate` struct for details. - Projection(PolyProjectionPredicate<'tcx>), - - /// No syntax: `T` well-formed. - WellFormed(GenericArg<'tcx>), - - /// Trait must be object-safe. - ObjectSafe(DefId), - - /// No direct syntax. May be thought of as `where T: FnFoo<...>` - /// for some substitutions `...` and `T` being a closure type. - /// Satisfied (or refuted) once we know the closure's kind. - ClosureKind(DefId, SubstsRef<'tcx>, ClosureKind), - - /// `T1 <: T2` - Subtype(PolySubtypePredicate<'tcx>), - /// Constant initializer must evaluate successfully. ConstEvaluatable(ty::WithOptConstParam, SubstsRef<'tcx>), /// Constants must be equal. The first component is the const that is expected. ConstEquate(&'tcx Const<'tcx>, &'tcx Const<'tcx>), -} -impl<'tcx> PredicateKint<'tcx> { - /// Skips `PredicateKint::ForAll`. - pub fn ignore_qualifiers(&'tcx self) -> Binder<&'tcx PredicateKint<'tcx>> { - match self { - &PredicateKint::ForAll(binder) => binder, - pred => Binder::dummy(pred), - } - } + /// `for<'a>: ...` + ForAll(Binder>), } /// The crate outlives map is computed during typeck and contains the @@ -1313,20 +1227,18 @@ impl<'tcx> Predicate<'tcx> { // this trick achieves that). let substs = trait_ref.skip_binder().substs; - let kind = match self.kint(tcx) { - PredicateKint::ForAll(binder) => *binder.skip_binder(), + let kind = match self.kind() { + PredicateKind::ForAll(binder) => binder.skip_binder().kind(), kind => kind, }; let new = kind.subst(tcx, substs); - let rebound = if new.has_escaping_bound_vars() { - PredicateKint::ForAll(Binder::bind(tcx.intern_predicate_kint(new))) + if new != *kind { + new.to_predicate(tcx).potentially_qualified(tcx, PredicateKind::ForAll) } else { - new - }; - - if rebound != *kind { rebound.to_predicate(tcx) } else { self } + self + } } } @@ -1451,94 +1363,33 @@ impl ToPredicate<'tcx> for PredicateKind<'tcx> { } } -impl ToPredicate<'tcx> for PredicateKint<'tcx> { - #[inline(always)] +impl<'tcx> ToPredicate<'tcx> for ConstnessAnd> { fn to_predicate(&self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { - let (predicate, in_binder) = if let PredicateKint::ForAll(binder) = self { - (*binder.skip_binder(), true) - } else { - (self, false) - }; - - macro_rules! bind { - ($expr:expr) => { - match $expr { - expr => { - if in_binder { - Binder::bind(expr) - } else { - Binder::dummy(expr) - } - } - } - }; - } - - match *predicate { - PredicateKint::ForAll(_) => bug!("unexpected PredicateKint: {:?}", self), - PredicateKint::Trait(data, ct) => PredicateKind::Trait(bind!(data), ct), - PredicateKint::RegionOutlives(data) => PredicateKind::RegionOutlives(bind!(data)), - PredicateKint::TypeOutlives(data) => PredicateKind::TypeOutlives(bind!(data)), - PredicateKint::Projection(data) => PredicateKind::Projection(bind!(data)), - PredicateKint::WellFormed(arg) => { - if in_binder { - bug!("unexpected ForAll: {:?}", self) - } else { - PredicateKind::WellFormed(arg) - } - } - PredicateKint::ObjectSafe(def_id) => { - if in_binder { - bug!("unexpected ForAll: {:?}", self) - } else { - PredicateKind::ObjectSafe(def_id) - } - } - PredicateKint::ClosureKind(def_id, substs, kind) => { - if in_binder { - bug!("unexpected ForAll: {:?}", self) - } else { - PredicateKind::ClosureKind(def_id, substs, kind) - } - } - PredicateKint::Subtype(data) => PredicateKind::Subtype(bind!(data)), - PredicateKint::ConstEvaluatable(def_id, substs) => { - if in_binder { - bug!("unexpected ForAll: {:?}", self) - } else { - PredicateKind::ConstEvaluatable(def_id, substs) - } - } - PredicateKint::ConstEquate(l, r) => { - if in_binder { - bug!("unexpected ForAll: {:?}", self) - } else { - PredicateKind::ConstEquate(l, r) - } - } - } - .to_predicate(tcx) + ty::PredicateKind::Trait(ty::TraitPredicate { trait_ref: self.value }, self.constness) + .to_predicate(tcx) } } -impl<'tcx> ToPredicate<'tcx> for ConstnessAnd> { +impl<'tcx> ToPredicate<'tcx> for ConstnessAnd> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { - ty::PredicateKint::Trait(ty::TraitPredicate { trait_ref: self.value }, self.constness) - .to_predicate(tcx) + ConstnessAnd { + value: self.value.map_bound(|trait_ref| ty::TraitPredicate { trait_ref }), + constness: self.constness, + } + .to_predicate(tcx) } } -impl<'tcx> ToPredicate<'tcx> for ConstnessAnd> { +impl<'tcx> ToPredicate<'tcx> for ConstnessAnd> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { - if let Some(trait_ref) = self.value.no_bound_vars() { - ty::PredicateKint::Trait(ty::TraitPredicate { trait_ref }, self.constness) + if let Some(pred) = self.value.no_bound_vars() { + ty::PredicateKind::Trait(pred, self.constness) } else { - ty::PredicateKint::ForAll(self.value.map_bound(|trait_ref| { - tcx.intern_predicate_kint(ty::PredicateKint::Trait( - ty::TraitPredicate { trait_ref }, - self.constness, - )) - })) + ty::PredicateKind::ForAll( + self.value.map_bound(|pred| { + ty::PredicateKind::Trait(pred, self.constness).to_predicate(tcx) + }), + ) } .to_predicate(tcx) } @@ -1547,11 +1398,13 @@ impl<'tcx> ToPredicate<'tcx> for ConstnessAnd> { impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { if let Some(outlives) = self.no_bound_vars() { - PredicateKint::RegionOutlives(outlives) + PredicateKind::RegionOutlives(outlives) } else { - ty::PredicateKint::ForAll(self.map_bound(|outlives| { - tcx.intern_predicate_kint(PredicateKint::RegionOutlives(outlives)) - })) + ty::PredicateKind::ForAll( + self.map_bound(|outlives| { + PredicateKind::RegionOutlives(outlives).to_predicate(tcx) + }), + ) } .to_predicate(tcx) } @@ -1559,45 +1412,68 @@ impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate<'tcx> { impl<'tcx> ToPredicate<'tcx> for PolyTypeOutlivesPredicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { - PredicateKind::TypeOutlives(self).to_predicate(tcx) + if let Some(outlives) = self.no_bound_vars() { + PredicateKind::TypeOutlives(outlives) + } else { + ty::PredicateKind::ForAll( + self.map_bound(|outlives| PredicateKind::TypeOutlives(outlives).to_predicate(tcx)), + ) + } + .to_predicate(tcx) } } impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { - PredicateKind::Projection(self).to_predicate(tcx) + if let Some(proj) = self.no_bound_vars() { + PredicateKind::Projection(proj) + } else { + ty::PredicateKind::ForAll( + self.map_bound(|proj| PredicateKind::Projection(proj).to_predicate(tcx)), + ) + } + .to_predicate(tcx) } } impl<'tcx> Predicate<'tcx> { - pub fn to_opt_poly_trait_ref(self) -> Option> { - match self.kind() { - &PredicateKind::Trait(ref t, _) => Some(t.to_poly_trait_ref()), - PredicateKind::Projection(..) - | PredicateKind::Subtype(..) - | PredicateKind::RegionOutlives(..) - | PredicateKind::WellFormed(..) - | PredicateKind::ObjectSafe(..) - | PredicateKind::ClosureKind(..) - | PredicateKind::TypeOutlives(..) - | PredicateKind::ConstEvaluatable(..) - | PredicateKind::ConstEquate(..) => None, - } + pub fn to_opt_poly_trait_ref(self, tcx: TyCtxt<'tcx>) -> Option> { + self.ignore_qualifiers(tcx) + .map_bound(|pred| match pred.kind() { + &PredicateKind::Trait(ref t, _) => Some(t.trait_ref), + PredicateKind::Projection(..) + | PredicateKind::Subtype(..) + | PredicateKind::RegionOutlives(..) + | PredicateKind::WellFormed(..) + | PredicateKind::ObjectSafe(..) + | PredicateKind::ClosureKind(..) + | PredicateKind::TypeOutlives(..) + | PredicateKind::ConstEvaluatable(..) + | PredicateKind::ConstEquate(..) => None, + PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", self), + }) + .transpose() } - pub fn to_opt_type_outlives(self) -> Option> { - match self.kind() { - &PredicateKind::TypeOutlives(data) => Some(data), - PredicateKind::Trait(..) - | PredicateKind::Projection(..) - | PredicateKind::Subtype(..) - | PredicateKind::RegionOutlives(..) - | PredicateKind::WellFormed(..) - | PredicateKind::ObjectSafe(..) - | PredicateKind::ClosureKind(..) - | PredicateKind::ConstEvaluatable(..) - | PredicateKind::ConstEquate(..) => None, - } + pub fn to_opt_type_outlives( + self, + tcx: TyCtxt<'tcx>, + ) -> Option> { + self.ignore_qualifiers(tcx) + .map_bound(|pred| match pred.kind() { + &PredicateKind::TypeOutlives(data) => Some(data), + PredicateKind::Trait(..) + | PredicateKind::Projection(..) + | PredicateKind::Subtype(..) + | PredicateKind::RegionOutlives(..) + | PredicateKind::WellFormed(..) + | PredicateKind::ObjectSafe(..) + | PredicateKind::ClosureKind(..) + | PredicateKind::ConstEvaluatable(..) + | PredicateKind::ConstEquate(..) => None, + PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", self), + }) + .transpose() } } diff --git a/src/librustc_middle/ty/print/pretty.rs b/src/librustc_middle/ty/print/pretty.rs index 043d85cb6efa6..1fdd7d4c82479 100644 --- a/src/librustc_middle/ty/print/pretty.rs +++ b/src/librustc_middle/ty/print/pretty.rs @@ -572,7 +572,7 @@ pub trait PrettyPrinter<'tcx>: let mut is_sized = false; p!(write("impl")); for predicate in bounds.predicates { - if let Some(trait_ref) = predicate.to_opt_poly_trait_ref() { + if let Some(trait_ref) = predicate.to_opt_poly_trait_ref(self.tcx()) { // Don't print +Sized, but rather +?Sized if absent. if Some(trait_ref.def_id()) == self.tcx().lang_items().sized_trait() { is_sized = true; @@ -2039,6 +2039,9 @@ define_print_and_forward_display! { print(c2), write("`")) } + ty::PredicateKind::ForAll(binder) => { + p!(print(binder)) + } } } diff --git a/src/librustc_middle/ty/structural_impls.rs b/src/librustc_middle/ty/structural_impls.rs index 31d221288bbd6..55fab4999053a 100644 --- a/src/librustc_middle/ty/structural_impls.rs +++ b/src/librustc_middle/ty/structural_impls.rs @@ -247,6 +247,7 @@ impl fmt::Debug for ty::PredicateKind<'tcx> { write!(f, "ConstEvaluatable({:?}, {:?})", def_id, substs) } ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2), + ty::PredicateKind::ForAll(binder) => write!(f, "ForAll({:?})", binder), } } } @@ -478,20 +479,18 @@ impl<'a, 'tcx> Lift<'tcx> for ty::PredicateKind<'a> { type Lifted = ty::PredicateKind<'tcx>; fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option { match *self { - ty::PredicateKind::Trait(ref binder, constness) => { - tcx.lift(binder).map(|binder| ty::PredicateKind::Trait(binder, constness)) + ty::PredicateKind::Trait(ref data, constness) => { + tcx.lift(data).map(|data| ty::PredicateKind::Trait(data, constness)) } - ty::PredicateKind::Subtype(ref binder) => { - tcx.lift(binder).map(ty::PredicateKind::Subtype) + ty::PredicateKind::Subtype(ref data) => tcx.lift(data).map(ty::PredicateKind::Subtype), + ty::PredicateKind::RegionOutlives(ref data) => { + tcx.lift(data).map(ty::PredicateKind::RegionOutlives) } - ty::PredicateKind::RegionOutlives(ref binder) => { - tcx.lift(binder).map(ty::PredicateKind::RegionOutlives) + ty::PredicateKind::TypeOutlives(ref data) => { + tcx.lift(data).map(ty::PredicateKind::TypeOutlives) } - ty::PredicateKind::TypeOutlives(ref binder) => { - tcx.lift(binder).map(ty::PredicateKind::TypeOutlives) - } - ty::PredicateKind::Projection(ref binder) => { - tcx.lift(binder).map(ty::PredicateKind::Projection) + ty::PredicateKind::Projection(ref data) => { + tcx.lift(data).map(ty::PredicateKind::Projection) } ty::PredicateKind::WellFormed(ty) => tcx.lift(&ty).map(ty::PredicateKind::WellFormed), ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => { @@ -508,6 +507,9 @@ impl<'a, 'tcx> Lift<'tcx> for ty::PredicateKind<'a> { ty::PredicateKind::ConstEquate(c1, c2) => { tcx.lift(&(c1, c2)).map(|(c1, c2)| ty::PredicateKind::ConstEquate(c1, c2)) } + ty::PredicateKind::ForAll(ref binder) => { + tcx.lift(binder).map(ty::PredicateKind::ForAll) + } } } } @@ -1028,17 +1030,6 @@ impl> PredicateVisitor<'tcx> for T { } } -impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::PredicateKint<'tcx> { - fn super_fold_with>(&self, folder: &mut F) -> Self { - let new = ty::PredicateKint::super_fold_with(self, folder); - folder.tcx().intern_predicate_kint(new) - } - - fn super_visit_with>(&self, visitor: &mut V) -> bool { - ty::PredicateKint::super_visit_with(self, visitor) - } -} - impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List> { fn super_fold_with>(&self, folder: &mut F) -> Self { fold_list(*self, folder, |tcx, v| tcx.intern_predicates(v)) diff --git a/src/librustc_middle/ty/sty.rs b/src/librustc_middle/ty/sty.rs index cf9d40a6dc6c5..58a89c7fdb110 100644 --- a/src/librustc_middle/ty/sty.rs +++ b/src/librustc_middle/ty/sty.rs @@ -895,6 +895,19 @@ impl Binder { Binder(value) } + /// Wraps `value` in a binder without actually binding any currently + /// unbound variables. + pub fn wrap_nonbinding(tcx: TyCtxt<'tcx>, value: T) -> Binder + where + T: TypeFoldable<'tcx>, + { + if value.has_escaping_bound_vars() { + Binder::bind(super::fold::shift_vars(tcx, &value, 1)) + } else { + Binder::dummy(value) + } + } + /// Skips the binder and returns the "bound" value. This is a /// risky thing to do because it's easy to get confused about /// De Bruijn indices and the like. It is usually better to @@ -979,6 +992,15 @@ impl Binder { } } +impl Binder> { + pub fn transpose(self) -> Option> { + match self.0 { + Some(v) => Some(Binder(v)), + None => None, + } + } +} + /// Represents the projection of an associated type. In explicit UFCS /// form this would be written `>::N`. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, RustcEncodable, RustcDecodable)] diff --git a/src/librustc_mir/borrow_check/diagnostics/region_errors.rs b/src/librustc_mir/borrow_check/diagnostics/region_errors.rs index cc8a5e0768cba..4daebcec6ffea 100644 --- a/src/librustc_mir/borrow_check/diagnostics/region_errors.rs +++ b/src/librustc_mir/borrow_check/diagnostics/region_errors.rs @@ -589,10 +589,10 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { let mut found = false; for predicate in bounds.predicates { - if let ty::PredicateKind::TypeOutlives(binder) = predicate.kind() { - if let ty::OutlivesPredicate(_, ty::RegionKind::ReStatic) = - binder.skip_binder() - { + if let ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(_, r)) = + predicate.ignore_qualifiers(self.infcx.tcx).skip_binder().kind() + { + if let ty::RegionKind::ReStatic = r { found = true; break; } else { diff --git a/src/librustc_mir/borrow_check/type_check/free_region_relations.rs b/src/librustc_mir/borrow_check/type_check/free_region_relations.rs index beee31812563e..fe8d924debe79 100644 --- a/src/librustc_mir/borrow_check/type_check/free_region_relations.rs +++ b/src/librustc_mir/borrow_check/type_check/free_region_relations.rs @@ -274,7 +274,7 @@ impl UniversalRegionRelationsBuilder<'cx, 'tcx> { // Insert the facts we know from the predicates. Why? Why not. let param_env = self.param_env; - self.add_outlives_bounds(outlives::explicit_outlives_bounds(param_env)); + self.add_outlives_bounds(outlives::explicit_outlives_bounds(self.infcx.tcx, param_env)); // Finally: // - outlives is reflexive, so `'r: 'r` for every region `'r` diff --git a/src/librustc_mir/borrow_check/type_check/mod.rs b/src/librustc_mir/borrow_check/type_check/mod.rs index 9fc5dc0d131c6..8fdedd72c483f 100644 --- a/src/librustc_mir/borrow_check/type_check/mod.rs +++ b/src/librustc_mir/borrow_check/type_check/mod.rs @@ -1021,7 +1021,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { } self.prove_predicate( - ty::PredicateKint::WellFormed(inferred_ty.into()).to_predicate(self.tcx()), + ty::PredicateKind::WellFormed(inferred_ty.into()).to_predicate(self.tcx()), Locations::All(span), ConstraintCategory::TypeAnnotation, ); @@ -1273,7 +1273,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { obligations.obligations.push(traits::Obligation::new( ObligationCause::dummy(), param_env, - ty::PredicateKint::WellFormed(revealed_ty.into()).to_predicate(infcx.tcx), + ty::PredicateKind::WellFormed(revealed_ty.into()).to_predicate(infcx.tcx), )); obligations.add( infcx @@ -1617,7 +1617,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { self.check_call_dest(body, term, &sig, destination, term_location); self.prove_predicates( - sig.inputs_and_output.iter().map(|ty| ty::PredicateKint::WellFormed(ty.into())), + sig.inputs_and_output.iter().map(|ty| ty::PredicateKind::WellFormed(ty.into())), term_location.to_locations(), ConstraintCategory::Boring, ); @@ -2702,7 +2702,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { category: ConstraintCategory, ) { self.prove_predicates( - Some(ty::PredicateKint::Trait( + Some(ty::PredicateKind::Trait( ty::TraitPredicate { trait_ref }, hir::Constness::NotConst, )), diff --git a/src/librustc_mir/transform/qualify_min_const_fn.rs b/src/librustc_mir/transform/qualify_min_const_fn.rs index 52b1eba3b93c2..0d4d102638181 100644 --- a/src/librustc_mir/transform/qualify_min_const_fn.rs +++ b/src/librustc_mir/transform/qualify_min_const_fn.rs @@ -24,7 +24,9 @@ pub fn is_min_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'a Body<'tcx>) - loop { let predicates = tcx.predicates_of(current); for (predicate, _) in predicates.predicates { - match predicate.kind() { + // TODO: forall + match predicate.ignore_qualifiers(tcx).skip_binder().kind() { + ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", predicate), ty::PredicateKind::RegionOutlives(_) | ty::PredicateKind::TypeOutlives(_) | ty::PredicateKind::WellFormed(_) @@ -44,7 +46,7 @@ pub fn is_min_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'a Body<'tcx>) - if Some(pred.def_id()) == tcx.lang_items().sized_trait() { continue; } - match pred.skip_binder().self_ty().kind { + match pred.self_ty().kind { ty::Param(ref p) => { // Allow `T: ?const Trait` if constness == hir::Constness::NotConst diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index 0b97695041be7..74f0e669ef3c3 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -87,16 +87,14 @@ where fn visit_predicates(&mut self, predicates: ty::GenericPredicates<'tcx>) -> bool { let ty::GenericPredicates { parent: _, predicates } = predicates; for (predicate, _span) in predicates { - match predicate.kind() { - ty::PredicateKind::Trait(poly_predicate, _) => { - let ty::TraitPredicate { trait_ref } = poly_predicate.skip_binder(); + // This visitor does not care about bound regions. + match predicate.ignore_qualifiers(self.def_id_visitor.tcx()).skip_binder().kind() { + &ty::PredicateKind::Trait(ty::TraitPredicate { trait_ref }, _) => { if self.visit_trait(trait_ref) { return true; } } - ty::PredicateKind::Projection(poly_predicate) => { - let ty::ProjectionPredicate { projection_ty, ty } = - poly_predicate.skip_binder(); + &ty::PredicateKind::Projection(ty::ProjectionPredicate { projection_ty, ty }) => { if ty.visit_with(self) { return true; } @@ -104,8 +102,7 @@ where return true; } } - ty::PredicateKind::TypeOutlives(poly_predicate) => { - let ty::OutlivesPredicate(ty, _region) = poly_predicate.skip_binder(); + &ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, _region)) => { if ty.visit_with(self) { return true; } diff --git a/src/librustc_trait_selection/opaque_types.rs b/src/librustc_trait_selection/opaque_types.rs index b60531833bd41..3c42400fbbc19 100644 --- a/src/librustc_trait_selection/opaque_types.rs +++ b/src/librustc_trait_selection/opaque_types.rs @@ -1154,8 +1154,10 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> { debug!("instantiate_opaque_types: ty_var={:?}", ty_var); for predicate in &bounds.predicates { - if let ty::PredicateKind::Projection(projection) = predicate.kind() { - if projection.skip_binder().ty.references_error() { + if let ty::PredicateKind::Projection(projection) = + predicate.ignore_qualifiers(tcx).skip_binder().kind() + { + if projection.ty.references_error() { // No point on adding these obligations since there's a type error involved. return ty_var; } @@ -1252,7 +1254,7 @@ crate fn required_region_bounds( traits::elaborate_predicates(tcx, predicates) .filter_map(|obligation| { debug!("required_region_bounds(obligation={:?})", obligation); - match obligation.predicate.kind() { + match obligation.predicate.ignore_qualifiers(tcx).skip_binder().kind() { ty::PredicateKind::Projection(..) | ty::PredicateKind::Trait(..) | ty::PredicateKind::Subtype(..) @@ -1262,7 +1264,8 @@ crate fn required_region_bounds( | ty::PredicateKind::RegionOutlives(..) | ty::PredicateKind::ConstEvaluatable(..) | ty::PredicateKind::ConstEquate(..) => None, - ty::PredicateKind::TypeOutlives(predicate) => { + ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", obligation), + ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ref t, ref r)) => { // Search for a bound of the form `erased_self_ty // : 'a`, but be wary of something like `for<'a> // erased_self_ty : 'a` (we interpret a @@ -1272,7 +1275,6 @@ crate fn required_region_bounds( // it's kind of a moot point since you could never // construct such an object, but this seems // correct even if that code changes). - let ty::OutlivesPredicate(ref t, ref r) = predicate.skip_binder(); if t == &erased_self_ty && !r.has_escaping_bound_vars() { Some(*r) } else { diff --git a/src/librustc_trait_selection/traits/auto_trait.rs b/src/librustc_trait_selection/traits/auto_trait.rs index 8c8550d377a6e..9c530912bf0e4 100644 --- a/src/librustc_trait_selection/traits/auto_trait.rs +++ b/src/librustc_trait_selection/traits/auto_trait.rs @@ -344,8 +344,7 @@ impl AutoTraitFinder<'tcx> { already_visited.remove(&pred); self.add_user_pred( &mut user_computed_preds, - ty::PredicateKind::Trait(pred, hir::Constness::NotConst) - .to_predicate(self.tcx), + pred.without_const().to_predicate(self.tcx), ); predicates.push_back(pred); } else { @@ -408,21 +407,23 @@ impl AutoTraitFinder<'tcx> { /// under which a type implements an auto trait. A trait predicate involving /// a HRTB means that the type needs to work with any choice of lifetime, /// not just one specific lifetime (e.g., `'static`). - fn add_user_pred<'c>( + fn add_user_pred( &self, - user_computed_preds: &mut FxHashSet>, - new_pred: ty::Predicate<'c>, + user_computed_preds: &mut FxHashSet>, + new_pred: ty::Predicate<'tcx>, ) { let mut should_add_new = true; user_computed_preds.retain(|&old_pred| { if let ( ty::PredicateKind::Trait(new_trait, _), ty::PredicateKind::Trait(old_trait, _), - ) = (new_pred.kind(), old_pred.kind()) - { + ) = ( + new_pred.ignore_qualifiers(self.tcx).skip_binder().kind(), + old_pred.ignore_qualifiers(self.tcx).skip_binder().kind(), + ) { if new_trait.def_id() == old_trait.def_id() { - let new_substs = new_trait.skip_binder().trait_ref.substs; - let old_substs = old_trait.skip_binder().trait_ref.substs; + let new_substs = new_trait.trait_ref.substs; + let old_substs = old_trait.trait_ref.substs; if !new_substs.types().eq(old_substs.types()) { // We can't compare lifetimes if the types are different, @@ -618,11 +619,12 @@ impl AutoTraitFinder<'tcx> { ) -> bool { let dummy_cause = ObligationCause::dummy(); - for (obligation, mut predicate) in nested.map(|o| (o.clone(), o.predicate)) { - let is_new_pred = fresh_preds.insert(self.clean_pred(select.infcx(), predicate)); + for obligation in nested { + let is_new_pred = + fresh_preds.insert(self.clean_pred(select.infcx(), obligation.predicate)); // Resolve any inference variables that we can, to help selection succeed - predicate = select.infcx().resolve_vars_if_possible(&predicate); + let predicate = select.infcx().resolve_vars_if_possible(&obligation.predicate); // We only add a predicate as a user-displayable bound if // it involves a generic parameter, and doesn't contain @@ -636,17 +638,20 @@ impl AutoTraitFinder<'tcx> { // // We check this by calling is_of_param on the relevant types // from the various possible predicates - match predicate.kind() { + + // TODO: forall + match predicate.ignore_qualifiers(self.tcx).skip_binder().kind() { &ty::PredicateKind::Trait(p, _) => { - if self.is_param_no_infer(p.skip_binder().trait_ref.substs) + if self.is_param_no_infer(p.trait_ref.substs) && !only_projections && is_new_pred { self.add_user_pred(computed_preds, predicate); } - predicates.push_back(p); + predicates.push_back(ty::Binder::bind(p)); } &ty::PredicateKind::Projection(p) => { + let p = ty::Binder::bind(p); debug!( "evaluate_nested_obligations: examining projection predicate {:?}", predicate @@ -772,11 +777,13 @@ impl AutoTraitFinder<'tcx> { } } &ty::PredicateKind::RegionOutlives(binder) => { + let binder = ty::Binder::bind(binder); if select.infcx().region_outlives_predicate(&dummy_cause, binder).is_err() { return false; } } &ty::PredicateKind::TypeOutlives(binder) => { + let binder = ty::Binder::bind(binder); match ( binder.no_bound_vars(), binder.map_bound_ref(|pred| pred.0).no_bound_vars(), diff --git a/src/librustc_trait_selection/traits/error_reporting/mod.rs b/src/librustc_trait_selection/traits/error_reporting/mod.rs index 112de68466084..8a2915bb30c71 100644 --- a/src/librustc_trait_selection/traits/error_reporting/mod.rs +++ b/src/librustc_trait_selection/traits/error_reporting/mod.rs @@ -255,9 +255,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { .emit(); return; } - match obligation.predicate.kind() { - ty::PredicateKind::Trait(ref trait_predicate, _) => { - let trait_predicate = self.resolve_vars_if_possible(trait_predicate); + + // TODO: forall + match obligation.predicate.ignore_qualifiers(tcx).skip_binder().kind() { + ty::PredicateKind::ForAll(_) => { + bug!("unexpected predicate: {:?}", obligation.predicate) + } + &ty::PredicateKind::Trait(trait_predicate, _) => { + let trait_predicate = ty::Binder::bind(trait_predicate); + let trait_predicate = self.resolve_vars_if_possible(&trait_predicate); if self.tcx.sess.has_errors() && trait_predicate.references_error() { return; @@ -503,14 +509,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { ); trait_pred }); - let unit_obligation = Obligation { - predicate: ty::PredicateKind::Trait( - predicate, - hir::Constness::NotConst, - ) - .to_predicate(self.tcx), - ..obligation.clone() - }; + let unit_obligation = + obligation.with(predicate.without_const().to_predicate(tcx)); if self.predicate_may_hold(&unit_obligation) { err.note( "the trait is implemented for `()`. \ @@ -526,15 +526,16 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { err } - ty::PredicateKind::Subtype(ref predicate) => { + ty::PredicateKind::Subtype(predicate) => { // Errors for Subtype predicates show up as // `FulfillmentErrorCode::CodeSubtypeError`, // not selection error. span_bug!(span, "subtype requirement gave wrong error: `{:?}`", predicate) } - ty::PredicateKind::RegionOutlives(ref predicate) => { - let predicate = self.resolve_vars_if_possible(predicate); + &ty::PredicateKind::RegionOutlives(predicate) => { + let predicate = ty::Binder::bind(predicate); + let predicate = self.resolve_vars_if_possible(&predicate); let err = self .region_outlives_predicate(&obligation.cause, predicate) .err() @@ -1089,8 +1090,14 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { return true; } - let (cond, error) = match (cond.kind(), error.kind()) { - (ty::PredicateKind::Trait(..), ty::PredicateKind::Trait(error, _)) => (cond, error), + // FIXME: It should be possible to deal with `ForAll` in a cleaner way. + let (cond, error) = match ( + cond.ignore_qualifiers(self.tcx).skip_binder().kind(), + error.ignore_qualifiers(self.tcx).skip_binder().kind(), + ) { + (ty::PredicateKind::Trait(..), &ty::PredicateKind::Trait(error, _)) => { + (cond, ty::Binder::bind(error)) + } _ => { // FIXME: make this work in other cases too. return false; @@ -1098,9 +1105,11 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { }; for obligation in super::elaborate_predicates(self.tcx, std::iter::once(cond)) { - if let ty::PredicateKind::Trait(implication, _) = obligation.predicate.kind() { + if let &ty::PredicateKind::Trait(implication, _) = + obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind() + { let error = error.to_poly_trait_ref(); - let implication = implication.to_poly_trait_ref(); + let implication = ty::Binder::bind(implication).to_poly_trait_ref(); // FIXME: I'm just not taking associated types at all here. // Eventually I'll need to implement param-env-aware // `Γ₁ ⊦ φ₁ => Γ₂ ⊦ φ₂` logic. @@ -1178,12 +1187,14 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { // // this can fail if the problem was higher-ranked, in which // cause I have no idea for a good error message. - if let ty::PredicateKind::Projection(ref data) = predicate.kind() { + if let &ty::PredicateKind::Projection(data) = + predicate.ignore_qualifiers(self.tcx).skip_binder().kind() + { let mut selcx = SelectionContext::new(self); let (data, _) = self.replace_bound_vars_with_fresh_vars( obligation.cause.span, infer::LateBoundRegionConversionTime::HigherRankedType, - data, + &ty::Binder::bind(data), ); let mut obligations = vec![]; let normalized_ty = super::normalize_projection_type( @@ -1470,9 +1481,10 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { return; } - let mut err = match predicate.kind() { - ty::PredicateKind::Trait(ref data, _) => { - let trait_ref = data.to_poly_trait_ref(); + // TODO: forall + let mut err = match predicate.ignore_qualifiers(self.tcx).skip_binder().kind() { + &ty::PredicateKind::Trait(data, _) => { + let trait_ref = ty::Binder::bind(data.trait_ref); let self_ty = trait_ref.skip_binder().self_ty(); debug!("self_ty {:?} {:?} trait_ref {:?}", self_ty, self_ty.kind, trait_ref); @@ -1571,6 +1583,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { } ty::PredicateKind::WellFormed(arg) => { + // TODO: forall + // Same hacky approach as above to avoid deluging user // with error messages. if arg.references_error() || self.tcx.sess.has_errors() { @@ -1595,15 +1609,15 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { // no need to overload user in such cases return; } - let SubtypePredicate { a_is_expected: _, a, b } = data.skip_binder(); + let &SubtypePredicate { a_is_expected: _, a, b } = data; // both must be type variables, or the other would've been instantiated assert!(a.is_ty_var() && b.is_ty_var()); self.need_type_info_err(body_id, span, a, ErrorCode::E0282) } - ty::PredicateKind::Projection(ref data) => { - let trait_ref = data.to_poly_trait_ref(self.tcx); + &ty::PredicateKind::Projection(data) => { + let trait_ref = ty::Binder::bind(data).to_poly_trait_ref(self.tcx); let self_ty = trait_ref.skip_binder().self_ty(); - let ty = data.skip_binder().ty; + let ty = data.ty; if predicate.references_error() { return; } @@ -1724,16 +1738,16 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { obligation: &PredicateObligation<'tcx>, ) { let (pred, item_def_id, span) = - match (obligation.predicate.kind(), &obligation.cause.code.peel_derives()) { + match (obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind(), obligation.cause.code.peel_derives()) { ( ty::PredicateKind::Trait(pred, _), - ObligationCauseCode::BindingObligation(item_def_id, span), + &ObligationCauseCode::BindingObligation(item_def_id, span), ) => (pred, item_def_id, span), _ => return, }; let node = match ( - self.tcx.hir().get_if_local(*item_def_id), + self.tcx.hir().get_if_local(item_def_id), Some(pred.def_id()) == self.tcx.lang_items().sized_trait(), ) { (Some(node), true) => node, @@ -1744,7 +1758,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { None => return, }; for param in generics.params { - if param.span != *span + if param.span != span || param.bounds.iter().any(|bound| { bound.trait_ref().and_then(|trait_ref| trait_ref.trait_def_id()) == self.tcx.lang_items().sized_trait() diff --git a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs index 0632ce2319aee..7e3d4908f9af6 100644 --- a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs +++ b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs @@ -1299,12 +1299,11 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { // the type. The last generator (`outer_generator` below) has information about where the // bound was introduced. At least one generator should be present for this diagnostic to be // modified. - let (mut trait_ref, mut target_ty) = match obligation.predicate.kind() { - ty::PredicateKind::Trait(p, _) => { - (Some(p.skip_binder().trait_ref), Some(p.skip_binder().self_ty())) - } - _ => (None, None), - }; + let (mut trait_ref, mut target_ty) = + match obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind() { + ty::PredicateKind::Trait(p, _) => (Some(p.trait_ref), Some(p.self_ty())), + _ => (None, None), + }; let mut generator = None; let mut outer_generator = None; let mut next_code = Some(&obligation.cause.code); diff --git a/src/librustc_trait_selection/traits/fulfill.rs b/src/librustc_trait_selection/traits/fulfill.rs index a6f2fe78d28cc..995be1d90de36 100644 --- a/src/librustc_trait_selection/traits/fulfill.rs +++ b/src/librustc_trait_selection/traits/fulfill.rs @@ -6,7 +6,7 @@ use rustc_errors::ErrorReported; use rustc_infer::traits::{PolyTraitObligation, TraitEngine, TraitEngineExt as _}; use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::ty::error::ExpectedFound; -use rustc_middle::ty::{self, Binder, Const, ToPredicate, Ty, TypeFoldable}; +use rustc_middle::ty::{self, Binder, Const, Ty, TypeFoldable}; use std::marker::PhantomData; use super::project; @@ -318,12 +318,12 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { let infcx = self.selcx.infcx(); - match obligation.predicate.kint(infcx.tcx) { - ty::PredicateKint::ForAll(binder) => match binder.skip_binder() { + match obligation.predicate.kind() { + ty::PredicateKind::ForAll(binder) => match binder.skip_binder().kind() { // Evaluation will discard candidates using the leak check. // This means we need to pass it the bound version of our // predicate. - rustc_middle::ty::PredicateKint::Trait(trait_ref, _constness) => { + ty::PredicateKind::Trait(trait_ref, _constness) => { let trait_obligation = obligation.with(Binder::bind(*trait_ref)); self.process_trait_obligation( @@ -332,7 +332,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { &mut pending_obligation.stalled_on, ) } - rustc_middle::ty::PredicateKint::Projection(projection) => { + ty::PredicateKind::Projection(projection) => { let project_obligation = obligation.with(Binder::bind(*projection)); self.process_projection_obligation( @@ -340,22 +340,20 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { &mut pending_obligation.stalled_on, ) } - rustc_middle::ty::PredicateKint::RegionOutlives(_) - | rustc_middle::ty::PredicateKint::TypeOutlives(_) - | rustc_middle::ty::PredicateKint::WellFormed(_) - | rustc_middle::ty::PredicateKint::ObjectSafe(_) - | rustc_middle::ty::PredicateKint::ClosureKind(..) - | rustc_middle::ty::PredicateKint::Subtype(_) - | rustc_middle::ty::PredicateKint::ConstEvaluatable(..) - | rustc_middle::ty::PredicateKint::ConstEquate(..) - | rustc_middle::ty::PredicateKint::ForAll(_) => { + ty::PredicateKind::RegionOutlives(_) + | ty::PredicateKind::TypeOutlives(_) + | ty::PredicateKind::WellFormed(_) + | ty::PredicateKind::ObjectSafe(_) + | ty::PredicateKind::ClosureKind(..) + | ty::PredicateKind::Subtype(_) + | ty::PredicateKind::ConstEvaluatable(..) + | ty::PredicateKind::ConstEquate(..) + | ty::PredicateKind::ForAll(_) => { let (pred, _) = infcx.replace_bound_vars_with_placeholders(binder); - ProcessResult::Changed(mk_pending(vec![ - obligation.with(pred.to_predicate(infcx.tcx)), - ])) + ProcessResult::Changed(mk_pending(vec![obligation.with(pred)])) } }, - ty::PredicateKint::Trait(ref data, _) => { + ty::PredicateKind::Trait(ref data, _) => { let trait_obligation = obligation.with(Binder::dummy(*data)); self.process_trait_obligation( @@ -365,14 +363,14 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { ) } - &ty::PredicateKint::RegionOutlives(data) => { + &ty::PredicateKind::RegionOutlives(data) => { match infcx.region_outlives_predicate(&obligation.cause, Binder::dummy(data)) { Ok(()) => ProcessResult::Changed(vec![]), Err(_) => ProcessResult::Error(CodeSelectionError(Unimplemented)), } } - ty::PredicateKint::TypeOutlives(ty::OutlivesPredicate(t_a, r_b)) => { + ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(t_a, r_b)) => { if self.register_region_obligations { self.selcx.infcx().register_region_obligation_with_cause( t_a, @@ -383,7 +381,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { ProcessResult::Changed(vec![]) } - ty::PredicateKint::Projection(ref data) => { + ty::PredicateKind::Projection(ref data) => { let project_obligation = obligation.with(Binder::dummy(*data)); self.process_projection_obligation( @@ -392,7 +390,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { ) } - &ty::PredicateKint::ObjectSafe(trait_def_id) => { + &ty::PredicateKind::ObjectSafe(trait_def_id) => { if !self.selcx.tcx().is_object_safe(trait_def_id) { ProcessResult::Error(CodeSelectionError(Unimplemented)) } else { @@ -400,7 +398,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { } } - &ty::PredicateKint::ClosureKind(_, closure_substs, kind) => { + &ty::PredicateKind::ClosureKind(_, closure_substs, kind) => { match self.selcx.infcx().closure_kind(closure_substs) { Some(closure_kind) => { if closure_kind.extends(kind) { @@ -413,7 +411,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { } } - &ty::PredicateKint::WellFormed(arg) => { + &ty::PredicateKind::WellFormed(arg) => { match wf::obligations( self.selcx.infcx(), obligation.param_env, @@ -430,7 +428,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { } } - &ty::PredicateKint::Subtype(subtype) => { + &ty::PredicateKind::Subtype(subtype) => { match self.selcx.infcx().subtype_predicate( &obligation.cause, obligation.param_env, @@ -456,7 +454,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { } } - &ty::PredicateKint::ConstEvaluatable(def_id, substs) => { + &ty::PredicateKind::ConstEvaluatable(def_id, substs) => { match self.selcx.infcx().const_eval_resolve( obligation.param_env, def_id, @@ -469,7 +467,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { } } - ty::PredicateKint::ConstEquate(c1, c2) => { + ty::PredicateKind::ConstEquate(c1, c2) => { debug!("equating consts: c1={:?} c2={:?}", c1, c2); let stalled_on = &mut pending_obligation.stalled_on; diff --git a/src/librustc_trait_selection/traits/mod.rs b/src/librustc_trait_selection/traits/mod.rs index 1c3755222495e..da17d89b8c13a 100644 --- a/src/librustc_trait_selection/traits/mod.rs +++ b/src/librustc_trait_selection/traits/mod.rs @@ -237,7 +237,7 @@ fn do_normalize_predicates<'tcx>( // We can use the `elaborated_env` here; the region code only // cares about declarations like `'a: 'b`. - let outlives_env = OutlivesEnvironment::new(elaborated_env); + let outlives_env = OutlivesEnvironment::new(tcx, elaborated_env); infcx.resolve_regions_and_report_errors( region_context, @@ -328,7 +328,7 @@ pub fn normalize_param_env_or_error<'tcx>( // This works fairly well because trait matching does not actually care about param-env // TypeOutlives predicates - these are normally used by regionck. let outlives_predicates: Vec<_> = predicates - .drain_filter(|predicate| match predicate.kind() { + .drain_filter(|predicate| match predicate.ignore_qualifiers(tcx).skip_binder().kind() { ty::PredicateKind::TypeOutlives(..) => true, _ => false, }) diff --git a/src/librustc_trait_selection/traits/object_safety.rs b/src/librustc_trait_selection/traits/object_safety.rs index f00d668e1ae50..cb03751bb7861 100644 --- a/src/librustc_trait_selection/traits/object_safety.rs +++ b/src/librustc_trait_selection/traits/object_safety.rs @@ -245,14 +245,11 @@ fn predicates_reference_self( .iter() .map(|(predicate, sp)| (predicate.subst_supertrait(tcx, &trait_ref), sp)) .filter_map(|(predicate, &sp)| { - match predicate.kind() { + // TODO: forall + match predicate.ignore_qualifiers(tcx).skip_binder().kind() { ty::PredicateKind::Trait(ref data, _) => { // In the case of a trait predicate, we can skip the "self" type. - if data.skip_binder().trait_ref.substs[1..].iter().any(has_self_ty) { - Some(sp) - } else { - None - } + if data.trait_ref.substs[1..].iter().any(has_self_ty) { Some(sp) } else { None } } ty::PredicateKind::Projection(ref data) => { // And similarly for projections. This should be redundant with @@ -267,10 +264,7 @@ fn predicates_reference_self( // // This is ALT2 in issue #56288, see that for discussion of the // possible alternatives. - if data.skip_binder().projection_ty.trait_ref(tcx).substs[1..] - .iter() - .any(has_self_ty) - { + if data.projection_ty.trait_ref(tcx).substs[1..].iter().any(has_self_ty) { Some(sp) } else { None @@ -284,6 +278,7 @@ fn predicates_reference_self( | ty::PredicateKind::Subtype(..) | ty::PredicateKind::ConstEvaluatable(..) | ty::PredicateKind::ConstEquate(..) => None, + ty::PredicateKind::ForAll(..) => bug!("unexpected predicate: {:?}", predicate), } }) .collect() @@ -305,10 +300,10 @@ fn generics_require_sized_self(tcx: TyCtxt<'_>, def_id: DefId) -> bool { let predicates = tcx.predicates_of(def_id); let predicates = predicates.instantiate_identity(tcx).predicates; elaborate_predicates(tcx, predicates.into_iter()).any(|obligation| { - match obligation.predicate.kind() { + // TODO: forall + match obligation.predicate.ignore_qualifiers(tcx).skip_binder().kind() { ty::PredicateKind::Trait(ref trait_pred, _) => { - trait_pred.def_id() == sized_def_id - && trait_pred.skip_binder().self_ty().is_param(0) + trait_pred.def_id() == sized_def_id && trait_pred.self_ty().is_param(0) } ty::PredicateKind::Projection(..) | ty::PredicateKind::Subtype(..) @@ -319,6 +314,9 @@ fn generics_require_sized_self(tcx: TyCtxt<'_>, def_id: DefId) -> bool { | ty::PredicateKind::TypeOutlives(..) | ty::PredicateKind::ConstEvaluatable(..) | ty::PredicateKind::ConstEquate(..) => false, + ty::PredicateKind::ForAll(_) => { + bug!("unexpected predicate: {:?}", obligation.predicate) + } } }) } @@ -404,7 +402,7 @@ fn virtual_call_violation_for_method<'tcx>( // A trait object can't claim to live more than the concrete type, // so outlives predicates will always hold. .cloned() - .filter(|(p, _)| p.to_opt_type_outlives().is_none()) + .filter(|(p, _)| p.to_opt_type_outlives(tcx).is_none()) .collect::>() // Do a shallow visit so that `contains_illegal_self_type_reference` // may apply it's custom visiting. diff --git a/src/librustc_trait_selection/traits/project.rs b/src/librustc_trait_selection/traits/project.rs index c08198ec373b8..30c86055e409e 100644 --- a/src/librustc_trait_selection/traits/project.rs +++ b/src/librustc_trait_selection/traits/project.rs @@ -664,23 +664,25 @@ fn prune_cache_value_obligations<'a, 'tcx>( let mut obligations: Vec<_> = result .obligations .iter() - .filter(|obligation| match obligation.predicate.kind() { - // We found a `T: Foo` predicate, let's check - // if `U` references any unresolved type - // variables. In principle, we only care if this - // projection can help resolve any of the type - // variables found in `result.value` -- but we just - // check for any type variables here, for fear of - // indirect obligations (e.g., we project to `?0`, - // but we have `T: Foo` and `?1: Bar`). - ty::PredicateKind::Projection(ref data) => { - infcx.unresolved_type_vars(&data.ty()).is_some() - } + .filter(|obligation| { + match obligation.predicate.ignore_qualifiers(infcx.tcx).skip_binder().kind() { + // We found a `T: Foo` predicate, let's check + // if `U` references any unresolved type + // variables. In principle, we only care if this + // projection can help resolve any of the type + // variables found in `result.value` -- but we just + // check for any type variables here, for fear of + // indirect obligations (e.g., we project to `?0`, + // but we have `T: Foo` and `?1: Bar`). + &ty::PredicateKind::Projection(data) => { + infcx.unresolved_type_vars(&ty::Binder::bind(data.ty)).is_some() + } - // We are only interested in `T: Foo` predicates, whre - // `U` references one of `unresolved_type_vars`. =) - _ => false, + // We are only interested in `T: Foo` predicates, whre + // `U` references one of `unresolved_type_vars`. =) + _ => false, + } }) .cloned() .collect(); @@ -931,7 +933,11 @@ fn assemble_candidates_from_predicates<'cx, 'tcx>( let infcx = selcx.infcx(); for predicate in env_predicates { debug!("assemble_candidates_from_predicates: predicate={:?}", predicate); - if let &ty::PredicateKind::Projection(data) = predicate.kind() { + // TODO: forall + if let &ty::PredicateKind::Projection(data) = + predicate.ignore_qualifiers(infcx.tcx).skip_binder().kind() + { + let data = ty::Binder::bind(data); let same_def_id = data.projection_def_id() == obligation.predicate.item_def_id; let is_match = same_def_id @@ -1221,13 +1227,17 @@ fn confirm_object_candidate<'cx, 'tcx>( // select only those projections that are actually projecting an // item with the correct name - let env_predicates = env_predicates.filter_map(|o| match o.predicate.kind() { - &ty::PredicateKind::Projection(data) - if data.projection_def_id() == obligation.predicate.item_def_id => - { - Some(data) + + // TODO: forall + let env_predicates = env_predicates.filter_map(|o| { + match o.predicate.ignore_qualifiers(selcx.tcx()).skip_binder().kind() { + &ty::PredicateKind::Projection(data) + if data.projection_ty.item_def_id == obligation.predicate.item_def_id => + { + Some(ty::Binder::bind(data)) + } + _ => None, } - _ => None, }); // select those with a relevant trait-ref diff --git a/src/librustc_trait_selection/traits/query/type_op/prove_predicate.rs b/src/librustc_trait_selection/traits/query/type_op/prove_predicate.rs index 5c8719da14e6f..f2a6677e2f626 100644 --- a/src/librustc_trait_selection/traits/query/type_op/prove_predicate.rs +++ b/src/librustc_trait_selection/traits/query/type_op/prove_predicate.rs @@ -15,10 +15,12 @@ impl<'tcx> super::QueryTypeOp<'tcx> for ProvePredicate<'tcx> { // `&T`, accounts for about 60% percentage of the predicates // we have to prove. No need to canonicalize and all that for // such cases. - if let ty::PredicateKind::Trait(trait_ref, _) = key.value.predicate.kind() { + if let ty::PredicateKind::Trait(trait_ref, _) = + key.value.predicate.ignore_qualifiers(tcx).skip_binder().kind() + { if let Some(sized_def_id) = tcx.lang_items().sized_trait() { if trait_ref.def_id() == sized_def_id { - if trait_ref.skip_binder().self_ty().is_trivially_sized(tcx) { + if trait_ref.self_ty().is_trivially_sized(tcx) { return Some(()); } } diff --git a/src/librustc_trait_selection/traits/select/candidate_assembly.rs b/src/librustc_trait_selection/traits/select/candidate_assembly.rs index 1d5441b8eff85..7b654856ddead 100644 --- a/src/librustc_trait_selection/traits/select/candidate_assembly.rs +++ b/src/librustc_trait_selection/traits/select/candidate_assembly.rs @@ -181,6 +181,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { stack: &TraitObligationStack<'o, 'tcx>, candidates: &mut SelectionCandidateSet<'tcx>, ) -> Result<(), SelectionError<'tcx>> { + let tcx = self.tcx(); debug!("assemble_candidates_from_caller_bounds({:?})", stack.obligation); let all_bounds = stack @@ -188,7 +189,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { .param_env .caller_bounds() .iter() - .filter_map(|o| o.to_opt_poly_trait_ref()); + .filter_map(move |o| o.to_opt_poly_trait_ref(tcx)); // Micro-optimization: filter out predicates relating to different traits. let matching_bounds = diff --git a/src/librustc_trait_selection/traits/select/confirmation.rs b/src/librustc_trait_selection/traits/select/confirmation.rs index b9f39fd23cbb6..fa970589bbbf6 100644 --- a/src/librustc_trait_selection/traits/select/confirmation.rs +++ b/src/librustc_trait_selection/traits/select/confirmation.rs @@ -532,7 +532,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { obligations.push(Obligation::new( obligation.cause.clone(), obligation.param_env, - ty::PredicateKint::ClosureKind(closure_def_id, substs, kind) + ty::PredicateKind::ClosureKind(closure_def_id, substs, kind) .to_predicate(self.tcx()), )); } diff --git a/src/librustc_trait_selection/traits/select/mod.rs b/src/librustc_trait_selection/traits/select/mod.rs index 5dc5fb797ff6c..5683303605511 100644 --- a/src/librustc_trait_selection/traits/select/mod.rs +++ b/src/librustc_trait_selection/traits/select/mod.rs @@ -35,7 +35,9 @@ use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::ty::fast_reject; use rustc_middle::ty::relate::TypeRelation; use rustc_middle::ty::subst::{GenericArgKind, Subst, SubstsRef}; -use rustc_middle::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, TypeFoldable}; +use rustc_middle::ty::{ + self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness, +}; use rustc_span::symbol::sym; use std::cell::{Cell, RefCell}; @@ -406,14 +408,20 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { None => self.check_recursion_limit(&obligation, &obligation)?, } - match obligation.predicate.kind() { + // TODO: forall + match obligation.predicate.ignore_qualifiers(self.tcx()).skip_binder().kind() { + ty::PredicateKind::ForAll(_) => { + bug!("unexpected predicate: {:?}", obligation.predicate) + } &ty::PredicateKind::Trait(t, _) => { + let t = ty::Binder::bind(t); debug_assert!(!t.has_escaping_bound_vars()); let obligation = obligation.with(t); self.evaluate_trait_predicate_recursively(previous_stack, obligation) } &ty::PredicateKind::Subtype(p) => { + let p = ty::Binder::bind(p); // Does this code ever run? match self.infcx.subtype_predicate(&obligation.cause, obligation.param_env, p) { Some(Ok(InferOk { mut obligations, .. })) => { @@ -456,6 +464,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } &ty::PredicateKind::Projection(data) => { + let data = ty::Binder::bind(data); let project_obligation = obligation.with(data); match project::poly_project_and_unify_type(self, &project_obligation) { Ok(Some(mut subobligations)) => { @@ -669,10 +678,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { // if the regions match exactly. let cycle = stack.iter().skip(1).take_while(|s| s.depth >= cycle_depth); let tcx = self.tcx(); - let cycle = cycle.map(|stack| { - ty::PredicateKind::Trait(stack.obligation.predicate, hir::Constness::NotConst) - .to_predicate(tcx) - }); + let cycle = + cycle.map(|stack| stack.obligation.predicate.without_const().to_predicate(tcx)); if self.coinductive_match(cycle) { debug!("evaluate_stack({:?}) --> recursive, coinductive", stack.fresh_trait_ref); Some(EvaluatedToOk) @@ -786,7 +793,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } fn coinductive_predicate(&self, predicate: ty::Predicate<'tcx>) -> bool { - let result = match predicate.kind() { + let result = match predicate.ignore_qualifiers(self.tcx()).skip_binder().kind() { ty::PredicateKind::Trait(ref data, _) => self.tcx().trait_is_auto(data.def_id()), _ => false, }; diff --git a/src/librustc_trait_selection/traits/specialize/mod.rs b/src/librustc_trait_selection/traits/specialize/mod.rs index 9b737d464174a..dda2e03d77937 100644 --- a/src/librustc_trait_selection/traits/specialize/mod.rs +++ b/src/librustc_trait_selection/traits/specialize/mod.rs @@ -497,7 +497,7 @@ fn to_pretty_impl_header(tcx: TyCtxt<'_>, impl_def_id: DefId) -> Option 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 let Some(poly_trait_ref) = p.to_opt_poly_trait_ref(tcx) { if Some(poly_trait_ref.def_id()) == sized_trait { types_without_default_bounds.remove(poly_trait_ref.self_ty().skip_binder()); continue; diff --git a/src/librustc_trait_selection/traits/util.rs b/src/librustc_trait_selection/traits/util.rs index cc8997078e0f0..d2fdfb45617cc 100644 --- a/src/librustc_trait_selection/traits/util.rs +++ b/src/librustc_trait_selection/traits/util.rs @@ -120,7 +120,7 @@ impl<'tcx> TraitAliasExpander<'tcx> { let items = predicates.predicates.iter().rev().filter_map(|(pred, span)| { pred.subst_supertrait(tcx, &trait_ref) - .to_opt_poly_trait_ref() + .to_opt_poly_trait_ref(tcx) .map(|trait_ref| item.clone_and_push(trait_ref, *span)) }); debug!("expand_trait_aliases: items={:?}", items.clone()); @@ -170,6 +170,7 @@ impl Iterator for SupertraitDefIds<'tcx> { type Item = DefId; fn next(&mut self) -> Option { + let tcx = self.tcx; let def_id = self.stack.pop()?; let predicates = self.tcx.super_predicates_of(def_id); let visited = &mut self.visited; @@ -177,7 +178,7 @@ impl Iterator for SupertraitDefIds<'tcx> { predicates .predicates .iter() - .filter_map(|(pred, _)| pred.to_opt_poly_trait_ref()) + .filter_map(move |(pred, _)| pred.to_opt_poly_trait_ref(tcx)) .map(|trait_ref| trait_ref.def_id()) .filter(|&super_def_id| visited.insert(super_def_id)), ); diff --git a/src/librustc_trait_selection/traits/wf.rs b/src/librustc_trait_selection/traits/wf.rs index b72359129e9b1..da1a34871d025 100644 --- a/src/librustc_trait_selection/traits/wf.rs +++ b/src/librustc_trait_selection/traits/wf.rs @@ -88,33 +88,33 @@ pub fn predicate_obligations<'a, 'tcx>( infcx: &InferCtxt<'a, 'tcx>, param_env: ty::ParamEnv<'tcx>, body_id: hir::HirId, - predicate: &'tcx ty::PredicateKint<'tcx>, + predicate: &'_ ty::Predicate<'tcx>, span: Span, ) -> Vec> { let mut wf = WfPredicates { infcx, param_env, body_id, span, out: vec![], item: None }; - match predicate { - ty::PredicateKint::ForAll(binder) => { + match predicate.kind() { + ty::PredicateKind::ForAll(binder) => { // It's ok to skip the binder here because wf code is prepared for it - return predicate_obligations(infcx, param_env, body_id, *binder.skip_binder(), span); + return predicate_obligations(infcx, param_env, body_id, binder.skip_binder(), span); } - ty::PredicateKint::Trait(t, _) => { + ty::PredicateKind::Trait(t, _) => { wf.compute_trait_ref(&t.trait_ref, Elaborate::None); } - ty::PredicateKint::RegionOutlives(..) => {} - &ty::PredicateKint::TypeOutlives(ty::OutlivesPredicate(ty, _reg)) => { + ty::PredicateKind::RegionOutlives(..) => {} + &ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, _reg)) => { wf.compute(ty.into()); } - ty::PredicateKint::Projection(t) => { + ty::PredicateKind::Projection(t) => { wf.compute_projection(t.projection_ty); wf.compute(t.ty.into()); } - &ty::PredicateKint::WellFormed(arg) => { + &ty::PredicateKind::WellFormed(arg) => { wf.compute(arg); } - ty::PredicateKint::ObjectSafe(_) => {} - ty::PredicateKint::ClosureKind(..) => {} - &ty::PredicateKint::Subtype(ty::SubtypePredicate { a, b, a_is_expected: _ }) => { + ty::PredicateKind::ObjectSafe(_) => {} + ty::PredicateKind::ClosureKind(..) => {} + &ty::PredicateKind::Subtype(ty::SubtypePredicate { a, b, a_is_expected: _ }) => { wf.compute(a.into()); wf.compute(b.into()); } @@ -126,7 +126,7 @@ pub fn predicate_obligations<'a, 'tcx>( wf.compute(arg); } } - &ty::PredicateKint::ConstEquate(c1, c2) => { + &ty::PredicateKind::ConstEquate(c1, c2) => { wf.compute(c1.into()); wf.compute(c2.into()); } @@ -178,7 +178,7 @@ fn extend_cause_with_original_assoc_item_obligation<'tcx>( trait_ref: &ty::TraitRef<'tcx>, item: Option<&hir::Item<'tcx>>, cause: &mut traits::ObligationCause<'tcx>, - pred: &ty::Predicate<'_>, + pred: &ty::Predicate<'tcx>, mut trait_assoc_items: impl Iterator, ) { debug!( @@ -194,15 +194,16 @@ fn extend_cause_with_original_assoc_item_obligation<'tcx>( hir::ImplItemKind::Const(ty, _) | hir::ImplItemKind::TyAlias(ty) => ty.span, _ => impl_item_ref.span, }; - match pred.kind() { + + // It is fine to skip the binder as we don't care about regions here. + match pred.ignore_qualifiers(tcx).skip_binder().kind() { ty::PredicateKind::Projection(proj) => { // The obligation comes not from the current `impl` nor the `trait` being implemented, // but rather from a "second order" obligation, where an associated type has a // projection coming from another associated type. See // `src/test/ui/associated-types/point-at-type-on-obligation-failure.rs` and // `traits-assoc-type-in-supertrait-bad.rs`. - let kind = &proj.ty().skip_binder().kind; - if let ty::Projection(projection_ty) = kind { + if let ty::Projection(projection_ty) = proj.ty.kind { let trait_assoc_item = tcx.associated_item(projection_ty.item_def_id); if let Some(impl_item_span) = items.iter().find(|item| item.ident == trait_assoc_item.ident).map(fix_span) @@ -215,11 +216,9 @@ fn extend_cause_with_original_assoc_item_obligation<'tcx>( // An associated item obligation born out of the `trait` failed to be met. An example // can be seen in `ui/associated-types/point-at-type-on-obligation-failure-2.rs`. debug!("extended_cause_with_original_assoc_item_obligation trait proj {:?}", pred); - if let ty::Projection(ty::ProjectionTy { item_def_id, .. }) = - &pred.skip_binder().self_ty().kind - { + if let ty::Projection(ty::ProjectionTy { item_def_id, .. }) = pred.self_ty().kind { if let Some(impl_item_span) = trait_assoc_items - .find(|i| i.def_id == *item_def_id) + .find(|i| i.def_id == item_def_id) .and_then(|trait_assoc_item| { items.iter().find(|i| i.ident == trait_assoc_item.ident).map(fix_span) }) @@ -270,7 +269,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { 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_ref(tcx) { let derived_cause = traits::DerivedObligationCause { parent_trait_ref, parent_code: Rc::new(obligation.cause.code.clone()), @@ -318,7 +317,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { traits::Obligation::new( new_cause, param_env, - ty::PredicateKint::WellFormed(arg).to_predicate(tcx), + ty::PredicateKind::WellFormed(arg).to_predicate(tcx), ) }), ); @@ -397,7 +396,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { self.out.push(traits::Obligation::new( cause, self.param_env, - ty::PredicateKint::WellFormed(resolved_constant.into()) + ty::PredicateKind::WellFormed(resolved_constant.into()) .to_predicate(self.tcx()), )); } @@ -483,7 +482,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { self.out.push(traits::Obligation::new( cause, param_env, - ty::PredicateKint::TypeOutlives(ty::OutlivesPredicate(rty, r)) + ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(rty, r)) .to_predicate(self.tcx()), )); } diff --git a/src/librustc_traits/chalk/lowering.rs b/src/librustc_traits/chalk/lowering.rs index ed021e5b9de1b..2abceb5fe2b05 100644 --- a/src/librustc_traits/chalk/lowering.rs +++ b/src/librustc_traits/chalk/lowering.rs @@ -78,10 +78,13 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment chalk_ir::InEnvironment>> { let clauses = self.environment.into_iter().filter_map(|clause| match clause { ChalkEnvironmentClause::Predicate(predicate) => { - match predicate.kind() { - ty::PredicateKind::Trait(predicate, _) => { + // FIXME(chalk): forall + match predicate.ignore_qualifiers(interner.tcx).skip_binder().kind() { + ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", predicate), + &ty::PredicateKind::Trait(predicate, _) => { + let predicate = ty::Binder::bind(predicate); let (predicate, binders, _named_regions) = - collect_bound_vars(interner, interner.tcx, predicate); + collect_bound_vars(interner, interner.tcx, &predicate); Some( chalk_ir::ProgramClauseData(chalk_ir::Binders::new( @@ -124,9 +127,10 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment None, - ty::PredicateKind::Projection(predicate) => { + &ty::PredicateKind::Projection(predicate) => { + let predicate = ty::Binder::bind(predicate); let (predicate, binders, _named_regions) = - collect_bound_vars(interner, interner.tcx, predicate); + collect_bound_vars(interner, interner.tcx, &predicate); Some( chalk_ir::ProgramClauseData(chalk_ir::Binders::new( @@ -181,8 +185,12 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment LowerInto<'tcx, chalk_ir::GoalData>> for ty::Predicate<'tcx> { fn lower_into(self, interner: &RustInterner<'tcx>) -> chalk_ir::GoalData> { - match self.kind() { - ty::PredicateKind::Trait(predicate, _) => predicate.lower_into(interner), + // FIXME(chalk): forall + match self.ignore_qualifiers(interner.tcx).skip_binder().kind() { + ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", self), + &ty::PredicateKind::Trait(predicate, _) => { + ty::Binder::bind(predicate).lower_into(interner) + } ty::PredicateKind::RegionOutlives(predicate) => { let (predicate, binders, _named_regions) = collect_bound_vars(interner, interner.tcx, predicate); @@ -205,7 +213,9 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::GoalData>> for ty::Predi ty::PredicateKind::TypeOutlives(_predicate) => { chalk_ir::GoalData::All(chalk_ir::Goals::new(interner)) } - ty::PredicateKind::Projection(predicate) => predicate.lower_into(interner), + &ty::PredicateKind::Projection(predicate) => { + ty::Binder::bind(predicate).lower_into(interner) + } ty::PredicateKind::WellFormed(arg) => match arg.unpack() { GenericArgKind::Type(ty) => match ty.kind { // FIXME(chalk): In Chalk, a placeholder is WellFormed if it @@ -532,8 +542,11 @@ impl<'tcx> LowerInto<'tcx, Option, ) -> Option>> { - match &self.kind() { - ty::PredicateKind::Trait(predicate, _) => { + // FIXME(chalk): forall + match self.ignore_qualifiers(interner.tcx).skip_binder().kind() { + ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", self), + &ty::PredicateKind::Trait(predicate, _) => { + let predicate = &ty::Binder::bind(predicate); let (predicate, binders, _named_regions) = collect_bound_vars(interner, interner.tcx, predicate); @@ -632,7 +645,9 @@ crate fn collect_bound_vars<'a, 'tcx, T: TypeFoldable<'tcx>>( } (0..parameters.len()).for_each(|i| { - parameters.get(&(i as u32)).expect(&format!("Skipped bound var index `{:?}`.", i)); + parameters + .get(&(i as u32)) + .or_else(|| bug!("Skipped bound var index: ty={:?}, parameters={:?}", ty, parameters)); }); let binders = chalk_ir::VariableKinds::from(interner, parameters.into_iter().map(|(_, v)| v)); diff --git a/src/librustc_traits/implied_outlives_bounds.rs b/src/librustc_traits/implied_outlives_bounds.rs index bda3da120e958..26a44bb5c2f1b 100644 --- a/src/librustc_traits/implied_outlives_bounds.rs +++ b/src/librustc_traits/implied_outlives_bounds.rs @@ -95,6 +95,7 @@ fn compute_implied_outlives_bounds<'tcx>( implied_bounds.extend(obligations.into_iter().flat_map(|obligation| { assert!(!obligation.has_escaping_bound_vars()); match obligation.predicate.kind() { + ty::PredicateKind::ForAll(..) => vec![], ty::PredicateKind::Trait(..) | ty::PredicateKind::Subtype(..) | ty::PredicateKind::Projection(..) @@ -102,28 +103,21 @@ fn compute_implied_outlives_bounds<'tcx>( | ty::PredicateKind::ObjectSafe(..) | ty::PredicateKind::ConstEvaluatable(..) | ty::PredicateKind::ConstEquate(..) => vec![], - &ty::PredicateKind::WellFormed(arg) => { wf_args.push(arg); vec![] } - ty::PredicateKind::RegionOutlives(ref data) => match data.no_bound_vars() { - None => vec![], - Some(ty::OutlivesPredicate(r_a, r_b)) => { - vec![OutlivesBound::RegionSubRegion(r_b, r_a)] - } - }, + &ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(r_a, r_b)) => { + vec![OutlivesBound::RegionSubRegion(r_b, r_a)] + } - ty::PredicateKind::TypeOutlives(ref data) => match data.no_bound_vars() { - None => vec![], - Some(ty::OutlivesPredicate(ty_a, r_b)) => { - let ty_a = infcx.resolve_vars_if_possible(&ty_a); - let mut components = smallvec![]; - tcx.push_outlives_components(ty_a, &mut components); - implied_bounds_from_components(r_b, components) - } - }, + &ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty_a, r_b)) => { + let ty_a = infcx.resolve_vars_if_possible(&ty_a); + let mut components = smallvec![]; + tcx.push_outlives_components(ty_a, &mut components); + implied_bounds_from_components(r_b, components) + } } })); } diff --git a/src/librustc_traits/normalize_erasing_regions.rs b/src/librustc_traits/normalize_erasing_regions.rs index 7092515af0882..06a90d145115d 100644 --- a/src/librustc_traits/normalize_erasing_regions.rs +++ b/src/librustc_traits/normalize_erasing_regions.rs @@ -27,7 +27,9 @@ fn normalize_generic_arg_after_erasing_regions<'tcx>( // always only region relations, and we are about to // erase those anyway: debug_assert_eq!( - normalized_obligations.iter().find(|p| not_outlives_predicate(&p.predicate)), + normalized_obligations + .iter() + .find(|p| not_outlives_predicate(tcx, &p.predicate)), None, ); @@ -39,9 +41,11 @@ fn normalize_generic_arg_after_erasing_regions<'tcx>( }) } -fn not_outlives_predicate(p: &ty::Predicate<'_>) -> bool { - match p.kind() { +fn not_outlives_predicate(tcx: TyCtxt<'tcx>, p: &ty::Predicate<'tcx>) -> bool { + // TODO: forall + match p.ignore_qualifiers(tcx).skip_binder().kind() { ty::PredicateKind::RegionOutlives(..) | ty::PredicateKind::TypeOutlives(..) => false, + ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", p), ty::PredicateKind::Trait(..) | ty::PredicateKind::Projection(..) | ty::PredicateKind::WellFormed(..) diff --git a/src/librustc_traits/type_op.rs b/src/librustc_traits/type_op.rs index 309de9bddb71e..9cc9a35b38b8a 100644 --- a/src/librustc_traits/type_op.rs +++ b/src/librustc_traits/type_op.rs @@ -140,7 +140,7 @@ impl AscribeUserTypeCx<'me, 'tcx> { self.relate(self_ty, Variance::Invariant, impl_self_ty)?; self.prove_predicate( - ty::PredicateKint::WellFormed(impl_self_ty.into()).to_predicate(self.tcx()), + ty::PredicateKind::WellFormed(impl_self_ty.into()).to_predicate(self.tcx()), ); } @@ -155,7 +155,7 @@ impl AscribeUserTypeCx<'me, 'tcx> { // them? This would only be relevant if some input // type were ill-formed but did not appear in `ty`, // which...could happen with normalization... - self.prove_predicate(ty::PredicateKint::WellFormed(ty.into()).to_predicate(self.tcx())); + self.prove_predicate(ty::PredicateKind::WellFormed(ty.into()).to_predicate(self.tcx())); Ok(()) } } diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 21b5f5c9033e6..ef7726e7705b4 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -1705,8 +1705,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { "conv_object_ty_poly_trait_ref: observing object predicate `{:?}`", obligation.predicate ); - match obligation.predicate.kind() { - ty::PredicateKind::Trait(pred, _) => { + + // TODO: forall + match obligation.predicate.ignore_qualifiers(tcx).skip_binder().kind() { + &ty::PredicateKind::Trait(pred, _) => { + let pred = ty::Binder::bind(pred); associated_types.entry(span).or_default().extend( tcx.associated_items(pred.def_id()) .in_definition_order() @@ -1715,6 +1718,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { ); } &ty::PredicateKind::Projection(pred) => { + let pred = ty::Binder::bind(pred); // A `Self` within the original bound will be substituted with a // `trait_object_dummy_self`, so check for that. let references_self = @@ -2094,7 +2098,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { || { traits::transitive_bounds( tcx, - predicates.iter().filter_map(|(p, _)| p.to_opt_poly_trait_ref()), + predicates.iter().filter_map(move |(p, _)| p.to_opt_poly_trait_ref(tcx)), ) }, || param_name.to_string(), diff --git a/src/librustc_typeck/check/closure.rs b/src/librustc_typeck/check/closure.rs index 8b18e759026b1..9d88a35901e4a 100644 --- a/src/librustc_typeck/check/closure.rs +++ b/src/librustc_typeck/check/closure.rs @@ -206,11 +206,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { obligation.predicate ); - if let &ty::PredicateKind::Projection(proj_predicate) = obligation.predicate.kind() + if let &ty::PredicateKind::Projection(proj_predicate) = + obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind() { // Given a Projection predicate, we can potentially infer // the complete signature. - self.deduce_sig_from_projection(Some(obligation.cause.span), proj_predicate) + self.deduce_sig_from_projection( + Some(obligation.cause.span), + ty::Binder::bind(proj_predicate), + ) } else { None } @@ -627,8 +631,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // where R is the return type we are expecting. This type `T` // will be our output. let output_ty = self.obligations_for_self_ty(ret_vid).find_map(|(_, obligation)| { - if let &ty::PredicateKind::Projection(proj_predicate) = obligation.predicate.kind() { - self.deduce_future_output_from_projection(obligation.cause.span, proj_predicate) + if let &ty::PredicateKind::Projection(proj_predicate) = + obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind() + { + self.deduce_future_output_from_projection( + obligation.cause.span, + ty::Binder::bind(proj_predicate), + ) } else { None } diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index 5cf2e2d64100c..6b3d986e7b1b5 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -582,24 +582,25 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { while !queue.is_empty() { let obligation = queue.remove(0); debug!("coerce_unsized resolve step: {:?}", obligation); - let trait_pred = match obligation.predicate.kind() { - &ty::PredicateKind::Trait(trait_pred, _) - if traits.contains(&trait_pred.def_id()) => - { - if unsize_did == trait_pred.def_id() { - let unsize_ty = trait_pred.skip_binder().trait_ref.substs[1].expect_ty(); - if let ty::Tuple(..) = unsize_ty.kind { - debug!("coerce_unsized: found unsized tuple coercion"); - has_unsized_tuple_coercion = true; + let trait_pred = + match obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind() { + &ty::PredicateKind::Trait(trait_pred, _) + if traits.contains(&trait_pred.def_id()) => + { + if unsize_did == trait_pred.def_id() { + let unsize_ty = trait_pred.trait_ref.substs[1].expect_ty(); + if let ty::Tuple(..) = unsize_ty.kind { + debug!("coerce_unsized: found unsized tuple coercion"); + has_unsized_tuple_coercion = true; + } } + ty::Binder::bind(trait_pred) } - trait_pred - } - _ => { - coercion.obligations.push(obligation); - continue; - } - }; + _ => { + coercion.obligations.push(obligation); + continue; + } + }; match selcx.select(&obligation.with(trait_pred)) { // Uncertain or unimplemented. Ok(None) => { diff --git a/src/librustc_typeck/check/dropck.rs b/src/librustc_typeck/check/dropck.rs index f6991120f3479..ae288ef6af781 100644 --- a/src/librustc_typeck/check/dropck.rs +++ b/src/librustc_typeck/check/dropck.rs @@ -127,7 +127,7 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>( // it did the wrong thing, so I chose to preserve existing // behavior, since it ought to be simply more // conservative. -nmatsakis - let outlives_env = OutlivesEnvironment::new(ty::ParamEnv::empty()); + let outlives_env = OutlivesEnvironment::new(infcx.tcx, ty::ParamEnv::empty()); infcx.resolve_regions_and_report_errors( drop_impl_did.to_def_id(), @@ -226,12 +226,15 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>( // could be extended easily also to the other `Predicate`. let predicate_matches_closure = |p: Predicate<'tcx>| { let mut relator: SimpleEqRelation<'tcx> = SimpleEqRelation::new(tcx, self_param_env); - match (predicate.kind(), p.kind()) { + match ( + predicate.ignore_qualifiers(tcx).skip_binder().kind(), + p.ignore_qualifiers(tcx).skip_binder().kind(), + ) { (&ty::PredicateKind::Trait(a, _), &ty::PredicateKind::Trait(b, _)) => { - relator.relate(a, b).is_ok() + relator.relate(ty::Binder::bind(a), ty::Binder::bind(b)).is_ok() } (&ty::PredicateKind::Projection(a), &ty::PredicateKind::Projection(b)) => { - relator.relate(a, b).is_ok() + relator.relate(ty::Binder::bind(a), ty::Binder::bind(b)).is_ok() } _ => predicate == p, } diff --git a/src/librustc_typeck/check/method/confirm.rs b/src/librustc_typeck/check/method/confirm.rs index ed84095ae6b0c..a9f663fff48a5 100644 --- a/src/librustc_typeck/check/method/confirm.rs +++ b/src/librustc_typeck/check/method/confirm.rs @@ -447,21 +447,27 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> { }; traits::elaborate_predicates(self.tcx, predicates.predicates.iter().copied()) - .filter_map(|obligation| match obligation.predicate.kind() { - ty::PredicateKind::Trait(trait_pred, _) if trait_pred.def_id() == sized_def_id => { - let span = predicates - .predicates - .iter() - .zip(predicates.spans.iter()) - .find_map( - |(p, span)| if *p == obligation.predicate { Some(*span) } else { None }, - ) - .unwrap_or(rustc_span::DUMMY_SP); - Some((trait_pred, span)) + // We don't care about regions here. + .filter_map(|obligation| { + match obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind() { + ty::PredicateKind::Trait(trait_pred, _) + if trait_pred.def_id() == sized_def_id => + { + let span = + predicates + .predicates + .iter() + .zip(predicates.spans.iter()) + .find_map(|(p, span)| { + if *p == obligation.predicate { Some(*span) } else { None } + }) + .unwrap_or(rustc_span::DUMMY_SP); + Some((trait_pred, span)) + } + _ => None, } - _ => None, }) - .find_map(|(trait_pred, span)| match trait_pred.skip_binder().self_ty().kind { + .find_map(|(trait_pred, span)| match trait_pred.self_ty().kind { ty::Dynamic(..) => Some(span), _ => None, }) diff --git a/src/librustc_typeck/check/method/mod.rs b/src/librustc_typeck/check/method/mod.rs index ee27b581311ea..64dce3e1738e3 100644 --- a/src/librustc_typeck/check/method/mod.rs +++ b/src/librustc_typeck/check/method/mod.rs @@ -399,7 +399,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { obligations.push(traits::Obligation::new( cause, self.param_env, - ty::PredicateKint::WellFormed(method_ty.into()).to_predicate(tcx), + ty::PredicateKind::WellFormed(method_ty.into()).to_predicate(tcx), )); let callee = MethodCallee { def_id, substs: trait_ref.substs, sig: fn_sig }; diff --git a/src/librustc_typeck/check/method/probe.rs b/src/librustc_typeck/check/method/probe.rs index 9c5e3cbc93844..90ebce8dc1887 100644 --- a/src/librustc_typeck/check/method/probe.rs +++ b/src/librustc_typeck/check/method/probe.rs @@ -795,6 +795,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { } fn assemble_inherent_candidates_from_param(&mut self, param_ty: ty::ParamTy) { + let tcx = self.tcx; // FIXME: do we want to commit to this behavior for param bounds? debug!("assemble_inherent_candidates_from_param(param_ty={:?})", param_ty); diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index b9f1f8064c861..34abbb9cef111 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -570,12 +570,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }; let mut type_params = FxHashMap::default(); let mut bound_spans = vec![]; - let mut collect_type_param_suggestions = - |self_ty: Ty<'_>, parent_pred: &ty::Predicate<'_>, obligation: &str| { - if let (ty::Param(_), ty::PredicateKind::Trait(p, _)) = - (&self_ty.kind, parent_pred.kind()) - { - if let ty::Adt(def, _) = p.skip_binder().trait_ref.self_ty().kind { + + let mut collect_type_param_suggestions = { + // We need to move `tcx` while only borrowing the rest, + // this is kind of ugly. + |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::PredicateKind::Trait(p, _)) = ( + &self_ty.kind, + parent_pred.ignore_qualifiers(tcx).skip_binder().kind(), + ) { + if let ty::Adt(def, _) = p.trait_ref.self_ty().kind { let node = def.did.as_local().map(|def_id| { self.tcx.hir().get(self.tcx.hir().as_local_hir_id(def_id)) }); @@ -597,7 +602,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } } - }; + } + }; let mut bound_span_label = |self_ty: Ty<'_>, obligation: &str, quiet: &str| { let msg = format!( "doesn't satisfy `{}`", @@ -625,8 +631,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } }; let mut format_pred = |pred: ty::Predicate<'tcx>| { - match pred.kind() { - ty::PredicateKind::Projection(pred) => { + // TODO: forall + match pred.ignore_qualifiers(tcx).skip_binder().kind() { + &ty::PredicateKind::Projection(pred) => { + let pred = ty::Binder::bind(pred); // `::Item = String`. let trait_ref = pred.skip_binder().projection_ty.trait_ref(self.tcx); @@ -644,7 +652,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { bound_span_label(trait_ref.self_ty(), &obligation, &quiet); Some((obligation, trait_ref.self_ty())) } - ty::PredicateKind::Trait(poly_trait_ref, _) => { + &ty::PredicateKind::Trait(poly_trait_ref, _) => { + let poly_trait_ref = ty::Binder::bind(poly_trait_ref); let p = poly_trait_ref.skip_binder().trait_ref; let self_ty = p.self_ty(); let path = p.print_only_trait_path(); @@ -950,12 +959,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // this isn't perfect (that is, there are cases when // implementing a trait would be legal but is rejected // here). - unsatisfied_predicates.iter().all(|(p, _)| match p.kind() { - // Hide traits if they are present in predicates as they can be fixed without - // having to implement them. - ty::PredicateKind::Trait(t, _) => t.def_id() == info.def_id, - ty::PredicateKind::Projection(p) => p.item_def_id() == info.def_id, - _ => false, + unsatisfied_predicates.iter().all(|(p, _)| { + match p.ignore_qualifiers(self.tcx).skip_binder().kind() { + // Hide traits if they are present in predicates as they can be fixed without + // having to implement them. + ty::PredicateKind::Trait(t, _) => t.def_id() == info.def_id, + ty::PredicateKind::Projection(p) => { + p.projection_ty.item_def_id == info.def_id + } + _ => false, + } }) && (type_is_local || info.def_id.is_local()) && self .associated_item(info.def_id, item_name, Namespace::ValueNS) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 7a96d6c678ad2..a98a7198435a3 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -2392,26 +2392,28 @@ fn missing_items_err( } /// Resugar `ty::GenericPredicates` in a way suitable to be used in structured suggestions. -fn bounds_from_generic_predicates( - tcx: TyCtxt<'_>, - predicates: ty::GenericPredicates<'_>, +fn bounds_from_generic_predicates<'tcx>( + tcx: TyCtxt<'tcx>, + predicates: ty::GenericPredicates<'tcx>, ) -> (String, String) { - let mut types: FxHashMap, Vec> = FxHashMap::default(); + let mut types: FxHashMap, Vec> = FxHashMap::default(); let mut projections = vec![]; for (predicate, _) in predicates.predicates { debug!("predicate {:?}", predicate); - match predicate.kind() { + // TODO: forall (we could keep the current behavior and just skip binders eagerly, + // not sure if we want to though) + match predicate.ignore_qualifiers(tcx).skip_binder().kind() { ty::PredicateKind::Trait(trait_predicate, _) => { - let entry = types.entry(trait_predicate.skip_binder().self_ty()).or_default(); - let def_id = trait_predicate.skip_binder().def_id(); + let entry = types.entry(trait_predicate.self_ty()).or_default(); + let def_id = trait_predicate.def_id(); if Some(def_id) != tcx.lang_items().sized_trait() { // Type params are `Sized` by default, do not add that restriction to the list // if it is a positive requirement. - entry.push(trait_predicate.skip_binder().def_id()); + entry.push(trait_predicate.def_id()); } } ty::PredicateKind::Projection(projection_pred) => { - projections.push(projection_pred); + projections.push(ty::Binder::bind(projection_pred)); } _ => {} } @@ -2456,11 +2458,11 @@ fn bounds_from_generic_predicates( } /// Return placeholder code for the given function. -fn fn_sig_suggestion( - tcx: TyCtxt<'_>, - sig: ty::FnSig<'_>, +fn fn_sig_suggestion<'tcx>( + tcx: TyCtxt<'tcx>, + sig: ty::FnSig<'tcx>, ident: Ident, - predicates: ty::GenericPredicates<'_>, + predicates: ty::GenericPredicates<'tcx>, assoc: &ty::AssocItem, ) -> String { let args = sig @@ -3612,7 +3614,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.register_predicate(traits::Obligation::new( cause, self.param_env, - ty::PredicateKint::WellFormed(arg).to_predicate(self.tcx), + ty::PredicateKind::WellFormed(arg).to_predicate(self.tcx), )); } @@ -3893,29 +3895,35 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .borrow() .pending_obligations() .into_iter() - .filter_map(move |obligation| match obligation.predicate.kind() { - ty::PredicateKind::Projection(ref data) => { - Some((data.to_poly_trait_ref(self.tcx), obligation)) - } - ty::PredicateKind::Trait(ref data, _) => { - Some((data.to_poly_trait_ref(), obligation)) + // TODO: forall + .filter_map(move |obligation| { + match obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind() { + ty::PredicateKind::ForAll(_) => { + bug!("unexpected predicate: {:?}", obligation.predicate) + } + &ty::PredicateKind::Projection(data) => { + Some((ty::Binder::bind(data).to_poly_trait_ref(self.tcx), obligation)) + } + &ty::PredicateKind::Trait(data, _) => { + Some((ty::Binder::bind(data).to_poly_trait_ref(), obligation)) + } + ty::PredicateKind::Subtype(..) => None, + ty::PredicateKind::RegionOutlives(..) => None, + ty::PredicateKind::TypeOutlives(..) => None, + ty::PredicateKind::WellFormed(..) => None, + ty::PredicateKind::ObjectSafe(..) => None, + ty::PredicateKind::ConstEvaluatable(..) => None, + ty::PredicateKind::ConstEquate(..) => None, + // N.B., this predicate is created by breaking down a + // `ClosureType: FnFoo()` predicate, where + // `ClosureType` represents some `Closure`. It can't + // possibly be referring to the current closure, + // because we haven't produced the `Closure` for + // this closure yet; this is exactly why the other + // code is looking for a self type of a unresolved + // inference variable. + ty::PredicateKind::ClosureKind(..) => None, } - ty::PredicateKind::Subtype(..) => None, - ty::PredicateKind::RegionOutlives(..) => None, - ty::PredicateKind::TypeOutlives(..) => None, - ty::PredicateKind::WellFormed(..) => None, - ty::PredicateKind::ObjectSafe(..) => None, - ty::PredicateKind::ConstEvaluatable(..) => None, - ty::PredicateKind::ConstEquate(..) => None, - // N.B., this predicate is created by breaking down a - // `ClosureType: FnFoo()` predicate, where - // `ClosureType` represents some `Closure`. It can't - // possibly be referring to the current closure, - // because we haven't produced the `Closure` for - // this closure yet; this is exactly why the other - // code is looking for a self type of a unresolved - // inference variable. - ty::PredicateKind::ClosureKind(..) => None, }) .filter(move |(tr, _)| self.self_type_matches_expected_vid(*tr, ty_var_root)) } @@ -4225,7 +4233,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { /// the corresponding argument's expression span instead of the `fn` call path span. fn point_at_arg_instead_of_call_if_possible( &self, - errors: &mut Vec>, + errors: &mut Vec>, final_arg_types: &[(usize, Ty<'tcx>, Ty<'tcx>)], call_sp: Span, args: &'tcx [hir::Expr<'tcx>], @@ -4244,7 +4252,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { continue; } - if let ty::PredicateKind::Trait(predicate, _) = error.obligation.predicate.kind() { + if let ty::PredicateKind::Trait(predicate, _) = + error.obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind() + { // Collect the argument position for all arguments that could have caused this // `FulfillmentError`. let mut referenced_in = final_arg_types @@ -4255,7 +4265,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let ty = self.resolve_vars_if_possible(&ty); // We walk the argument type because the argument's type could have // been `Option`, but the `FulfillmentError` references `T`. - if ty.walk().any(|arg| arg == predicate.skip_binder().self_ty().into()) { + if ty.walk().any(|arg| arg == predicate.self_ty().into()) { Some(i) } else { None @@ -4284,15 +4294,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { /// instead of the `fn` call path span. fn point_at_type_arg_instead_of_call_if_possible( &self, - errors: &mut Vec>, + errors: &mut Vec>, call_expr: &'tcx hir::Expr<'tcx>, ) { if let hir::ExprKind::Call(path, _) = &call_expr.kind { if let hir::ExprKind::Path(qpath) = &path.kind { if let hir::QPath::Resolved(_, path) = &qpath { for error in errors { - if let ty::PredicateKind::Trait(predicate, _) = - error.obligation.predicate.kind() + if let ty::PredicateKind::Trait(predicate, _) = error + .obligation + .predicate + .ignore_qualifiers(self.tcx) + .skip_binder() + .kind() { // If any of the type arguments in this path segment caused the // `FullfillmentError`, point at its span (#61860). @@ -4313,7 +4327,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } else { let ty = AstConv::ast_ty_to_ty(self, hir_ty); let ty = self.resolve_vars_if_possible(&ty); - if ty == predicate.skip_binder().self_ty() { + if ty == predicate.self_ty() { error.obligation.cause.make_mut().span = hir_ty.span; } } @@ -5365,12 +5379,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { item_def_id, }; - let predicate = - ty::PredicateKind::Projection(ty::Binder::bind(ty::ProjectionPredicate { - projection_ty, - ty: expected, - })) - .to_predicate(self.tcx); + let predicate = ty::PredicateKind::Projection(ty::ProjectionPredicate { + projection_ty, + ty: expected, + }) + .to_predicate(self.tcx) + .potentially_qualified(self.tcx, ty::PredicateKind::ForAll); let obligation = traits::Obligation::new(self.misc(sp), self.param_env, predicate); debug!("suggest_missing_await: trying obligation {:?}", obligation); diff --git a/src/librustc_typeck/check/regionck.rs b/src/librustc_typeck/check/regionck.rs index 221e5f72dc977..71187244601e8 100644 --- a/src/librustc_typeck/check/regionck.rs +++ b/src/librustc_typeck/check/regionck.rs @@ -194,7 +194,7 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> { param_env: ty::ParamEnv<'tcx>, ) -> RegionCtxt<'a, 'tcx> { let region_scope_tree = fcx.tcx.region_scope_tree(subject); - let outlives_environment = OutlivesEnvironment::new(param_env); + let outlives_environment = OutlivesEnvironment::new(fcx.tcx, param_env); RegionCtxt { fcx, region_scope_tree, diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index aa40d7de6e005..80f03e2211a08 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -828,8 +828,8 @@ fn check_where_clauses<'tcx, 'fcx>( debug!("check_where_clauses: predicates={:?}", predicates.predicates); assert_eq!(predicates.predicates.len(), predicates.spans.len()); let wf_obligations = - predicates.predicates.iter().zip(predicates.spans.iter()).flat_map(|(&p, &sp)| { - traits::wf::predicate_obligations(fcx, fcx.param_env, fcx.body_id, p.kint(tcx), sp) + predicates.predicates.iter().zip(predicates.spans.iter()).flat_map(|(p, &sp)| { + traits::wf::predicate_obligations(fcx, fcx.param_env, fcx.body_id, p, sp) }); for obligation in wf_obligations.chain(default_obligations) { diff --git a/src/librustc_typeck/coherence/builtin.rs b/src/librustc_typeck/coherence/builtin.rs index 8c6161a626473..d40abf1f54edb 100644 --- a/src/librustc_typeck/coherence/builtin.rs +++ b/src/librustc_typeck/coherence/builtin.rs @@ -292,7 +292,7 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef } // Finally, resolve all regions. - let outlives_env = OutlivesEnvironment::new(param_env); + let outlives_env = OutlivesEnvironment::new(tcx, param_env); infcx.resolve_regions_and_report_errors( impl_did.to_def_id(), &outlives_env, @@ -549,7 +549,7 @@ pub fn coerce_unsized_info(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUnsizedI } // Finally, resolve all regions. - let outlives_env = OutlivesEnvironment::new(param_env); + let outlives_env = OutlivesEnvironment::new(tcx, param_env); infcx.resolve_regions_and_report_errors(impl_did, &outlives_env, RegionckMode::default()); CoerceUnsizedInfo { custom_kind: kind } diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index acf68be1176dd..23cf639f3c8cf 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -552,10 +552,8 @@ fn type_param_predicates( let extra_predicates = extend.into_iter().chain( icx.type_parameter_bounds_in_generics(ast_generics, param_id, ty, OnlySelfBounds(true)) .into_iter() - .filter(|(predicate, _)| match predicate.kind() { - ty::PredicateKind::Trait(ref data, _) => { - data.skip_binder().self_ty().is_param(index) - } + .filter(|(predicate, _)| match predicate.ignore_qualifiers(tcx).skip_binder().kind() { + ty::PredicateKind::Trait(data, _) => data.self_ty().is_param(index), _ => false, }), ); @@ -1006,7 +1004,8 @@ fn super_predicates_of(tcx: TyCtxt<'_>, trait_def_id: DefId) -> ty::GenericPredi // which will, in turn, reach indirect supertraits. for &(pred, span) in superbounds { debug!("superbound: {:?}", pred); - if let ty::PredicateKind::Trait(bound, _) = pred.kind() { + if let ty::PredicateKind::Trait(bound, _) = pred.ignore_qualifiers(tcx).skip_binder().kind() + { tcx.at(span).super_predicates_of(bound.def_id()); } } @@ -1961,9 +1960,10 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat &hir::GenericBound::Outlives(ref lifetime) => { let region = AstConv::ast_region_to_region(&icx, lifetime, None); - let pred = ty::Binder::bind(ty::OutlivesPredicate(ty, region)); predicates.push(( - ty::PredicateKind::TypeOutlives(pred).to_predicate(tcx), + ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, region)) + .to_predicate(tcx) + .potentially_qualified(tcx, ty::PredicateKind::ForAll), lifetime.span, )) } @@ -1980,9 +1980,10 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat } _ => bug!(), }; - let pred = ty::Binder::bind(ty::OutlivesPredicate(r1, r2)); + let pred = ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(r1, r2)) + .to_predicate(icx.tcx); - (ty::PredicateKind::RegionOutlives(pred).to_predicate(icx.tcx), span) + (pred.potentially_qualified(icx.tcx, ty::PredicateKind::ForAll), span) })) } @@ -2110,8 +2111,10 @@ fn predicates_from_bound<'tcx>( } hir::GenericBound::Outlives(ref lifetime) => { let region = astconv.ast_region_to_region(lifetime, None); - let pred = ty::Binder::bind(ty::OutlivesPredicate(param_ty, region)); - vec![(ty::PredicateKind::TypeOutlives(pred).to_predicate(astconv.tcx()), lifetime.span)] + let pred = ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(param_ty, region)) + .to_predicate(astconv.tcx()) + .potentially_qualified(astconv.tcx(), ty::PredicateKind::ForAll); + vec![(pred, lifetime.span)] } } } diff --git a/src/librustc_typeck/constrained_generic_params.rs b/src/librustc_typeck/constrained_generic_params.rs index 34497d12a4ece..8d469d0e42988 100644 --- a/src/librustc_typeck/constrained_generic_params.rs +++ b/src/librustc_typeck/constrained_generic_params.rs @@ -180,11 +180,11 @@ pub fn setup_constraining_predicates<'tcx>( changed = false; for j in i..predicates.len() { - if let ty::PredicateKind::Projection(ref poly_projection) = predicates[j].0.kind() { - // Note that we can skip binder here because the impl - // trait ref never contains any late-bound regions. - let projection = poly_projection.skip_binder(); - + // Note that we don't have to care about binders here, + // as the impl trait ref never contains any late-bound regions. + if let ty::PredicateKind::Projection(projection) = + predicates[j].0.ignore_qualifiers(tcx).skip_binder().kind() + { // Special case: watch out for some kind of sneaky attempt // to project out an associated type defined by this very // trait. diff --git a/src/librustc_typeck/impl_wf_check/min_specialization.rs b/src/librustc_typeck/impl_wf_check/min_specialization.rs index e4bffedd620b9..c1c8e73358bca 100644 --- a/src/librustc_typeck/impl_wf_check/min_specialization.rs +++ b/src/librustc_typeck/impl_wf_check/min_specialization.rs @@ -163,7 +163,7 @@ fn get_impl_substs<'tcx>( let impl2_substs = translate_substs(infcx, param_env, impl1_def_id, impl1_substs, impl2_node); // Conservatively use an empty `ParamEnv`. - let outlives_env = OutlivesEnvironment::new(ty::ParamEnv::empty()); + let outlives_env = OutlivesEnvironment::new(tcx, ty::ParamEnv::empty()); infcx.resolve_regions_and_report_errors(impl1_def_id, &outlives_env, RegionckMode::default()); let impl2_substs = match infcx.fully_resolve(&impl2_substs) { Ok(s) => s, @@ -198,9 +198,11 @@ fn unconstrained_parent_impl_substs<'tcx>( // the functions in `cgp` add the constrained parameters to a list of // unconstrained parameters. for (predicate, _) in impl_generic_predicates.predicates.iter() { - if let ty::PredicateKind::Projection(proj) = predicate.kind() { - let projection_ty = proj.skip_binder().projection_ty; - let projected_ty = proj.skip_binder().ty; + if let ty::PredicateKind::Projection(proj) = + predicate.ignore_qualifiers(tcx).skip_binder().kind() + { + let projection_ty = proj.projection_ty; + let projected_ty = proj.ty; let unbound_trait_ref = projection_ty.trait_ref(tcx); if Some(unbound_trait_ref) == impl_trait_ref { @@ -359,7 +361,7 @@ fn check_predicates<'tcx>( fn check_specialization_on<'tcx>(tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tcx>, span: Span) { debug!("can_specialize_on(predicate = {:?})", predicate); - match predicate.kind() { + match predicate.ignore_qualifiers(tcx).skip_binder().kind() { // Global predicates are either always true or always false, so we // are fine to specialize on. _ if predicate.is_global() => (), @@ -392,7 +394,8 @@ fn trait_predicate_kind<'tcx>( tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tcx>, ) -> Option { - match predicate.kind() { + match predicate.ignore_qualifiers(tcx).skip_binder().kind() { + ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", predicate), ty::PredicateKind::Trait(pred, hir::Constness::NotConst) => { Some(tcx.trait_def(pred.def_id()).specialization_kind) } diff --git a/src/librustc_typeck/outlives/explicit.rs b/src/librustc_typeck/outlives/explicit.rs index 5740cc224cc57..1e946c73c5040 100644 --- a/src/librustc_typeck/outlives/explicit.rs +++ b/src/librustc_typeck/outlives/explicit.rs @@ -29,9 +29,12 @@ impl<'tcx> ExplicitPredicatesMap<'tcx> { // process predicates and convert to `RequiredPredicates` entry, see below for &(predicate, span) in predicates.predicates { - match predicate.kind() { + // TODO: forall + match predicate.ignore_qualifiers(tcx).skip_binder().kind() { + ty::PredicateKind::ForAll(_) => bug!("unepected predicate: {:?}", predicate), + ty::PredicateKind::TypeOutlives(predicate) => { - let OutlivesPredicate(ref ty, ref reg) = predicate.skip_binder(); + let OutlivesPredicate(ref ty, ref reg) = predicate; insert_outlives_predicate( tcx, (*ty).into(), @@ -42,7 +45,7 @@ impl<'tcx> ExplicitPredicatesMap<'tcx> { } ty::PredicateKind::RegionOutlives(predicate) => { - let OutlivesPredicate(ref reg1, ref reg2) = predicate.skip_binder(); + let OutlivesPredicate(ref reg1, ref reg2) = predicate; insert_outlives_predicate( tcx, (*reg1).into(), diff --git a/src/librustc_typeck/outlives/mod.rs b/src/librustc_typeck/outlives/mod.rs index cc5858314597c..d450a27941ff9 100644 --- a/src/librustc_typeck/outlives/mod.rs +++ b/src/librustc_typeck/outlives/mod.rs @@ -85,17 +85,17 @@ fn inferred_outlives_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> CratePredica |(ty::OutlivesPredicate(kind1, region2), &span)| { match kind1.unpack() { GenericArgKind::Type(ty1) => Some(( - ty::PredicateKind::TypeOutlives(ty::Binder::bind( - ty::OutlivesPredicate(ty1, region2), - )) - .to_predicate(tcx), + ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty1, region2)) + .to_predicate(tcx) + .potentially_qualified(tcx, ty::PredicateKind::ForAll), span, )), GenericArgKind::Lifetime(region1) => Some(( - ty::PredicateKind::RegionOutlives(ty::Binder::bind( - ty::OutlivesPredicate(region1, region2), + ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate( + region1, region2, )) - .to_predicate(tcx), + .to_predicate(tcx) + .potentially_qualified(tcx, ty::PredicateKind::ForAll), span, )), GenericArgKind::Const(_) => { diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs index 3a798158c8bc6..ddd27d42df45e 100644 --- a/src/librustdoc/clean/auto_trait.rs +++ b/src/librustdoc/clean/auto_trait.rs @@ -465,7 +465,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> { .iter() .filter(|p| { !orig_bounds.contains(p) - || match p.kind() { + || match p.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::Trait(pred, _) => pred.def_id() == sized_trait, _ => false, } diff --git a/src/test/ui/specialization/min_specialization/repeated_projection_type.stderr b/src/test/ui/specialization/min_specialization/repeated_projection_type.stderr index 7cc4357a704c0..1361117f6c4da 100644 --- a/src/test/ui/specialization/min_specialization/repeated_projection_type.stderr +++ b/src/test/ui/specialization/min_specialization/repeated_projection_type.stderr @@ -1,4 +1,4 @@ -error: cannot specialize on `Binder(ProjectionPredicate(ProjectionTy { substs: [V], item_def_id: DefId(0:6 ~ repeated_projection_type[317d]::Id[0]::This[0]) }, (I,)))` +error: cannot specialize on `ProjectionPredicate(ProjectionTy { substs: [V], item_def_id: DefId(0:6 ~ repeated_projection_type[317d]::Id[0]::This[0]) }, (I,))` --> $DIR/repeated_projection_type.rs:19:1 | LL | / impl> X for V { From b79f7fbda86748909a6a833521b2fab97de6e654 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Fri, 19 Jun 2020 10:05:05 +0200 Subject: [PATCH 13/28] rustdoc --- src/librustdoc/clean/auto_trait.rs | 10 +++---- src/librustdoc/clean/mod.rs | 48 +++++++++++++++++------------- src/librustdoc/clean/simplify.rs | 9 ++---- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs index ddd27d42df45e..d49afb551bd8b 100644 --- a/src/librustdoc/clean/auto_trait.rs +++ b/src/librustdoc/clean/auto_trait.rs @@ -315,12 +315,12 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> { tcx: TyCtxt<'tcx>, pred: ty::Predicate<'tcx>, ) -> FxHashSet { - let regions = match pred.kind() { - ty::PredicateKind::Trait(poly_trait_pred, _) => { - tcx.collect_referenced_late_bound_regions(&poly_trait_pred) + let regions = match pred.ignore_qualifiers().skip_binder().kind() { + &ty::PredicateKind::Trait(poly_trait_pred, _) => { + tcx.collect_referenced_late_bound_regions(&ty::Binder::bind(poly_trait_pred)) } - ty::PredicateKind::Projection(poly_proj_pred) => { - tcx.collect_referenced_late_bound_regions(&poly_proj_pred) + &ty::PredicateKind::Projection(poly_proj_pred) => { + tcx.collect_referenced_late_bound_regions(&ty::Binder::bind(poly_proj_pred)) } _ => return FxHashSet::default(), }; diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 94d95115dcdbc..fdfdddea0066d 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -480,13 +480,14 @@ impl Clean for hir::WherePredicate<'_> { impl<'a> Clean> for ty::Predicate<'a> { fn clean(&self, cx: &DocContext<'_>) -> Option { - match self.kind() { - ty::PredicateKind::Trait(ref pred, _) => Some(pred.clean(cx)), - ty::PredicateKind::Subtype(ref pred) => Some(pred.clean(cx)), - ty::PredicateKind::RegionOutlives(ref pred) => pred.clean(cx), - ty::PredicateKind::TypeOutlives(ref pred) => pred.clean(cx), - ty::PredicateKind::Projection(ref pred) => Some(pred.clean(cx)), - + match self.ignore_qualifiers().skip_binder().kind() { + &ty::PredicateKind::Trait(pred, _) => Some(ty::Binder::bind(pred).clean(cx)), + &ty::PredicateKind::Subtype(pred) => Some(ty::Binder::bind(pred).clean(cx)), + &ty::PredicateKind::RegionOutlives(pred) => ty::Binder::bind(pred).clean(cx), + &ty::PredicateKind::TypeOutlives(pred) => ty::Binder::bind(pred).clean(cx), + &ty::PredicateKind::Projection(pred) => Some(ty::Binder::bind(pred).clean(cx)), + + ty::PredicateKind::ForAll(_) => panic!("unexpected predicate: {:?}", self), ty::PredicateKind::WellFormed(..) | ty::PredicateKind::ObjectSafe(..) | ty::PredicateKind::ClosureKind(..) @@ -754,19 +755,24 @@ impl<'a, 'tcx> Clean for (&'a ty::Generics, ty::GenericPredicates<'tcx .flat_map(|(p, _)| { let mut projection = None; let param_idx = (|| { - if let Some(trait_ref) = p.to_opt_poly_trait_ref() { - if let ty::Param(param) = trait_ref.skip_binder().self_ty().kind { - return Some(param.index); + match p.ignore_qualifiers().skip_binder().kind() { + &ty::PredicateKind::Trait(pred, _constness) => { + if let ty::Param(param) = pred.self_ty().kind { + return Some(param.index); + } } - } else if let Some(outlives) = p.to_opt_type_outlives() { - if let ty::Param(param) = outlives.skip_binder().0.kind { - return Some(param.index); + &ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, _reg)) => { + if let ty::Param(param) = ty.kind { + return Some(param.index); + } } - } else if let ty::PredicateKind::Projection(p) = p.kind() { - if let ty::Param(param) = p.skip_binder().projection_ty.self_ty().kind { - projection = Some(p); - return Some(param.index); + &ty::PredicateKind::Projection(p) => { + if let ty::Param(param) = p.projection_ty.self_ty().kind { + projection = Some(ty::Binder::bind(p)); + return Some(param.index); + } } + _ => (), } None @@ -1657,7 +1663,7 @@ impl<'tcx> Clean for Ty<'tcx> { .filter_map(|predicate| { let trait_ref = if let Some(tr) = predicate.to_opt_poly_trait_ref() { tr - } else if let ty::PredicateKind::TypeOutlives(pred) = predicate.kind() { + } else if let Some(pred) = predicate.to_opt_type_outlives() { // these should turn up at the end if let Some(r) = pred.skip_binder().1.clean(cx) { regions.push(GenericBound::Outlives(r)); @@ -1678,8 +1684,10 @@ impl<'tcx> Clean for Ty<'tcx> { .predicates .iter() .filter_map(|pred| { - if let ty::PredicateKind::Projection(proj) = pred.kind() { - let proj = proj.skip_binder(); + if let ty::PredicateKind::Projection(proj) = + pred.ignore_qualifiers().skip_binder().kind() + { + let proj = proj; if proj.projection_ty.trait_ref(cx.tcx) == trait_ref.skip_binder() { diff --git a/src/librustdoc/clean/simplify.rs b/src/librustdoc/clean/simplify.rs index 37c613f41224a..75c5fb4c9f873 100644 --- a/src/librustdoc/clean/simplify.rs +++ b/src/librustdoc/clean/simplify.rs @@ -141,12 +141,9 @@ fn trait_is_same_or_supertrait(cx: &DocContext<'_>, child: DefId, trait_: DefId) .predicates .iter() .filter_map(|(pred, _)| { - if let ty::PredicateKind::Trait(ref pred, _) = pred.kind() { - if pred.skip_binder().trait_ref.self_ty() == self_ty { - Some(pred.def_id()) - } else { - None - } + if let ty::PredicateKind::Trait(pred, _) = pred.ignore_qualifiers().skip_binder().kind() + { + if pred.trait_ref.self_ty() == self_ty { Some(pred.def_id()) } else { None } } else { None } From 8d4c99ad88169689ce560ad05eedf60de1d5e9c4 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Fri, 19 Jun 2020 10:22:25 +0200 Subject: [PATCH 14/28] clippy --- .../clippy_lints/src/future_not_send.rs | 11 +++++----- .../clippy/clippy_lints/src/methods/mod.rs | 9 +++------ .../src/needless_pass_by_value.rs | 20 +++++++++++-------- .../clippy/clippy_lints/src/utils/mod.rs | 4 ++-- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/tools/clippy/clippy_lints/src/future_not_send.rs b/src/tools/clippy/clippy_lints/src/future_not_send.rs index 92c7e66a0eb85..ae28015b4c9e3 100644 --- a/src/tools/clippy/clippy_lints/src/future_not_send.rs +++ b/src/tools/clippy/clippy_lints/src/future_not_send.rs @@ -3,7 +3,7 @@ use rustc_hir::intravisit::FnKind; use rustc_hir::{Body, FnDecl, HirId}; use rustc_infer::infer::TyCtxtInferExt; use rustc_lint::{LateContext, LateLintPass}; -use rustc_middle::ty::{Opaque, PredicateKind::Trait, ToPolyTraitRef}; +use rustc_middle::ty::{Opaque, PredicateKind::Trait}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::{sym, Span}; use rustc_trait_selection::traits::error_reporting::suggestions::InferCtxtExt; @@ -91,12 +91,11 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend { cx.tcx.infer_ctxt().enter(|infcx| { for FulfillmentError { obligation, .. } in send_errors { infcx.maybe_note_obligation_cause_for_async_await(db, &obligation); - if let Trait(trait_pred, _) = obligation.predicate.kind() { - let trait_ref = trait_pred.to_poly_trait_ref(); - db.note(&*format!( + if let Trait(trait_pred, _) = obligation.predicate.ignore_qualifiers().skip_binder().kind() { + db.note(&format!( "`{}` doesn't implement `{}`", - trait_ref.skip_binder().self_ty(), - trait_ref.print_only_trait_path(), + trait_pred.self_ty(), + trait_pred.trait_ref.print_only_trait_path(), )); } } diff --git a/src/tools/clippy/clippy_lints/src/methods/mod.rs b/src/tools/clippy/clippy_lints/src/methods/mod.rs index 97cc58023f55e..c3cdaeedd1955 100644 --- a/src/tools/clippy/clippy_lints/src/methods/mod.rs +++ b/src/tools/clippy/clippy_lints/src/methods/mod.rs @@ -1558,13 +1558,10 @@ impl<'tcx> LateLintPass<'tcx> for Methods { // if return type is impl trait, check the associated types if let ty::Opaque(def_id, _) = ret_ty.kind { // one of the associated types must be Self - for predicate in cx.tcx.predicates_of(def_id).predicates { - if let ty::PredicateKind::Projection(poly_projection_predicate) = predicate.0.kind() { - let binder = poly_projection_predicate.ty(); - let associated_type = binder.skip_binder(); - + for &(predicate, _span) in cx.tcx.predicates_of(def_id).predicates { + if let ty::PredicateKind::Projection(projection_predicate) = predicate.ignore_qualifiers().skip_binder().kind() { // walk the associated type and check for Self - if contains_self_ty(associated_type) { + if contains_self_ty(projection_predicate.ty) { return; } } diff --git a/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs b/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs index 81774b617ac2e..e39fb23365a27 100644 --- a/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs +++ b/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs @@ -114,12 +114,12 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue { let preds = traits::elaborate_predicates(cx.tcx, cx.param_env.caller_bounds().iter()) .filter(|p| !p.is_global()) .filter_map(|obligation| { - if let ty::PredicateKind::Trait(poly_trait_ref, _) = obligation.predicate.kind() { - if poly_trait_ref.def_id() == sized_trait || poly_trait_ref.skip_binder().has_escaping_bound_vars() - { + // Note that we do not want to deal with qualified predicates here. + if let ty::PredicateKind::Trait(pred, _) = obligation.predicate.kind() { + if pred.def_id() == sized_trait { return None; } - Some(poly_trait_ref) + Some(pred) } else { None } @@ -159,14 +159,13 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue { } } - // // * Exclude a type that is specifically bounded by `Borrow`. // * Exclude a type whose reference also fulfills its bound. (e.g., `std::convert::AsRef`, // `serde::Serialize`) let (implements_borrow_trait, all_borrowable_trait) = { let preds = preds .iter() - .filter(|t| t.skip_binder().self_ty() == ty) + .filter(|t| t.self_ty() == ty) .collect::>(); ( @@ -174,8 +173,13 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue { !preds.is_empty() && { let ty_empty_region = cx.tcx.mk_imm_ref(cx.tcx.lifetimes.re_root_empty, ty); preds.iter().all(|t| { - let ty_params = &t.skip_binder().trait_ref.substs.iter().skip(1).collect::>(); - implements_trait(cx, ty_empty_region, t.def_id(), ty_params) + let ty_params = t + .trait_ref + .substs + .iter() + .skip(1) + .collect::>(); + implements_trait(cx, ty_empty_region, t.def_id(), &ty_params) }) }, ) diff --git a/src/tools/clippy/clippy_lints/src/utils/mod.rs b/src/tools/clippy/clippy_lints/src/utils/mod.rs index a4bee1c278059..426772f3e9a94 100644 --- a/src/tools/clippy/clippy_lints/src/utils/mod.rs +++ b/src/tools/clippy/clippy_lints/src/utils/mod.rs @@ -1263,8 +1263,8 @@ pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool { ty::Tuple(ref substs) => substs.types().any(|ty| is_must_use_ty(cx, ty)), ty::Opaque(ref def_id, _) => { for (predicate, _) in cx.tcx.predicates_of(*def_id).predicates { - if let ty::PredicateKind::Trait(ref poly_trait_predicate, _) = predicate.kind() { - if must_use_attr(&cx.tcx.get_attrs(poly_trait_predicate.skip_binder().trait_ref.def_id)).is_some() { + if let ty::PredicateKind::Trait(trait_predicate, _) = predicate.ignore_qualifiers().skip_binder().kind() { + if must_use_attr(&cx.tcx.get_attrs(trait_predicate.trait_ref.def_id)).is_some() { return true; } } From bbd581c583a7168359bf94beaa111d6ad3316837 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Fri, 19 Jun 2020 19:19:21 +0200 Subject: [PATCH 15/28] fix elaborate for predicates with unbound variables --- src/librustc_infer/traits/util.rs | 14 +++++--------- src/librustc_trait_selection/traits/auto_trait.rs | 2 +- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/librustc_infer/traits/util.rs b/src/librustc_infer/traits/util.rs index 1bee16f7556a1..6aeb225a6e9cb 100644 --- a/src/librustc_infer/traits/util.rs +++ b/src/librustc_infer/traits/util.rs @@ -151,15 +151,11 @@ impl Elaborator<'tcx> { fn elaborate(&mut self, obligation: &PredicateObligation<'tcx>) { let tcx = self.visited.tcx; - let pred = match obligation.predicate.kind() { - // We have to be careful and rebind this when - // dealing with a predicate further down. - ty::PredicateKind::ForAll(binder) => binder.skip_binder().kind(), - pred => pred, - }; - - match pred { - ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", pred), + + match obligation.predicate.ignore_qualifiers(tcx).skip_binder().kind() { + ty::PredicateKind::ForAll(_) => { + bug!("unexpected predicate: {:?}", obligation.predicate) + } ty::PredicateKind::Trait(data, _) => { // Get predicates declared on the trait. let predicates = tcx.super_predicates_of(data.def_id()); diff --git a/src/librustc_trait_selection/traits/auto_trait.rs b/src/librustc_trait_selection/traits/auto_trait.rs index 9c530912bf0e4..74a4939ae10d5 100644 --- a/src/librustc_trait_selection/traits/auto_trait.rs +++ b/src/librustc_trait_selection/traits/auto_trait.rs @@ -763,7 +763,7 @@ impl AutoTraitFinder<'tcx> { } } Ok(None) => { - // It's ok not to make progress when hvave no inference variables - + // It's ok not to make progress when have no inference variables - // in that case, we were only performing unifcation to check if an // error occurred (which would indicate that it's impossible for our // type to implement the auto trait). From c6c0d17c8d6be89c6ef00de14fbafaf76d276d55 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sat, 20 Jun 2020 00:21:59 +0200 Subject: [PATCH 16/28] review --- src/librustc_infer/traits/util.rs | 42 +++++++------------ src/librustc_middle/ty/mod.rs | 16 ++----- .../transform/qualify_min_const_fn.rs | 1 - .../traits/auto_trait.rs | 1 - .../traits/error_reporting/mod.rs | 24 +++++------ .../traits/object_safety.rs | 2 - .../traits/project.rs | 2 - .../traits/select/mod.rs | 1 - src/librustc_traits/chalk/lowering.rs | 18 ++++++-- .../normalize_erasing_regions.rs | 1 - src/librustc_typeck/astconv.rs | 1 - src/librustc_typeck/check/method/suggest.rs | 1 - src/librustc_typeck/check/mod.rs | 3 -- src/librustc_typeck/outlives/explicit.rs | 7 +--- 14 files changed, 45 insertions(+), 75 deletions(-) diff --git a/src/librustc_infer/traits/util.rs b/src/librustc_infer/traits/util.rs index 6aeb225a6e9cb..a1944781df28a 100644 --- a/src/librustc_infer/traits/util.rs +++ b/src/librustc_infer/traits/util.rs @@ -11,36 +11,22 @@ pub fn anonymize_predicate<'tcx>( pred: ty::Predicate<'tcx>, ) -> ty::Predicate<'tcx> { let kind = pred.kind(); - let new = match kind { + match kind { ty::PredicateKind::ForAll(binder) => { - ty::PredicateKind::ForAll(tcx.anonymize_late_bound_regions(binder)) + let new = ty::PredicateKind::ForAll(tcx.anonymize_late_bound_regions(binder)); + if new != *kind { new.to_predicate(tcx) } else { pred } } - &ty::PredicateKind::Trait(data, constness) => ty::PredicateKind::Trait(data, constness), - - &ty::PredicateKind::RegionOutlives(data) => ty::PredicateKind::RegionOutlives(data), - - &ty::PredicateKind::TypeOutlives(data) => ty::PredicateKind::TypeOutlives(data), - - &ty::PredicateKind::Projection(data) => ty::PredicateKind::Projection(data), - - &ty::PredicateKind::WellFormed(data) => ty::PredicateKind::WellFormed(data), - - &ty::PredicateKind::ObjectSafe(data) => ty::PredicateKind::ObjectSafe(data), - - &ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => { - ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) - } - - &ty::PredicateKind::Subtype(data) => ty::PredicateKind::Subtype(data), - - &ty::PredicateKind::ConstEvaluatable(def_id, substs) => { - ty::PredicateKind::ConstEvaluatable(def_id, substs) - } - - &ty::PredicateKind::ConstEquate(c1, c2) => ty::PredicateKind::ConstEquate(c1, c2), - }; - - if new != *kind { new.to_predicate(tcx) } else { pred } + ty::PredicateKind::Trait(_, _) + | ty::PredicateKind::RegionOutlives(_) + | ty::PredicateKind::TypeOutlives(_) + | ty::PredicateKind::Projection(_) + | ty::PredicateKind::WellFormed(_) + | ty::PredicateKind::ObjectSafe(_) + | ty::PredicateKind::ClosureKind(_, _, _) + | ty::PredicateKind::Subtype(_) + | ty::PredicateKind::ConstEvaluatable(_, _) + | ty::PredicateKind::ConstEquate(_, _) => pred, + } } struct PredicateSet<'tcx> { diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index 3b154bf1518d6..5b5996e84ee7d 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -1225,20 +1225,10 @@ impl<'tcx> Predicate<'tcx> { // substitution code expects equal binding levels in the values // from the substitution and the value being substituted into, and // this trick achieves that). - let substs = trait_ref.skip_binder().substs; - let kind = match self.kind() { - PredicateKind::ForAll(binder) => binder.skip_binder().kind(), - kind => kind, - }; - - let new = kind.subst(tcx, substs); - - if new != *kind { - new.to_predicate(tcx).potentially_qualified(tcx, PredicateKind::ForAll) - } else { - self - } + let pred = *self.ignore_qualifiers(tcx).skip_binder(); + let new = pred.subst(tcx, substs); + if new != pred { new.potentially_qualified(tcx, PredicateKind::ForAll) } else { self } } } diff --git a/src/librustc_mir/transform/qualify_min_const_fn.rs b/src/librustc_mir/transform/qualify_min_const_fn.rs index 0d4d102638181..f4bfc7d4b9872 100644 --- a/src/librustc_mir/transform/qualify_min_const_fn.rs +++ b/src/librustc_mir/transform/qualify_min_const_fn.rs @@ -24,7 +24,6 @@ pub fn is_min_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'a Body<'tcx>) - loop { let predicates = tcx.predicates_of(current); for (predicate, _) in predicates.predicates { - // TODO: forall match predicate.ignore_qualifiers(tcx).skip_binder().kind() { ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", predicate), ty::PredicateKind::RegionOutlives(_) diff --git a/src/librustc_trait_selection/traits/auto_trait.rs b/src/librustc_trait_selection/traits/auto_trait.rs index 74a4939ae10d5..aebe96f7ddc7e 100644 --- a/src/librustc_trait_selection/traits/auto_trait.rs +++ b/src/librustc_trait_selection/traits/auto_trait.rs @@ -639,7 +639,6 @@ impl AutoTraitFinder<'tcx> { // We check this by calling is_of_param on the relevant types // from the various possible predicates - // TODO: forall match predicate.ignore_qualifiers(self.tcx).skip_binder().kind() { &ty::PredicateKind::Trait(p, _) => { if self.is_param_no_infer(p.trait_ref.substs) diff --git a/src/librustc_trait_selection/traits/error_reporting/mod.rs b/src/librustc_trait_selection/traits/error_reporting/mod.rs index 8a2915bb30c71..75fe9b7701e07 100644 --- a/src/librustc_trait_selection/traits/error_reporting/mod.rs +++ b/src/librustc_trait_selection/traits/error_reporting/mod.rs @@ -256,7 +256,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { return; } - // TODO: forall match obligation.predicate.ignore_qualifiers(tcx).skip_binder().kind() { ty::PredicateKind::ForAll(_) => { bug!("unexpected predicate: {:?}", obligation.predicate) @@ -1481,7 +1480,6 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { return; } - // TODO: forall let mut err = match predicate.ignore_qualifiers(self.tcx).skip_binder().kind() { &ty::PredicateKind::Trait(data, _) => { let trait_ref = ty::Binder::bind(data.trait_ref); @@ -1583,8 +1581,6 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { } ty::PredicateKind::WellFormed(arg) => { - // TODO: forall - // Same hacky approach as above to avoid deluging user // with error messages. if arg.references_error() || self.tcx.sess.has_errors() { @@ -1604,7 +1600,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { } } - ty::PredicateKind::Subtype(ref data) => { + ty::PredicateKind::Subtype(data) => { if data.references_error() || self.tcx.sess.has_errors() { // no need to overload user in such cases return; @@ -1737,14 +1733,16 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { err: &mut DiagnosticBuilder<'tcx>, obligation: &PredicateObligation<'tcx>, ) { - let (pred, item_def_id, span) = - match (obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind(), obligation.cause.code.peel_derives()) { - ( - ty::PredicateKind::Trait(pred, _), - &ObligationCauseCode::BindingObligation(item_def_id, span), - ) => (pred, item_def_id, span), - _ => return, - }; + let (pred, item_def_id, span) = match ( + obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind(), + obligation.cause.code.peel_derives(), + ) { + ( + ty::PredicateKind::Trait(pred, _), + &ObligationCauseCode::BindingObligation(item_def_id, span), + ) => (pred, item_def_id, span), + _ => return, + }; let node = match ( self.tcx.hir().get_if_local(item_def_id), diff --git a/src/librustc_trait_selection/traits/object_safety.rs b/src/librustc_trait_selection/traits/object_safety.rs index cb03751bb7861..5ad43084e7f1a 100644 --- a/src/librustc_trait_selection/traits/object_safety.rs +++ b/src/librustc_trait_selection/traits/object_safety.rs @@ -245,7 +245,6 @@ fn predicates_reference_self( .iter() .map(|(predicate, sp)| (predicate.subst_supertrait(tcx, &trait_ref), sp)) .filter_map(|(predicate, &sp)| { - // TODO: forall match predicate.ignore_qualifiers(tcx).skip_binder().kind() { ty::PredicateKind::Trait(ref data, _) => { // In the case of a trait predicate, we can skip the "self" type. @@ -300,7 +299,6 @@ fn generics_require_sized_self(tcx: TyCtxt<'_>, def_id: DefId) -> bool { let predicates = tcx.predicates_of(def_id); let predicates = predicates.instantiate_identity(tcx).predicates; elaborate_predicates(tcx, predicates.into_iter()).any(|obligation| { - // TODO: forall match obligation.predicate.ignore_qualifiers(tcx).skip_binder().kind() { ty::PredicateKind::Trait(ref trait_pred, _) => { trait_pred.def_id() == sized_def_id && trait_pred.self_ty().is_param(0) diff --git a/src/librustc_trait_selection/traits/project.rs b/src/librustc_trait_selection/traits/project.rs index 30c86055e409e..c2ae26d9d25a5 100644 --- a/src/librustc_trait_selection/traits/project.rs +++ b/src/librustc_trait_selection/traits/project.rs @@ -933,7 +933,6 @@ fn assemble_candidates_from_predicates<'cx, 'tcx>( let infcx = selcx.infcx(); for predicate in env_predicates { debug!("assemble_candidates_from_predicates: predicate={:?}", predicate); - // TODO: forall if let &ty::PredicateKind::Projection(data) = predicate.ignore_qualifiers(infcx.tcx).skip_binder().kind() { @@ -1228,7 +1227,6 @@ fn confirm_object_candidate<'cx, 'tcx>( // select only those projections that are actually projecting an // item with the correct name - // TODO: forall let env_predicates = env_predicates.filter_map(|o| { match o.predicate.ignore_qualifiers(selcx.tcx()).skip_binder().kind() { &ty::PredicateKind::Projection(data) diff --git a/src/librustc_trait_selection/traits/select/mod.rs b/src/librustc_trait_selection/traits/select/mod.rs index 5683303605511..1bc53f0c5c53e 100644 --- a/src/librustc_trait_selection/traits/select/mod.rs +++ b/src/librustc_trait_selection/traits/select/mod.rs @@ -408,7 +408,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { None => self.check_recursion_limit(&obligation, &obligation)?, } - // TODO: forall match obligation.predicate.ignore_qualifiers(self.tcx()).skip_binder().kind() { ty::PredicateKind::ForAll(_) => { bug!("unexpected predicate: {:?}", obligation.predicate) diff --git a/src/librustc_traits/chalk/lowering.rs b/src/librustc_traits/chalk/lowering.rs index 2abceb5fe2b05..7154b3fb3784c 100644 --- a/src/librustc_traits/chalk/lowering.rs +++ b/src/librustc_traits/chalk/lowering.rs @@ -223,9 +223,21 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::GoalData>> for ty::Predi // the environment. ty::Placeholder(..) => chalk_ir::GoalData::All(chalk_ir::Goals::new(interner)), - _ => chalk_ir::GoalData::DomainGoal(chalk_ir::DomainGoal::WellFormed( - chalk_ir::WellFormed::Ty(ty.lower_into(interner)), - )), + _ => { + let (ty, binders, _named_regions) = + collect_bound_vars(interner, interner.tcx, &ty::Binder::bind(ty)); + + chalk_ir::GoalData::Quantified( + chalk_ir::QuantifierKind::ForAll, + chalk_ir::Binders::new( + binders, + chalk_ir::GoalData::DomainGoal(chalk_ir::DomainGoal::WellFormed( + chalk_ir::WellFormed::Ty(ty.lower_into(interner)), + )) + .intern(interner), + ), + ) + } }, // FIXME(chalk): handle well formed consts GenericArgKind::Const(..) => { diff --git a/src/librustc_traits/normalize_erasing_regions.rs b/src/librustc_traits/normalize_erasing_regions.rs index 06a90d145115d..8415690d41f6c 100644 --- a/src/librustc_traits/normalize_erasing_regions.rs +++ b/src/librustc_traits/normalize_erasing_regions.rs @@ -42,7 +42,6 @@ fn normalize_generic_arg_after_erasing_regions<'tcx>( } fn not_outlives_predicate(tcx: TyCtxt<'tcx>, p: &ty::Predicate<'tcx>) -> bool { - // TODO: forall match p.ignore_qualifiers(tcx).skip_binder().kind() { ty::PredicateKind::RegionOutlives(..) | ty::PredicateKind::TypeOutlives(..) => false, ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", p), diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index ef7726e7705b4..c028597ccd299 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -1706,7 +1706,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { obligation.predicate ); - // TODO: forall match obligation.predicate.ignore_qualifiers(tcx).skip_binder().kind() { &ty::PredicateKind::Trait(pred, _) => { let pred = ty::Binder::bind(pred); diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 34abbb9cef111..ddf539277b94d 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -631,7 +631,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } }; let mut format_pred = |pred: ty::Predicate<'tcx>| { - // TODO: forall match pred.ignore_qualifiers(tcx).skip_binder().kind() { &ty::PredicateKind::Projection(pred) => { let pred = ty::Binder::bind(pred); diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index a98a7198435a3..201288022edb6 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -2400,8 +2400,6 @@ fn bounds_from_generic_predicates<'tcx>( let mut projections = vec![]; for (predicate, _) in predicates.predicates { debug!("predicate {:?}", predicate); - // TODO: forall (we could keep the current behavior and just skip binders eagerly, - // not sure if we want to though) match predicate.ignore_qualifiers(tcx).skip_binder().kind() { ty::PredicateKind::Trait(trait_predicate, _) => { let entry = types.entry(trait_predicate.self_ty()).or_default(); @@ -3895,7 +3893,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .borrow() .pending_obligations() .into_iter() - // TODO: forall .filter_map(move |obligation| { match obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind() { ty::PredicateKind::ForAll(_) => { diff --git a/src/librustc_typeck/outlives/explicit.rs b/src/librustc_typeck/outlives/explicit.rs index 1e946c73c5040..d6551a5a38716 100644 --- a/src/librustc_typeck/outlives/explicit.rs +++ b/src/librustc_typeck/outlives/explicit.rs @@ -29,12 +29,10 @@ impl<'tcx> ExplicitPredicatesMap<'tcx> { // process predicates and convert to `RequiredPredicates` entry, see below for &(predicate, span) in predicates.predicates { - // TODO: forall match predicate.ignore_qualifiers(tcx).skip_binder().kind() { ty::PredicateKind::ForAll(_) => bug!("unepected predicate: {:?}", predicate), - ty::PredicateKind::TypeOutlives(predicate) => { - let OutlivesPredicate(ref ty, ref reg) = predicate; + ty::PredicateKind::TypeOutlives(OutlivesPredicate(ref ty, ref reg)) => { insert_outlives_predicate( tcx, (*ty).into(), @@ -44,8 +42,7 @@ impl<'tcx> ExplicitPredicatesMap<'tcx> { ) } - ty::PredicateKind::RegionOutlives(predicate) => { - let OutlivesPredicate(ref reg1, ref reg2) = predicate; + ty::PredicateKind::RegionOutlives(OutlivesPredicate(ref reg1, ref reg2)) => { insert_outlives_predicate( tcx, (*reg1).into(), From 1151d6204919202340e22cef15ca69aa183b41d1 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sun, 21 Jun 2020 12:26:17 +0200 Subject: [PATCH 17/28] split ignore_qualifiers --- src/librustc_infer/infer/outlives/env.rs | 6 +-- src/librustc_infer/infer/outlives/mod.rs | 3 +- src/librustc_infer/infer/outlives/verify.rs | 3 +- src/librustc_infer/traits/util.rs | 21 ++++---- src/librustc_lint/builtin.rs | 24 +++------- src/librustc_lint/unused.rs | 2 +- src/librustc_middle/ty/mod.rs | 38 +++++++++++---- src/librustc_middle/ty/print/pretty.rs | 11 ++++- .../borrow_check/diagnostics/region_errors.rs | 2 +- .../type_check/free_region_relations.rs | 2 +- .../transform/qualify_min_const_fn.rs | 2 +- src/librustc_privacy/lib.rs | 48 ++++++++++--------- src/librustc_trait_selection/opaque_types.rs | 4 +- .../traits/auto_trait.rs | 6 +-- .../traits/error_reporting/mod.rs | 14 +++--- .../traits/error_reporting/suggestions.rs | 2 +- src/librustc_trait_selection/traits/mod.rs | 4 +- .../traits/object_safety.rs | 6 +-- .../traits/project.rs | 6 +-- .../traits/query/type_op/prove_predicate.rs | 2 +- .../traits/select/candidate_assembly.rs | 3 +- .../traits/select/mod.rs | 10 ++-- .../traits/specialize/mod.rs | 2 +- src/librustc_trait_selection/traits/util.rs | 5 +- src/librustc_trait_selection/traits/wf.rs | 4 +- src/librustc_traits/chalk/lowering.rs | 29 ++++++----- .../normalize_erasing_regions.rs | 8 ++-- src/librustc_ty/ty.rs | 17 ++++--- src/librustc_typeck/astconv.rs | 4 +- src/librustc_typeck/check/closure.rs | 4 +- src/librustc_typeck/check/coercion.rs | 33 +++++++------ src/librustc_typeck/check/dropck.rs | 6 +-- src/librustc_typeck/check/method/confirm.rs | 2 +- src/librustc_typeck/check/method/probe.rs | 1 - src/librustc_typeck/check/method/suggest.rs | 6 +-- src/librustc_typeck/check/mod.rs | 14 ++---- src/librustc_typeck/check/regionck.rs | 2 +- src/librustc_typeck/coherence/builtin.rs | 4 +- src/librustc_typeck/collect.rs | 5 +- .../constrained_generic_params.rs | 2 +- .../impl_wf_check/min_specialization.rs | 8 ++-- src/librustc_typeck/outlives/explicit.rs | 2 +- 42 files changed, 196 insertions(+), 181 deletions(-) diff --git a/src/librustc_infer/infer/outlives/env.rs b/src/librustc_infer/infer/outlives/env.rs index 59f3d01777c7b..1a9e20e79fe1e 100644 --- a/src/librustc_infer/infer/outlives/env.rs +++ b/src/librustc_infer/infer/outlives/env.rs @@ -3,7 +3,7 @@ use crate::infer::{GenericKind, InferCtxt}; use crate::traits::query::OutlivesBound; use rustc_data_structures::fx::FxHashMap; use rustc_hir as hir; -use rustc_middle::ty::{self, TyCtxt}; +use rustc_middle::ty; use super::explicit_outlives_bounds; @@ -69,7 +69,7 @@ pub struct OutlivesEnvironment<'tcx> { pub type RegionBoundPairs<'tcx> = Vec<(ty::Region<'tcx>, GenericKind<'tcx>)>; impl<'a, 'tcx> OutlivesEnvironment<'tcx> { - pub fn new(tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self { + pub fn new(param_env: ty::ParamEnv<'tcx>) -> Self { let mut env = OutlivesEnvironment { param_env, free_region_map: Default::default(), @@ -77,7 +77,7 @@ impl<'a, 'tcx> OutlivesEnvironment<'tcx> { region_bound_pairs_accum: vec![], }; - env.add_outlives_bounds(None, explicit_outlives_bounds(tcx, param_env)); + env.add_outlives_bounds(None, explicit_outlives_bounds(param_env)); env } diff --git a/src/librustc_infer/infer/outlives/mod.rs b/src/librustc_infer/infer/outlives/mod.rs index ad1579083b646..541a2f18045c4 100644 --- a/src/librustc_infer/infer/outlives/mod.rs +++ b/src/librustc_infer/infer/outlives/mod.rs @@ -5,10 +5,9 @@ pub mod obligations; pub mod verify; use rustc_middle::traits::query::OutlivesBound; -use rustc_middle::ty::{self, TyCtxt}; +use rustc_middle::ty; pub fn explicit_outlives_bounds<'tcx>( - tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, ) -> impl Iterator> + 'tcx { debug!("explicit_outlives_bounds()"); diff --git a/src/librustc_infer/infer/outlives/verify.rs b/src/librustc_infer/infer/outlives/verify.rs index 27d21dd0b70ef..8f20b5743df4f 100644 --- a/src/librustc_infer/infer/outlives/verify.rs +++ b/src/librustc_infer/infer/outlives/verify.rs @@ -331,9 +331,8 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> { compare_ty: impl Fn(Ty<'tcx>) -> bool, predicates: impl Iterator>, ) -> impl Iterator, ty::Region<'tcx>>> { - let tcx = self.tcx; predicates - .filter_map(move |p| p.to_opt_type_outlives(tcx)) + .filter_map(|p| p.to_opt_type_outlives()) .filter_map(|p| p.no_bound_vars()) .filter(move |p| compare_ty(p.0)) } diff --git a/src/librustc_infer/traits/util.rs b/src/librustc_infer/traits/util.rs index a1944781df28a..901685d1d844a 100644 --- a/src/librustc_infer/traits/util.rs +++ b/src/librustc_infer/traits/util.rs @@ -131,14 +131,14 @@ fn predicate_obligation<'tcx>( } impl Elaborator<'tcx> { - pub fn filter_to_traits(self) -> FilterToTraits<'tcx, Self> { - FilterToTraits::new(self.visited.tcx, self) + pub fn filter_to_traits(self) -> FilterToTraits { + FilterToTraits::new(self) } fn elaborate(&mut self, obligation: &PredicateObligation<'tcx>) { let tcx = self.visited.tcx; - match obligation.predicate.ignore_qualifiers(tcx).skip_binder().kind() { + match obligation.predicate.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::ForAll(_) => { bug!("unexpected predicate: {:?}", obligation.predicate) } @@ -275,7 +275,7 @@ impl Iterator for Elaborator<'tcx> { // Supertrait iterator /////////////////////////////////////////////////////////////////////////// -pub type Supertraits<'tcx> = FilterToTraits<'tcx, Elaborator<'tcx>>; +pub type Supertraits<'tcx> = FilterToTraits>; pub fn supertraits<'tcx>( tcx: TyCtxt<'tcx>, @@ -297,23 +297,22 @@ pub fn transitive_bounds<'tcx>( /// A filter around an iterator of predicates that makes it yield up /// just trait references. -pub struct FilterToTraits<'tcx, I> { - tcx: TyCtxt<'tcx>, +pub struct FilterToTraits { base_iterator: I, } -impl<'tcx, I> FilterToTraits<'tcx, I> { - fn new(tcx: TyCtxt<'tcx>, base: I) -> FilterToTraits<'tcx, I> { - FilterToTraits { tcx, base_iterator: base } +impl FilterToTraits { + fn new(base: I) -> FilterToTraits { + FilterToTraits { base_iterator: base } } } -impl<'tcx, I: Iterator>> Iterator for FilterToTraits<'tcx, I> { +impl<'tcx, I: Iterator>> Iterator for FilterToTraits { type Item = ty::PolyTraitRef<'tcx>; fn next(&mut self) -> Option> { while let Some(obligation) = self.base_iterator.next() { - if let Some(data) = obligation.predicate.to_opt_poly_trait_ref(self.tcx) { + if let Some(data) = obligation.predicate.to_opt_poly_trait_ref() { return Some(data); } } diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index cedf742b9a949..9566cdb164359 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -1210,7 +1210,7 @@ impl<'tcx> LateLintPass<'tcx> for TrivialConstraints { for &(predicate, span) in predicates.predicates { // We don't actually look inside of the predicate, // so it is safe to skip this binder here. - let predicate_kind_name = match predicate.ignore_qualifiers(cx.tcx).skip_binder().kind() { + let predicate_kind_name = match predicate.ignore_qualifiers().skip_binder().kind() { Trait(..) => "Trait", TypeOutlives(..) | RegionOutlives(..) => "Lifetime", @@ -1495,13 +1495,12 @@ declare_lint_pass!(ExplicitOutlivesRequirements => [EXPLICIT_OUTLIVES_REQUIREMEN impl ExplicitOutlivesRequirements { fn lifetimes_outliving_lifetime<'tcx>( - tcx: TyCtxt<'tcx>, inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)], index: u32, ) -> Vec> { inferred_outlives .iter() - .filter_map(|(pred, _)| match pred.ignore_qualifiers(tcx).skip_binder().kind() { + .filter_map(|(pred, _)| match pred.ignore_qualifiers().skip_binder().kind() { &ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => match a { ty::ReEarlyBound(ebr) if ebr.index == index => Some(b), _ => None, @@ -1512,13 +1511,12 @@ impl ExplicitOutlivesRequirements { } fn lifetimes_outliving_type<'tcx>( - tcx: TyCtxt<'tcx>, inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)], index: u32, ) -> Vec> { inferred_outlives .iter() - .filter_map(|(pred, _)| match pred.ignore_qualifiers(tcx).skip_binder().kind() { + .filter_map(|(pred, _)| match pred.ignore_qualifiers().skip_binder().kind() { &ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(a, b)) => { a.is_param(index).then_some(b) } @@ -1539,10 +1537,10 @@ impl ExplicitOutlivesRequirements { match param.kind { hir::GenericParamKind::Lifetime { .. } => { - Self::lifetimes_outliving_lifetime(tcx, inferred_outlives, index) + Self::lifetimes_outliving_lifetime(inferred_outlives, index) } hir::GenericParamKind::Type { .. } => { - Self::lifetimes_outliving_type(tcx, inferred_outlives, index) + Self::lifetimes_outliving_type(inferred_outlives, index) } hir::GenericParamKind::Const { .. } => Vec::new(), } @@ -1694,11 +1692,7 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements { cx.tcx.named_region(predicate.lifetime.hir_id) { ( - Self::lifetimes_outliving_lifetime( - cx.tcx, - inferred_outlives, - index, - ), + Self::lifetimes_outliving_lifetime(inferred_outlives, index), &predicate.bounds, predicate.span, ) @@ -1714,11 +1708,7 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements { if let Res::Def(DefKind::TyParam, def_id) = path.res { let index = ty_generics.param_def_id_to_index[&def_id]; ( - Self::lifetimes_outliving_type( - cx.tcx, - inferred_outlives, - index, - ), + Self::lifetimes_outliving_type(inferred_outlives, index), &predicate.bounds, predicate.span, ) diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index 1430b799afce2..3f1f397fc8e5a 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -148,7 +148,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { for (predicate, _) in cx.tcx.predicates_of(def).predicates { // We only look at the `DefId`, so it is safe to skip the binder here. if let ty::PredicateKind::Trait(ref poly_trait_predicate, _) = - predicate.ignore_qualifiers(cx.tcx).skip_binder().kind() + predicate.ignore_qualifiers().skip_binder().kind() { let def_id = poly_trait_predicate.trait_ref.def_id; let descr_pre = diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index 5b5996e84ee7d..3782f2b840480 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -1050,7 +1050,30 @@ impl<'tcx> Predicate<'tcx> { } /// Skips `PredicateKind::ForAll`. - pub fn ignore_qualifiers(self, tcx: TyCtxt<'tcx>) -> Binder> { + pub fn ignore_qualifiers(self) -> Binder> { + match self.kind() { + &PredicateKind::ForAll(binder) => binder, + ty::PredicateKind::Projection(..) + | ty::PredicateKind::Trait(..) + | ty::PredicateKind::Subtype(..) + | ty::PredicateKind::WellFormed(..) + | ty::PredicateKind::ObjectSafe(..) + | ty::PredicateKind::ClosureKind(..) + | ty::PredicateKind::TypeOutlives(..) + | ty::PredicateKind::ConstEvaluatable(..) + | ty::PredicateKind::ConstEquate(..) + | ty::PredicateKind::RegionOutlives(..) => Binder::dummy(self), + } + } + + /// Skips `PredicateKind::ForAll`, while allowing for unbound variables. + /// + /// This method requires the `TyCtxt` as it has to shift the unbound variables + /// outwards. + /// + /// Do not use this method if you may end up just skipping the binder, as this + /// would leave the unbound variables at an incorrect binding level. + pub fn ignore_qualifiers_with_unbound_vars(self, tcx: TyCtxt<'tcx>) -> Binder> { match self.kind() { &PredicateKind::ForAll(binder) => binder, ty::PredicateKind::Projection(..) @@ -1226,7 +1249,7 @@ impl<'tcx> Predicate<'tcx> { // from the substitution and the value being substituted into, and // this trick achieves that). let substs = trait_ref.skip_binder().substs; - let pred = *self.ignore_qualifiers(tcx).skip_binder(); + let pred = *self.ignore_qualifiers().skip_binder(); let new = pred.subst(tcx, substs); if new != pred { new.potentially_qualified(tcx, PredicateKind::ForAll) } else { self } } @@ -1427,8 +1450,8 @@ impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> { } impl<'tcx> Predicate<'tcx> { - pub fn to_opt_poly_trait_ref(self, tcx: TyCtxt<'tcx>) -> Option> { - self.ignore_qualifiers(tcx) + pub fn to_opt_poly_trait_ref(self) -> Option> { + self.ignore_qualifiers() .map_bound(|pred| match pred.kind() { &PredicateKind::Trait(ref t, _) => Some(t.trait_ref), PredicateKind::Projection(..) @@ -1445,11 +1468,8 @@ impl<'tcx> Predicate<'tcx> { .transpose() } - pub fn to_opt_type_outlives( - self, - tcx: TyCtxt<'tcx>, - ) -> Option> { - self.ignore_qualifiers(tcx) + pub fn to_opt_type_outlives(self) -> Option> { + self.ignore_qualifiers() .map_bound(|pred| match pred.kind() { &PredicateKind::TypeOutlives(data) => Some(data), PredicateKind::Trait(..) diff --git a/src/librustc_middle/ty/print/pretty.rs b/src/librustc_middle/ty/print/pretty.rs index 1fdd7d4c82479..1f9fc4818839f 100644 --- a/src/librustc_middle/ty/print/pretty.rs +++ b/src/librustc_middle/ty/print/pretty.rs @@ -572,7 +572,16 @@ pub trait PrettyPrinter<'tcx>: let mut is_sized = false; p!(write("impl")); for predicate in bounds.predicates { - if let Some(trait_ref) = predicate.to_opt_poly_trait_ref(self.tcx()) { + // Note: We can't use `to_opt_poly_trait_ref` here as `predicate` + // may contain unbound variables. We therefore do this manually. + // + // FIXME(lcnr): Find out why exactly this is the case :) + if let ty::PredicateKind::Trait(pred, _) = predicate + .ignore_qualifiers_with_unbound_vars(self.tcx()) + .skip_binder() + .kind() + { + let trait_ref = ty::Binder::bind(pred.trait_ref); // Don't print +Sized, but rather +?Sized if absent. if Some(trait_ref.def_id()) == self.tcx().lang_items().sized_trait() { is_sized = true; diff --git a/src/librustc_mir/borrow_check/diagnostics/region_errors.rs b/src/librustc_mir/borrow_check/diagnostics/region_errors.rs index 4daebcec6ffea..c28f23dc7d611 100644 --- a/src/librustc_mir/borrow_check/diagnostics/region_errors.rs +++ b/src/librustc_mir/borrow_check/diagnostics/region_errors.rs @@ -590,7 +590,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { let mut found = false; for predicate in bounds.predicates { if let ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(_, r)) = - predicate.ignore_qualifiers(self.infcx.tcx).skip_binder().kind() + predicate.ignore_qualifiers().skip_binder().kind() { if let ty::RegionKind::ReStatic = r { found = true; diff --git a/src/librustc_mir/borrow_check/type_check/free_region_relations.rs b/src/librustc_mir/borrow_check/type_check/free_region_relations.rs index fe8d924debe79..beee31812563e 100644 --- a/src/librustc_mir/borrow_check/type_check/free_region_relations.rs +++ b/src/librustc_mir/borrow_check/type_check/free_region_relations.rs @@ -274,7 +274,7 @@ impl UniversalRegionRelationsBuilder<'cx, 'tcx> { // Insert the facts we know from the predicates. Why? Why not. let param_env = self.param_env; - self.add_outlives_bounds(outlives::explicit_outlives_bounds(self.infcx.tcx, param_env)); + self.add_outlives_bounds(outlives::explicit_outlives_bounds(param_env)); // Finally: // - outlives is reflexive, so `'r: 'r` for every region `'r` diff --git a/src/librustc_mir/transform/qualify_min_const_fn.rs b/src/librustc_mir/transform/qualify_min_const_fn.rs index f4bfc7d4b9872..c176fbf90b63f 100644 --- a/src/librustc_mir/transform/qualify_min_const_fn.rs +++ b/src/librustc_mir/transform/qualify_min_const_fn.rs @@ -24,7 +24,7 @@ pub fn is_min_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'a Body<'tcx>) - loop { let predicates = tcx.predicates_of(current); for (predicate, _) in predicates.predicates { - match predicate.ignore_qualifiers(tcx).skip_binder().kind() { + match predicate.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", predicate), ty::PredicateKind::RegionOutlives(_) | ty::PredicateKind::TypeOutlives(_) diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index 74f0e669ef3c3..3591a707ac039 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -84,31 +84,33 @@ where || (!self.def_id_visitor.shallow() && substs.visit_with(self)) } + fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> bool { + match predicate.kind() { + &ty::PredicateKind::ForAll(pred) => { + // This visitor does not care about bound regions as we only + // look at `DefId`s. + self.visit_predicate(*pred.skip_binder()) + } + &ty::PredicateKind::Trait(ty::TraitPredicate { trait_ref }, _) => { + self.visit_trait(trait_ref) + } + &ty::PredicateKind::Projection(ty::ProjectionPredicate { projection_ty, ty }) => { + ty.visit_with(self) + || self.visit_trait(projection_ty.trait_ref(self.def_id_visitor.tcx())) + } + &ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, _region)) => { + ty.visit_with(self) + } + ty::PredicateKind::RegionOutlives(..) => false, + _ => bug!("unexpected predicate: {:?}", predicate), + } + } + fn visit_predicates(&mut self, predicates: ty::GenericPredicates<'tcx>) -> bool { let ty::GenericPredicates { parent: _, predicates } = predicates; - for (predicate, _span) in predicates { - // This visitor does not care about bound regions. - match predicate.ignore_qualifiers(self.def_id_visitor.tcx()).skip_binder().kind() { - &ty::PredicateKind::Trait(ty::TraitPredicate { trait_ref }, _) => { - if self.visit_trait(trait_ref) { - return true; - } - } - &ty::PredicateKind::Projection(ty::ProjectionPredicate { projection_ty, ty }) => { - if ty.visit_with(self) { - return true; - } - if self.visit_trait(projection_ty.trait_ref(self.def_id_visitor.tcx())) { - return true; - } - } - &ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, _region)) => { - if ty.visit_with(self) { - return true; - } - } - ty::PredicateKind::RegionOutlives(..) => {} - _ => bug!("unexpected predicate: {:?}", predicate), + for &(predicate, _span) in predicates { + if self.visit_predicate(predicate) { + return true; } } false diff --git a/src/librustc_trait_selection/opaque_types.rs b/src/librustc_trait_selection/opaque_types.rs index 3c42400fbbc19..b482f073858b9 100644 --- a/src/librustc_trait_selection/opaque_types.rs +++ b/src/librustc_trait_selection/opaque_types.rs @@ -1155,7 +1155,7 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> { for predicate in &bounds.predicates { if let ty::PredicateKind::Projection(projection) = - predicate.ignore_qualifiers(tcx).skip_binder().kind() + predicate.ignore_qualifiers().skip_binder().kind() { if projection.ty.references_error() { // No point on adding these obligations since there's a type error involved. @@ -1254,7 +1254,7 @@ crate fn required_region_bounds( traits::elaborate_predicates(tcx, predicates) .filter_map(|obligation| { debug!("required_region_bounds(obligation={:?})", obligation); - match obligation.predicate.ignore_qualifiers(tcx).skip_binder().kind() { + match obligation.predicate.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::Projection(..) | ty::PredicateKind::Trait(..) | ty::PredicateKind::Subtype(..) diff --git a/src/librustc_trait_selection/traits/auto_trait.rs b/src/librustc_trait_selection/traits/auto_trait.rs index aebe96f7ddc7e..2dbb40363971c 100644 --- a/src/librustc_trait_selection/traits/auto_trait.rs +++ b/src/librustc_trait_selection/traits/auto_trait.rs @@ -418,8 +418,8 @@ impl AutoTraitFinder<'tcx> { ty::PredicateKind::Trait(new_trait, _), ty::PredicateKind::Trait(old_trait, _), ) = ( - new_pred.ignore_qualifiers(self.tcx).skip_binder().kind(), - old_pred.ignore_qualifiers(self.tcx).skip_binder().kind(), + new_pred.ignore_qualifiers().skip_binder().kind(), + old_pred.ignore_qualifiers().skip_binder().kind(), ) { if new_trait.def_id() == old_trait.def_id() { let new_substs = new_trait.trait_ref.substs; @@ -639,7 +639,7 @@ impl AutoTraitFinder<'tcx> { // We check this by calling is_of_param on the relevant types // from the various possible predicates - match predicate.ignore_qualifiers(self.tcx).skip_binder().kind() { + match predicate.ignore_qualifiers().skip_binder().kind() { &ty::PredicateKind::Trait(p, _) => { if self.is_param_no_infer(p.trait_ref.substs) && !only_projections diff --git a/src/librustc_trait_selection/traits/error_reporting/mod.rs b/src/librustc_trait_selection/traits/error_reporting/mod.rs index 75fe9b7701e07..f5717285defbc 100644 --- a/src/librustc_trait_selection/traits/error_reporting/mod.rs +++ b/src/librustc_trait_selection/traits/error_reporting/mod.rs @@ -256,7 +256,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { return; } - match obligation.predicate.ignore_qualifiers(tcx).skip_binder().kind() { + match obligation.predicate.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::ForAll(_) => { bug!("unexpected predicate: {:?}", obligation.predicate) } @@ -1091,8 +1091,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { // FIXME: It should be possible to deal with `ForAll` in a cleaner way. let (cond, error) = match ( - cond.ignore_qualifiers(self.tcx).skip_binder().kind(), - error.ignore_qualifiers(self.tcx).skip_binder().kind(), + cond.ignore_qualifiers().skip_binder().kind(), + error.ignore_qualifiers().skip_binder().kind(), ) { (ty::PredicateKind::Trait(..), &ty::PredicateKind::Trait(error, _)) => { (cond, ty::Binder::bind(error)) @@ -1105,7 +1105,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { for obligation in super::elaborate_predicates(self.tcx, std::iter::once(cond)) { if let &ty::PredicateKind::Trait(implication, _) = - obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind() + obligation.predicate.ignore_qualifiers().skip_binder().kind() { let error = error.to_poly_trait_ref(); let implication = ty::Binder::bind(implication).to_poly_trait_ref(); @@ -1187,7 +1187,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { // this can fail if the problem was higher-ranked, in which // cause I have no idea for a good error message. if let &ty::PredicateKind::Projection(data) = - predicate.ignore_qualifiers(self.tcx).skip_binder().kind() + predicate.ignore_qualifiers().skip_binder().kind() { let mut selcx = SelectionContext::new(self); let (data, _) = self.replace_bound_vars_with_fresh_vars( @@ -1480,7 +1480,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { return; } - let mut err = match predicate.ignore_qualifiers(self.tcx).skip_binder().kind() { + let mut err = match predicate.ignore_qualifiers().skip_binder().kind() { &ty::PredicateKind::Trait(data, _) => { let trait_ref = ty::Binder::bind(data.trait_ref); let self_ty = trait_ref.skip_binder().self_ty(); @@ -1734,7 +1734,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { obligation: &PredicateObligation<'tcx>, ) { let (pred, item_def_id, span) = match ( - obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind(), + obligation.predicate.ignore_qualifiers().skip_binder().kind(), obligation.cause.code.peel_derives(), ) { ( diff --git a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs index 7e3d4908f9af6..1b618cea6008f 100644 --- a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs +++ b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs @@ -1300,7 +1300,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { // bound was introduced. At least one generator should be present for this diagnostic to be // modified. let (mut trait_ref, mut target_ty) = - match obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind() { + match obligation.predicate.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::Trait(p, _) => (Some(p.trait_ref), Some(p.self_ty())), _ => (None, None), }; diff --git a/src/librustc_trait_selection/traits/mod.rs b/src/librustc_trait_selection/traits/mod.rs index da17d89b8c13a..54df3559683f3 100644 --- a/src/librustc_trait_selection/traits/mod.rs +++ b/src/librustc_trait_selection/traits/mod.rs @@ -237,7 +237,7 @@ fn do_normalize_predicates<'tcx>( // We can use the `elaborated_env` here; the region code only // cares about declarations like `'a: 'b`. - let outlives_env = OutlivesEnvironment::new(tcx, elaborated_env); + let outlives_env = OutlivesEnvironment::new(elaborated_env); infcx.resolve_regions_and_report_errors( region_context, @@ -328,7 +328,7 @@ pub fn normalize_param_env_or_error<'tcx>( // This works fairly well because trait matching does not actually care about param-env // TypeOutlives predicates - these are normally used by regionck. let outlives_predicates: Vec<_> = predicates - .drain_filter(|predicate| match predicate.ignore_qualifiers(tcx).skip_binder().kind() { + .drain_filter(|predicate| match predicate.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::TypeOutlives(..) => true, _ => false, }) diff --git a/src/librustc_trait_selection/traits/object_safety.rs b/src/librustc_trait_selection/traits/object_safety.rs index 5ad43084e7f1a..b3525ddeb2147 100644 --- a/src/librustc_trait_selection/traits/object_safety.rs +++ b/src/librustc_trait_selection/traits/object_safety.rs @@ -245,7 +245,7 @@ fn predicates_reference_self( .iter() .map(|(predicate, sp)| (predicate.subst_supertrait(tcx, &trait_ref), sp)) .filter_map(|(predicate, &sp)| { - match predicate.ignore_qualifiers(tcx).skip_binder().kind() { + match predicate.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::Trait(ref data, _) => { // In the case of a trait predicate, we can skip the "self" type. if data.trait_ref.substs[1..].iter().any(has_self_ty) { Some(sp) } else { None } @@ -299,7 +299,7 @@ fn generics_require_sized_self(tcx: TyCtxt<'_>, def_id: DefId) -> bool { let predicates = tcx.predicates_of(def_id); let predicates = predicates.instantiate_identity(tcx).predicates; elaborate_predicates(tcx, predicates.into_iter()).any(|obligation| { - match obligation.predicate.ignore_qualifiers(tcx).skip_binder().kind() { + match obligation.predicate.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::Trait(ref trait_pred, _) => { trait_pred.def_id() == sized_def_id && trait_pred.self_ty().is_param(0) } @@ -400,7 +400,7 @@ fn virtual_call_violation_for_method<'tcx>( // A trait object can't claim to live more than the concrete type, // so outlives predicates will always hold. .cloned() - .filter(|(p, _)| p.to_opt_type_outlives(tcx).is_none()) + .filter(|(p, _)| p.to_opt_type_outlives().is_none()) .collect::>() // Do a shallow visit so that `contains_illegal_self_type_reference` // may apply it's custom visiting. diff --git a/src/librustc_trait_selection/traits/project.rs b/src/librustc_trait_selection/traits/project.rs index c2ae26d9d25a5..a5199b7fbefa8 100644 --- a/src/librustc_trait_selection/traits/project.rs +++ b/src/librustc_trait_selection/traits/project.rs @@ -665,7 +665,7 @@ fn prune_cache_value_obligations<'a, 'tcx>( .obligations .iter() .filter(|obligation| { - match obligation.predicate.ignore_qualifiers(infcx.tcx).skip_binder().kind() { + match obligation.predicate.ignore_qualifiers().skip_binder().kind() { // We found a `T: Foo` predicate, let's check // if `U` references any unresolved type // variables. In principle, we only care if this @@ -934,7 +934,7 @@ fn assemble_candidates_from_predicates<'cx, 'tcx>( for predicate in env_predicates { debug!("assemble_candidates_from_predicates: predicate={:?}", predicate); if let &ty::PredicateKind::Projection(data) = - predicate.ignore_qualifiers(infcx.tcx).skip_binder().kind() + predicate.ignore_qualifiers().skip_binder().kind() { let data = ty::Binder::bind(data); let same_def_id = data.projection_def_id() == obligation.predicate.item_def_id; @@ -1228,7 +1228,7 @@ fn confirm_object_candidate<'cx, 'tcx>( // item with the correct name let env_predicates = env_predicates.filter_map(|o| { - match o.predicate.ignore_qualifiers(selcx.tcx()).skip_binder().kind() { + match o.predicate.ignore_qualifiers().skip_binder().kind() { &ty::PredicateKind::Projection(data) if data.projection_ty.item_def_id == obligation.predicate.item_def_id => { diff --git a/src/librustc_trait_selection/traits/query/type_op/prove_predicate.rs b/src/librustc_trait_selection/traits/query/type_op/prove_predicate.rs index f2a6677e2f626..76154adb1751c 100644 --- a/src/librustc_trait_selection/traits/query/type_op/prove_predicate.rs +++ b/src/librustc_trait_selection/traits/query/type_op/prove_predicate.rs @@ -16,7 +16,7 @@ impl<'tcx> super::QueryTypeOp<'tcx> for ProvePredicate<'tcx> { // we have to prove. No need to canonicalize and all that for // such cases. if let ty::PredicateKind::Trait(trait_ref, _) = - key.value.predicate.ignore_qualifiers(tcx).skip_binder().kind() + key.value.predicate.ignore_qualifiers().skip_binder().kind() { if let Some(sized_def_id) = tcx.lang_items().sized_trait() { if trait_ref.def_id() == sized_def_id { diff --git a/src/librustc_trait_selection/traits/select/candidate_assembly.rs b/src/librustc_trait_selection/traits/select/candidate_assembly.rs index 7b654856ddead..1d5441b8eff85 100644 --- a/src/librustc_trait_selection/traits/select/candidate_assembly.rs +++ b/src/librustc_trait_selection/traits/select/candidate_assembly.rs @@ -181,7 +181,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { stack: &TraitObligationStack<'o, 'tcx>, candidates: &mut SelectionCandidateSet<'tcx>, ) -> Result<(), SelectionError<'tcx>> { - let tcx = self.tcx(); debug!("assemble_candidates_from_caller_bounds({:?})", stack.obligation); let all_bounds = stack @@ -189,7 +188,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { .param_env .caller_bounds() .iter() - .filter_map(move |o| o.to_opt_poly_trait_ref(tcx)); + .filter_map(|o| o.to_opt_poly_trait_ref()); // Micro-optimization: filter out predicates relating to different traits. let matching_bounds = diff --git a/src/librustc_trait_selection/traits/select/mod.rs b/src/librustc_trait_selection/traits/select/mod.rs index 1bc53f0c5c53e..f8b2e0925819f 100644 --- a/src/librustc_trait_selection/traits/select/mod.rs +++ b/src/librustc_trait_selection/traits/select/mod.rs @@ -408,7 +408,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { None => self.check_recursion_limit(&obligation, &obligation)?, } - match obligation.predicate.ignore_qualifiers(self.tcx()).skip_binder().kind() { + match obligation.predicate.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::ForAll(_) => { bug!("unexpected predicate: {:?}", obligation.predicate) } @@ -792,7 +792,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } fn coinductive_predicate(&self, predicate: ty::Predicate<'tcx>) -> bool { - let result = match predicate.ignore_qualifiers(self.tcx()).skip_binder().kind() { + let result = match predicate.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::Trait(ref data, _) => self.tcx().trait_is_auto(data.def_id()), _ => false, }; @@ -1301,8 +1301,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { }; let matching_bound = predicates.iter().find_map(|bound| { - if let ty::PredicateKind::Trait(bound, _) = bound.kind() { - let bound = bound.to_poly_trait_ref(); + if let ty::PredicateKind::Trait(pred, _) = + bound.ignore_qualifiers().skip_binder().kind() + { + let bound = ty::Binder::bind(pred.trait_ref); if self.infcx.probe(|_| { self.match_projection(obligation, bound, placeholder_trait_predicate.trait_ref) }) { diff --git a/src/librustc_trait_selection/traits/specialize/mod.rs b/src/librustc_trait_selection/traits/specialize/mod.rs index dda2e03d77937..9b737d464174a 100644 --- a/src/librustc_trait_selection/traits/specialize/mod.rs +++ b/src/librustc_trait_selection/traits/specialize/mod.rs @@ -497,7 +497,7 @@ fn to_pretty_impl_header(tcx: TyCtxt<'_>, impl_def_id: DefId) -> Option 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(tcx) { + if let Some(poly_trait_ref) = p.to_opt_poly_trait_ref() { if Some(poly_trait_ref.def_id()) == sized_trait { types_without_default_bounds.remove(poly_trait_ref.self_ty().skip_binder()); continue; diff --git a/src/librustc_trait_selection/traits/util.rs b/src/librustc_trait_selection/traits/util.rs index d2fdfb45617cc..cc8997078e0f0 100644 --- a/src/librustc_trait_selection/traits/util.rs +++ b/src/librustc_trait_selection/traits/util.rs @@ -120,7 +120,7 @@ impl<'tcx> TraitAliasExpander<'tcx> { let items = predicates.predicates.iter().rev().filter_map(|(pred, span)| { pred.subst_supertrait(tcx, &trait_ref) - .to_opt_poly_trait_ref(tcx) + .to_opt_poly_trait_ref() .map(|trait_ref| item.clone_and_push(trait_ref, *span)) }); debug!("expand_trait_aliases: items={:?}", items.clone()); @@ -170,7 +170,6 @@ impl Iterator for SupertraitDefIds<'tcx> { type Item = DefId; fn next(&mut self) -> Option { - let tcx = self.tcx; let def_id = self.stack.pop()?; let predicates = self.tcx.super_predicates_of(def_id); let visited = &mut self.visited; @@ -178,7 +177,7 @@ impl Iterator for SupertraitDefIds<'tcx> { predicates .predicates .iter() - .filter_map(move |(pred, _)| pred.to_opt_poly_trait_ref(tcx)) + .filter_map(|(pred, _)| pred.to_opt_poly_trait_ref()) .map(|trait_ref| trait_ref.def_id()) .filter(|&super_def_id| visited.insert(super_def_id)), ); diff --git a/src/librustc_trait_selection/traits/wf.rs b/src/librustc_trait_selection/traits/wf.rs index da1a34871d025..fd762f9d57156 100644 --- a/src/librustc_trait_selection/traits/wf.rs +++ b/src/librustc_trait_selection/traits/wf.rs @@ -196,7 +196,7 @@ fn extend_cause_with_original_assoc_item_obligation<'tcx>( }; // It is fine to skip the binder as we don't care about regions here. - match pred.ignore_qualifiers(tcx).skip_binder().kind() { + match pred.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::Projection(proj) => { // The obligation comes not from the current `impl` nor the `trait` being implemented, // but rather from a "second order" obligation, where an associated type has a @@ -269,7 +269,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { let extend = |obligation: traits::PredicateObligation<'tcx>| { let mut cause = cause.clone(); - if let Some(parent_trait_ref) = obligation.predicate.to_opt_poly_trait_ref(tcx) { + if let Some(parent_trait_ref) = obligation.predicate.to_opt_poly_trait_ref() { let derived_cause = traits::DerivedObligationCause { parent_trait_ref, parent_code: Rc::new(obligation.cause.code.clone()), diff --git a/src/librustc_traits/chalk/lowering.rs b/src/librustc_traits/chalk/lowering.rs index 7154b3fb3784c..7857b19bd90b7 100644 --- a/src/librustc_traits/chalk/lowering.rs +++ b/src/librustc_traits/chalk/lowering.rs @@ -79,7 +79,11 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment { // FIXME(chalk): forall - match predicate.ignore_qualifiers(interner.tcx).skip_binder().kind() { + match predicate + .ignore_qualifiers_with_unbound_vars(interner.tcx) + .skip_binder() + .kind() + { ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", predicate), &ty::PredicateKind::Trait(predicate, _) => { let predicate = ty::Binder::bind(predicate); @@ -102,9 +106,10 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment { + &ty::PredicateKind::RegionOutlives(predicate) => { + let predicate = ty::Binder::bind(predicate); let (predicate, binders, _named_regions) = - collect_bound_vars(interner, interner.tcx, predicate); + collect_bound_vars(interner, interner.tcx, &predicate); Some( chalk_ir::ProgramClauseData(chalk_ir::Binders::new( @@ -186,14 +191,15 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment LowerInto<'tcx, chalk_ir::GoalData>> for ty::Predicate<'tcx> { fn lower_into(self, interner: &RustInterner<'tcx>) -> chalk_ir::GoalData> { // FIXME(chalk): forall - match self.ignore_qualifiers(interner.tcx).skip_binder().kind() { + match self.ignore_qualifiers_with_unbound_vars(interner.tcx).skip_binder().kind() { ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", self), &ty::PredicateKind::Trait(predicate, _) => { ty::Binder::bind(predicate).lower_into(interner) } - ty::PredicateKind::RegionOutlives(predicate) => { + &ty::PredicateKind::RegionOutlives(predicate) => { + let predicate = ty::Binder::bind(predicate); let (predicate, binders, _named_regions) = - collect_bound_vars(interner, interner.tcx, predicate); + collect_bound_vars(interner, interner.tcx, &predicate); chalk_ir::GoalData::Quantified( chalk_ir::QuantifierKind::ForAll, @@ -555,21 +561,22 @@ impl<'tcx> LowerInto<'tcx, Option, ) -> Option>> { // FIXME(chalk): forall - match self.ignore_qualifiers(interner.tcx).skip_binder().kind() { + match self.ignore_qualifiers_with_unbound_vars(interner.tcx).skip_binder().kind() { ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", self), &ty::PredicateKind::Trait(predicate, _) => { - let predicate = &ty::Binder::bind(predicate); + let predicate = ty::Binder::bind(predicate); let (predicate, binders, _named_regions) = - collect_bound_vars(interner, interner.tcx, predicate); + collect_bound_vars(interner, interner.tcx, &predicate); Some(chalk_ir::Binders::new( binders, chalk_ir::WhereClause::Implemented(predicate.trait_ref.lower_into(interner)), )) } - ty::PredicateKind::RegionOutlives(predicate) => { + &ty::PredicateKind::RegionOutlives(predicate) => { + let predicate = ty::Binder::bind(predicate); let (predicate, binders, _named_regions) = - collect_bound_vars(interner, interner.tcx, predicate); + collect_bound_vars(interner, interner.tcx, &predicate); Some(chalk_ir::Binders::new( binders, diff --git a/src/librustc_traits/normalize_erasing_regions.rs b/src/librustc_traits/normalize_erasing_regions.rs index 8415690d41f6c..cdc22b987a798 100644 --- a/src/librustc_traits/normalize_erasing_regions.rs +++ b/src/librustc_traits/normalize_erasing_regions.rs @@ -27,9 +27,7 @@ fn normalize_generic_arg_after_erasing_regions<'tcx>( // always only region relations, and we are about to // erase those anyway: debug_assert_eq!( - normalized_obligations - .iter() - .find(|p| not_outlives_predicate(tcx, &p.predicate)), + normalized_obligations.iter().find(|p| not_outlives_predicate(&p.predicate)), None, ); @@ -41,8 +39,8 @@ fn normalize_generic_arg_after_erasing_regions<'tcx>( }) } -fn not_outlives_predicate(tcx: TyCtxt<'tcx>, p: &ty::Predicate<'tcx>) -> bool { - match p.ignore_qualifiers(tcx).skip_binder().kind() { +fn not_outlives_predicate(p: &ty::Predicate<'tcx>) -> bool { + match p.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::RegionOutlives(..) | ty::PredicateKind::TypeOutlives(..) => false, ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", p), ty::PredicateKind::Trait(..) diff --git a/src/librustc_ty/ty.rs b/src/librustc_ty/ty.rs index c99bc8a47e33b..9daf665dd41ab 100644 --- a/src/librustc_ty/ty.rs +++ b/src/librustc_ty/ty.rs @@ -392,23 +392,23 @@ fn associated_type_projection_predicates( let predicates = item_predicates.filter_map(|obligation| { let pred = obligation.predicate; - match pred.kind() { + match pred.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::Trait(tr, _) => { - if let ty::Projection(p) = tr.skip_binder().self_ty().kind { + if let ty::Projection(p) = tr.self_ty().kind { if p == assoc_item_ty { return Some(pred); } } } ty::PredicateKind::Projection(proj) => { - if let ty::Projection(p) = proj.skip_binder().projection_ty.self_ty().kind { + if let ty::Projection(p) = proj.projection_ty.self_ty().kind { if p == assoc_item_ty { return Some(pred); } } } ty::PredicateKind::TypeOutlives(outlives) => { - if let ty::Projection(p) = outlives.skip_binder().0.kind { + if let ty::Projection(p) = outlives.0.kind { if p == assoc_item_ty { return Some(pred); } @@ -443,17 +443,16 @@ fn opaque_type_projection_predicates( let filtered_predicates = predicates.filter_map(|obligation| { let pred = obligation.predicate; - match pred.kind() { + match pred.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::Trait(tr, _) => { - if let ty::Opaque(opaque_def_id, opaque_substs) = tr.skip_binder().self_ty().kind { + if let ty::Opaque(opaque_def_id, opaque_substs) = tr.self_ty().kind { if opaque_def_id == def_id && opaque_substs == substs { return Some(pred); } } } ty::PredicateKind::Projection(proj) => { - if let ty::Opaque(opaque_def_id, opaque_substs) = - proj.skip_binder().projection_ty.self_ty().kind + if let ty::Opaque(opaque_def_id, opaque_substs) = proj.projection_ty.self_ty().kind { if opaque_def_id == def_id && opaque_substs == substs { return Some(pred); @@ -461,7 +460,7 @@ fn opaque_type_projection_predicates( } } ty::PredicateKind::TypeOutlives(outlives) => { - if let ty::Opaque(opaque_def_id, opaque_substs) = outlives.skip_binder().0.kind { + if let ty::Opaque(opaque_def_id, opaque_substs) = outlives.0.kind { if opaque_def_id == def_id && opaque_substs == substs { return Some(pred); } diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index c028597ccd299..01d48ece663fb 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -1706,7 +1706,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { obligation.predicate ); - match obligation.predicate.ignore_qualifiers(tcx).skip_binder().kind() { + match obligation.predicate.ignore_qualifiers().skip_binder().kind() { &ty::PredicateKind::Trait(pred, _) => { let pred = ty::Binder::bind(pred); associated_types.entry(span).or_default().extend( @@ -2097,7 +2097,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { || { traits::transitive_bounds( tcx, - predicates.iter().filter_map(move |(p, _)| p.to_opt_poly_trait_ref(tcx)), + predicates.iter().filter_map(|(p, _)| p.to_opt_poly_trait_ref()), ) }, || param_name.to_string(), diff --git a/src/librustc_typeck/check/closure.rs b/src/librustc_typeck/check/closure.rs index 9d88a35901e4a..3918eac23eaf8 100644 --- a/src/librustc_typeck/check/closure.rs +++ b/src/librustc_typeck/check/closure.rs @@ -207,7 +207,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ); if let &ty::PredicateKind::Projection(proj_predicate) = - obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind() + obligation.predicate.ignore_qualifiers().skip_binder().kind() { // Given a Projection predicate, we can potentially infer // the complete signature. @@ -632,7 +632,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // will be our output. let output_ty = self.obligations_for_self_ty(ret_vid).find_map(|(_, obligation)| { if let &ty::PredicateKind::Projection(proj_predicate) = - obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind() + obligation.predicate.ignore_qualifiers().skip_binder().kind() { self.deduce_future_output_from_projection( obligation.cause.span, diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index 6b3d986e7b1b5..4df39971afa0e 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -582,25 +582,24 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { while !queue.is_empty() { let obligation = queue.remove(0); debug!("coerce_unsized resolve step: {:?}", obligation); - let trait_pred = - match obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind() { - &ty::PredicateKind::Trait(trait_pred, _) - if traits.contains(&trait_pred.def_id()) => - { - if unsize_did == trait_pred.def_id() { - let unsize_ty = trait_pred.trait_ref.substs[1].expect_ty(); - if let ty::Tuple(..) = unsize_ty.kind { - debug!("coerce_unsized: found unsized tuple coercion"); - has_unsized_tuple_coercion = true; - } + let trait_pred = match obligation.predicate.ignore_qualifiers().skip_binder().kind() { + &ty::PredicateKind::Trait(trait_pred, _) + if traits.contains(&trait_pred.def_id()) => + { + if unsize_did == trait_pred.def_id() { + let unsize_ty = trait_pred.trait_ref.substs[1].expect_ty(); + if let ty::Tuple(..) = unsize_ty.kind { + debug!("coerce_unsized: found unsized tuple coercion"); + has_unsized_tuple_coercion = true; } - ty::Binder::bind(trait_pred) - } - _ => { - coercion.obligations.push(obligation); - continue; } - }; + ty::Binder::bind(trait_pred) + } + _ => { + coercion.obligations.push(obligation); + continue; + } + }; match selcx.select(&obligation.with(trait_pred)) { // Uncertain or unimplemented. Ok(None) => { diff --git a/src/librustc_typeck/check/dropck.rs b/src/librustc_typeck/check/dropck.rs index ae288ef6af781..c06bbc3d015c3 100644 --- a/src/librustc_typeck/check/dropck.rs +++ b/src/librustc_typeck/check/dropck.rs @@ -127,7 +127,7 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>( // it did the wrong thing, so I chose to preserve existing // behavior, since it ought to be simply more // conservative. -nmatsakis - let outlives_env = OutlivesEnvironment::new(infcx.tcx, ty::ParamEnv::empty()); + let outlives_env = OutlivesEnvironment::new(ty::ParamEnv::empty()); infcx.resolve_regions_and_report_errors( drop_impl_did.to_def_id(), @@ -227,8 +227,8 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>( let predicate_matches_closure = |p: Predicate<'tcx>| { let mut relator: SimpleEqRelation<'tcx> = SimpleEqRelation::new(tcx, self_param_env); match ( - predicate.ignore_qualifiers(tcx).skip_binder().kind(), - p.ignore_qualifiers(tcx).skip_binder().kind(), + predicate.ignore_qualifiers().skip_binder().kind(), + p.ignore_qualifiers().skip_binder().kind(), ) { (&ty::PredicateKind::Trait(a, _), &ty::PredicateKind::Trait(b, _)) => { relator.relate(ty::Binder::bind(a), ty::Binder::bind(b)).is_ok() diff --git a/src/librustc_typeck/check/method/confirm.rs b/src/librustc_typeck/check/method/confirm.rs index a9f663fff48a5..645862fa924d0 100644 --- a/src/librustc_typeck/check/method/confirm.rs +++ b/src/librustc_typeck/check/method/confirm.rs @@ -449,7 +449,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> { traits::elaborate_predicates(self.tcx, predicates.predicates.iter().copied()) // We don't care about regions here. .filter_map(|obligation| { - match obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind() { + match obligation.predicate.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::Trait(trait_pred, _) if trait_pred.def_id() == sized_def_id => { diff --git a/src/librustc_typeck/check/method/probe.rs b/src/librustc_typeck/check/method/probe.rs index 90ebce8dc1887..9c5e3cbc93844 100644 --- a/src/librustc_typeck/check/method/probe.rs +++ b/src/librustc_typeck/check/method/probe.rs @@ -795,7 +795,6 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { } fn assemble_inherent_candidates_from_param(&mut self, param_ty: ty::ParamTy) { - let tcx = self.tcx; // FIXME: do we want to commit to this behavior for param bounds? debug!("assemble_inherent_candidates_from_param(param_ty={:?})", param_ty); diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index ddf539277b94d..0900bc583ae1f 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -578,7 +578,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // We don't care about regions here, so it's fine to skip the binder here. if let (ty::Param(_), ty::PredicateKind::Trait(p, _)) = ( &self_ty.kind, - parent_pred.ignore_qualifiers(tcx).skip_binder().kind(), + parent_pred.ignore_qualifiers().skip_binder().kind(), ) { if let ty::Adt(def, _) = p.trait_ref.self_ty().kind { let node = def.did.as_local().map(|def_id| { @@ -631,7 +631,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } }; let mut format_pred = |pred: ty::Predicate<'tcx>| { - match pred.ignore_qualifiers(tcx).skip_binder().kind() { + match pred.ignore_qualifiers().skip_binder().kind() { &ty::PredicateKind::Projection(pred) => { let pred = ty::Binder::bind(pred); // `::Item = String`. @@ -959,7 +959,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // implementing a trait would be legal but is rejected // here). unsatisfied_predicates.iter().all(|(p, _)| { - match p.ignore_qualifiers(self.tcx).skip_binder().kind() { + match p.ignore_qualifiers().skip_binder().kind() { // Hide traits if they are present in predicates as they can be fixed without // having to implement them. ty::PredicateKind::Trait(t, _) => t.def_id() == info.def_id, diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 201288022edb6..0cb0c7d0e3044 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -2400,7 +2400,7 @@ fn bounds_from_generic_predicates<'tcx>( let mut projections = vec![]; for (predicate, _) in predicates.predicates { debug!("predicate {:?}", predicate); - match predicate.ignore_qualifiers(tcx).skip_binder().kind() { + match predicate.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::Trait(trait_predicate, _) => { let entry = types.entry(trait_predicate.self_ty()).or_default(); let def_id = trait_predicate.def_id(); @@ -3894,7 +3894,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .pending_obligations() .into_iter() .filter_map(move |obligation| { - match obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind() { + match obligation.predicate.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::ForAll(_) => { bug!("unexpected predicate: {:?}", obligation.predicate) } @@ -4250,7 +4250,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } if let ty::PredicateKind::Trait(predicate, _) = - error.obligation.predicate.ignore_qualifiers(self.tcx).skip_binder().kind() + error.obligation.predicate.ignore_qualifiers().skip_binder().kind() { // Collect the argument position for all arguments that could have caused this // `FulfillmentError`. @@ -4298,12 +4298,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if let hir::ExprKind::Path(qpath) = &path.kind { if let hir::QPath::Resolved(_, path) = &qpath { for error in errors { - if let ty::PredicateKind::Trait(predicate, _) = error - .obligation - .predicate - .ignore_qualifiers(self.tcx) - .skip_binder() - .kind() + if let ty::PredicateKind::Trait(predicate, _) = + error.obligation.predicate.ignore_qualifiers().skip_binder().kind() { // If any of the type arguments in this path segment caused the // `FullfillmentError`, point at its span (#61860). diff --git a/src/librustc_typeck/check/regionck.rs b/src/librustc_typeck/check/regionck.rs index 71187244601e8..221e5f72dc977 100644 --- a/src/librustc_typeck/check/regionck.rs +++ b/src/librustc_typeck/check/regionck.rs @@ -194,7 +194,7 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> { param_env: ty::ParamEnv<'tcx>, ) -> RegionCtxt<'a, 'tcx> { let region_scope_tree = fcx.tcx.region_scope_tree(subject); - let outlives_environment = OutlivesEnvironment::new(fcx.tcx, param_env); + let outlives_environment = OutlivesEnvironment::new(param_env); RegionCtxt { fcx, region_scope_tree, diff --git a/src/librustc_typeck/coherence/builtin.rs b/src/librustc_typeck/coherence/builtin.rs index d40abf1f54edb..8c6161a626473 100644 --- a/src/librustc_typeck/coherence/builtin.rs +++ b/src/librustc_typeck/coherence/builtin.rs @@ -292,7 +292,7 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef } // Finally, resolve all regions. - let outlives_env = OutlivesEnvironment::new(tcx, param_env); + let outlives_env = OutlivesEnvironment::new(param_env); infcx.resolve_regions_and_report_errors( impl_did.to_def_id(), &outlives_env, @@ -549,7 +549,7 @@ pub fn coerce_unsized_info(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUnsizedI } // Finally, resolve all regions. - let outlives_env = OutlivesEnvironment::new(tcx, param_env); + let outlives_env = OutlivesEnvironment::new(param_env); infcx.resolve_regions_and_report_errors(impl_did, &outlives_env, RegionckMode::default()); CoerceUnsizedInfo { custom_kind: kind } diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 23cf639f3c8cf..636c178d34877 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -552,7 +552,7 @@ fn type_param_predicates( let extra_predicates = extend.into_iter().chain( icx.type_parameter_bounds_in_generics(ast_generics, param_id, ty, OnlySelfBounds(true)) .into_iter() - .filter(|(predicate, _)| match predicate.ignore_qualifiers(tcx).skip_binder().kind() { + .filter(|(predicate, _)| match predicate.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::Trait(data, _) => data.self_ty().is_param(index), _ => false, }), @@ -1004,8 +1004,7 @@ fn super_predicates_of(tcx: TyCtxt<'_>, trait_def_id: DefId) -> ty::GenericPredi // which will, in turn, reach indirect supertraits. for &(pred, span) in superbounds { debug!("superbound: {:?}", pred); - if let ty::PredicateKind::Trait(bound, _) = pred.ignore_qualifiers(tcx).skip_binder().kind() - { + if let ty::PredicateKind::Trait(bound, _) = pred.ignore_qualifiers().skip_binder().kind() { tcx.at(span).super_predicates_of(bound.def_id()); } } diff --git a/src/librustc_typeck/constrained_generic_params.rs b/src/librustc_typeck/constrained_generic_params.rs index 8d469d0e42988..85fe2dba25021 100644 --- a/src/librustc_typeck/constrained_generic_params.rs +++ b/src/librustc_typeck/constrained_generic_params.rs @@ -183,7 +183,7 @@ pub fn setup_constraining_predicates<'tcx>( // Note that we don't have to care about binders here, // as the impl trait ref never contains any late-bound regions. if let ty::PredicateKind::Projection(projection) = - predicates[j].0.ignore_qualifiers(tcx).skip_binder().kind() + predicates[j].0.ignore_qualifiers().skip_binder().kind() { // Special case: watch out for some kind of sneaky attempt // to project out an associated type defined by this very diff --git a/src/librustc_typeck/impl_wf_check/min_specialization.rs b/src/librustc_typeck/impl_wf_check/min_specialization.rs index c1c8e73358bca..50312a99175db 100644 --- a/src/librustc_typeck/impl_wf_check/min_specialization.rs +++ b/src/librustc_typeck/impl_wf_check/min_specialization.rs @@ -163,7 +163,7 @@ fn get_impl_substs<'tcx>( let impl2_substs = translate_substs(infcx, param_env, impl1_def_id, impl1_substs, impl2_node); // Conservatively use an empty `ParamEnv`. - let outlives_env = OutlivesEnvironment::new(tcx, ty::ParamEnv::empty()); + let outlives_env = OutlivesEnvironment::new(ty::ParamEnv::empty()); infcx.resolve_regions_and_report_errors(impl1_def_id, &outlives_env, RegionckMode::default()); let impl2_substs = match infcx.fully_resolve(&impl2_substs) { Ok(s) => s, @@ -199,7 +199,7 @@ fn unconstrained_parent_impl_substs<'tcx>( // unconstrained parameters. for (predicate, _) in impl_generic_predicates.predicates.iter() { if let ty::PredicateKind::Projection(proj) = - predicate.ignore_qualifiers(tcx).skip_binder().kind() + predicate.ignore_qualifiers().skip_binder().kind() { let projection_ty = proj.projection_ty; let projected_ty = proj.ty; @@ -361,7 +361,7 @@ fn check_predicates<'tcx>( fn check_specialization_on<'tcx>(tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tcx>, span: Span) { debug!("can_specialize_on(predicate = {:?})", predicate); - match predicate.ignore_qualifiers(tcx).skip_binder().kind() { + match predicate.ignore_qualifiers().skip_binder().kind() { // Global predicates are either always true or always false, so we // are fine to specialize on. _ if predicate.is_global() => (), @@ -394,7 +394,7 @@ fn trait_predicate_kind<'tcx>( tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tcx>, ) -> Option { - match predicate.ignore_qualifiers(tcx).skip_binder().kind() { + match predicate.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", predicate), ty::PredicateKind::Trait(pred, hir::Constness::NotConst) => { Some(tcx.trait_def(pred.def_id()).specialization_kind) diff --git a/src/librustc_typeck/outlives/explicit.rs b/src/librustc_typeck/outlives/explicit.rs index d6551a5a38716..ac2f8ae36c3df 100644 --- a/src/librustc_typeck/outlives/explicit.rs +++ b/src/librustc_typeck/outlives/explicit.rs @@ -29,7 +29,7 @@ impl<'tcx> ExplicitPredicatesMap<'tcx> { // process predicates and convert to `RequiredPredicates` entry, see below for &(predicate, span) in predicates.predicates { - match predicate.ignore_qualifiers(tcx).skip_binder().kind() { + match predicate.ignore_qualifiers().skip_binder().kind() { ty::PredicateKind::ForAll(_) => bug!("unepected predicate: {:?}", predicate), ty::PredicateKind::TypeOutlives(OutlivesPredicate(ref ty, ref reg)) => { From 562d4784215cb477c7fcec7c2c34df898d59c674 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sun, 21 Jun 2020 14:42:47 +0200 Subject: [PATCH 18/28] fix rustdoc --- src/librustdoc/clean/mod.rs | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index fdfdddea0066d..23defc51a3764 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1661,16 +1661,23 @@ impl<'tcx> Clean for Ty<'tcx> { .predicates .iter() .filter_map(|predicate| { - let trait_ref = if let Some(tr) = predicate.to_opt_poly_trait_ref() { - tr - } else if let Some(pred) = predicate.to_opt_type_outlives() { - // these should turn up at the end - if let Some(r) = pred.skip_binder().1.clean(cx) { - regions.push(GenericBound::Outlives(r)); + // Note: The substs of opaque types can contain unbound variables, + // meaning that we have to use `ignore_qualifiers_with_unbound_vars` here. + let trait_ref = match predicate + .ignore_qualifiers_with_unbound_vars(cx.tcx) + .skip_binder() + .kind() + { + ty::PredicateKind::Trait(tr, _constness) => { + ty::Binder::bind(tr.trait_ref) + } + ty::PredicateKind::TypeOutlives(pred) => { + if let Some(r) = pred.1.clean(cx) { + regions.push(GenericBound::Outlives(r)); + } + return None; } - return None; - } else { - return None; + _ => return None, }; if let Some(sized) = cx.tcx.lang_items().sized_trait() { @@ -1684,10 +1691,11 @@ impl<'tcx> Clean for Ty<'tcx> { .predicates .iter() .filter_map(|pred| { - if let ty::PredicateKind::Projection(proj) = - pred.ignore_qualifiers().skip_binder().kind() + if let ty::PredicateKind::Projection(proj) = pred + .ignore_qualifiers_with_unbound_vars(cx.tcx) + .skip_binder() + .kind() { - let proj = proj; if proj.projection_ty.trait_ref(cx.tcx) == trait_ref.skip_binder() { From 3ba61922d2500e156df7f76ffeccf022cdeb0f9a Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Wed, 24 Jun 2020 17:40:28 +0200 Subject: [PATCH 19/28] this might be unqualified, but at least it's now quantified --- src/librustc_infer/traits/util.rs | 2 +- src/librustc_lint/builtin.rs | 6 +++--- src/librustc_lint/unused.rs | 2 +- src/librustc_middle/ty/mod.rs | 17 ++++++++++------- src/librustc_middle/ty/print/pretty.rs | 2 +- .../borrow_check/diagnostics/region_errors.rs | 2 +- .../transform/qualify_min_const_fn.rs | 2 +- src/librustc_trait_selection/opaque_types.rs | 4 ++-- .../traits/auto_trait.rs | 6 +++--- .../traits/error_reporting/mod.rs | 14 +++++++------- .../traits/error_reporting/suggestions.rs | 2 +- src/librustc_trait_selection/traits/mod.rs | 2 +- .../traits/object_safety.rs | 4 ++-- src/librustc_trait_selection/traits/project.rs | 6 +++--- .../traits/query/type_op/prove_predicate.rs | 2 +- .../traits/select/mod.rs | 6 +++--- src/librustc_trait_selection/traits/wf.rs | 2 +- src/librustc_traits/chalk/lowering.rs | 6 +++--- .../normalize_erasing_regions.rs | 2 +- src/librustc_ty/ty.rs | 4 ++-- src/librustc_typeck/astconv.rs | 2 +- src/librustc_typeck/check/closure.rs | 4 ++-- src/librustc_typeck/check/coercion.rs | 2 +- src/librustc_typeck/check/dropck.rs | 4 ++-- src/librustc_typeck/check/method/confirm.rs | 2 +- src/librustc_typeck/check/method/suggest.rs | 6 +++--- src/librustc_typeck/check/mod.rs | 10 +++++----- src/librustc_typeck/collect.rs | 10 +++++----- .../constrained_generic_params.rs | 2 +- .../impl_wf_check/min_specialization.rs | 6 +++--- src/librustc_typeck/outlives/explicit.rs | 2 +- src/librustc_typeck/outlives/mod.rs | 4 ++-- src/librustdoc/clean/auto_trait.rs | 4 ++-- src/librustdoc/clean/mod.rs | 10 +++++----- src/librustdoc/clean/simplify.rs | 3 ++- .../clippy/clippy_lints/src/future_not_send.rs | 2 +- .../clippy/clippy_lints/src/methods/mod.rs | 2 +- src/tools/clippy/clippy_lints/src/utils/mod.rs | 2 +- 38 files changed, 87 insertions(+), 83 deletions(-) diff --git a/src/librustc_infer/traits/util.rs b/src/librustc_infer/traits/util.rs index 901685d1d844a..cb2f2ca0e9287 100644 --- a/src/librustc_infer/traits/util.rs +++ b/src/librustc_infer/traits/util.rs @@ -138,7 +138,7 @@ impl Elaborator<'tcx> { fn elaborate(&mut self, obligation: &PredicateObligation<'tcx>) { let tcx = self.visited.tcx; - match obligation.predicate.ignore_qualifiers().skip_binder().kind() { + match obligation.predicate.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::ForAll(_) => { bug!("unexpected predicate: {:?}", obligation.predicate) } diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 9566cdb164359..00d532c7ba0ba 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -1210,7 +1210,7 @@ impl<'tcx> LateLintPass<'tcx> for TrivialConstraints { for &(predicate, span) in predicates.predicates { // We don't actually look inside of the predicate, // so it is safe to skip this binder here. - let predicate_kind_name = match predicate.ignore_qualifiers().skip_binder().kind() { + let predicate_kind_name = match predicate.ignore_quantifiers().skip_binder().kind() { Trait(..) => "Trait", TypeOutlives(..) | RegionOutlives(..) => "Lifetime", @@ -1500,7 +1500,7 @@ impl ExplicitOutlivesRequirements { ) -> Vec> { inferred_outlives .iter() - .filter_map(|(pred, _)| match pred.ignore_qualifiers().skip_binder().kind() { + .filter_map(|(pred, _)| match pred.ignore_quantifiers().skip_binder().kind() { &ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => match a { ty::ReEarlyBound(ebr) if ebr.index == index => Some(b), _ => None, @@ -1516,7 +1516,7 @@ impl ExplicitOutlivesRequirements { ) -> Vec> { inferred_outlives .iter() - .filter_map(|(pred, _)| match pred.ignore_qualifiers().skip_binder().kind() { + .filter_map(|(pred, _)| match pred.ignore_quantifiers().skip_binder().kind() { &ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(a, b)) => { a.is_param(index).then_some(b) } diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index 3f1f397fc8e5a..8f21cb51695a2 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -148,7 +148,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { for (predicate, _) in cx.tcx.predicates_of(def).predicates { // We only look at the `DefId`, so it is safe to skip the binder here. if let ty::PredicateKind::Trait(ref poly_trait_predicate, _) = - predicate.ignore_qualifiers().skip_binder().kind() + predicate.ignore_quantifiers().skip_binder().kind() { let def_id = poly_trait_predicate.trait_ref.def_id; let descr_pre = diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index 3782f2b840480..dd74fbc945748 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -1050,7 +1050,7 @@ impl<'tcx> Predicate<'tcx> { } /// Skips `PredicateKind::ForAll`. - pub fn ignore_qualifiers(self) -> Binder> { + pub fn ignore_quantifiers(self) -> Binder> { match self.kind() { &PredicateKind::ForAll(binder) => binder, ty::PredicateKind::Projection(..) @@ -1073,7 +1073,10 @@ impl<'tcx> Predicate<'tcx> { /// /// Do not use this method if you may end up just skipping the binder, as this /// would leave the unbound variables at an incorrect binding level. - pub fn ignore_qualifiers_with_unbound_vars(self, tcx: TyCtxt<'tcx>) -> Binder> { + pub fn ignore_quantifiers_with_unbound_vars( + self, + tcx: TyCtxt<'tcx>, + ) -> Binder> { match self.kind() { &PredicateKind::ForAll(binder) => binder, ty::PredicateKind::Projection(..) @@ -1090,7 +1093,7 @@ impl<'tcx> Predicate<'tcx> { } /// Wraps `self` with the given qualifier if this predicate has any unbound variables. - pub fn potentially_qualified( + pub fn potentially_quantified( self, tcx: TyCtxt<'tcx>, qualifier: impl FnOnce(Binder>) -> PredicateKind<'tcx>, @@ -1249,9 +1252,9 @@ impl<'tcx> Predicate<'tcx> { // from the substitution and the value being substituted into, and // this trick achieves that). let substs = trait_ref.skip_binder().substs; - let pred = *self.ignore_qualifiers().skip_binder(); + let pred = *self.ignore_quantifiers().skip_binder(); let new = pred.subst(tcx, substs); - if new != pred { new.potentially_qualified(tcx, PredicateKind::ForAll) } else { self } + if new != pred { new.potentially_quantified(tcx, PredicateKind::ForAll) } else { self } } } @@ -1451,7 +1454,7 @@ impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> { impl<'tcx> Predicate<'tcx> { pub fn to_opt_poly_trait_ref(self) -> Option> { - self.ignore_qualifiers() + self.ignore_quantifiers() .map_bound(|pred| match pred.kind() { &PredicateKind::Trait(ref t, _) => Some(t.trait_ref), PredicateKind::Projection(..) @@ -1469,7 +1472,7 @@ impl<'tcx> Predicate<'tcx> { } pub fn to_opt_type_outlives(self) -> Option> { - self.ignore_qualifiers() + self.ignore_quantifiers() .map_bound(|pred| match pred.kind() { &PredicateKind::TypeOutlives(data) => Some(data), PredicateKind::Trait(..) diff --git a/src/librustc_middle/ty/print/pretty.rs b/src/librustc_middle/ty/print/pretty.rs index 1f9fc4818839f..d22a0e8c38e04 100644 --- a/src/librustc_middle/ty/print/pretty.rs +++ b/src/librustc_middle/ty/print/pretty.rs @@ -577,7 +577,7 @@ pub trait PrettyPrinter<'tcx>: // // FIXME(lcnr): Find out why exactly this is the case :) if let ty::PredicateKind::Trait(pred, _) = predicate - .ignore_qualifiers_with_unbound_vars(self.tcx()) + .ignore_quantifiers_with_unbound_vars(self.tcx()) .skip_binder() .kind() { diff --git a/src/librustc_mir/borrow_check/diagnostics/region_errors.rs b/src/librustc_mir/borrow_check/diagnostics/region_errors.rs index c28f23dc7d611..ffeec8758c46f 100644 --- a/src/librustc_mir/borrow_check/diagnostics/region_errors.rs +++ b/src/librustc_mir/borrow_check/diagnostics/region_errors.rs @@ -590,7 +590,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { let mut found = false; for predicate in bounds.predicates { if let ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(_, r)) = - predicate.ignore_qualifiers().skip_binder().kind() + predicate.ignore_quantifiers().skip_binder().kind() { if let ty::RegionKind::ReStatic = r { found = true; diff --git a/src/librustc_mir/transform/qualify_min_const_fn.rs b/src/librustc_mir/transform/qualify_min_const_fn.rs index c176fbf90b63f..69e8c6b5d2acb 100644 --- a/src/librustc_mir/transform/qualify_min_const_fn.rs +++ b/src/librustc_mir/transform/qualify_min_const_fn.rs @@ -24,7 +24,7 @@ pub fn is_min_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'a Body<'tcx>) - loop { let predicates = tcx.predicates_of(current); for (predicate, _) in predicates.predicates { - match predicate.ignore_qualifiers().skip_binder().kind() { + match predicate.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", predicate), ty::PredicateKind::RegionOutlives(_) | ty::PredicateKind::TypeOutlives(_) diff --git a/src/librustc_trait_selection/opaque_types.rs b/src/librustc_trait_selection/opaque_types.rs index b482f073858b9..1067ca20a174f 100644 --- a/src/librustc_trait_selection/opaque_types.rs +++ b/src/librustc_trait_selection/opaque_types.rs @@ -1155,7 +1155,7 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> { for predicate in &bounds.predicates { if let ty::PredicateKind::Projection(projection) = - predicate.ignore_qualifiers().skip_binder().kind() + predicate.ignore_quantifiers().skip_binder().kind() { if projection.ty.references_error() { // No point on adding these obligations since there's a type error involved. @@ -1254,7 +1254,7 @@ crate fn required_region_bounds( traits::elaborate_predicates(tcx, predicates) .filter_map(|obligation| { debug!("required_region_bounds(obligation={:?})", obligation); - match obligation.predicate.ignore_qualifiers().skip_binder().kind() { + match obligation.predicate.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::Projection(..) | ty::PredicateKind::Trait(..) | ty::PredicateKind::Subtype(..) diff --git a/src/librustc_trait_selection/traits/auto_trait.rs b/src/librustc_trait_selection/traits/auto_trait.rs index 2dbb40363971c..b5bea9724283d 100644 --- a/src/librustc_trait_selection/traits/auto_trait.rs +++ b/src/librustc_trait_selection/traits/auto_trait.rs @@ -418,8 +418,8 @@ impl AutoTraitFinder<'tcx> { ty::PredicateKind::Trait(new_trait, _), ty::PredicateKind::Trait(old_trait, _), ) = ( - new_pred.ignore_qualifiers().skip_binder().kind(), - old_pred.ignore_qualifiers().skip_binder().kind(), + new_pred.ignore_quantifiers().skip_binder().kind(), + old_pred.ignore_quantifiers().skip_binder().kind(), ) { if new_trait.def_id() == old_trait.def_id() { let new_substs = new_trait.trait_ref.substs; @@ -639,7 +639,7 @@ impl AutoTraitFinder<'tcx> { // We check this by calling is_of_param on the relevant types // from the various possible predicates - match predicate.ignore_qualifiers().skip_binder().kind() { + match predicate.ignore_quantifiers().skip_binder().kind() { &ty::PredicateKind::Trait(p, _) => { if self.is_param_no_infer(p.trait_ref.substs) && !only_projections diff --git a/src/librustc_trait_selection/traits/error_reporting/mod.rs b/src/librustc_trait_selection/traits/error_reporting/mod.rs index f5717285defbc..8af4e12bd2b0f 100644 --- a/src/librustc_trait_selection/traits/error_reporting/mod.rs +++ b/src/librustc_trait_selection/traits/error_reporting/mod.rs @@ -256,7 +256,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { return; } - match obligation.predicate.ignore_qualifiers().skip_binder().kind() { + match obligation.predicate.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::ForAll(_) => { bug!("unexpected predicate: {:?}", obligation.predicate) } @@ -1091,8 +1091,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { // FIXME: It should be possible to deal with `ForAll` in a cleaner way. let (cond, error) = match ( - cond.ignore_qualifiers().skip_binder().kind(), - error.ignore_qualifiers().skip_binder().kind(), + cond.ignore_quantifiers().skip_binder().kind(), + error.ignore_quantifiers().skip_binder().kind(), ) { (ty::PredicateKind::Trait(..), &ty::PredicateKind::Trait(error, _)) => { (cond, ty::Binder::bind(error)) @@ -1105,7 +1105,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { for obligation in super::elaborate_predicates(self.tcx, std::iter::once(cond)) { if let &ty::PredicateKind::Trait(implication, _) = - obligation.predicate.ignore_qualifiers().skip_binder().kind() + obligation.predicate.ignore_quantifiers().skip_binder().kind() { let error = error.to_poly_trait_ref(); let implication = ty::Binder::bind(implication).to_poly_trait_ref(); @@ -1187,7 +1187,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { // this can fail if the problem was higher-ranked, in which // cause I have no idea for a good error message. if let &ty::PredicateKind::Projection(data) = - predicate.ignore_qualifiers().skip_binder().kind() + predicate.ignore_quantifiers().skip_binder().kind() { let mut selcx = SelectionContext::new(self); let (data, _) = self.replace_bound_vars_with_fresh_vars( @@ -1480,7 +1480,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { return; } - let mut err = match predicate.ignore_qualifiers().skip_binder().kind() { + let mut err = match predicate.ignore_quantifiers().skip_binder().kind() { &ty::PredicateKind::Trait(data, _) => { let trait_ref = ty::Binder::bind(data.trait_ref); let self_ty = trait_ref.skip_binder().self_ty(); @@ -1734,7 +1734,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { obligation: &PredicateObligation<'tcx>, ) { let (pred, item_def_id, span) = match ( - obligation.predicate.ignore_qualifiers().skip_binder().kind(), + obligation.predicate.ignore_quantifiers().skip_binder().kind(), obligation.cause.code.peel_derives(), ) { ( diff --git a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs index 1b618cea6008f..9180325fb74f6 100644 --- a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs +++ b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs @@ -1300,7 +1300,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { // bound was introduced. At least one generator should be present for this diagnostic to be // modified. let (mut trait_ref, mut target_ty) = - match obligation.predicate.ignore_qualifiers().skip_binder().kind() { + match obligation.predicate.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::Trait(p, _) => (Some(p.trait_ref), Some(p.self_ty())), _ => (None, None), }; diff --git a/src/librustc_trait_selection/traits/mod.rs b/src/librustc_trait_selection/traits/mod.rs index 54df3559683f3..bd0ba5570c7bb 100644 --- a/src/librustc_trait_selection/traits/mod.rs +++ b/src/librustc_trait_selection/traits/mod.rs @@ -328,7 +328,7 @@ pub fn normalize_param_env_or_error<'tcx>( // This works fairly well because trait matching does not actually care about param-env // TypeOutlives predicates - these are normally used by regionck. let outlives_predicates: Vec<_> = predicates - .drain_filter(|predicate| match predicate.ignore_qualifiers().skip_binder().kind() { + .drain_filter(|predicate| match predicate.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::TypeOutlives(..) => true, _ => false, }) diff --git a/src/librustc_trait_selection/traits/object_safety.rs b/src/librustc_trait_selection/traits/object_safety.rs index b3525ddeb2147..8da5e7318c002 100644 --- a/src/librustc_trait_selection/traits/object_safety.rs +++ b/src/librustc_trait_selection/traits/object_safety.rs @@ -245,7 +245,7 @@ fn predicates_reference_self( .iter() .map(|(predicate, sp)| (predicate.subst_supertrait(tcx, &trait_ref), sp)) .filter_map(|(predicate, &sp)| { - match predicate.ignore_qualifiers().skip_binder().kind() { + match predicate.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::Trait(ref data, _) => { // In the case of a trait predicate, we can skip the "self" type. if data.trait_ref.substs[1..].iter().any(has_self_ty) { Some(sp) } else { None } @@ -299,7 +299,7 @@ fn generics_require_sized_self(tcx: TyCtxt<'_>, def_id: DefId) -> bool { let predicates = tcx.predicates_of(def_id); let predicates = predicates.instantiate_identity(tcx).predicates; elaborate_predicates(tcx, predicates.into_iter()).any(|obligation| { - match obligation.predicate.ignore_qualifiers().skip_binder().kind() { + match obligation.predicate.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::Trait(ref trait_pred, _) => { trait_pred.def_id() == sized_def_id && trait_pred.self_ty().is_param(0) } diff --git a/src/librustc_trait_selection/traits/project.rs b/src/librustc_trait_selection/traits/project.rs index a5199b7fbefa8..c12f9eb112f9d 100644 --- a/src/librustc_trait_selection/traits/project.rs +++ b/src/librustc_trait_selection/traits/project.rs @@ -665,7 +665,7 @@ fn prune_cache_value_obligations<'a, 'tcx>( .obligations .iter() .filter(|obligation| { - match obligation.predicate.ignore_qualifiers().skip_binder().kind() { + match obligation.predicate.ignore_quantifiers().skip_binder().kind() { // We found a `T: Foo` predicate, let's check // if `U` references any unresolved type // variables. In principle, we only care if this @@ -934,7 +934,7 @@ fn assemble_candidates_from_predicates<'cx, 'tcx>( for predicate in env_predicates { debug!("assemble_candidates_from_predicates: predicate={:?}", predicate); if let &ty::PredicateKind::Projection(data) = - predicate.ignore_qualifiers().skip_binder().kind() + predicate.ignore_quantifiers().skip_binder().kind() { let data = ty::Binder::bind(data); let same_def_id = data.projection_def_id() == obligation.predicate.item_def_id; @@ -1228,7 +1228,7 @@ fn confirm_object_candidate<'cx, 'tcx>( // item with the correct name let env_predicates = env_predicates.filter_map(|o| { - match o.predicate.ignore_qualifiers().skip_binder().kind() { + match o.predicate.ignore_quantifiers().skip_binder().kind() { &ty::PredicateKind::Projection(data) if data.projection_ty.item_def_id == obligation.predicate.item_def_id => { diff --git a/src/librustc_trait_selection/traits/query/type_op/prove_predicate.rs b/src/librustc_trait_selection/traits/query/type_op/prove_predicate.rs index 76154adb1751c..24551299df252 100644 --- a/src/librustc_trait_selection/traits/query/type_op/prove_predicate.rs +++ b/src/librustc_trait_selection/traits/query/type_op/prove_predicate.rs @@ -16,7 +16,7 @@ impl<'tcx> super::QueryTypeOp<'tcx> for ProvePredicate<'tcx> { // we have to prove. No need to canonicalize and all that for // such cases. if let ty::PredicateKind::Trait(trait_ref, _) = - key.value.predicate.ignore_qualifiers().skip_binder().kind() + key.value.predicate.ignore_quantifiers().skip_binder().kind() { if let Some(sized_def_id) = tcx.lang_items().sized_trait() { if trait_ref.def_id() == sized_def_id { diff --git a/src/librustc_trait_selection/traits/select/mod.rs b/src/librustc_trait_selection/traits/select/mod.rs index f8b2e0925819f..4b1e2013feff2 100644 --- a/src/librustc_trait_selection/traits/select/mod.rs +++ b/src/librustc_trait_selection/traits/select/mod.rs @@ -408,7 +408,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { None => self.check_recursion_limit(&obligation, &obligation)?, } - match obligation.predicate.ignore_qualifiers().skip_binder().kind() { + match obligation.predicate.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::ForAll(_) => { bug!("unexpected predicate: {:?}", obligation.predicate) } @@ -792,7 +792,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } fn coinductive_predicate(&self, predicate: ty::Predicate<'tcx>) -> bool { - let result = match predicate.ignore_qualifiers().skip_binder().kind() { + let result = match predicate.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::Trait(ref data, _) => self.tcx().trait_is_auto(data.def_id()), _ => false, }; @@ -1302,7 +1302,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let matching_bound = predicates.iter().find_map(|bound| { if let ty::PredicateKind::Trait(pred, _) = - bound.ignore_qualifiers().skip_binder().kind() + bound.ignore_quantifiers().skip_binder().kind() { let bound = ty::Binder::bind(pred.trait_ref); if self.infcx.probe(|_| { diff --git a/src/librustc_trait_selection/traits/wf.rs b/src/librustc_trait_selection/traits/wf.rs index fd762f9d57156..ffafbabddfcde 100644 --- a/src/librustc_trait_selection/traits/wf.rs +++ b/src/librustc_trait_selection/traits/wf.rs @@ -196,7 +196,7 @@ fn extend_cause_with_original_assoc_item_obligation<'tcx>( }; // It is fine to skip the binder as we don't care about regions here. - match pred.ignore_qualifiers().skip_binder().kind() { + match pred.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::Projection(proj) => { // The obligation comes not from the current `impl` nor the `trait` being implemented, // but rather from a "second order" obligation, where an associated type has a diff --git a/src/librustc_traits/chalk/lowering.rs b/src/librustc_traits/chalk/lowering.rs index 7857b19bd90b7..b781c2e2a6e9a 100644 --- a/src/librustc_traits/chalk/lowering.rs +++ b/src/librustc_traits/chalk/lowering.rs @@ -80,7 +80,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment { // FIXME(chalk): forall match predicate - .ignore_qualifiers_with_unbound_vars(interner.tcx) + .ignore_quantifiers_with_unbound_vars(interner.tcx) .skip_binder() .kind() { @@ -191,7 +191,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment LowerInto<'tcx, chalk_ir::GoalData>> for ty::Predicate<'tcx> { fn lower_into(self, interner: &RustInterner<'tcx>) -> chalk_ir::GoalData> { // FIXME(chalk): forall - match self.ignore_qualifiers_with_unbound_vars(interner.tcx).skip_binder().kind() { + match self.ignore_quantifiers_with_unbound_vars(interner.tcx).skip_binder().kind() { ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", self), &ty::PredicateKind::Trait(predicate, _) => { ty::Binder::bind(predicate).lower_into(interner) @@ -561,7 +561,7 @@ impl<'tcx> LowerInto<'tcx, Option, ) -> Option>> { // FIXME(chalk): forall - match self.ignore_qualifiers_with_unbound_vars(interner.tcx).skip_binder().kind() { + match self.ignore_quantifiers_with_unbound_vars(interner.tcx).skip_binder().kind() { ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", self), &ty::PredicateKind::Trait(predicate, _) => { let predicate = ty::Binder::bind(predicate); diff --git a/src/librustc_traits/normalize_erasing_regions.rs b/src/librustc_traits/normalize_erasing_regions.rs index cdc22b987a798..cefc20972143a 100644 --- a/src/librustc_traits/normalize_erasing_regions.rs +++ b/src/librustc_traits/normalize_erasing_regions.rs @@ -40,7 +40,7 @@ fn normalize_generic_arg_after_erasing_regions<'tcx>( } fn not_outlives_predicate(p: &ty::Predicate<'tcx>) -> bool { - match p.ignore_qualifiers().skip_binder().kind() { + match p.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::RegionOutlives(..) | ty::PredicateKind::TypeOutlives(..) => false, ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", p), ty::PredicateKind::Trait(..) diff --git a/src/librustc_ty/ty.rs b/src/librustc_ty/ty.rs index 9daf665dd41ab..48802893926b7 100644 --- a/src/librustc_ty/ty.rs +++ b/src/librustc_ty/ty.rs @@ -392,7 +392,7 @@ fn associated_type_projection_predicates( let predicates = item_predicates.filter_map(|obligation| { let pred = obligation.predicate; - match pred.ignore_qualifiers().skip_binder().kind() { + match pred.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::Trait(tr, _) => { if let ty::Projection(p) = tr.self_ty().kind { if p == assoc_item_ty { @@ -443,7 +443,7 @@ fn opaque_type_projection_predicates( let filtered_predicates = predicates.filter_map(|obligation| { let pred = obligation.predicate; - match pred.ignore_qualifiers().skip_binder().kind() { + match pred.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::Trait(tr, _) => { if let ty::Opaque(opaque_def_id, opaque_substs) = tr.self_ty().kind { if opaque_def_id == def_id && opaque_substs == substs { diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 01d48ece663fb..841bfbfba7028 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -1706,7 +1706,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { obligation.predicate ); - match obligation.predicate.ignore_qualifiers().skip_binder().kind() { + match obligation.predicate.ignore_quantifiers().skip_binder().kind() { &ty::PredicateKind::Trait(pred, _) => { let pred = ty::Binder::bind(pred); associated_types.entry(span).or_default().extend( diff --git a/src/librustc_typeck/check/closure.rs b/src/librustc_typeck/check/closure.rs index 3918eac23eaf8..6b7848c2eb982 100644 --- a/src/librustc_typeck/check/closure.rs +++ b/src/librustc_typeck/check/closure.rs @@ -207,7 +207,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ); if let &ty::PredicateKind::Projection(proj_predicate) = - obligation.predicate.ignore_qualifiers().skip_binder().kind() + obligation.predicate.ignore_quantifiers().skip_binder().kind() { // Given a Projection predicate, we can potentially infer // the complete signature. @@ -632,7 +632,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // will be our output. let output_ty = self.obligations_for_self_ty(ret_vid).find_map(|(_, obligation)| { if let &ty::PredicateKind::Projection(proj_predicate) = - obligation.predicate.ignore_qualifiers().skip_binder().kind() + obligation.predicate.ignore_quantifiers().skip_binder().kind() { self.deduce_future_output_from_projection( obligation.cause.span, diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index 4df39971afa0e..2b6362230f836 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -582,7 +582,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { while !queue.is_empty() { let obligation = queue.remove(0); debug!("coerce_unsized resolve step: {:?}", obligation); - let trait_pred = match obligation.predicate.ignore_qualifiers().skip_binder().kind() { + let trait_pred = match obligation.predicate.ignore_quantifiers().skip_binder().kind() { &ty::PredicateKind::Trait(trait_pred, _) if traits.contains(&trait_pred.def_id()) => { diff --git a/src/librustc_typeck/check/dropck.rs b/src/librustc_typeck/check/dropck.rs index c06bbc3d015c3..4bbdb2a5ad3db 100644 --- a/src/librustc_typeck/check/dropck.rs +++ b/src/librustc_typeck/check/dropck.rs @@ -227,8 +227,8 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>( let predicate_matches_closure = |p: Predicate<'tcx>| { let mut relator: SimpleEqRelation<'tcx> = SimpleEqRelation::new(tcx, self_param_env); match ( - predicate.ignore_qualifiers().skip_binder().kind(), - p.ignore_qualifiers().skip_binder().kind(), + predicate.ignore_quantifiers().skip_binder().kind(), + p.ignore_quantifiers().skip_binder().kind(), ) { (&ty::PredicateKind::Trait(a, _), &ty::PredicateKind::Trait(b, _)) => { relator.relate(ty::Binder::bind(a), ty::Binder::bind(b)).is_ok() diff --git a/src/librustc_typeck/check/method/confirm.rs b/src/librustc_typeck/check/method/confirm.rs index 645862fa924d0..b97dd8bf348f5 100644 --- a/src/librustc_typeck/check/method/confirm.rs +++ b/src/librustc_typeck/check/method/confirm.rs @@ -449,7 +449,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> { traits::elaborate_predicates(self.tcx, predicates.predicates.iter().copied()) // We don't care about regions here. .filter_map(|obligation| { - match obligation.predicate.ignore_qualifiers().skip_binder().kind() { + match obligation.predicate.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::Trait(trait_pred, _) if trait_pred.def_id() == sized_def_id => { diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 0900bc583ae1f..cedd7926290ea 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -578,7 +578,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // We don't care about regions here, so it's fine to skip the binder here. if let (ty::Param(_), ty::PredicateKind::Trait(p, _)) = ( &self_ty.kind, - parent_pred.ignore_qualifiers().skip_binder().kind(), + parent_pred.ignore_quantifiers().skip_binder().kind(), ) { if let ty::Adt(def, _) = p.trait_ref.self_ty().kind { let node = def.did.as_local().map(|def_id| { @@ -631,7 +631,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } }; let mut format_pred = |pred: ty::Predicate<'tcx>| { - match pred.ignore_qualifiers().skip_binder().kind() { + match pred.ignore_quantifiers().skip_binder().kind() { &ty::PredicateKind::Projection(pred) => { let pred = ty::Binder::bind(pred); // `::Item = String`. @@ -959,7 +959,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // implementing a trait would be legal but is rejected // here). unsatisfied_predicates.iter().all(|(p, _)| { - match p.ignore_qualifiers().skip_binder().kind() { + match p.ignore_quantifiers().skip_binder().kind() { // Hide traits if they are present in predicates as they can be fixed without // having to implement them. ty::PredicateKind::Trait(t, _) => t.def_id() == info.def_id, diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 0cb0c7d0e3044..f54a59486656b 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -2400,7 +2400,7 @@ fn bounds_from_generic_predicates<'tcx>( let mut projections = vec![]; for (predicate, _) in predicates.predicates { debug!("predicate {:?}", predicate); - match predicate.ignore_qualifiers().skip_binder().kind() { + match predicate.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::Trait(trait_predicate, _) => { let entry = types.entry(trait_predicate.self_ty()).or_default(); let def_id = trait_predicate.def_id(); @@ -3894,7 +3894,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .pending_obligations() .into_iter() .filter_map(move |obligation| { - match obligation.predicate.ignore_qualifiers().skip_binder().kind() { + match obligation.predicate.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::ForAll(_) => { bug!("unexpected predicate: {:?}", obligation.predicate) } @@ -4250,7 +4250,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } if let ty::PredicateKind::Trait(predicate, _) = - error.obligation.predicate.ignore_qualifiers().skip_binder().kind() + error.obligation.predicate.ignore_quantifiers().skip_binder().kind() { // Collect the argument position for all arguments that could have caused this // `FulfillmentError`. @@ -4299,7 +4299,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if let hir::QPath::Resolved(_, path) = &qpath { for error in errors { if let ty::PredicateKind::Trait(predicate, _) = - error.obligation.predicate.ignore_qualifiers().skip_binder().kind() + error.obligation.predicate.ignore_quantifiers().skip_binder().kind() { // If any of the type arguments in this path segment caused the // `FullfillmentError`, point at its span (#61860). @@ -5377,7 +5377,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ty: expected, }) .to_predicate(self.tcx) - .potentially_qualified(self.tcx, ty::PredicateKind::ForAll); + .potentially_quantified(self.tcx, ty::PredicateKind::ForAll); let obligation = traits::Obligation::new(self.misc(sp), self.param_env, predicate); debug!("suggest_missing_await: trying obligation {:?}", obligation); diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 636c178d34877..e5d2c569004e1 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -552,7 +552,7 @@ fn type_param_predicates( let extra_predicates = extend.into_iter().chain( icx.type_parameter_bounds_in_generics(ast_generics, param_id, ty, OnlySelfBounds(true)) .into_iter() - .filter(|(predicate, _)| match predicate.ignore_qualifiers().skip_binder().kind() { + .filter(|(predicate, _)| match predicate.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::Trait(data, _) => data.self_ty().is_param(index), _ => false, }), @@ -1004,7 +1004,7 @@ fn super_predicates_of(tcx: TyCtxt<'_>, trait_def_id: DefId) -> ty::GenericPredi // which will, in turn, reach indirect supertraits. for &(pred, span) in superbounds { debug!("superbound: {:?}", pred); - if let ty::PredicateKind::Trait(bound, _) = pred.ignore_qualifiers().skip_binder().kind() { + if let ty::PredicateKind::Trait(bound, _) = pred.ignore_quantifiers().skip_binder().kind() { tcx.at(span).super_predicates_of(bound.def_id()); } } @@ -1962,7 +1962,7 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat predicates.push(( ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, region)) .to_predicate(tcx) - .potentially_qualified(tcx, ty::PredicateKind::ForAll), + .potentially_quantified(tcx, ty::PredicateKind::ForAll), lifetime.span, )) } @@ -1982,7 +1982,7 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat let pred = ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(r1, r2)) .to_predicate(icx.tcx); - (pred.potentially_qualified(icx.tcx, ty::PredicateKind::ForAll), span) + (pred.potentially_quantified(icx.tcx, ty::PredicateKind::ForAll), span) })) } @@ -2112,7 +2112,7 @@ fn predicates_from_bound<'tcx>( let region = astconv.ast_region_to_region(lifetime, None); let pred = ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(param_ty, region)) .to_predicate(astconv.tcx()) - .potentially_qualified(astconv.tcx(), ty::PredicateKind::ForAll); + .potentially_quantified(astconv.tcx(), ty::PredicateKind::ForAll); vec![(pred, lifetime.span)] } } diff --git a/src/librustc_typeck/constrained_generic_params.rs b/src/librustc_typeck/constrained_generic_params.rs index 85fe2dba25021..7936d003f54ca 100644 --- a/src/librustc_typeck/constrained_generic_params.rs +++ b/src/librustc_typeck/constrained_generic_params.rs @@ -183,7 +183,7 @@ pub fn setup_constraining_predicates<'tcx>( // Note that we don't have to care about binders here, // as the impl trait ref never contains any late-bound regions. if let ty::PredicateKind::Projection(projection) = - predicates[j].0.ignore_qualifiers().skip_binder().kind() + predicates[j].0.ignore_quantifiers().skip_binder().kind() { // Special case: watch out for some kind of sneaky attempt // to project out an associated type defined by this very diff --git a/src/librustc_typeck/impl_wf_check/min_specialization.rs b/src/librustc_typeck/impl_wf_check/min_specialization.rs index 50312a99175db..0cc99a7a54e9e 100644 --- a/src/librustc_typeck/impl_wf_check/min_specialization.rs +++ b/src/librustc_typeck/impl_wf_check/min_specialization.rs @@ -199,7 +199,7 @@ fn unconstrained_parent_impl_substs<'tcx>( // unconstrained parameters. for (predicate, _) in impl_generic_predicates.predicates.iter() { if let ty::PredicateKind::Projection(proj) = - predicate.ignore_qualifiers().skip_binder().kind() + predicate.ignore_quantifiers().skip_binder().kind() { let projection_ty = proj.projection_ty; let projected_ty = proj.ty; @@ -361,7 +361,7 @@ fn check_predicates<'tcx>( fn check_specialization_on<'tcx>(tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tcx>, span: Span) { debug!("can_specialize_on(predicate = {:?})", predicate); - match predicate.ignore_qualifiers().skip_binder().kind() { + match predicate.ignore_quantifiers().skip_binder().kind() { // Global predicates are either always true or always false, so we // are fine to specialize on. _ if predicate.is_global() => (), @@ -394,7 +394,7 @@ fn trait_predicate_kind<'tcx>( tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tcx>, ) -> Option { - match predicate.ignore_qualifiers().skip_binder().kind() { + match predicate.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", predicate), ty::PredicateKind::Trait(pred, hir::Constness::NotConst) => { Some(tcx.trait_def(pred.def_id()).specialization_kind) diff --git a/src/librustc_typeck/outlives/explicit.rs b/src/librustc_typeck/outlives/explicit.rs index ac2f8ae36c3df..bbde0b63cbbba 100644 --- a/src/librustc_typeck/outlives/explicit.rs +++ b/src/librustc_typeck/outlives/explicit.rs @@ -29,7 +29,7 @@ impl<'tcx> ExplicitPredicatesMap<'tcx> { // process predicates and convert to `RequiredPredicates` entry, see below for &(predicate, span) in predicates.predicates { - match predicate.ignore_qualifiers().skip_binder().kind() { + match predicate.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::ForAll(_) => bug!("unepected predicate: {:?}", predicate), ty::PredicateKind::TypeOutlives(OutlivesPredicate(ref ty, ref reg)) => { diff --git a/src/librustc_typeck/outlives/mod.rs b/src/librustc_typeck/outlives/mod.rs index d450a27941ff9..56badb324c73e 100644 --- a/src/librustc_typeck/outlives/mod.rs +++ b/src/librustc_typeck/outlives/mod.rs @@ -87,7 +87,7 @@ fn inferred_outlives_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> CratePredica GenericArgKind::Type(ty1) => Some(( ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty1, region2)) .to_predicate(tcx) - .potentially_qualified(tcx, ty::PredicateKind::ForAll), + .potentially_quantified(tcx, ty::PredicateKind::ForAll), span, )), GenericArgKind::Lifetime(region1) => Some(( @@ -95,7 +95,7 @@ fn inferred_outlives_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> CratePredica region1, region2, )) .to_predicate(tcx) - .potentially_qualified(tcx, ty::PredicateKind::ForAll), + .potentially_quantified(tcx, ty::PredicateKind::ForAll), span, )), GenericArgKind::Const(_) => { diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs index d49afb551bd8b..498fa25836c95 100644 --- a/src/librustdoc/clean/auto_trait.rs +++ b/src/librustdoc/clean/auto_trait.rs @@ -315,7 +315,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> { tcx: TyCtxt<'tcx>, pred: ty::Predicate<'tcx>, ) -> FxHashSet { - let regions = match pred.ignore_qualifiers().skip_binder().kind() { + let regions = match pred.ignore_quantifiers().skip_binder().kind() { &ty::PredicateKind::Trait(poly_trait_pred, _) => { tcx.collect_referenced_late_bound_regions(&ty::Binder::bind(poly_trait_pred)) } @@ -465,7 +465,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> { .iter() .filter(|p| { !orig_bounds.contains(p) - || match p.ignore_qualifiers().skip_binder().kind() { + || match p.ignore_quantifiers().skip_binder().kind() { ty::PredicateKind::Trait(pred, _) => pred.def_id() == sized_trait, _ => false, } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 23defc51a3764..a86ee12fa99b1 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -480,7 +480,7 @@ impl Clean for hir::WherePredicate<'_> { impl<'a> Clean> for ty::Predicate<'a> { fn clean(&self, cx: &DocContext<'_>) -> Option { - match self.ignore_qualifiers().skip_binder().kind() { + match self.ignore_quantifiers().skip_binder().kind() { &ty::PredicateKind::Trait(pred, _) => Some(ty::Binder::bind(pred).clean(cx)), &ty::PredicateKind::Subtype(pred) => Some(ty::Binder::bind(pred).clean(cx)), &ty::PredicateKind::RegionOutlives(pred) => ty::Binder::bind(pred).clean(cx), @@ -755,7 +755,7 @@ impl<'a, 'tcx> Clean for (&'a ty::Generics, ty::GenericPredicates<'tcx .flat_map(|(p, _)| { let mut projection = None; let param_idx = (|| { - match p.ignore_qualifiers().skip_binder().kind() { + match p.ignore_quantifiers().skip_binder().kind() { &ty::PredicateKind::Trait(pred, _constness) => { if let ty::Param(param) = pred.self_ty().kind { return Some(param.index); @@ -1662,9 +1662,9 @@ impl<'tcx> Clean for Ty<'tcx> { .iter() .filter_map(|predicate| { // Note: The substs of opaque types can contain unbound variables, - // meaning that we have to use `ignore_qualifiers_with_unbound_vars` here. + // meaning that we have to use `ignore_quantifiers_with_unbound_vars` here. let trait_ref = match predicate - .ignore_qualifiers_with_unbound_vars(cx.tcx) + .ignore_quantifiers_with_unbound_vars(cx.tcx) .skip_binder() .kind() { @@ -1692,7 +1692,7 @@ impl<'tcx> Clean for Ty<'tcx> { .iter() .filter_map(|pred| { if let ty::PredicateKind::Projection(proj) = pred - .ignore_qualifiers_with_unbound_vars(cx.tcx) + .ignore_quantifiers_with_unbound_vars(cx.tcx) .skip_binder() .kind() { diff --git a/src/librustdoc/clean/simplify.rs b/src/librustdoc/clean/simplify.rs index 75c5fb4c9f873..98e461fe69563 100644 --- a/src/librustdoc/clean/simplify.rs +++ b/src/librustdoc/clean/simplify.rs @@ -141,7 +141,8 @@ fn trait_is_same_or_supertrait(cx: &DocContext<'_>, child: DefId, trait_: DefId) .predicates .iter() .filter_map(|(pred, _)| { - if let ty::PredicateKind::Trait(pred, _) = pred.ignore_qualifiers().skip_binder().kind() + if let ty::PredicateKind::Trait(pred, _) = + pred.ignore_quantifiers().skip_binder().kind() { if pred.trait_ref.self_ty() == self_ty { Some(pred.def_id()) } else { None } } else { diff --git a/src/tools/clippy/clippy_lints/src/future_not_send.rs b/src/tools/clippy/clippy_lints/src/future_not_send.rs index ae28015b4c9e3..7eeb6a75ea7b0 100644 --- a/src/tools/clippy/clippy_lints/src/future_not_send.rs +++ b/src/tools/clippy/clippy_lints/src/future_not_send.rs @@ -91,7 +91,7 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend { cx.tcx.infer_ctxt().enter(|infcx| { for FulfillmentError { obligation, .. } in send_errors { infcx.maybe_note_obligation_cause_for_async_await(db, &obligation); - if let Trait(trait_pred, _) = obligation.predicate.ignore_qualifiers().skip_binder().kind() { + if let Trait(trait_pred, _) = obligation.predicate.ignore_quantifiers().skip_binder().kind() { db.note(&format!( "`{}` doesn't implement `{}`", trait_pred.self_ty(), diff --git a/src/tools/clippy/clippy_lints/src/methods/mod.rs b/src/tools/clippy/clippy_lints/src/methods/mod.rs index c3cdaeedd1955..a450d5f16f8cb 100644 --- a/src/tools/clippy/clippy_lints/src/methods/mod.rs +++ b/src/tools/clippy/clippy_lints/src/methods/mod.rs @@ -1559,7 +1559,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods { if let ty::Opaque(def_id, _) = ret_ty.kind { // one of the associated types must be Self for &(predicate, _span) in cx.tcx.predicates_of(def_id).predicates { - if let ty::PredicateKind::Projection(projection_predicate) = predicate.ignore_qualifiers().skip_binder().kind() { + if let ty::PredicateKind::Projection(projection_predicate) = predicate.ignore_quantifiers().skip_binder().kind() { // walk the associated type and check for Self if contains_self_ty(projection_predicate.ty) { return; diff --git a/src/tools/clippy/clippy_lints/src/utils/mod.rs b/src/tools/clippy/clippy_lints/src/utils/mod.rs index 426772f3e9a94..c4603418ee3c6 100644 --- a/src/tools/clippy/clippy_lints/src/utils/mod.rs +++ b/src/tools/clippy/clippy_lints/src/utils/mod.rs @@ -1263,7 +1263,7 @@ pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool { ty::Tuple(ref substs) => substs.types().any(|ty| is_must_use_ty(cx, ty)), ty::Opaque(ref def_id, _) => { for (predicate, _) in cx.tcx.predicates_of(*def_id).predicates { - if let ty::PredicateKind::Trait(trait_predicate, _) = predicate.ignore_qualifiers().skip_binder().kind() { + if let ty::PredicateKind::Trait(trait_predicate, _) = predicate.ignore_quantifiers().skip_binder().kind() { if must_use_attr(&cx.tcx.get_attrs(trait_predicate.trait_ref.def_id)).is_some() { return true; } From d030752f63b9d52bb5fa8733080e674697640866 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Wed, 24 Jun 2020 17:54:13 +0200 Subject: [PATCH 20/28] refactor query_outlives_constraints_into_obligations --- .../infer/canonical/query_response.rs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/librustc_infer/infer/canonical/query_response.rs b/src/librustc_infer/infer/canonical/query_response.rs index 3541bf3b80938..1a54fa0657c56 100644 --- a/src/librustc_infer/infer/canonical/query_response.rs +++ b/src/librustc_infer/infer/canonical/query_response.rs @@ -525,12 +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 constraint = substitute_value(self.tcx, result_subst, constraint); + let ty::OutlivesPredicate(k1, r2) = + *substitute_value(self.tcx, result_subst, constraint).skip_binder(); - let to_predicate = |ty::OutlivesPredicate(k1, r2): ty::OutlivesPredicate< - GenericArg<'tcx>, - ty::Region<'tcx>, - >| match k1.unpack() { + let predicate = match k1.unpack() { GenericArgKind::Lifetime(r1) => { ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(r1, r2)) .to_predicate(self.tcx) @@ -541,16 +539,11 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> { } GenericArgKind::Const(..) => { // Consts cannot outlive one another, so we don't expect to - // ecounter this branch. + // encounter this branch. span_bug!(cause.span, "unexpected const outlives {:?}", constraint); } - }; - - let predicate = if let Some(constraint) = constraint.no_bound_vars() { - to_predicate(constraint) - } else { - ty::PredicateKind::ForAll(constraint.map_bound(to_predicate)).to_predicate(self.tcx) - }; + } + .potentially_quantified(self.tcx, ty::PredicateKind::ForAll); Obligation::new(cause.clone(), param_env, predicate) }) From 52af82bdb9bdf2ee481fee82c63993807f771119 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Wed, 24 Jun 2020 18:06:04 +0200 Subject: [PATCH 21/28] add reuse_or_mk_predicate --- src/librustc_infer/infer/canonical/query_response.rs | 2 +- src/librustc_infer/traits/util.rs | 5 ++--- src/librustc_middle/ty/context.rs | 11 ++++++++++- src/librustc_middle/ty/flags.rs | 4 ++-- src/librustc_middle/ty/mod.rs | 2 +- src/librustc_middle/ty/structural_impls.rs | 2 +- src/librustc_privacy/lib.rs | 2 +- src/librustc_trait_selection/traits/wf.rs | 2 +- src/librustc_typeck/check/wfcheck.rs | 2 +- 9 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/librustc_infer/infer/canonical/query_response.rs b/src/librustc_infer/infer/canonical/query_response.rs index 1a54fa0657c56..a6229e61ae77d 100644 --- a/src/librustc_infer/infer/canonical/query_response.rs +++ b/src/librustc_infer/infer/canonical/query_response.rs @@ -526,7 +526,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, '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(); + substitute_value(self.tcx, result_subst, constraint).skip_binder(); let predicate = match k1.unpack() { GenericArgKind::Lifetime(r1) => { diff --git a/src/librustc_infer/traits/util.rs b/src/librustc_infer/traits/util.rs index cb2f2ca0e9287..0d343be2c264f 100644 --- a/src/librustc_infer/traits/util.rs +++ b/src/librustc_infer/traits/util.rs @@ -10,11 +10,10 @@ pub fn anonymize_predicate<'tcx>( tcx: TyCtxt<'tcx>, pred: ty::Predicate<'tcx>, ) -> ty::Predicate<'tcx> { - let kind = pred.kind(); - match kind { + match pred.kind() { ty::PredicateKind::ForAll(binder) => { let new = ty::PredicateKind::ForAll(tcx.anonymize_late_bound_regions(binder)); - if new != *kind { new.to_predicate(tcx) } else { pred } + tcx.reuse_or_mk_predicate(pred, new) } ty::PredicateKind::Trait(_, _) | ty::PredicateKind::RegionOutlives(_) diff --git a/src/librustc_middle/ty/context.rs b/src/librustc_middle/ty/context.rs index 1e7fceb8e22e1..eeb58a0c55a3e 100644 --- a/src/librustc_middle/ty/context.rs +++ b/src/librustc_middle/ty/context.rs @@ -2132,11 +2132,20 @@ impl<'tcx> TyCtxt<'tcx> { } #[inline] - pub fn mk_predicate(&self, kind: PredicateKind<'tcx>) -> Predicate<'tcx> { + pub fn mk_predicate(self, kind: PredicateKind<'tcx>) -> Predicate<'tcx> { let inner = self.interners.intern_predicate(kind); Predicate { inner } } + #[inline] + pub fn reuse_or_mk_predicate( + self, + pred: Predicate<'tcx>, + kind: PredicateKind<'tcx>, + ) -> Predicate<'tcx> { + if *pred.kind() != kind { self.mk_predicate(kind) } else { pred } + } + pub fn mk_mach_int(self, tm: ast::IntTy) -> Ty<'tcx> { match tm { ast::IntTy::Isize => self.types.isize, diff --git a/src/librustc_middle/ty/flags.rs b/src/librustc_middle/ty/flags.rs index c8c8475b056da..4b1f970edb09c 100644 --- a/src/librustc_middle/ty/flags.rs +++ b/src/librustc_middle/ty/flags.rs @@ -201,7 +201,7 @@ impl FlagComputation { } } - fn add_predicate(&mut self, pred: &ty::Predicate<'_>) { + fn add_predicate(&mut self, pred: ty::Predicate<'_>) { self.add_flags(pred.inner.flags); self.add_exclusive_binder(pred.inner.outer_exclusive_binder); } @@ -223,7 +223,7 @@ impl FlagComputation { self.add_ty(a); self.add_ty(b); } - ty::PredicateKind::Projection(ty::ProjectionPredicate { projection_ty, ty }) => { + &ty::PredicateKind::Projection(ty::ProjectionPredicate { projection_ty, ty }) => { self.add_projection_ty(projection_ty); self.add_ty(ty); } diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index dd74fbc945748..c8ad8ae10dc35 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -1252,7 +1252,7 @@ impl<'tcx> Predicate<'tcx> { // from the substitution and the value being substituted into, and // this trick achieves that). let substs = trait_ref.skip_binder().substs; - let pred = *self.ignore_quantifiers().skip_binder(); + let pred = self.ignore_quantifiers().skip_binder(); let new = pred.subst(tcx, substs); if new != pred { new.potentially_quantified(tcx, PredicateKind::ForAll) } else { self } } diff --git a/src/librustc_middle/ty/structural_impls.rs b/src/librustc_middle/ty/structural_impls.rs index 55fab4999053a..e6237853f21cd 100644 --- a/src/librustc_middle/ty/structural_impls.rs +++ b/src/librustc_middle/ty/structural_impls.rs @@ -1000,7 +1000,7 @@ impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> { impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> { fn super_fold_with>(&self, folder: &mut F) -> Self { let new = ty::PredicateKind::super_fold_with(&self.inner.kind, folder); - if new != self.inner.kind { folder.tcx().mk_predicate(new) } else { *self } + folder.tcx().reuse_or_mk_predicate(*self, new) } fn super_visit_with>(&self, visitor: &mut V) -> bool { diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index 3591a707ac039..223e92e0482cd 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -89,7 +89,7 @@ where &ty::PredicateKind::ForAll(pred) => { // This visitor does not care about bound regions as we only // look at `DefId`s. - self.visit_predicate(*pred.skip_binder()) + self.visit_predicate(pred.skip_binder()) } &ty::PredicateKind::Trait(ty::TraitPredicate { trait_ref }, _) => { self.visit_trait(trait_ref) diff --git a/src/librustc_trait_selection/traits/wf.rs b/src/librustc_trait_selection/traits/wf.rs index ffafbabddfcde..afa2270b7af0f 100644 --- a/src/librustc_trait_selection/traits/wf.rs +++ b/src/librustc_trait_selection/traits/wf.rs @@ -88,7 +88,7 @@ pub fn predicate_obligations<'a, 'tcx>( infcx: &InferCtxt<'a, 'tcx>, param_env: ty::ParamEnv<'tcx>, body_id: hir::HirId, - predicate: &'_ ty::Predicate<'tcx>, + predicate: ty::Predicate<'tcx>, span: Span, ) -> Vec> { let mut wf = WfPredicates { infcx, param_env, body_id, span, out: vec![], item: None }; diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index 80f03e2211a08..dabae6cbc4137 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -828,7 +828,7 @@ fn check_where_clauses<'tcx, 'fcx>( debug!("check_where_clauses: predicates={:?}", predicates.predicates); assert_eq!(predicates.predicates.len(), predicates.spans.len()); let wf_obligations = - predicates.predicates.iter().zip(predicates.spans.iter()).flat_map(|(p, &sp)| { + predicates.predicates.iter().zip(predicates.spans.iter()).flat_map(|(&p, &sp)| { traits::wf::predicate_obligations(fcx, fcx.param_env, fcx.body_id, p, sp) }); From d8cf8ba5f7e4154913eab3be13fd1cc0b3e06906 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Thu, 9 Jul 2020 00:35:55 +0200 Subject: [PATCH 22/28] introduce PredicateAtom --- .../infer/canonical/query_response.rs | 6 +- src/librustc_infer/infer/combine.rs | 6 +- src/librustc_infer/infer/outlives/mod.rs | 34 +- src/librustc_infer/infer/sub.rs | 2 +- src/librustc_infer/traits/util.rs | 40 +-- src/librustc_lint/builtin.rs | 13 +- src/librustc_lint/unused.rs | 4 +- src/librustc_middle/ty/flags.rs | 68 ++-- src/librustc_middle/ty/mod.rs | 166 +++++----- src/librustc_middle/ty/print/pretty.rs | 70 ++-- src/librustc_middle/ty/structural_impls.rs | 81 +++-- .../borrow_check/diagnostics/region_errors.rs | 4 +- .../borrow_check/type_check/mod.rs | 8 +- .../transform/qualify_min_const_fn.rs | 23 +- src/librustc_privacy/lib.rs | 15 +- src/librustc_trait_selection/opaque_types.rs | 27 +- .../traits/auto_trait.rs | 20 +- .../traits/error_reporting/mod.rs | 68 ++-- .../traits/error_reporting/suggestions.rs | 9 +- .../traits/fulfill.rs | 304 +++++++++--------- src/librustc_trait_selection/traits/mod.rs | 4 +- .../traits/object_safety.rs | 48 ++- .../traits/project.rs | 22 +- .../traits/query/type_op/prove_predicate.rs | 4 +- .../traits/select/confirmation.rs | 2 +- .../traits/select/mod.rs | 31 +- src/librustc_trait_selection/traits/wf.rs | 80 ++--- src/librustc_traits/chalk/lowering.rs | 77 ++--- .../implied_outlives_bounds.rs | 44 +-- .../normalize_erasing_regions.rs | 21 +- src/librustc_traits/type_op.rs | 4 +- src/librustc_ty/ty.rs | 18 +- src/librustc_typeck/astconv.rs | 6 +- src/librustc_typeck/check/closure.rs | 8 +- src/librustc_typeck/check/coercion.rs | 4 +- src/librustc_typeck/check/dropck.rs | 9 +- src/librustc_typeck/check/method/confirm.rs | 31 +- src/librustc_typeck/check/method/mod.rs | 2 +- src/librustc_typeck/check/method/probe.rs | 27 +- src/librustc_typeck/check/method/suggest.rs | 19 +- src/librustc_typeck/check/mod.rs | 49 ++- src/librustc_typeck/check/wfcheck.rs | 2 +- src/librustc_typeck/collect.rs | 12 +- .../constrained_generic_params.rs | 4 +- .../impl_wf_check/min_specialization.rs | 33 +- src/librustc_typeck/outlives/explicit.rs | 24 +- src/librustc_typeck/outlives/mod.rs | 12 +- src/librustdoc/clean/auto_trait.rs | 10 +- src/librustdoc/clean/mod.rs | 48 ++- src/librustdoc/clean/simplify.rs | 4 +- .../clippy_lints/src/future_not_send.rs | 4 +- .../clippy/clippy_lints/src/methods/mod.rs | 2 +- .../src/needless_pass_by_value.rs | 2 +- .../clippy/clippy_lints/src/utils/mod.rs | 2 +- 54 files changed, 795 insertions(+), 842 deletions(-) diff --git a/src/librustc_infer/infer/canonical/query_response.rs b/src/librustc_infer/infer/canonical/query_response.rs index a6229e61ae77d..8406eb9bc175b 100644 --- a/src/librustc_infer/infer/canonical/query_response.rs +++ b/src/librustc_infer/infer/canonical/query_response.rs @@ -530,11 +530,11 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> { let predicate = match k1.unpack() { GenericArgKind::Lifetime(r1) => { - ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(r1, r2)) + ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(r1, r2)) .to_predicate(self.tcx) } GenericArgKind::Type(t1) => { - ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(t1, r2)) + ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(t1, r2)) .to_predicate(self.tcx) } GenericArgKind::Const(..) => { @@ -665,7 +665,7 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for QueryTypeRelatingDelegate<'_, 'tcx> { self.obligations.push(Obligation { cause: self.cause.clone(), param_env: self.param_env, - predicate: ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(sup, sub)) + predicate: ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(sup, sub)) .to_predicate(self.infcx.tcx), recursion_depth: 0, }); diff --git a/src/librustc_infer/infer/combine.rs b/src/librustc_infer/infer/combine.rs index c63464e5baec9..5b4d91de3ca92 100644 --- a/src/librustc_infer/infer/combine.rs +++ b/src/librustc_infer/infer/combine.rs @@ -308,7 +308,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> { self.obligations.push(Obligation::new( self.trace.cause.clone(), self.param_env, - ty::PredicateKind::WellFormed(b_ty.into()).to_predicate(self.infcx.tcx), + ty::PredicateAtom::WellFormed(b_ty.into()).to_predicate(self.infcx.tcx), )); } @@ -400,9 +400,9 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> { b: &'tcx ty::Const<'tcx>, ) { let predicate = if a_is_expected { - ty::PredicateKind::ConstEquate(a, b) + ty::PredicateAtom::ConstEquate(a, b) } else { - ty::PredicateKind::ConstEquate(b, a) + ty::PredicateAtom::ConstEquate(b, a) }; self.obligations.push(Obligation::new( self.trace.cause.clone(), diff --git a/src/librustc_infer/infer/outlives/mod.rs b/src/librustc_infer/infer/outlives/mod.rs index 541a2f18045c4..6009d4e65793b 100644 --- a/src/librustc_infer/infer/outlives/mod.rs +++ b/src/librustc_infer/infer/outlives/mod.rs @@ -6,23 +6,29 @@ pub mod verify; use rustc_middle::traits::query::OutlivesBound; use rustc_middle::ty; +use rustc_middle::ty::fold::TypeFoldable; pub fn explicit_outlives_bounds<'tcx>( param_env: ty::ParamEnv<'tcx>, ) -> impl Iterator> + 'tcx { debug!("explicit_outlives_bounds()"); - param_env.caller_bounds().into_iter().filter_map(move |predicate| match predicate.kind() { - ty::PredicateKind::Projection(..) - | ty::PredicateKind::Trait(..) - | ty::PredicateKind::Subtype(..) - | ty::PredicateKind::WellFormed(..) - | ty::PredicateKind::ObjectSafe(..) - | ty::PredicateKind::ClosureKind(..) - | ty::PredicateKind::TypeOutlives(..) - | ty::PredicateKind::ConstEvaluatable(..) - | ty::PredicateKind::ConstEquate(..) => None, - ty::PredicateKind::RegionOutlives(ref data) => data - .no_bound_vars() - .map(|ty::OutlivesPredicate(r_a, r_b)| OutlivesBound::RegionSubRegion(r_b, r_a)), - }) + param_env + .caller_bounds() + .into_iter() + .map(ty::Predicate::skip_binders) + .filter(TypeFoldable::has_escaping_bound_vars) + .filter_map(move |atom| match atom { + ty::PredicateAtom::Projection(..) + | ty::PredicateAtom::Trait(..) + | ty::PredicateAtom::Subtype(..) + | ty::PredicateAtom::WellFormed(..) + | ty::PredicateAtom::ObjectSafe(..) + | ty::PredicateAtom::ClosureKind(..) + | ty::PredicateAtom::TypeOutlives(..) + | ty::PredicateAtom::ConstEvaluatable(..) + | ty::PredicateAtom::ConstEquate(..) => None, + ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(r_a, r_b)) => { + Some(OutlivesBound::RegionSubRegion(r_b, r_a)) + } + }) } diff --git a/src/librustc_infer/infer/sub.rs b/src/librustc_infer/infer/sub.rs index 5ad08f0b8952a..4f860c77d6541 100644 --- a/src/librustc_infer/infer/sub.rs +++ b/src/librustc_infer/infer/sub.rs @@ -100,7 +100,7 @@ impl TypeRelation<'tcx> for Sub<'combine, 'infcx, 'tcx> { self.fields.obligations.push(Obligation::new( self.fields.trace.cause.clone(), self.fields.param_env, - ty::PredicateKind::Subtype(ty::SubtypePredicate { + ty::PredicateAtom::Subtype(ty::SubtypePredicate { a_is_expected: self.a_is_expected, a, b, diff --git a/src/librustc_infer/traits/util.rs b/src/librustc_infer/traits/util.rs index 0d343be2c264f..93fc7f1f3b8a7 100644 --- a/src/librustc_infer/traits/util.rs +++ b/src/librustc_infer/traits/util.rs @@ -15,16 +15,7 @@ pub fn anonymize_predicate<'tcx>( let new = ty::PredicateKind::ForAll(tcx.anonymize_late_bound_regions(binder)); tcx.reuse_or_mk_predicate(pred, new) } - ty::PredicateKind::Trait(_, _) - | ty::PredicateKind::RegionOutlives(_) - | ty::PredicateKind::TypeOutlives(_) - | ty::PredicateKind::Projection(_) - | ty::PredicateKind::WellFormed(_) - | ty::PredicateKind::ObjectSafe(_) - | ty::PredicateKind::ClosureKind(_, _, _) - | ty::PredicateKind::Subtype(_) - | ty::PredicateKind::ConstEvaluatable(_, _) - | ty::PredicateKind::ConstEquate(_, _) => pred, + ty::PredicateKind::Atom(_) => pred, } } @@ -137,11 +128,8 @@ impl Elaborator<'tcx> { fn elaborate(&mut self, obligation: &PredicateObligation<'tcx>) { let tcx = self.visited.tcx; - match obligation.predicate.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::ForAll(_) => { - bug!("unexpected predicate: {:?}", obligation.predicate) - } - ty::PredicateKind::Trait(data, _) => { + match obligation.predicate.skip_binders() { + ty::PredicateAtom::Trait(data, _) => { // Get predicates declared on the trait. let predicates = tcx.super_predicates_of(data.def_id()); @@ -162,36 +150,36 @@ impl Elaborator<'tcx> { self.stack.extend(obligations); } - ty::PredicateKind::WellFormed(..) => { + ty::PredicateAtom::WellFormed(..) => { // Currently, we do not elaborate WF predicates, // although we easily could. } - ty::PredicateKind::ObjectSafe(..) => { + ty::PredicateAtom::ObjectSafe(..) => { // Currently, we do not elaborate object-safe // predicates. } - ty::PredicateKind::Subtype(..) => { + ty::PredicateAtom::Subtype(..) => { // Currently, we do not "elaborate" predicates like `X <: Y`, // though conceivably we might. } - ty::PredicateKind::Projection(..) => { + ty::PredicateAtom::Projection(..) => { // Nothing to elaborate in a projection predicate. } - ty::PredicateKind::ClosureKind(..) => { + ty::PredicateAtom::ClosureKind(..) => { // Nothing to elaborate when waiting for a closure's kind to be inferred. } - ty::PredicateKind::ConstEvaluatable(..) => { + ty::PredicateAtom::ConstEvaluatable(..) => { // Currently, we do not elaborate const-evaluatable // predicates. } - ty::PredicateKind::ConstEquate(..) => { + ty::PredicateAtom::ConstEquate(..) => { // Currently, we do not elaborate const-equate // predicates. } - ty::PredicateKind::RegionOutlives(..) => { + ty::PredicateAtom::RegionOutlives(..) => { // Nothing to elaborate from `'a: 'b`. } - ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty_max, r_min)) => { + ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty_max, r_min)) => { // We know that `T: 'a` for some type `T`. We can // often elaborate this. For example, if we know that // `[U]: 'a`, that implies that `U: 'a`. Similarly, if @@ -221,7 +209,7 @@ impl Elaborator<'tcx> { if r.is_late_bound() { None } else { - Some(ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate( + Some(ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate( r, r_min, ))) } @@ -229,7 +217,7 @@ impl Elaborator<'tcx> { Component::Param(p) => { let ty = tcx.mk_ty_param(p.index, p.name); - Some(ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate( + Some(ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate( ty, r_min, ))) } diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 00d532c7ba0ba..d67be44e4f0c5 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -1202,7 +1202,7 @@ declare_lint_pass!( impl<'tcx> LateLintPass<'tcx> for TrivialConstraints { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) { use rustc_middle::ty::fold::TypeFoldable; - use rustc_middle::ty::PredicateKind::*; + use rustc_middle::ty::PredicateAtom::*; if cx.tcx.features().trivial_bounds { let def_id = cx.tcx.hir().local_def_id(item.hir_id); @@ -1210,7 +1210,7 @@ impl<'tcx> LateLintPass<'tcx> for TrivialConstraints { for &(predicate, span) in predicates.predicates { // We don't actually look inside of the predicate, // so it is safe to skip this binder here. - let predicate_kind_name = match predicate.ignore_quantifiers().skip_binder().kind() { + let predicate_kind_name = match predicate.skip_binders() { Trait(..) => "Trait", TypeOutlives(..) | RegionOutlives(..) => "Lifetime", @@ -1225,7 +1225,6 @@ impl<'tcx> LateLintPass<'tcx> for TrivialConstraints { Subtype(..) | ConstEvaluatable(..) | ConstEquate(..) => continue, - ForAll(_) => bug!("unexpected predicate: {:?}", predicate) }; if predicate.is_global() { cx.struct_span_lint(TRIVIAL_BOUNDS, span, |lint| { @@ -1500,8 +1499,8 @@ impl ExplicitOutlivesRequirements { ) -> Vec> { inferred_outlives .iter() - .filter_map(|(pred, _)| match pred.ignore_quantifiers().skip_binder().kind() { - &ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => match a { + .filter_map(|(pred, _)| match pred.skip_binders() { + ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(a, b)) => match a { ty::ReEarlyBound(ebr) if ebr.index == index => Some(b), _ => None, }, @@ -1516,8 +1515,8 @@ impl ExplicitOutlivesRequirements { ) -> Vec> { inferred_outlives .iter() - .filter_map(|(pred, _)| match pred.ignore_quantifiers().skip_binder().kind() { - &ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(a, b)) => { + .filter_map(|(pred, _)| match pred.skip_binders() { + ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(a, b)) => { a.is_param(index).then_some(b) } _ => None, diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index 8f21cb51695a2..dcb44ab644498 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -147,8 +147,8 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { let mut has_emitted = false; for (predicate, _) in cx.tcx.predicates_of(def).predicates { // We only look at the `DefId`, so it is safe to skip the binder here. - if let ty::PredicateKind::Trait(ref poly_trait_predicate, _) = - predicate.ignore_quantifiers().skip_binder().kind() + if let ty::PredicateAtom::Trait(ref poly_trait_predicate, _) = + predicate.skip_binders() { let def_id = poly_trait_predicate.trait_ref.def_id; let descr_pre = diff --git a/src/librustc_middle/ty/flags.rs b/src/librustc_middle/ty/flags.rs index 4b1f970edb09c..7452089658feb 100644 --- a/src/librustc_middle/ty/flags.rs +++ b/src/librustc_middle/ty/flags.rs @@ -208,39 +208,6 @@ impl FlagComputation { fn add_predicate_kind(&mut self, kind: &ty::PredicateKind<'_>) { match kind { - ty::PredicateKind::Trait(trait_pred, _constness) => { - self.add_substs(trait_pred.trait_ref.substs); - } - ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => { - self.add_region(a); - self.add_region(b); - } - ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, region)) => { - self.add_ty(ty); - self.add_region(region); - } - ty::PredicateKind::Subtype(ty::SubtypePredicate { a_is_expected: _, a, b }) => { - self.add_ty(a); - self.add_ty(b); - } - &ty::PredicateKind::Projection(ty::ProjectionPredicate { projection_ty, ty }) => { - self.add_projection_ty(projection_ty); - self.add_ty(ty); - } - ty::PredicateKind::WellFormed(arg) => { - self.add_substs(slice::from_ref(arg)); - } - ty::PredicateKind::ObjectSafe(_def_id) => {} - ty::PredicateKind::ClosureKind(_def_id, substs, _kind) => { - self.add_substs(substs); - } - ty::PredicateKind::ConstEvaluatable(_def_id, substs) => { - self.add_substs(substs); - } - ty::PredicateKind::ConstEquate(expected, found) => { - self.add_const(expected); - self.add_const(found); - } ty::PredicateKind::ForAll(binder) => { let mut computation = FlagComputation::new(); @@ -248,6 +215,41 @@ impl FlagComputation { self.add_bound_computation(computation); } + &ty::PredicateKind::Atom(atom) => match atom { + ty::PredicateAtom::Trait(trait_pred, _constness) => { + self.add_substs(trait_pred.trait_ref.substs); + } + ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(a, b)) => { + self.add_region(a); + self.add_region(b); + } + ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty, region)) => { + self.add_ty(ty); + self.add_region(region); + } + ty::PredicateAtom::Subtype(ty::SubtypePredicate { a_is_expected: _, a, b }) => { + self.add_ty(a); + self.add_ty(b); + } + ty::PredicateAtom::Projection(ty::ProjectionPredicate { projection_ty, ty }) => { + self.add_projection_ty(projection_ty); + self.add_ty(ty); + } + ty::PredicateAtom::WellFormed(arg) => { + self.add_substs(slice::from_ref(&arg)); + } + ty::PredicateAtom::ObjectSafe(_def_id) => {} + ty::PredicateAtom::ClosureKind(_def_id, substs, _kind) => { + self.add_substs(substs); + } + ty::PredicateAtom::ConstEvaluatable(_def_id, substs) => { + self.add_substs(substs); + } + ty::PredicateAtom::ConstEquate(expected, found) => { + self.add_const(expected); + self.add_const(found); + } + }, } } diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index c8ad8ae10dc35..0ff475fb288a6 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -1018,7 +1018,7 @@ crate struct PredicateInner<'tcx> { } #[cfg(target_arch = "x86_64")] -static_assert_size!(PredicateInner<'_>, 40); +static_assert_size!(PredicateInner<'_>, 48); #[derive(Clone, Copy, Lift)] pub struct Predicate<'tcx> { @@ -1049,46 +1049,35 @@ impl<'tcx> Predicate<'tcx> { &self.inner.kind } - /// Skips `PredicateKind::ForAll`. - pub fn ignore_quantifiers(self) -> Binder> { + /// Returns the inner `PredicateAtom`. + /// + /// Note that this method panics in case this predicate has unbound variables. + pub fn skip_binders(self) -> PredicateAtom<'tcx> { match self.kind() { - &PredicateKind::ForAll(binder) => binder, - ty::PredicateKind::Projection(..) - | ty::PredicateKind::Trait(..) - | ty::PredicateKind::Subtype(..) - | ty::PredicateKind::WellFormed(..) - | ty::PredicateKind::ObjectSafe(..) - | ty::PredicateKind::ClosureKind(..) - | ty::PredicateKind::TypeOutlives(..) - | ty::PredicateKind::ConstEvaluatable(..) - | ty::PredicateKind::ConstEquate(..) - | ty::PredicateKind::RegionOutlives(..) => Binder::dummy(self), + &PredicateKind::ForAll(binder) => binder.skip_binder().skip_binders(), + &ty::PredicateKind::Atom(atom) => atom, } } - /// Skips `PredicateKind::ForAll`, while allowing for unbound variables. - /// - /// This method requires the `TyCtxt` as it has to shift the unbound variables - /// outwards. + /// Returns the inner `PredicateAtom`. /// - /// Do not use this method if you may end up just skipping the binder, as this - /// would leave the unbound variables at an incorrect binding level. - pub fn ignore_quantifiers_with_unbound_vars( - self, - tcx: TyCtxt<'tcx>, - ) -> Binder> { + /// Note that this method does not check if predicate has unbound variables, + /// rebinding the returned atom potentially 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().skip_binders(), + &ty::PredicateKind::Atom(atom) => atom, + } + } + + pub fn bound_atom(self, tcx: TyCtxt<'tcx>) -> Binder> { match self.kind() { - &PredicateKind::ForAll(binder) => binder, - ty::PredicateKind::Projection(..) - | ty::PredicateKind::Trait(..) - | ty::PredicateKind::Subtype(..) - | ty::PredicateKind::WellFormed(..) - | ty::PredicateKind::ObjectSafe(..) - | ty::PredicateKind::ClosureKind(..) - | ty::PredicateKind::TypeOutlives(..) - | ty::PredicateKind::ConstEvaluatable(..) - | ty::PredicateKind::ConstEquate(..) - | ty::PredicateKind::RegionOutlives(..) => Binder::wrap_nonbinding(tcx, self), + &PredicateKind::ForAll(binder) => binder.map_bound(|inner| match inner.kind() { + ty::PredicateKind::ForAll(_) => bug!("unexpect forall"), + &ty::PredicateKind::Atom(atom) => atom, + }), + &ty::PredicateKind::Atom(atom) => Binder::wrap_nonbinding(tcx, atom), } } @@ -1124,6 +1113,15 @@ impl<'a, 'tcx> HashStable> for Predicate<'tcx> { #[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)] #[derive(HashStable, TypeFoldable)] pub enum PredicateKind<'tcx> { + /// `for<'a>: ...` + ForAll(Binder>), + + Atom(PredicateAtom<'tcx>), +} + +#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)] +#[derive(HashStable, TypeFoldable)] +pub enum PredicateAtom<'tcx> { /// Corresponds to `where Foo: Bar`. `Foo` here would be /// the `Self` type of the trait reference and `A`, `B`, and `C` /// would be the type parameters. @@ -1162,9 +1160,6 @@ pub enum PredicateKind<'tcx> { /// Constants must be equal. The first component is the const that is expected. ConstEquate(&'tcx Const<'tcx>, &'tcx Const<'tcx>), - - /// `for<'a>: ...` - ForAll(Binder>), } /// The crate outlives map is computed during typeck and contains the @@ -1252,9 +1247,13 @@ impl<'tcx> Predicate<'tcx> { // from the substitution and the value being substituted into, and // this trick achieves that). let substs = trait_ref.skip_binder().substs; - let pred = self.ignore_quantifiers().skip_binder(); + 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 { + new.to_predicate(tcx).potentially_quantified(tcx, PredicateKind::ForAll) + } else { + self + } } } @@ -1379,9 +1378,16 @@ impl ToPredicate<'tcx> for PredicateKind<'tcx> { } } +impl ToPredicate<'tcx> for PredicateAtom<'tcx> { + #[inline(always)] + fn to_predicate(&self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { + tcx.mk_predicate(ty::PredicateKind::Atom(*self)) + } +} + impl<'tcx> ToPredicate<'tcx> for ConstnessAnd> { fn to_predicate(&self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { - ty::PredicateKind::Trait(ty::TraitPredicate { trait_ref: self.value }, self.constness) + ty::PredicateAtom::Trait(ty::TraitPredicate { trait_ref: self.value }, self.constness) .to_predicate(tcx) } } @@ -1399,94 +1405,88 @@ impl<'tcx> ToPredicate<'tcx> for ConstnessAnd> { impl<'tcx> ToPredicate<'tcx> for ConstnessAnd> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { if let Some(pred) = self.value.no_bound_vars() { - ty::PredicateKind::Trait(pred, self.constness) + ty::PredicateAtom::Trait(pred, self.constness).to_predicate(tcx) } else { ty::PredicateKind::ForAll( self.value.map_bound(|pred| { - ty::PredicateKind::Trait(pred, self.constness).to_predicate(tcx) + ty::PredicateAtom::Trait(pred, self.constness).to_predicate(tcx) }), ) + .to_predicate(tcx) } - .to_predicate(tcx) } } impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { if let Some(outlives) = self.no_bound_vars() { - PredicateKind::RegionOutlives(outlives) + PredicateAtom::RegionOutlives(outlives).to_predicate(tcx) } else { ty::PredicateKind::ForAll( self.map_bound(|outlives| { - PredicateKind::RegionOutlives(outlives).to_predicate(tcx) + PredicateAtom::RegionOutlives(outlives).to_predicate(tcx) }), ) + .to_predicate(tcx) } - .to_predicate(tcx) } } impl<'tcx> ToPredicate<'tcx> for PolyTypeOutlivesPredicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { if let Some(outlives) = self.no_bound_vars() { - PredicateKind::TypeOutlives(outlives) + PredicateAtom::TypeOutlives(outlives).to_predicate(tcx) } else { ty::PredicateKind::ForAll( - self.map_bound(|outlives| PredicateKind::TypeOutlives(outlives).to_predicate(tcx)), + self.map_bound(|outlives| PredicateAtom::TypeOutlives(outlives).to_predicate(tcx)), ) + .to_predicate(tcx) } - .to_predicate(tcx) } } impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { if let Some(proj) = self.no_bound_vars() { - PredicateKind::Projection(proj) + PredicateAtom::Projection(proj).to_predicate(tcx) } else { ty::PredicateKind::ForAll( - self.map_bound(|proj| PredicateKind::Projection(proj).to_predicate(tcx)), + self.map_bound(|proj| PredicateAtom::Projection(proj).to_predicate(tcx)), ) + .to_predicate(tcx) } - .to_predicate(tcx) } } impl<'tcx> Predicate<'tcx> { pub fn to_opt_poly_trait_ref(self) -> Option> { - self.ignore_quantifiers() - .map_bound(|pred| match pred.kind() { - &PredicateKind::Trait(ref t, _) => Some(t.trait_ref), - PredicateKind::Projection(..) - | PredicateKind::Subtype(..) - | PredicateKind::RegionOutlives(..) - | PredicateKind::WellFormed(..) - | PredicateKind::ObjectSafe(..) - | PredicateKind::ClosureKind(..) - | PredicateKind::TypeOutlives(..) - | PredicateKind::ConstEvaluatable(..) - | PredicateKind::ConstEquate(..) => None, - PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", self), - }) - .transpose() + match self.skip_binders() { + PredicateAtom::Trait(t, _) => Some(ty::Binder::bind(t.trait_ref)), + PredicateAtom::Projection(..) + | PredicateAtom::Subtype(..) + | PredicateAtom::RegionOutlives(..) + | PredicateAtom::WellFormed(..) + | PredicateAtom::ObjectSafe(..) + | PredicateAtom::ClosureKind(..) + | PredicateAtom::TypeOutlives(..) + | PredicateAtom::ConstEvaluatable(..) + | PredicateAtom::ConstEquate(..) => None, + } } pub fn to_opt_type_outlives(self) -> Option> { - self.ignore_quantifiers() - .map_bound(|pred| match pred.kind() { - &PredicateKind::TypeOutlives(data) => Some(data), - PredicateKind::Trait(..) - | PredicateKind::Projection(..) - | PredicateKind::Subtype(..) - | PredicateKind::RegionOutlives(..) - | PredicateKind::WellFormed(..) - | PredicateKind::ObjectSafe(..) - | PredicateKind::ClosureKind(..) - | PredicateKind::ConstEvaluatable(..) - | PredicateKind::ConstEquate(..) => None, - PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", self), - }) - .transpose() + match self.skip_binders() { + PredicateAtom::TypeOutlives(data) => Some(ty::Binder::bind(data)), + PredicateAtom::Trait(..) + | PredicateAtom::Projection(..) + | PredicateAtom::Subtype(..) + | PredicateAtom::RegionOutlives(..) + | PredicateAtom::WellFormed(..) + | PredicateAtom::ObjectSafe(..) + | PredicateAtom::ClosureKind(..) + | PredicateAtom::ConstEvaluatable(..) + | PredicateAtom::ConstEquate(..) => None, + } } } diff --git a/src/librustc_middle/ty/print/pretty.rs b/src/librustc_middle/ty/print/pretty.rs index d22a0e8c38e04..b0de57e15cc12 100644 --- a/src/librustc_middle/ty/print/pretty.rs +++ b/src/librustc_middle/ty/print/pretty.rs @@ -576,10 +576,8 @@ pub trait PrettyPrinter<'tcx>: // may contain unbound variables. We therefore do this manually. // // FIXME(lcnr): Find out why exactly this is the case :) - if let ty::PredicateKind::Trait(pred, _) = predicate - .ignore_quantifiers_with_unbound_vars(self.tcx()) - .skip_binder() - .kind() + if let ty::PredicateAtom::Trait(pred, _) = + predicate.bound_atom(self.tcx()).skip_binder() { let trait_ref = ty::Binder::bind(pred.trait_ref); // Don't print +Sized, but rather +?Sized if absent. @@ -2015,38 +2013,40 @@ define_print_and_forward_display! { ty::Predicate<'tcx> { match self.kind() { - &ty::PredicateKind::Trait(ref data, constness) => { - if let hir::Constness::Const = constness { - p!(write("const ")); + &ty::PredicateKind::Atom(atom) => match atom { + ty::PredicateAtom::Trait(ref data, constness) => { + if let hir::Constness::Const = constness { + p!(write("const ")); + } + p!(print(data)) + } + ty::PredicateAtom::Subtype(predicate) => p!(print(predicate)), + ty::PredicateAtom::RegionOutlives(predicate) => p!(print(predicate)), + ty::PredicateAtom::TypeOutlives(predicate) => p!(print(predicate)), + ty::PredicateAtom::Projection(predicate) => p!(print(predicate)), + ty::PredicateAtom::WellFormed(arg) => p!(print(arg), write(" well-formed")), + ty::PredicateAtom::ObjectSafe(trait_def_id) => { + p!(write("the trait `"), + print_def_path(trait_def_id, &[]), + write("` is object-safe")) + } + ty::PredicateAtom::ClosureKind(closure_def_id, _closure_substs, kind) => { + p!(write("the closure `"), + print_value_path(closure_def_id, &[]), + write("` implements the trait `{}`", kind)) + } + ty::PredicateAtom::ConstEvaluatable(def, substs) => { + p!(write("the constant `"), + print_value_path(def.did, substs), + write("` can be evaluated")) + } + ty::PredicateAtom::ConstEquate(c1, c2) => { + p!(write("the constant `"), + print(c1), + write("` equals `"), + print(c2), + write("`")) } - p!(print(data)) - } - ty::PredicateKind::Subtype(predicate) => p!(print(predicate)), - ty::PredicateKind::RegionOutlives(predicate) => p!(print(predicate)), - ty::PredicateKind::TypeOutlives(predicate) => p!(print(predicate)), - ty::PredicateKind::Projection(predicate) => p!(print(predicate)), - ty::PredicateKind::WellFormed(arg) => p!(print(arg), write(" well-formed")), - &ty::PredicateKind::ObjectSafe(trait_def_id) => { - p!(write("the trait `"), - print_def_path(trait_def_id, &[]), - write("` is object-safe")) - } - &ty::PredicateKind::ClosureKind(closure_def_id, _closure_substs, kind) => { - p!(write("the closure `"), - print_value_path(closure_def_id, &[]), - write("` implements the trait `{}`", kind)) - } - &ty::PredicateKind::ConstEvaluatable(def, substs) => { - p!(write("the constant `"), - print_value_path(def.did, substs), - write("` can be evaluated")) - } - ty::PredicateKind::ConstEquate(c1, c2) => { - p!(write("the constant `"), - print(c1), - write("` equals `"), - print(c2), - write("`")) } ty::PredicateKind::ForAll(binder) => { p!(print(binder)) diff --git a/src/librustc_middle/ty/structural_impls.rs b/src/librustc_middle/ty/structural_impls.rs index e6237853f21cd..cfe076e120702 100644 --- a/src/librustc_middle/ty/structural_impls.rs +++ b/src/librustc_middle/ty/structural_impls.rs @@ -226,28 +226,36 @@ impl fmt::Debug for ty::Predicate<'tcx> { impl fmt::Debug for ty::PredicateKind<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { - ty::PredicateKind::Trait(ref a, constness) => { + ty::PredicateKind::ForAll(binder) => write!(f, "ForAll({:?})", binder), + ty::PredicateKind::Atom(atom) => write!(f, "{:?}", atom), + } + } +} + +impl fmt::Debug for ty::PredicateAtom<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + ty::PredicateAtom::Trait(ref a, constness) => { if let hir::Constness::Const = constness { write!(f, "const ")?; } a.fmt(f) } - ty::PredicateKind::Subtype(ref pair) => pair.fmt(f), - ty::PredicateKind::RegionOutlives(ref pair) => pair.fmt(f), - ty::PredicateKind::TypeOutlives(ref pair) => pair.fmt(f), - ty::PredicateKind::Projection(ref pair) => pair.fmt(f), - ty::PredicateKind::WellFormed(data) => write!(f, "WellFormed({:?})", data), - ty::PredicateKind::ObjectSafe(trait_def_id) => { + ty::PredicateAtom::Subtype(ref pair) => pair.fmt(f), + ty::PredicateAtom::RegionOutlives(ref pair) => pair.fmt(f), + ty::PredicateAtom::TypeOutlives(ref pair) => pair.fmt(f), + ty::PredicateAtom::Projection(ref pair) => pair.fmt(f), + ty::PredicateAtom::WellFormed(data) => write!(f, "WellFormed({:?})", data), + ty::PredicateAtom::ObjectSafe(trait_def_id) => { write!(f, "ObjectSafe({:?})", trait_def_id) } - ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => { + ty::PredicateAtom::ClosureKind(closure_def_id, closure_substs, kind) => { write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind) } - ty::PredicateKind::ConstEvaluatable(def_id, substs) => { + ty::PredicateAtom::ConstEvaluatable(def_id, substs) => { write!(f, "ConstEvaluatable({:?}, {:?})", def_id, substs) } - ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2), - ty::PredicateKind::ForAll(binder) => write!(f, "ForAll({:?})", binder), + ty::PredicateAtom::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2), } } } @@ -479,36 +487,45 @@ impl<'a, 'tcx> Lift<'tcx> for ty::PredicateKind<'a> { type Lifted = ty::PredicateKind<'tcx>; fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option { match *self { - ty::PredicateKind::Trait(ref data, constness) => { - tcx.lift(data).map(|data| ty::PredicateKind::Trait(data, constness)) + ty::PredicateKind::ForAll(ref binder) => { + tcx.lift(binder).map(ty::PredicateKind::ForAll) + } + ty::PredicateKind::Atom(ref atom) => tcx.lift(atom).map(ty::PredicateKind::Atom), + } + } +} + +impl<'a, 'tcx> Lift<'tcx> for ty::PredicateAtom<'a> { + type Lifted = ty::PredicateAtom<'tcx>; + fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option { + match *self { + ty::PredicateAtom::Trait(ref data, constness) => { + tcx.lift(data).map(|data| ty::PredicateAtom::Trait(data, constness)) } - ty::PredicateKind::Subtype(ref data) => tcx.lift(data).map(ty::PredicateKind::Subtype), - ty::PredicateKind::RegionOutlives(ref data) => { - tcx.lift(data).map(ty::PredicateKind::RegionOutlives) + ty::PredicateAtom::Subtype(ref data) => tcx.lift(data).map(ty::PredicateAtom::Subtype), + ty::PredicateAtom::RegionOutlives(ref data) => { + tcx.lift(data).map(ty::PredicateAtom::RegionOutlives) } - ty::PredicateKind::TypeOutlives(ref data) => { - tcx.lift(data).map(ty::PredicateKind::TypeOutlives) + ty::PredicateAtom::TypeOutlives(ref data) => { + tcx.lift(data).map(ty::PredicateAtom::TypeOutlives) } - ty::PredicateKind::Projection(ref data) => { - tcx.lift(data).map(ty::PredicateKind::Projection) + ty::PredicateAtom::Projection(ref data) => { + tcx.lift(data).map(ty::PredicateAtom::Projection) } - ty::PredicateKind::WellFormed(ty) => tcx.lift(&ty).map(ty::PredicateKind::WellFormed), - ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => { + ty::PredicateAtom::WellFormed(ty) => tcx.lift(&ty).map(ty::PredicateAtom::WellFormed), + ty::PredicateAtom::ClosureKind(closure_def_id, closure_substs, kind) => { tcx.lift(&closure_substs).map(|closure_substs| { - ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) + ty::PredicateAtom::ClosureKind(closure_def_id, closure_substs, kind) }) } - ty::PredicateKind::ObjectSafe(trait_def_id) => { - Some(ty::PredicateKind::ObjectSafe(trait_def_id)) - } - ty::PredicateKind::ConstEvaluatable(def_id, substs) => { - tcx.lift(&substs).map(|substs| ty::PredicateKind::ConstEvaluatable(def_id, substs)) + ty::PredicateAtom::ObjectSafe(trait_def_id) => { + Some(ty::PredicateAtom::ObjectSafe(trait_def_id)) } - ty::PredicateKind::ConstEquate(c1, c2) => { - tcx.lift(&(c1, c2)).map(|(c1, c2)| ty::PredicateKind::ConstEquate(c1, c2)) + ty::PredicateAtom::ConstEvaluatable(def_id, substs) => { + tcx.lift(&substs).map(|substs| ty::PredicateAtom::ConstEvaluatable(def_id, substs)) } - ty::PredicateKind::ForAll(ref binder) => { - tcx.lift(binder).map(ty::PredicateKind::ForAll) + ty::PredicateAtom::ConstEquate(c1, c2) => { + tcx.lift(&(c1, c2)).map(|(c1, c2)| ty::PredicateAtom::ConstEquate(c1, c2)) } } } diff --git a/src/librustc_mir/borrow_check/diagnostics/region_errors.rs b/src/librustc_mir/borrow_check/diagnostics/region_errors.rs index ffeec8758c46f..a0d99ac33c04e 100644 --- a/src/librustc_mir/borrow_check/diagnostics/region_errors.rs +++ b/src/librustc_mir/borrow_check/diagnostics/region_errors.rs @@ -589,8 +589,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { let mut found = false; for predicate in bounds.predicates { - if let ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(_, r)) = - predicate.ignore_quantifiers().skip_binder().kind() + if let ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(_, r)) = + predicate.skip_binders() { if let ty::RegionKind::ReStatic = r { found = true; diff --git a/src/librustc_mir/borrow_check/type_check/mod.rs b/src/librustc_mir/borrow_check/type_check/mod.rs index 8fdedd72c483f..bc5c144cd742c 100644 --- a/src/librustc_mir/borrow_check/type_check/mod.rs +++ b/src/librustc_mir/borrow_check/type_check/mod.rs @@ -1021,7 +1021,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { } self.prove_predicate( - ty::PredicateKind::WellFormed(inferred_ty.into()).to_predicate(self.tcx()), + ty::PredicateAtom::WellFormed(inferred_ty.into()).to_predicate(self.tcx()), Locations::All(span), ConstraintCategory::TypeAnnotation, ); @@ -1273,7 +1273,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { obligations.obligations.push(traits::Obligation::new( ObligationCause::dummy(), param_env, - ty::PredicateKind::WellFormed(revealed_ty.into()).to_predicate(infcx.tcx), + ty::PredicateAtom::WellFormed(revealed_ty.into()).to_predicate(infcx.tcx), )); obligations.add( infcx @@ -1617,7 +1617,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { self.check_call_dest(body, term, &sig, destination, term_location); self.prove_predicates( - sig.inputs_and_output.iter().map(|ty| ty::PredicateKind::WellFormed(ty.into())), + sig.inputs_and_output.iter().map(|ty| ty::PredicateAtom::WellFormed(ty.into())), term_location.to_locations(), ConstraintCategory::Boring, ); @@ -2702,7 +2702,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { category: ConstraintCategory, ) { self.prove_predicates( - Some(ty::PredicateKind::Trait( + Some(ty::PredicateAtom::Trait( ty::TraitPredicate { trait_ref }, hir::Constness::NotConst, )), diff --git a/src/librustc_mir/transform/qualify_min_const_fn.rs b/src/librustc_mir/transform/qualify_min_const_fn.rs index 69e8c6b5d2acb..de7d7f27186f2 100644 --- a/src/librustc_mir/transform/qualify_min_const_fn.rs +++ b/src/librustc_mir/transform/qualify_min_const_fn.rs @@ -24,24 +24,23 @@ pub fn is_min_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'a Body<'tcx>) - loop { let predicates = tcx.predicates_of(current); for (predicate, _) in predicates.predicates { - match predicate.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", predicate), - ty::PredicateKind::RegionOutlives(_) - | ty::PredicateKind::TypeOutlives(_) - | ty::PredicateKind::WellFormed(_) - | ty::PredicateKind::Projection(_) - | ty::PredicateKind::ConstEvaluatable(..) - | ty::PredicateKind::ConstEquate(..) => continue, - ty::PredicateKind::ObjectSafe(_) => { + match predicate.skip_binders() { + ty::PredicateAtom::RegionOutlives(_) + | ty::PredicateAtom::TypeOutlives(_) + | ty::PredicateAtom::WellFormed(_) + | ty::PredicateAtom::Projection(_) + | ty::PredicateAtom::ConstEvaluatable(..) + | ty::PredicateAtom::ConstEquate(..) => continue, + ty::PredicateAtom::ObjectSafe(_) => { bug!("object safe predicate on function: {:#?}", predicate) } - ty::PredicateKind::ClosureKind(..) => { + ty::PredicateAtom::ClosureKind(..) => { bug!("closure kind predicate on function: {:#?}", predicate) } - ty::PredicateKind::Subtype(_) => { + ty::PredicateAtom::Subtype(_) => { bug!("subtype predicate on function: {:#?}", predicate) } - &ty::PredicateKind::Trait(pred, constness) => { + ty::PredicateAtom::Trait(pred, constness) => { if Some(pred.def_id()) == tcx.lang_items().sized_trait() { continue; } diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index 223e92e0482cd..9c5fb4ce73450 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -85,23 +85,18 @@ where } fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> bool { - match predicate.kind() { - &ty::PredicateKind::ForAll(pred) => { - // This visitor does not care about bound regions as we only - // look at `DefId`s. - self.visit_predicate(pred.skip_binder()) - } - &ty::PredicateKind::Trait(ty::TraitPredicate { trait_ref }, _) => { + match predicate.skip_binders() { + ty::PredicateAtom::Trait(ty::TraitPredicate { trait_ref }, _) => { self.visit_trait(trait_ref) } - &ty::PredicateKind::Projection(ty::ProjectionPredicate { projection_ty, ty }) => { + ty::PredicateAtom::Projection(ty::ProjectionPredicate { projection_ty, ty }) => { ty.visit_with(self) || self.visit_trait(projection_ty.trait_ref(self.def_id_visitor.tcx())) } - &ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, _region)) => { + ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty, _region)) => { ty.visit_with(self) } - ty::PredicateKind::RegionOutlives(..) => false, + ty::PredicateAtom::RegionOutlives(..) => false, _ => bug!("unexpected predicate: {:?}", predicate), } } diff --git a/src/librustc_trait_selection/opaque_types.rs b/src/librustc_trait_selection/opaque_types.rs index 1067ca20a174f..b84ad93341e81 100644 --- a/src/librustc_trait_selection/opaque_types.rs +++ b/src/librustc_trait_selection/opaque_types.rs @@ -1154,9 +1154,7 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> { debug!("instantiate_opaque_types: ty_var={:?}", ty_var); for predicate in &bounds.predicates { - if let ty::PredicateKind::Projection(projection) = - predicate.ignore_quantifiers().skip_binder().kind() - { + if let ty::PredicateAtom::Projection(projection) = predicate.skip_binders() { if projection.ty.references_error() { // No point on adding these obligations since there's a type error involved. return ty_var; @@ -1254,18 +1252,17 @@ crate fn required_region_bounds( traits::elaborate_predicates(tcx, predicates) .filter_map(|obligation| { debug!("required_region_bounds(obligation={:?})", obligation); - match obligation.predicate.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::Projection(..) - | ty::PredicateKind::Trait(..) - | ty::PredicateKind::Subtype(..) - | ty::PredicateKind::WellFormed(..) - | ty::PredicateKind::ObjectSafe(..) - | ty::PredicateKind::ClosureKind(..) - | ty::PredicateKind::RegionOutlives(..) - | ty::PredicateKind::ConstEvaluatable(..) - | ty::PredicateKind::ConstEquate(..) => None, - ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", obligation), - ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ref t, ref r)) => { + match obligation.predicate.skip_binders() { + ty::PredicateAtom::Projection(..) + | ty::PredicateAtom::Trait(..) + | ty::PredicateAtom::Subtype(..) + | ty::PredicateAtom::WellFormed(..) + | ty::PredicateAtom::ObjectSafe(..) + | ty::PredicateAtom::ClosureKind(..) + | ty::PredicateAtom::RegionOutlives(..) + | ty::PredicateAtom::ConstEvaluatable(..) + | ty::PredicateAtom::ConstEquate(..) => None, + ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ref t, ref r)) => { // Search for a bound of the form `erased_self_ty // : 'a`, but be wary of something like `for<'a> // erased_self_ty : 'a` (we interpret a diff --git a/src/librustc_trait_selection/traits/auto_trait.rs b/src/librustc_trait_selection/traits/auto_trait.rs index b5bea9724283d..6fe67509660bc 100644 --- a/src/librustc_trait_selection/traits/auto_trait.rs +++ b/src/librustc_trait_selection/traits/auto_trait.rs @@ -415,12 +415,10 @@ impl AutoTraitFinder<'tcx> { let mut should_add_new = true; user_computed_preds.retain(|&old_pred| { if let ( - ty::PredicateKind::Trait(new_trait, _), - ty::PredicateKind::Trait(old_trait, _), - ) = ( - new_pred.ignore_quantifiers().skip_binder().kind(), - old_pred.ignore_quantifiers().skip_binder().kind(), - ) { + ty::PredicateAtom::Trait(new_trait, _), + ty::PredicateAtom::Trait(old_trait, _), + ) = (new_pred.skip_binders(), old_pred.skip_binders()) + { if new_trait.def_id() == old_trait.def_id() { let new_substs = new_trait.trait_ref.substs; let old_substs = old_trait.trait_ref.substs; @@ -639,8 +637,8 @@ impl AutoTraitFinder<'tcx> { // We check this by calling is_of_param on the relevant types // from the various possible predicates - match predicate.ignore_quantifiers().skip_binder().kind() { - &ty::PredicateKind::Trait(p, _) => { + match predicate.skip_binders() { + ty::PredicateAtom::Trait(p, _) => { if self.is_param_no_infer(p.trait_ref.substs) && !only_projections && is_new_pred @@ -649,7 +647,7 @@ impl AutoTraitFinder<'tcx> { } predicates.push_back(ty::Binder::bind(p)); } - &ty::PredicateKind::Projection(p) => { + ty::PredicateAtom::Projection(p) => { let p = ty::Binder::bind(p); debug!( "evaluate_nested_obligations: examining projection predicate {:?}", @@ -775,13 +773,13 @@ impl AutoTraitFinder<'tcx> { } } } - &ty::PredicateKind::RegionOutlives(binder) => { + ty::PredicateAtom::RegionOutlives(binder) => { let binder = ty::Binder::bind(binder); if select.infcx().region_outlives_predicate(&dummy_cause, binder).is_err() { return false; } } - &ty::PredicateKind::TypeOutlives(binder) => { + ty::PredicateAtom::TypeOutlives(binder) => { let binder = ty::Binder::bind(binder); match ( binder.no_bound_vars(), diff --git a/src/librustc_trait_selection/traits/error_reporting/mod.rs b/src/librustc_trait_selection/traits/error_reporting/mod.rs index 8af4e12bd2b0f..951e0b2202666 100644 --- a/src/librustc_trait_selection/traits/error_reporting/mod.rs +++ b/src/librustc_trait_selection/traits/error_reporting/mod.rs @@ -256,11 +256,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { return; } - match obligation.predicate.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::ForAll(_) => { - bug!("unexpected predicate: {:?}", obligation.predicate) - } - &ty::PredicateKind::Trait(trait_predicate, _) => { + match obligation.predicate.skip_binders() { + ty::PredicateAtom::Trait(trait_predicate, _) => { let trait_predicate = ty::Binder::bind(trait_predicate); let trait_predicate = self.resolve_vars_if_possible(&trait_predicate); @@ -525,14 +522,14 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { err } - ty::PredicateKind::Subtype(predicate) => { + ty::PredicateAtom::Subtype(predicate) => { // Errors for Subtype predicates show up as // `FulfillmentErrorCode::CodeSubtypeError`, // not selection error. span_bug!(span, "subtype requirement gave wrong error: `{:?}`", predicate) } - &ty::PredicateKind::RegionOutlives(predicate) => { + ty::PredicateAtom::RegionOutlives(predicate) => { let predicate = ty::Binder::bind(predicate); let predicate = self.resolve_vars_if_possible(&predicate); let err = self @@ -549,7 +546,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { ) } - ty::PredicateKind::Projection(..) | ty::PredicateKind::TypeOutlives(..) => { + ty::PredicateAtom::Projection(..) | ty::PredicateAtom::TypeOutlives(..) => { let predicate = self.resolve_vars_if_possible(&obligation.predicate); struct_span_err!( self.tcx.sess, @@ -560,12 +557,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { ) } - &ty::PredicateKind::ObjectSafe(trait_def_id) => { + ty::PredicateAtom::ObjectSafe(trait_def_id) => { let violations = self.tcx.object_safety_violations(trait_def_id); report_object_safety_error(self.tcx, span, trait_def_id, violations) } - &ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => { + ty::PredicateAtom::ClosureKind(closure_def_id, closure_substs, kind) => { let found_kind = self.closure_kind(closure_substs).unwrap(); let closure_span = self.tcx.sess.source_map().guess_head_span( @@ -624,7 +621,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { return; } - ty::PredicateKind::WellFormed(ty) => { + ty::PredicateAtom::WellFormed(ty) => { if !self.tcx.sess.opts.debugging_opts.chalk { // WF predicates cannot themselves make // errors. They can only block due to @@ -642,7 +639,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { } } - ty::PredicateKind::ConstEvaluatable(..) => { + ty::PredicateAtom::ConstEvaluatable(..) => { // Errors for `ConstEvaluatable` predicates show up as // `SelectionError::ConstEvalFailure`, // not `Unimplemented`. @@ -653,7 +650,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { ) } - ty::PredicateKind::ConstEquate(..) => { + ty::PredicateAtom::ConstEquate(..) => { // Errors for `ConstEquate` predicates show up as // `SelectionError::ConstEvalFailure`, // not `Unimplemented`. @@ -1090,11 +1087,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { } // FIXME: It should be possible to deal with `ForAll` in a cleaner way. - let (cond, error) = match ( - cond.ignore_quantifiers().skip_binder().kind(), - error.ignore_quantifiers().skip_binder().kind(), - ) { - (ty::PredicateKind::Trait(..), &ty::PredicateKind::Trait(error, _)) => { + let (cond, error) = match (cond.skip_binders(), error.skip_binders()) { + (ty::PredicateAtom::Trait(..), ty::PredicateAtom::Trait(error, _)) => { (cond, ty::Binder::bind(error)) } _ => { @@ -1104,9 +1098,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { }; for obligation in super::elaborate_predicates(self.tcx, std::iter::once(cond)) { - if let &ty::PredicateKind::Trait(implication, _) = - obligation.predicate.ignore_quantifiers().skip_binder().kind() - { + if let ty::PredicateAtom::Trait(implication, _) = obligation.predicate.skip_binders() { let error = error.to_poly_trait_ref(); let implication = ty::Binder::bind(implication).to_poly_trait_ref(); // FIXME: I'm just not taking associated types at all here. @@ -1186,9 +1178,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { // // this can fail if the problem was higher-ranked, in which // cause I have no idea for a good error message. - if let &ty::PredicateKind::Projection(data) = - predicate.ignore_quantifiers().skip_binder().kind() - { + if let ty::PredicateAtom::Projection(data) = predicate.skip_binders() { let mut selcx = SelectionContext::new(self); let (data, _) = self.replace_bound_vars_with_fresh_vars( obligation.cause.span, @@ -1480,8 +1470,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { return; } - let mut err = match predicate.ignore_quantifiers().skip_binder().kind() { - &ty::PredicateKind::Trait(data, _) => { + let mut err = match predicate.skip_binders() { + ty::PredicateAtom::Trait(data, _) => { let trait_ref = ty::Binder::bind(data.trait_ref); let self_ty = trait_ref.skip_binder().self_ty(); debug!("self_ty {:?} {:?} trait_ref {:?}", self_ty, self_ty.kind, trait_ref); @@ -1580,7 +1570,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { err } - ty::PredicateKind::WellFormed(arg) => { + ty::PredicateAtom::WellFormed(arg) => { // Same hacky approach as above to avoid deluging user // with error messages. if arg.references_error() || self.tcx.sess.has_errors() { @@ -1600,17 +1590,17 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { } } - ty::PredicateKind::Subtype(data) => { + ty::PredicateAtom::Subtype(data) => { if data.references_error() || self.tcx.sess.has_errors() { // no need to overload user in such cases return; } - let &SubtypePredicate { a_is_expected: _, a, b } = data; + let SubtypePredicate { a_is_expected: _, a, b } = data; // both must be type variables, or the other would've been instantiated assert!(a.is_ty_var() && b.is_ty_var()); self.need_type_info_err(body_id, span, a, ErrorCode::E0282) } - &ty::PredicateKind::Projection(data) => { + ty::PredicateAtom::Projection(data) => { let trait_ref = ty::Binder::bind(data).to_poly_trait_ref(self.tcx); let self_ty = trait_ref.skip_binder().self_ty(); let ty = data.ty; @@ -1733,16 +1723,14 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { err: &mut DiagnosticBuilder<'tcx>, obligation: &PredicateObligation<'tcx>, ) { - let (pred, item_def_id, span) = match ( - obligation.predicate.ignore_quantifiers().skip_binder().kind(), - obligation.cause.code.peel_derives(), - ) { - ( - ty::PredicateKind::Trait(pred, _), - &ObligationCauseCode::BindingObligation(item_def_id, span), - ) => (pred, item_def_id, span), - _ => return, - }; + let (pred, item_def_id, span) = + match (obligation.predicate.skip_binders(), obligation.cause.code.peel_derives()) { + ( + ty::PredicateAtom::Trait(pred, _), + &ObligationCauseCode::BindingObligation(item_def_id, span), + ) => (pred, item_def_id, span), + _ => return, + }; let node = match ( self.tcx.hir().get_if_local(item_def_id), diff --git a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs index 9180325fb74f6..200de47244e4f 100644 --- a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs +++ b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs @@ -1299,11 +1299,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { // the type. The last generator (`outer_generator` below) has information about where the // bound was introduced. At least one generator should be present for this diagnostic to be // modified. - let (mut trait_ref, mut target_ty) = - match obligation.predicate.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::Trait(p, _) => (Some(p.trait_ref), Some(p.self_ty())), - _ => (None, None), - }; + let (mut trait_ref, mut target_ty) = match obligation.predicate.skip_binders() { + ty::PredicateAtom::Trait(p, _) => (Some(p.trait_ref), Some(p.self_ty())), + _ => (None, None), + }; let mut generator = None; let mut outer_generator = None; let mut next_code = Some(&obligation.cause.code); diff --git a/src/librustc_trait_selection/traits/fulfill.rs b/src/librustc_trait_selection/traits/fulfill.rs index 995be1d90de36..0f6f362490604 100644 --- a/src/librustc_trait_selection/traits/fulfill.rs +++ b/src/librustc_trait_selection/traits/fulfill.rs @@ -320,11 +320,44 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { match obligation.predicate.kind() { ty::PredicateKind::ForAll(binder) => match binder.skip_binder().kind() { + ty::PredicateKind::ForAll(_) => bug!("unexpected forall"), // Evaluation will discard candidates using the leak check. // This means we need to pass it the bound version of our // predicate. - ty::PredicateKind::Trait(trait_ref, _constness) => { - let trait_obligation = obligation.with(Binder::bind(*trait_ref)); + &ty::PredicateKind::Atom(atom) => match atom { + ty::PredicateAtom::Trait(trait_ref, _constness) => { + let trait_obligation = obligation.with(Binder::bind(trait_ref)); + + self.process_trait_obligation( + obligation, + trait_obligation, + &mut pending_obligation.stalled_on, + ) + } + ty::PredicateAtom::Projection(projection) => { + let project_obligation = obligation.with(Binder::bind(projection)); + + self.process_projection_obligation( + project_obligation, + &mut pending_obligation.stalled_on, + ) + } + ty::PredicateAtom::RegionOutlives(_) + | ty::PredicateAtom::TypeOutlives(_) + | ty::PredicateAtom::WellFormed(_) + | ty::PredicateAtom::ObjectSafe(_) + | ty::PredicateAtom::ClosureKind(..) + | ty::PredicateAtom::Subtype(_) + | ty::PredicateAtom::ConstEvaluatable(..) + | ty::PredicateAtom::ConstEquate(..) => { + let (pred, _) = infcx.replace_bound_vars_with_placeholders(binder); + ProcessResult::Changed(mk_pending(vec![obligation.with(pred)])) + } + }, + }, + &ty::PredicateKind::Atom(atom) => match atom { + ty::PredicateAtom::Trait(ref data, _) => { + let trait_obligation = obligation.with(Binder::dummy(*data)); self.process_trait_obligation( obligation, @@ -332,142 +365,112 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { &mut pending_obligation.stalled_on, ) } - ty::PredicateKind::Projection(projection) => { - let project_obligation = obligation.with(Binder::bind(*projection)); - - self.process_projection_obligation( - project_obligation, - &mut pending_obligation.stalled_on, - ) - } - ty::PredicateKind::RegionOutlives(_) - | ty::PredicateKind::TypeOutlives(_) - | ty::PredicateKind::WellFormed(_) - | ty::PredicateKind::ObjectSafe(_) - | ty::PredicateKind::ClosureKind(..) - | ty::PredicateKind::Subtype(_) - | ty::PredicateKind::ConstEvaluatable(..) - | ty::PredicateKind::ConstEquate(..) - | ty::PredicateKind::ForAll(_) => { - let (pred, _) = infcx.replace_bound_vars_with_placeholders(binder); - ProcessResult::Changed(mk_pending(vec![obligation.with(pred)])) - } - }, - ty::PredicateKind::Trait(ref data, _) => { - let trait_obligation = obligation.with(Binder::dummy(*data)); - - self.process_trait_obligation( - obligation, - trait_obligation, - &mut pending_obligation.stalled_on, - ) - } - &ty::PredicateKind::RegionOutlives(data) => { - match infcx.region_outlives_predicate(&obligation.cause, Binder::dummy(data)) { - Ok(()) => ProcessResult::Changed(vec![]), - Err(_) => ProcessResult::Error(CodeSelectionError(Unimplemented)), + ty::PredicateAtom::RegionOutlives(data) => { + match infcx.region_outlives_predicate(&obligation.cause, Binder::dummy(data)) { + Ok(()) => ProcessResult::Changed(vec![]), + Err(_) => ProcessResult::Error(CodeSelectionError(Unimplemented)), + } } - } - ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(t_a, r_b)) => { - if self.register_region_obligations { - self.selcx.infcx().register_region_obligation_with_cause( - t_a, - r_b, - &obligation.cause, - ); + ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(t_a, r_b)) => { + if self.register_region_obligations { + self.selcx.infcx().register_region_obligation_with_cause( + t_a, + r_b, + &obligation.cause, + ); + } + ProcessResult::Changed(vec![]) } - ProcessResult::Changed(vec![]) - } - ty::PredicateKind::Projection(ref data) => { - let project_obligation = obligation.with(Binder::dummy(*data)); + ty::PredicateAtom::Projection(ref data) => { + let project_obligation = obligation.with(Binder::dummy(*data)); - self.process_projection_obligation( - project_obligation, - &mut pending_obligation.stalled_on, - ) - } + self.process_projection_obligation( + project_obligation, + &mut pending_obligation.stalled_on, + ) + } - &ty::PredicateKind::ObjectSafe(trait_def_id) => { - if !self.selcx.tcx().is_object_safe(trait_def_id) { - ProcessResult::Error(CodeSelectionError(Unimplemented)) - } else { - ProcessResult::Changed(vec![]) + ty::PredicateAtom::ObjectSafe(trait_def_id) => { + if !self.selcx.tcx().is_object_safe(trait_def_id) { + ProcessResult::Error(CodeSelectionError(Unimplemented)) + } else { + ProcessResult::Changed(vec![]) + } } - } - &ty::PredicateKind::ClosureKind(_, closure_substs, kind) => { - match self.selcx.infcx().closure_kind(closure_substs) { - Some(closure_kind) => { - if closure_kind.extends(kind) { - ProcessResult::Changed(vec![]) - } else { - ProcessResult::Error(CodeSelectionError(Unimplemented)) + ty::PredicateAtom::ClosureKind(_, closure_substs, kind) => { + match self.selcx.infcx().closure_kind(closure_substs) { + Some(closure_kind) => { + if closure_kind.extends(kind) { + ProcessResult::Changed(vec![]) + } else { + ProcessResult::Error(CodeSelectionError(Unimplemented)) + } } + None => ProcessResult::Unchanged, } - None => ProcessResult::Unchanged, } - } - &ty::PredicateKind::WellFormed(arg) => { - match wf::obligations( - self.selcx.infcx(), - obligation.param_env, - obligation.cause.body_id, - arg, - obligation.cause.span, - ) { - None => { - pending_obligation.stalled_on = - vec![TyOrConstInferVar::maybe_from_generic_arg(arg).unwrap()]; - ProcessResult::Unchanged + ty::PredicateAtom::WellFormed(arg) => { + match wf::obligations( + self.selcx.infcx(), + obligation.param_env, + obligation.cause.body_id, + arg, + obligation.cause.span, + ) { + None => { + pending_obligation.stalled_on = + vec![TyOrConstInferVar::maybe_from_generic_arg(arg).unwrap()]; + ProcessResult::Unchanged + } + Some(os) => ProcessResult::Changed(mk_pending(os)), } - Some(os) => ProcessResult::Changed(mk_pending(os)), } - } - &ty::PredicateKind::Subtype(subtype) => { - match self.selcx.infcx().subtype_predicate( - &obligation.cause, - obligation.param_env, - Binder::dummy(subtype), - ) { - None => { - // None means that both are unresolved. - pending_obligation.stalled_on = vec![ - TyOrConstInferVar::maybe_from_ty(subtype.a).unwrap(), - TyOrConstInferVar::maybe_from_ty(subtype.b).unwrap(), - ]; - ProcessResult::Unchanged - } - Some(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)), - Some(Err(err)) => { - let expected_found = - ExpectedFound::new(subtype.a_is_expected, subtype.a, subtype.b); - ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError( - expected_found, - err, - )) + ty::PredicateAtom::Subtype(subtype) => { + match self.selcx.infcx().subtype_predicate( + &obligation.cause, + obligation.param_env, + Binder::dummy(subtype), + ) { + None => { + // None means that both are unresolved. + pending_obligation.stalled_on = vec![ + TyOrConstInferVar::maybe_from_ty(subtype.a).unwrap(), + TyOrConstInferVar::maybe_from_ty(subtype.b).unwrap(), + ]; + ProcessResult::Unchanged + } + Some(Ok(ok)) => ProcessResult::Changed(mk_pending(ok.obligations)), + Some(Err(err)) => { + let expected_found = + ExpectedFound::new(subtype.a_is_expected, subtype.a, subtype.b); + ProcessResult::Error(FulfillmentErrorCode::CodeSubtypeError( + expected_found, + err, + )) + } } } - } - &ty::PredicateKind::ConstEvaluatable(def_id, substs) => { - match self.selcx.infcx().const_eval_resolve( - obligation.param_env, - def_id, - substs, - None, - Some(obligation.cause.span), - ) { - Ok(_) => ProcessResult::Changed(vec![]), - Err(err) => ProcessResult::Error(CodeSelectionError(ConstEvalFailure(err))), + ty::PredicateAtom::ConstEvaluatable(def_id, substs) => { + match self.selcx.infcx().const_eval_resolve( + obligation.param_env, + def_id, + substs, + None, + Some(obligation.cause.span), + ) { + Ok(_) => ProcessResult::Changed(vec![]), + Err(err) => ProcessResult::Error(CodeSelectionError(ConstEvalFailure(err))), + } } - } - ty::PredicateKind::ConstEquate(c1, c2) => { + ty::PredicateAtom::ConstEquate(c1, c2) => { debug!("equating consts: c1={:?} c2={:?}", c1, c2); let stalled_on = &mut pending_obligation.stalled_on; @@ -491,43 +494,46 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { ); Err(ErrorHandled::TooGeneric) } - Err(err) => Err(err), + } else { + Ok(c) } - } else { - Ok(c) - } - }; - - match (evaluate(c1), evaluate(c2)) { - (Ok(c1), Ok(c2)) => { - match self - .selcx - .infcx() - .at(&obligation.cause, obligation.param_env) - .eq(c1, c2) - { - Ok(_) => ProcessResult::Changed(vec![]), - Err(err) => { - ProcessResult::Error(FulfillmentErrorCode::CodeConstEquateError( - ExpectedFound::new(true, c1, c2), - err, - )) + }; + + match (evaluate(c1), evaluate(c2)) { + (Ok(c1), Ok(c2)) => { + match self + .selcx + .infcx() + .at(&obligation.cause, obligation.param_env) + .eq(c1, c2) + { + Ok(_) => ProcessResult::Changed(vec![]), + Err(err) => ProcessResult::Error( + FulfillmentErrorCode::CodeConstEquateError( + ExpectedFound::new(true, c1, c2), + err, + ), + ), } } - } - (Err(ErrorHandled::Reported(ErrorReported)), _) - | (_, Err(ErrorHandled::Reported(ErrorReported))) => ProcessResult::Error( - CodeSelectionError(ConstEvalFailure(ErrorHandled::Reported(ErrorReported))), - ), - (Err(ErrorHandled::Linted), _) | (_, Err(ErrorHandled::Linted)) => span_bug!( - obligation.cause.span(self.selcx.tcx()), - "ConstEquate: const_eval_resolve returned an unexpected error" - ), - (Err(ErrorHandled::TooGeneric), _) | (_, Err(ErrorHandled::TooGeneric)) => { - ProcessResult::Unchanged + (Err(ErrorHandled::Reported(ErrorReported)), _) + | (_, Err(ErrorHandled::Reported(ErrorReported))) => { + ProcessResult::Error(CodeSelectionError(ConstEvalFailure( + ErrorHandled::Reported(ErrorReported), + ))) + } + (Err(ErrorHandled::Linted), _) | (_, Err(ErrorHandled::Linted)) => { + span_bug!( + obligation.cause.span(self.selcx.tcx()), + "ConstEquate: const_eval_resolve returned an unexpected error" + ) + } + (Err(ErrorHandled::TooGeneric), _) | (_, Err(ErrorHandled::TooGeneric)) => { + ProcessResult::Unchanged + } } } - } + }, } } diff --git a/src/librustc_trait_selection/traits/mod.rs b/src/librustc_trait_selection/traits/mod.rs index bd0ba5570c7bb..afa48c2f76cf8 100644 --- a/src/librustc_trait_selection/traits/mod.rs +++ b/src/librustc_trait_selection/traits/mod.rs @@ -328,8 +328,8 @@ pub fn normalize_param_env_or_error<'tcx>( // This works fairly well because trait matching does not actually care about param-env // TypeOutlives predicates - these are normally used by regionck. let outlives_predicates: Vec<_> = predicates - .drain_filter(|predicate| match predicate.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::TypeOutlives(..) => true, + .drain_filter(|predicate| match predicate.skip_binders() { + ty::PredicateAtom::TypeOutlives(..) => true, _ => false, }) .collect(); diff --git a/src/librustc_trait_selection/traits/object_safety.rs b/src/librustc_trait_selection/traits/object_safety.rs index 8da5e7318c002..c003e4f806873 100644 --- a/src/librustc_trait_selection/traits/object_safety.rs +++ b/src/librustc_trait_selection/traits/object_safety.rs @@ -245,12 +245,12 @@ fn predicates_reference_self( .iter() .map(|(predicate, sp)| (predicate.subst_supertrait(tcx, &trait_ref), sp)) .filter_map(|(predicate, &sp)| { - match predicate.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::Trait(ref data, _) => { + match predicate.skip_binders() { + ty::PredicateAtom::Trait(ref data, _) => { // In the case of a trait predicate, we can skip the "self" type. if data.trait_ref.substs[1..].iter().any(has_self_ty) { Some(sp) } else { None } } - ty::PredicateKind::Projection(ref data) => { + ty::PredicateAtom::Projection(ref data) => { // And similarly for projections. This should be redundant with // the previous check because any projection should have a // matching `Trait` predicate with the same inputs, but we do @@ -269,15 +269,14 @@ fn predicates_reference_self( None } } - ty::PredicateKind::WellFormed(..) - | ty::PredicateKind::ObjectSafe(..) - | ty::PredicateKind::TypeOutlives(..) - | ty::PredicateKind::RegionOutlives(..) - | ty::PredicateKind::ClosureKind(..) - | ty::PredicateKind::Subtype(..) - | ty::PredicateKind::ConstEvaluatable(..) - | ty::PredicateKind::ConstEquate(..) => None, - ty::PredicateKind::ForAll(..) => bug!("unexpected predicate: {:?}", predicate), + ty::PredicateAtom::WellFormed(..) + | ty::PredicateAtom::ObjectSafe(..) + | ty::PredicateAtom::TypeOutlives(..) + | ty::PredicateAtom::RegionOutlives(..) + | ty::PredicateAtom::ClosureKind(..) + | ty::PredicateAtom::Subtype(..) + | ty::PredicateAtom::ConstEvaluatable(..) + | ty::PredicateAtom::ConstEquate(..) => None, } }) .collect() @@ -299,22 +298,19 @@ fn generics_require_sized_self(tcx: TyCtxt<'_>, def_id: DefId) -> bool { let predicates = tcx.predicates_of(def_id); let predicates = predicates.instantiate_identity(tcx).predicates; elaborate_predicates(tcx, predicates.into_iter()).any(|obligation| { - match obligation.predicate.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::Trait(ref trait_pred, _) => { + match obligation.predicate.skip_binders() { + ty::PredicateAtom::Trait(ref trait_pred, _) => { trait_pred.def_id() == sized_def_id && trait_pred.self_ty().is_param(0) } - ty::PredicateKind::Projection(..) - | ty::PredicateKind::Subtype(..) - | ty::PredicateKind::RegionOutlives(..) - | ty::PredicateKind::WellFormed(..) - | ty::PredicateKind::ObjectSafe(..) - | ty::PredicateKind::ClosureKind(..) - | ty::PredicateKind::TypeOutlives(..) - | ty::PredicateKind::ConstEvaluatable(..) - | ty::PredicateKind::ConstEquate(..) => false, - ty::PredicateKind::ForAll(_) => { - bug!("unexpected predicate: {:?}", obligation.predicate) - } + ty::PredicateAtom::Projection(..) + | ty::PredicateAtom::Subtype(..) + | ty::PredicateAtom::RegionOutlives(..) + | ty::PredicateAtom::WellFormed(..) + | ty::PredicateAtom::ObjectSafe(..) + | ty::PredicateAtom::ClosureKind(..) + | ty::PredicateAtom::TypeOutlives(..) + | ty::PredicateAtom::ConstEvaluatable(..) + | ty::PredicateAtom::ConstEquate(..) => false, } }) } diff --git a/src/librustc_trait_selection/traits/project.rs b/src/librustc_trait_selection/traits/project.rs index c12f9eb112f9d..717b7e2fe574f 100644 --- a/src/librustc_trait_selection/traits/project.rs +++ b/src/librustc_trait_selection/traits/project.rs @@ -665,7 +665,7 @@ fn prune_cache_value_obligations<'a, 'tcx>( .obligations .iter() .filter(|obligation| { - match obligation.predicate.ignore_quantifiers().skip_binder().kind() { + match obligation.predicate.skip_binders() { // We found a `T: Foo` predicate, let's check // if `U` references any unresolved type // variables. In principle, we only care if this @@ -675,7 +675,7 @@ fn prune_cache_value_obligations<'a, 'tcx>( // indirect obligations (e.g., we project to `?0`, // but we have `T: Foo` and `?1: Bar`). - &ty::PredicateKind::Projection(data) => { + ty::PredicateAtom::Projection(data) => { infcx.unresolved_type_vars(&ty::Binder::bind(data.ty)).is_some() } @@ -933,9 +933,7 @@ fn assemble_candidates_from_predicates<'cx, 'tcx>( let infcx = selcx.infcx(); for predicate in env_predicates { debug!("assemble_candidates_from_predicates: predicate={:?}", predicate); - if let &ty::PredicateKind::Projection(data) = - predicate.ignore_quantifiers().skip_binder().kind() - { + if let ty::PredicateAtom::Projection(data) = predicate.skip_binders() { let data = ty::Binder::bind(data); let same_def_id = data.projection_def_id() == obligation.predicate.item_def_id; @@ -1227,15 +1225,13 @@ fn confirm_object_candidate<'cx, 'tcx>( // select only those projections that are actually projecting an // item with the correct name - let env_predicates = env_predicates.filter_map(|o| { - match o.predicate.ignore_quantifiers().skip_binder().kind() { - &ty::PredicateKind::Projection(data) - if data.projection_ty.item_def_id == obligation.predicate.item_def_id => - { - Some(ty::Binder::bind(data)) - } - _ => None, + let env_predicates = env_predicates.filter_map(|o| match o.predicate.skip_binders() { + ty::PredicateAtom::Projection(data) + if data.projection_ty.item_def_id == obligation.predicate.item_def_id => + { + Some(ty::Binder::bind(data)) } + _ => None, }); // select those with a relevant trait-ref diff --git a/src/librustc_trait_selection/traits/query/type_op/prove_predicate.rs b/src/librustc_trait_selection/traits/query/type_op/prove_predicate.rs index 24551299df252..93ddcb6855400 100644 --- a/src/librustc_trait_selection/traits/query/type_op/prove_predicate.rs +++ b/src/librustc_trait_selection/traits/query/type_op/prove_predicate.rs @@ -15,9 +15,7 @@ impl<'tcx> super::QueryTypeOp<'tcx> for ProvePredicate<'tcx> { // `&T`, accounts for about 60% percentage of the predicates // we have to prove. No need to canonicalize and all that for // such cases. - if let ty::PredicateKind::Trait(trait_ref, _) = - key.value.predicate.ignore_quantifiers().skip_binder().kind() - { + if let ty::PredicateAtom::Trait(trait_ref, _) = key.value.predicate.skip_binders() { if let Some(sized_def_id) = tcx.lang_items().sized_trait() { if trait_ref.def_id() == sized_def_id { if trait_ref.self_ty().is_trivially_sized(tcx) { diff --git a/src/librustc_trait_selection/traits/select/confirmation.rs b/src/librustc_trait_selection/traits/select/confirmation.rs index fa970589bbbf6..a04636af5796a 100644 --- a/src/librustc_trait_selection/traits/select/confirmation.rs +++ b/src/librustc_trait_selection/traits/select/confirmation.rs @@ -532,7 +532,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { obligations.push(Obligation::new( obligation.cause.clone(), obligation.param_env, - ty::PredicateKind::ClosureKind(closure_def_id, substs, kind) + ty::PredicateAtom::ClosureKind(closure_def_id, substs, kind) .to_predicate(self.tcx()), )); } diff --git a/src/librustc_trait_selection/traits/select/mod.rs b/src/librustc_trait_selection/traits/select/mod.rs index 4b1e2013feff2..4123f2938261c 100644 --- a/src/librustc_trait_selection/traits/select/mod.rs +++ b/src/librustc_trait_selection/traits/select/mod.rs @@ -408,18 +408,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { None => self.check_recursion_limit(&obligation, &obligation)?, } - match obligation.predicate.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::ForAll(_) => { - bug!("unexpected predicate: {:?}", obligation.predicate) - } - &ty::PredicateKind::Trait(t, _) => { + match obligation.predicate.skip_binders() { + ty::PredicateAtom::Trait(t, _) => { let t = ty::Binder::bind(t); debug_assert!(!t.has_escaping_bound_vars()); let obligation = obligation.with(t); self.evaluate_trait_predicate_recursively(previous_stack, obligation) } - &ty::PredicateKind::Subtype(p) => { + ty::PredicateAtom::Subtype(p) => { let p = ty::Binder::bind(p); // Does this code ever run? match self.infcx.subtype_predicate(&obligation.cause, obligation.param_env, p) { @@ -435,7 +432,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } } - &ty::PredicateKind::WellFormed(arg) => match wf::obligations( + ty::PredicateAtom::WellFormed(arg) => match wf::obligations( self.infcx, obligation.param_env, obligation.cause.body_id, @@ -449,12 +446,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { None => Ok(EvaluatedToAmbig), }, - ty::PredicateKind::TypeOutlives(..) | ty::PredicateKind::RegionOutlives(..) => { + ty::PredicateAtom::TypeOutlives(..) | ty::PredicateAtom::RegionOutlives(..) => { // We do not consider region relationships when evaluating trait matches. Ok(EvaluatedToOkModuloRegions) } - &ty::PredicateKind::ObjectSafe(trait_def_id) => { + ty::PredicateAtom::ObjectSafe(trait_def_id) => { if self.tcx().is_object_safe(trait_def_id) { Ok(EvaluatedToOk) } else { @@ -462,7 +459,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } } - &ty::PredicateKind::Projection(data) => { + ty::PredicateAtom::Projection(data) => { let data = ty::Binder::bind(data); let project_obligation = obligation.with(data); match project::poly_project_and_unify_type(self, &project_obligation) { @@ -484,7 +481,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } } - &ty::PredicateKind::ClosureKind(_, closure_substs, kind) => { + ty::PredicateAtom::ClosureKind(_, closure_substs, kind) => { match self.infcx.closure_kind(closure_substs) { Some(closure_kind) => { if closure_kind.extends(kind) { @@ -497,7 +494,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } } - &ty::PredicateKind::ConstEvaluatable(def_id, substs) => { + ty::PredicateAtom::ConstEvaluatable(def_id, substs) => { match self.tcx().const_eval_resolve( obligation.param_env, def_id, @@ -511,7 +508,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } } - ty::PredicateKind::ConstEquate(c1, c2) => { + ty::PredicateAtom::ConstEquate(c1, c2) => { debug!("evaluate_predicate_recursively: equating consts c1={:?} c2={:?}", c1, c2); let evaluate = |c: &'tcx ty::Const<'tcx>| { @@ -792,8 +789,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } fn coinductive_predicate(&self, predicate: ty::Predicate<'tcx>) -> bool { - let result = match predicate.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::Trait(ref data, _) => self.tcx().trait_is_auto(data.def_id()), + let result = match predicate.skip_binders() { + ty::PredicateAtom::Trait(ref data, _) => self.tcx().trait_is_auto(data.def_id()), _ => false, }; debug!("coinductive_predicate({:?}) = {:?}", predicate, result); @@ -1301,9 +1298,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { }; let matching_bound = predicates.iter().find_map(|bound| { - if let ty::PredicateKind::Trait(pred, _) = - bound.ignore_quantifiers().skip_binder().kind() - { + if let ty::PredicateAtom::Trait(pred, _) = bound.skip_binders() { let bound = ty::Binder::bind(pred.trait_ref); if self.infcx.probe(|_| { self.match_projection(obligation, bound, placeholder_trait_predicate.trait_ref) diff --git a/src/librustc_trait_selection/traits/wf.rs b/src/librustc_trait_selection/traits/wf.rs index afa2270b7af0f..0ca69c0f76e7a 100644 --- a/src/librustc_trait_selection/traits/wf.rs +++ b/src/librustc_trait_selection/traits/wf.rs @@ -98,38 +98,40 @@ pub fn predicate_obligations<'a, 'tcx>( // It's ok to skip the binder here because wf code is prepared for it return predicate_obligations(infcx, param_env, body_id, binder.skip_binder(), span); } - ty::PredicateKind::Trait(t, _) => { - wf.compute_trait_ref(&t.trait_ref, Elaborate::None); - } - ty::PredicateKind::RegionOutlives(..) => {} - &ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, _reg)) => { - wf.compute(ty.into()); - } - ty::PredicateKind::Projection(t) => { - wf.compute_projection(t.projection_ty); - wf.compute(t.ty.into()); - } - &ty::PredicateKind::WellFormed(arg) => { - wf.compute(arg); - } - ty::PredicateKind::ObjectSafe(_) => {} - ty::PredicateKind::ClosureKind(..) => {} - &ty::PredicateKind::Subtype(ty::SubtypePredicate { a, b, a_is_expected: _ }) => { - wf.compute(a.into()); - wf.compute(b.into()); - } - &ty::PredicateKind::ConstEvaluatable(def, substs) => { - let obligations = wf.nominal_obligations(def.did, substs); - wf.out.extend(obligations); - - for arg in substs.iter() { + &ty::PredicateKind::Atom(atom) => match atom { + ty::PredicateAtom::Trait(t, _) => { + wf.compute_trait_ref(&t.trait_ref, Elaborate::None); + } + ty::PredicateAtom::RegionOutlives(..) => {} + ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty, _reg)) => { + wf.compute(ty.into()); + } + ty::PredicateAtom::Projection(t) => { + wf.compute_projection(t.projection_ty); + wf.compute(t.ty.into()); + } + ty::PredicateAtom::WellFormed(arg) => { wf.compute(arg); } - } - &ty::PredicateKind::ConstEquate(c1, c2) => { - wf.compute(c1.into()); - wf.compute(c2.into()); - } + ty::PredicateAtom::ObjectSafe(_) => {} + ty::PredicateAtom::ClosureKind(..) => {} + ty::PredicateAtom::Subtype(ty::SubtypePredicate { a, b, a_is_expected: _ }) => { + wf.compute(a.into()); + wf.compute(b.into()); + } + ty::PredicateAtom::ConstEvaluatable(def, substs) => { + let obligations = wf.nominal_obligations(def.did, substs); + wf.out.extend(obligations); + + for arg in substs.iter() { + wf.compute(arg); + } + } + ty::PredicateAtom::ConstEquate(c1, c2) => { + wf.compute(c1.into()); + wf.compute(c2.into()); + } + }, } wf.normalize() @@ -196,8 +198,8 @@ fn extend_cause_with_original_assoc_item_obligation<'tcx>( }; // It is fine to skip the binder as we don't care about regions here. - match pred.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::Projection(proj) => { + match pred.skip_binders() { + ty::PredicateAtom::Projection(proj) => { // The obligation comes not from the current `impl` nor the `trait` being implemented, // but rather from a "second order" obligation, where an associated type has a // projection coming from another associated type. See @@ -212,7 +214,7 @@ fn extend_cause_with_original_assoc_item_obligation<'tcx>( } } } - ty::PredicateKind::Trait(pred, _) => { + ty::PredicateAtom::Trait(pred, _) => { // An associated item obligation born out of the `trait` failed to be met. An example // can be seen in `ui/associated-types/point-at-type-on-obligation-failure-2.rs`. debug!("extended_cause_with_original_assoc_item_obligation trait proj {:?}", pred); @@ -317,7 +319,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { traits::Obligation::new( new_cause, param_env, - ty::PredicateKind::WellFormed(arg).to_predicate(tcx), + ty::PredicateAtom::WellFormed(arg).to_predicate(tcx), ) }), ); @@ -374,7 +376,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { let obligations = self.nominal_obligations(def.did, substs); self.out.extend(obligations); - let predicate = ty::PredicateKind::ConstEvaluatable(def, substs) + let predicate = ty::PredicateAtom::ConstEvaluatable(def, substs) .to_predicate(self.tcx()); let cause = self.cause(traits::MiscObligation); self.out.push(traits::Obligation::new( @@ -396,7 +398,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { self.out.push(traits::Obligation::new( cause, self.param_env, - ty::PredicateKind::WellFormed(resolved_constant.into()) + ty::PredicateAtom::WellFormed(resolved_constant.into()) .to_predicate(self.tcx()), )); } @@ -482,7 +484,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { self.out.push(traits::Obligation::new( cause, param_env, - ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(rty, r)) + ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(rty, r)) .to_predicate(self.tcx()), )); } @@ -573,7 +575,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { traits::Obligation::new( cause.clone(), param_env, - ty::PredicateKind::ObjectSafe(did).to_predicate(tcx), + ty::PredicateAtom::ObjectSafe(did).to_predicate(tcx), ) })); } @@ -599,7 +601,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { self.out.push(traits::Obligation::new( cause, param_env, - ty::PredicateKind::WellFormed(ty.into()).to_predicate(self.tcx()), + ty::PredicateAtom::WellFormed(ty.into()).to_predicate(self.tcx()), )); } else { // Yes, resolved, proceed with the result. diff --git a/src/librustc_traits/chalk/lowering.rs b/src/librustc_traits/chalk/lowering.rs index b781c2e2a6e9a..75785076d9ac1 100644 --- a/src/librustc_traits/chalk/lowering.rs +++ b/src/librustc_traits/chalk/lowering.rs @@ -79,13 +79,8 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment { // FIXME(chalk): forall - match predicate - .ignore_quantifiers_with_unbound_vars(interner.tcx) - .skip_binder() - .kind() - { - ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", predicate), - &ty::PredicateKind::Trait(predicate, _) => { + match predicate.bound_atom(interner.tcx).skip_binder() { + ty::PredicateAtom::Trait(predicate, _) => { let predicate = ty::Binder::bind(predicate); let (predicate, binders, _named_regions) = collect_bound_vars(interner, interner.tcx, &predicate); @@ -106,7 +101,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment { + ty::PredicateAtom::RegionOutlives(predicate) => { let predicate = ty::Binder::bind(predicate); let (predicate, binders, _named_regions) = collect_bound_vars(interner, interner.tcx, &predicate); @@ -131,8 +126,8 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment None, - &ty::PredicateKind::Projection(predicate) => { + ty::PredicateAtom::TypeOutlives(_) => None, + ty::PredicateAtom::Projection(predicate) => { let predicate = ty::Binder::bind(predicate); let (predicate, binders, _named_regions) = collect_bound_vars(interner, interner.tcx, &predicate); @@ -153,12 +148,12 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment { + ty::PredicateAtom::WellFormed(..) + | ty::PredicateAtom::ObjectSafe(..) + | ty::PredicateAtom::ClosureKind(..) + | ty::PredicateAtom::Subtype(..) + | ty::PredicateAtom::ConstEvaluatable(..) + | ty::PredicateAtom::ConstEquate(..) => { bug!("unexpected predicate {}", predicate) } } @@ -191,12 +186,11 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::InEnvironment LowerInto<'tcx, chalk_ir::GoalData>> for ty::Predicate<'tcx> { fn lower_into(self, interner: &RustInterner<'tcx>) -> chalk_ir::GoalData> { // FIXME(chalk): forall - match self.ignore_quantifiers_with_unbound_vars(interner.tcx).skip_binder().kind() { - ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", self), - &ty::PredicateKind::Trait(predicate, _) => { + match self.bound_atom(interner.tcx).skip_binder() { + ty::PredicateAtom::Trait(predicate, _) => { ty::Binder::bind(predicate).lower_into(interner) } - &ty::PredicateKind::RegionOutlives(predicate) => { + ty::PredicateAtom::RegionOutlives(predicate) => { let predicate = ty::Binder::bind(predicate); let (predicate, binders, _named_regions) = collect_bound_vars(interner, interner.tcx, &predicate); @@ -216,13 +210,13 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::GoalData>> for ty::Predi ) } // FIXME(chalk): TypeOutlives - ty::PredicateKind::TypeOutlives(_predicate) => { + ty::PredicateAtom::TypeOutlives(_predicate) => { chalk_ir::GoalData::All(chalk_ir::Goals::new(interner)) } - &ty::PredicateKind::Projection(predicate) => { + ty::PredicateAtom::Projection(predicate) => { ty::Binder::bind(predicate).lower_into(interner) } - ty::PredicateKind::WellFormed(arg) => match arg.unpack() { + ty::PredicateAtom::WellFormed(arg) => match arg.unpack() { GenericArgKind::Type(ty) => match ty.kind { // FIXME(chalk): In Chalk, a placeholder is WellFormed if it // `FromEnv`. However, when we "lower" Params, we don't update @@ -252,18 +246,18 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::GoalData>> for ty::Predi GenericArgKind::Lifetime(lt) => bug!("unexpect well formed predicate: {:?}", lt), }, - ty::PredicateKind::ObjectSafe(t) => chalk_ir::GoalData::DomainGoal( - chalk_ir::DomainGoal::ObjectSafe(chalk_ir::TraitId(*t)), + ty::PredicateAtom::ObjectSafe(t) => chalk_ir::GoalData::DomainGoal( + chalk_ir::DomainGoal::ObjectSafe(chalk_ir::TraitId(t)), ), // FIXME(chalk): other predicates // // We can defer this, but ultimately we'll want to express // some of these in terms of chalk operations. - ty::PredicateKind::ClosureKind(..) - | ty::PredicateKind::Subtype(..) - | ty::PredicateKind::ConstEvaluatable(..) - | ty::PredicateKind::ConstEquate(..) => { + ty::PredicateAtom::ClosureKind(..) + | ty::PredicateAtom::Subtype(..) + | ty::PredicateAtom::ConstEvaluatable(..) + | ty::PredicateAtom::ConstEquate(..) => { chalk_ir::GoalData::All(chalk_ir::Goals::new(interner)) } } @@ -561,9 +555,8 @@ impl<'tcx> LowerInto<'tcx, Option, ) -> Option>> { // FIXME(chalk): forall - match self.ignore_quantifiers_with_unbound_vars(interner.tcx).skip_binder().kind() { - ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", self), - &ty::PredicateKind::Trait(predicate, _) => { + match self.bound_atom(interner.tcx).skip_binder() { + ty::PredicateAtom::Trait(predicate, _) => { let predicate = ty::Binder::bind(predicate); let (predicate, binders, _named_regions) = collect_bound_vars(interner, interner.tcx, &predicate); @@ -573,7 +566,7 @@ impl<'tcx> LowerInto<'tcx, Option { + ty::PredicateAtom::RegionOutlives(predicate) => { let predicate = ty::Binder::bind(predicate); let (predicate, binders, _named_regions) = collect_bound_vars(interner, interner.tcx, &predicate); @@ -586,15 +579,15 @@ impl<'tcx> LowerInto<'tcx, Option None, - ty::PredicateKind::Projection(_predicate) => None, - ty::PredicateKind::WellFormed(_ty) => None, - - ty::PredicateKind::ObjectSafe(..) - | ty::PredicateKind::ClosureKind(..) - | ty::PredicateKind::Subtype(..) - | ty::PredicateKind::ConstEvaluatable(..) - | ty::PredicateKind::ConstEquate(..) => bug!("unexpected predicate {}", &self), + ty::PredicateAtom::TypeOutlives(_predicate) => None, + ty::PredicateAtom::Projection(_predicate) => None, + ty::PredicateAtom::WellFormed(_ty) => None, + + ty::PredicateAtom::ObjectSafe(..) + | ty::PredicateAtom::ClosureKind(..) + | ty::PredicateAtom::Subtype(..) + | ty::PredicateAtom::ConstEvaluatable(..) + | ty::PredicateAtom::ConstEquate(..) => bug!("unexpected predicate {}", &self), } } } diff --git a/src/librustc_traits/implied_outlives_bounds.rs b/src/librustc_traits/implied_outlives_bounds.rs index 26a44bb5c2f1b..de3096eac9b19 100644 --- a/src/librustc_traits/implied_outlives_bounds.rs +++ b/src/librustc_traits/implied_outlives_bounds.rs @@ -95,29 +95,31 @@ fn compute_implied_outlives_bounds<'tcx>( implied_bounds.extend(obligations.into_iter().flat_map(|obligation| { assert!(!obligation.has_escaping_bound_vars()); match obligation.predicate.kind() { - ty::PredicateKind::ForAll(..) => vec![], - ty::PredicateKind::Trait(..) - | ty::PredicateKind::Subtype(..) - | ty::PredicateKind::Projection(..) - | ty::PredicateKind::ClosureKind(..) - | ty::PredicateKind::ObjectSafe(..) - | ty::PredicateKind::ConstEvaluatable(..) - | ty::PredicateKind::ConstEquate(..) => vec![], - &ty::PredicateKind::WellFormed(arg) => { - wf_args.push(arg); - vec![] - } + &ty::PredicateKind::ForAll(..) => vec![], + &ty::PredicateKind::Atom(atom) => match atom { + ty::PredicateAtom::Trait(..) + | ty::PredicateAtom::Subtype(..) + | ty::PredicateAtom::Projection(..) + | ty::PredicateAtom::ClosureKind(..) + | ty::PredicateAtom::ObjectSafe(..) + | ty::PredicateAtom::ConstEvaluatable(..) + | ty::PredicateAtom::ConstEquate(..) => vec![], + ty::PredicateAtom::WellFormed(arg) => { + wf_args.push(arg); + vec![] + } - &ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(r_a, r_b)) => { - vec![OutlivesBound::RegionSubRegion(r_b, r_a)] - } + ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(r_a, r_b)) => { + vec![OutlivesBound::RegionSubRegion(r_b, r_a)] + } - &ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty_a, r_b)) => { - let ty_a = infcx.resolve_vars_if_possible(&ty_a); - let mut components = smallvec![]; - tcx.push_outlives_components(ty_a, &mut components); - implied_bounds_from_components(r_b, components) - } + ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty_a, r_b)) => { + let ty_a = infcx.resolve_vars_if_possible(&ty_a); + let mut components = smallvec![]; + tcx.push_outlives_components(ty_a, &mut components); + implied_bounds_from_components(r_b, components) + } + }, } })); } diff --git a/src/librustc_traits/normalize_erasing_regions.rs b/src/librustc_traits/normalize_erasing_regions.rs index cefc20972143a..83aee31a39f3c 100644 --- a/src/librustc_traits/normalize_erasing_regions.rs +++ b/src/librustc_traits/normalize_erasing_regions.rs @@ -40,16 +40,15 @@ fn normalize_generic_arg_after_erasing_regions<'tcx>( } fn not_outlives_predicate(p: &ty::Predicate<'tcx>) -> bool { - match p.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::RegionOutlives(..) | ty::PredicateKind::TypeOutlives(..) => false, - ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", p), - ty::PredicateKind::Trait(..) - | ty::PredicateKind::Projection(..) - | ty::PredicateKind::WellFormed(..) - | ty::PredicateKind::ObjectSafe(..) - | ty::PredicateKind::ClosureKind(..) - | ty::PredicateKind::Subtype(..) - | ty::PredicateKind::ConstEvaluatable(..) - | ty::PredicateKind::ConstEquate(..) => true, + match p.skip_binders() { + ty::PredicateAtom::RegionOutlives(..) | ty::PredicateAtom::TypeOutlives(..) => false, + ty::PredicateAtom::Trait(..) + | ty::PredicateAtom::Projection(..) + | ty::PredicateAtom::WellFormed(..) + | ty::PredicateAtom::ObjectSafe(..) + | ty::PredicateAtom::ClosureKind(..) + | ty::PredicateAtom::Subtype(..) + | ty::PredicateAtom::ConstEvaluatable(..) + | ty::PredicateAtom::ConstEquate(..) => true, } } diff --git a/src/librustc_traits/type_op.rs b/src/librustc_traits/type_op.rs index 9cc9a35b38b8a..139ed6dcd350c 100644 --- a/src/librustc_traits/type_op.rs +++ b/src/librustc_traits/type_op.rs @@ -140,7 +140,7 @@ impl AscribeUserTypeCx<'me, 'tcx> { self.relate(self_ty, Variance::Invariant, impl_self_ty)?; self.prove_predicate( - ty::PredicateKind::WellFormed(impl_self_ty.into()).to_predicate(self.tcx()), + ty::PredicateAtom::WellFormed(impl_self_ty.into()).to_predicate(self.tcx()), ); } @@ -155,7 +155,7 @@ impl AscribeUserTypeCx<'me, 'tcx> { // them? This would only be relevant if some input // type were ill-formed but did not appear in `ty`, // which...could happen with normalization... - self.prove_predicate(ty::PredicateKind::WellFormed(ty.into()).to_predicate(self.tcx())); + self.prove_predicate(ty::PredicateAtom::WellFormed(ty.into()).to_predicate(self.tcx())); Ok(()) } } diff --git a/src/librustc_ty/ty.rs b/src/librustc_ty/ty.rs index 48802893926b7..7f954dacf3e89 100644 --- a/src/librustc_ty/ty.rs +++ b/src/librustc_ty/ty.rs @@ -392,22 +392,22 @@ fn associated_type_projection_predicates( let predicates = item_predicates.filter_map(|obligation| { let pred = obligation.predicate; - match pred.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::Trait(tr, _) => { + match pred.skip_binders() { + ty::PredicateAtom::Trait(tr, _) => { if let ty::Projection(p) = tr.self_ty().kind { if p == assoc_item_ty { return Some(pred); } } } - ty::PredicateKind::Projection(proj) => { + ty::PredicateAtom::Projection(proj) => { if let ty::Projection(p) = proj.projection_ty.self_ty().kind { if p == assoc_item_ty { return Some(pred); } } } - ty::PredicateKind::TypeOutlives(outlives) => { + ty::PredicateAtom::TypeOutlives(outlives) => { if let ty::Projection(p) = outlives.0.kind { if p == assoc_item_ty { return Some(pred); @@ -443,15 +443,15 @@ fn opaque_type_projection_predicates( let filtered_predicates = predicates.filter_map(|obligation| { let pred = obligation.predicate; - match pred.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::Trait(tr, _) => { + match pred.skip_binders() { + ty::PredicateAtom::Trait(tr, _) => { if let ty::Opaque(opaque_def_id, opaque_substs) = tr.self_ty().kind { if opaque_def_id == def_id && opaque_substs == substs { return Some(pred); } } } - ty::PredicateKind::Projection(proj) => { + ty::PredicateAtom::Projection(proj) => { if let ty::Opaque(opaque_def_id, opaque_substs) = proj.projection_ty.self_ty().kind { if opaque_def_id == def_id && opaque_substs == substs { @@ -459,7 +459,7 @@ fn opaque_type_projection_predicates( } } } - ty::PredicateKind::TypeOutlives(outlives) => { + ty::PredicateAtom::TypeOutlives(outlives) => { if let ty::Opaque(opaque_def_id, opaque_substs) = outlives.0.kind { if opaque_def_id == def_id && opaque_substs == substs { return Some(pred); @@ -470,7 +470,7 @@ fn opaque_type_projection_predicates( } } // These can come from elaborating other predicates - ty::PredicateKind::RegionOutlives(_) => return None, + ty::PredicateAtom::RegionOutlives(_) => return None, _ => {} } tcx.sess.delay_span_bug( diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 841bfbfba7028..79d2f104a525f 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -1706,8 +1706,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { obligation.predicate ); - match obligation.predicate.ignore_quantifiers().skip_binder().kind() { - &ty::PredicateKind::Trait(pred, _) => { + match obligation.predicate.skip_binders() { + ty::PredicateAtom::Trait(pred, _) => { let pred = ty::Binder::bind(pred); associated_types.entry(span).or_default().extend( tcx.associated_items(pred.def_id()) @@ -1716,7 +1716,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { .map(|item| item.def_id), ); } - &ty::PredicateKind::Projection(pred) => { + ty::PredicateAtom::Projection(pred) => { let pred = ty::Binder::bind(pred); // A `Self` within the original bound will be substituted with a // `trait_object_dummy_self`, so check for that. diff --git a/src/librustc_typeck/check/closure.rs b/src/librustc_typeck/check/closure.rs index 6b7848c2eb982..255f611cfa357 100644 --- a/src/librustc_typeck/check/closure.rs +++ b/src/librustc_typeck/check/closure.rs @@ -206,8 +206,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { obligation.predicate ); - if let &ty::PredicateKind::Projection(proj_predicate) = - obligation.predicate.ignore_quantifiers().skip_binder().kind() + if let ty::PredicateAtom::Projection(proj_predicate) = + obligation.predicate.skip_binders() { // Given a Projection predicate, we can potentially infer // the complete signature. @@ -631,8 +631,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // where R is the return type we are expecting. This type `T` // will be our output. let output_ty = self.obligations_for_self_ty(ret_vid).find_map(|(_, obligation)| { - if let &ty::PredicateKind::Projection(proj_predicate) = - obligation.predicate.ignore_quantifiers().skip_binder().kind() + if let ty::PredicateAtom::Projection(proj_predicate) = + obligation.predicate.skip_binders() { self.deduce_future_output_from_projection( obligation.cause.span, diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index 2b6362230f836..c7e9b97e2dbde 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -582,8 +582,8 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { while !queue.is_empty() { let obligation = queue.remove(0); debug!("coerce_unsized resolve step: {:?}", obligation); - let trait_pred = match obligation.predicate.ignore_quantifiers().skip_binder().kind() { - &ty::PredicateKind::Trait(trait_pred, _) + let trait_pred = match obligation.predicate.skip_binders() { + ty::PredicateAtom::Trait(trait_pred, _) if traits.contains(&trait_pred.def_id()) => { if unsize_did == trait_pred.def_id() { diff --git a/src/librustc_typeck/check/dropck.rs b/src/librustc_typeck/check/dropck.rs index 4bbdb2a5ad3db..88c47b38ccc40 100644 --- a/src/librustc_typeck/check/dropck.rs +++ b/src/librustc_typeck/check/dropck.rs @@ -226,14 +226,11 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>( // could be extended easily also to the other `Predicate`. let predicate_matches_closure = |p: Predicate<'tcx>| { let mut relator: SimpleEqRelation<'tcx> = SimpleEqRelation::new(tcx, self_param_env); - match ( - predicate.ignore_quantifiers().skip_binder().kind(), - p.ignore_quantifiers().skip_binder().kind(), - ) { - (&ty::PredicateKind::Trait(a, _), &ty::PredicateKind::Trait(b, _)) => { + match (predicate.skip_binders(), p.skip_binders()) { + (ty::PredicateAtom::Trait(a, _), ty::PredicateAtom::Trait(b, _)) => { relator.relate(ty::Binder::bind(a), ty::Binder::bind(b)).is_ok() } - (&ty::PredicateKind::Projection(a), &ty::PredicateKind::Projection(b)) => { + (ty::PredicateAtom::Projection(a), ty::PredicateAtom::Projection(b)) => { relator.relate(ty::Binder::bind(a), ty::Binder::bind(b)).is_ok() } _ => predicate == p, diff --git a/src/librustc_typeck/check/method/confirm.rs b/src/librustc_typeck/check/method/confirm.rs index b97dd8bf348f5..41e37ee975252 100644 --- a/src/librustc_typeck/check/method/confirm.rs +++ b/src/librustc_typeck/check/method/confirm.rs @@ -448,24 +448,21 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> { traits::elaborate_predicates(self.tcx, predicates.predicates.iter().copied()) // We don't care about regions here. - .filter_map(|obligation| { - match obligation.predicate.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::Trait(trait_pred, _) - if trait_pred.def_id() == sized_def_id => - { - let span = - predicates - .predicates - .iter() - .zip(predicates.spans.iter()) - .find_map(|(p, span)| { - if *p == obligation.predicate { Some(*span) } else { None } - }) - .unwrap_or(rustc_span::DUMMY_SP); - Some((trait_pred, span)) - } - _ => None, + .filter_map(|obligation| match obligation.predicate.skip_binders() { + ty::PredicateAtom::Trait(trait_pred, _) if trait_pred.def_id() == sized_def_id => { + let span = predicates + .predicates + .iter() + .zip(predicates.spans.iter()) + .find_map( + |(p, span)| { + if *p == obligation.predicate { Some(*span) } else { None } + }, + ) + .unwrap_or(rustc_span::DUMMY_SP); + Some((trait_pred, span)) } + _ => None, }) .find_map(|(trait_pred, span)| match trait_pred.self_ty().kind { ty::Dynamic(..) => Some(span), diff --git a/src/librustc_typeck/check/method/mod.rs b/src/librustc_typeck/check/method/mod.rs index 64dce3e1738e3..c9a4df0317abc 100644 --- a/src/librustc_typeck/check/method/mod.rs +++ b/src/librustc_typeck/check/method/mod.rs @@ -399,7 +399,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { obligations.push(traits::Obligation::new( cause, self.param_env, - ty::PredicateKind::WellFormed(method_ty.into()).to_predicate(tcx), + ty::PredicateAtom::WellFormed(method_ty.into()).to_predicate(tcx), )); let callee = MethodCallee { def_id, substs: trait_ref.substs, sig: fn_sig }; diff --git a/src/librustc_typeck/check/method/probe.rs b/src/librustc_typeck/check/method/probe.rs index 9c5e3cbc93844..e569d1c443a69 100644 --- a/src/librustc_typeck/check/method/probe.rs +++ b/src/librustc_typeck/check/method/probe.rs @@ -798,24 +798,23 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { // FIXME: do we want to commit to this behavior for param bounds? debug!("assemble_inherent_candidates_from_param(param_ty={:?})", param_ty); - let bounds = self.param_env.caller_bounds().iter().filter_map(|predicate| match predicate - .kind() + let bounds = self.param_env.caller_bounds().iter().map(ty::Predicate::skip_binders).filter_map(|predicate| match predicate { - ty::PredicateKind::Trait(ref trait_predicate, _) => { - match trait_predicate.skip_binder().trait_ref.self_ty().kind { - ty::Param(ref p) if *p == param_ty => Some(trait_predicate.to_poly_trait_ref()), + ty::PredicateAtom::Trait(trait_predicate, _) => { + match trait_predicate.trait_ref.self_ty().kind { + ty::Param(ref p) if *p == param_ty => Some(ty::Binder::bind(trait_predicate.trait_ref)), _ => None, } } - ty::PredicateKind::Subtype(..) - | ty::PredicateKind::Projection(..) - | ty::PredicateKind::RegionOutlives(..) - | ty::PredicateKind::WellFormed(..) - | ty::PredicateKind::ObjectSafe(..) - | ty::PredicateKind::ClosureKind(..) - | ty::PredicateKind::TypeOutlives(..) - | ty::PredicateKind::ConstEvaluatable(..) - | ty::PredicateKind::ConstEquate(..) => None, + ty::PredicateAtom::Subtype(..) + | ty::PredicateAtom::Projection(..) + | ty::PredicateAtom::RegionOutlives(..) + | ty::PredicateAtom::WellFormed(..) + | ty::PredicateAtom::ObjectSafe(..) + | ty::PredicateAtom::ClosureKind(..) + | ty::PredicateAtom::TypeOutlives(..) + | ty::PredicateAtom::ConstEvaluatable(..) + | ty::PredicateAtom::ConstEquate(..) => None, }); self.elaborate_bounds(bounds, |this, poly_trait_ref, item| { diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index cedd7926290ea..07cc8332b84cf 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -576,10 +576,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // this is kind of ugly. |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::PredicateKind::Trait(p, _)) = ( - &self_ty.kind, - parent_pred.ignore_quantifiers().skip_binder().kind(), - ) { + if let (ty::Param(_), ty::PredicateAtom::Trait(p, _)) = + (&self_ty.kind, parent_pred.skip_binders()) + { if let ty::Adt(def, _) = p.trait_ref.self_ty().kind { let node = def.did.as_local().map(|def_id| { self.tcx.hir().get(self.tcx.hir().as_local_hir_id(def_id)) @@ -631,8 +630,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } }; let mut format_pred = |pred: ty::Predicate<'tcx>| { - match pred.ignore_quantifiers().skip_binder().kind() { - &ty::PredicateKind::Projection(pred) => { + match pred.skip_binders() { + ty::PredicateAtom::Projection(pred) => { let pred = ty::Binder::bind(pred); // `::Item = String`. let trait_ref = @@ -651,7 +650,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { bound_span_label(trait_ref.self_ty(), &obligation, &quiet); Some((obligation, trait_ref.self_ty())) } - &ty::PredicateKind::Trait(poly_trait_ref, _) => { + ty::PredicateAtom::Trait(poly_trait_ref, _) => { let poly_trait_ref = ty::Binder::bind(poly_trait_ref); let p = poly_trait_ref.skip_binder().trait_ref; let self_ty = p.self_ty(); @@ -959,11 +958,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // implementing a trait would be legal but is rejected // here). unsatisfied_predicates.iter().all(|(p, _)| { - match p.ignore_quantifiers().skip_binder().kind() { + match p.skip_binders() { // Hide traits if they are present in predicates as they can be fixed without // having to implement them. - ty::PredicateKind::Trait(t, _) => t.def_id() == info.def_id, - ty::PredicateKind::Projection(p) => { + ty::PredicateAtom::Trait(t, _) => t.def_id() == info.def_id, + ty::PredicateAtom::Projection(p) => { p.projection_ty.item_def_id == info.def_id } _ => false, diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index f54a59486656b..61520e2923650 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -2400,8 +2400,8 @@ fn bounds_from_generic_predicates<'tcx>( let mut projections = vec![]; for (predicate, _) in predicates.predicates { debug!("predicate {:?}", predicate); - match predicate.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::Trait(trait_predicate, _) => { + match predicate.skip_binders() { + ty::PredicateAtom::Trait(trait_predicate, _) => { let entry = types.entry(trait_predicate.self_ty()).or_default(); let def_id = trait_predicate.def_id(); if Some(def_id) != tcx.lang_items().sized_trait() { @@ -2410,7 +2410,7 @@ fn bounds_from_generic_predicates<'tcx>( entry.push(trait_predicate.def_id()); } } - ty::PredicateKind::Projection(projection_pred) => { + ty::PredicateAtom::Projection(projection_pred) => { projections.push(ty::Binder::bind(projection_pred)); } _ => {} @@ -2938,9 +2938,9 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> { parent: None, predicates: tcx.arena.alloc_from_iter( self.param_env.caller_bounds().iter().filter_map(|predicate| { - match predicate.kind() { - ty::PredicateKind::Trait(ref data, _) - if data.skip_binder().self_ty().is_param(index) => + match predicate.skip_binders() { + ty::PredicateAtom::Trait(data, _) + if data.self_ty().is_param(index) => { // HACK(eddyb) should get the original `Span`. let span = tcx.def_span(def_id); @@ -3612,7 +3612,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.register_predicate(traits::Obligation::new( cause, self.param_env, - ty::PredicateKind::WellFormed(arg).to_predicate(self.tcx), + ty::PredicateAtom::WellFormed(arg).to_predicate(self.tcx), )); } @@ -3894,23 +3894,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .pending_obligations() .into_iter() .filter_map(move |obligation| { - match obligation.predicate.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::ForAll(_) => { - bug!("unexpected predicate: {:?}", obligation.predicate) - } - &ty::PredicateKind::Projection(data) => { + match obligation.predicate.skip_binders() { + ty::PredicateAtom::Projection(data) => { Some((ty::Binder::bind(data).to_poly_trait_ref(self.tcx), obligation)) } - &ty::PredicateKind::Trait(data, _) => { + ty::PredicateAtom::Trait(data, _) => { Some((ty::Binder::bind(data).to_poly_trait_ref(), obligation)) } - ty::PredicateKind::Subtype(..) => None, - ty::PredicateKind::RegionOutlives(..) => None, - ty::PredicateKind::TypeOutlives(..) => None, - ty::PredicateKind::WellFormed(..) => None, - ty::PredicateKind::ObjectSafe(..) => None, - ty::PredicateKind::ConstEvaluatable(..) => None, - ty::PredicateKind::ConstEquate(..) => None, + ty::PredicateAtom::Subtype(..) => None, + ty::PredicateAtom::RegionOutlives(..) => None, + ty::PredicateAtom::TypeOutlives(..) => None, + ty::PredicateAtom::WellFormed(..) => None, + ty::PredicateAtom::ObjectSafe(..) => None, + ty::PredicateAtom::ConstEvaluatable(..) => None, + ty::PredicateAtom::ConstEquate(..) => None, // N.B., this predicate is created by breaking down a // `ClosureType: FnFoo()` predicate, where // `ClosureType` represents some `Closure`. It can't @@ -3919,7 +3916,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // this closure yet; this is exactly why the other // code is looking for a self type of a unresolved // inference variable. - ty::PredicateKind::ClosureKind(..) => None, + ty::PredicateAtom::ClosureKind(..) => None, } }) .filter(move |(tr, _)| self.self_type_matches_expected_vid(*tr, ty_var_root)) @@ -4249,8 +4246,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { continue; } - if let ty::PredicateKind::Trait(predicate, _) = - error.obligation.predicate.ignore_quantifiers().skip_binder().kind() + if let ty::PredicateAtom::Trait(predicate, _) = + error.obligation.predicate.skip_binders() { // Collect the argument position for all arguments that could have caused this // `FulfillmentError`. @@ -4298,8 +4295,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if let hir::ExprKind::Path(qpath) = &path.kind { if let hir::QPath::Resolved(_, path) = &qpath { for error in errors { - if let ty::PredicateKind::Trait(predicate, _) = - error.obligation.predicate.ignore_quantifiers().skip_binder().kind() + if let ty::PredicateAtom::Trait(predicate, _) = + error.obligation.predicate.skip_binders() { // If any of the type arguments in this path segment caused the // `FullfillmentError`, point at its span (#61860). @@ -5372,7 +5369,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { item_def_id, }; - let predicate = ty::PredicateKind::Projection(ty::ProjectionPredicate { + let predicate = ty::PredicateAtom::Projection(ty::ProjectionPredicate { projection_ty, ty: expected, }) diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index dabae6cbc4137..50d9a1ebd2c24 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -429,7 +429,7 @@ fn check_type_defn<'tcx, F>( fcx.register_predicate(traits::Obligation::new( cause, fcx.param_env, - ty::PredicateKind::ConstEvaluatable( + ty::PredicateAtom::ConstEvaluatable( ty::WithOptConstParam::unknown(discr_def_id.to_def_id()), discr_substs, ) diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index e5d2c569004e1..d906c5c05c019 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -552,8 +552,8 @@ fn type_param_predicates( let extra_predicates = extend.into_iter().chain( icx.type_parameter_bounds_in_generics(ast_generics, param_id, ty, OnlySelfBounds(true)) .into_iter() - .filter(|(predicate, _)| match predicate.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::Trait(data, _) => data.self_ty().is_param(index), + .filter(|(predicate, _)| match predicate.skip_binders() { + ty::PredicateAtom::Trait(data, _) => data.self_ty().is_param(index), _ => false, }), ); @@ -1004,7 +1004,7 @@ fn super_predicates_of(tcx: TyCtxt<'_>, trait_def_id: DefId) -> ty::GenericPredi // which will, in turn, reach indirect supertraits. for &(pred, span) in superbounds { debug!("superbound: {:?}", pred); - if let ty::PredicateKind::Trait(bound, _) = pred.ignore_quantifiers().skip_binder().kind() { + if let ty::PredicateAtom::Trait(bound, _) = pred.skip_binders() { tcx.at(span).super_predicates_of(bound.def_id()); } } @@ -1960,7 +1960,7 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat &hir::GenericBound::Outlives(ref lifetime) => { let region = AstConv::ast_region_to_region(&icx, lifetime, None); predicates.push(( - ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, region)) + ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty, region)) .to_predicate(tcx) .potentially_quantified(tcx, ty::PredicateKind::ForAll), lifetime.span, @@ -1979,7 +1979,7 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat } _ => bug!(), }; - let pred = ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(r1, r2)) + let pred = ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(r1, r2)) .to_predicate(icx.tcx); (pred.potentially_quantified(icx.tcx, ty::PredicateKind::ForAll), span) @@ -2110,7 +2110,7 @@ fn predicates_from_bound<'tcx>( } hir::GenericBound::Outlives(ref lifetime) => { let region = astconv.ast_region_to_region(lifetime, None); - let pred = ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(param_ty, region)) + let pred = ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(param_ty, region)) .to_predicate(astconv.tcx()) .potentially_quantified(astconv.tcx(), ty::PredicateKind::ForAll); vec![(pred, lifetime.span)] diff --git a/src/librustc_typeck/constrained_generic_params.rs b/src/librustc_typeck/constrained_generic_params.rs index 7936d003f54ca..7c80315ee194d 100644 --- a/src/librustc_typeck/constrained_generic_params.rs +++ b/src/librustc_typeck/constrained_generic_params.rs @@ -182,9 +182,7 @@ pub fn setup_constraining_predicates<'tcx>( for j in i..predicates.len() { // Note that we don't have to care about binders here, // as the impl trait ref never contains any late-bound regions. - if let ty::PredicateKind::Projection(projection) = - predicates[j].0.ignore_quantifiers().skip_binder().kind() - { + if let ty::PredicateAtom::Projection(projection) = predicates[j].0.skip_binders() { // Special case: watch out for some kind of sneaky attempt // to project out an associated type defined by this very // trait. diff --git a/src/librustc_typeck/impl_wf_check/min_specialization.rs b/src/librustc_typeck/impl_wf_check/min_specialization.rs index 0cc99a7a54e9e..8257c6ce92547 100644 --- a/src/librustc_typeck/impl_wf_check/min_specialization.rs +++ b/src/librustc_typeck/impl_wf_check/min_specialization.rs @@ -198,9 +198,7 @@ fn unconstrained_parent_impl_substs<'tcx>( // the functions in `cgp` add the constrained parameters to a list of // unconstrained parameters. for (predicate, _) in impl_generic_predicates.predicates.iter() { - if let ty::PredicateKind::Projection(proj) = - predicate.ignore_quantifiers().skip_binder().kind() - { + if let ty::PredicateAtom::Projection(proj) = predicate.skip_binders() { let projection_ty = proj.projection_ty; let projected_ty = proj.ty; @@ -361,13 +359,13 @@ fn check_predicates<'tcx>( fn check_specialization_on<'tcx>(tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tcx>, span: Span) { debug!("can_specialize_on(predicate = {:?})", predicate); - match predicate.ignore_quantifiers().skip_binder().kind() { + match predicate.skip_binders() { // Global predicates are either always true or always false, so we // are fine to specialize on. _ if predicate.is_global() => (), // We allow specializing on explicitly marked traits with no associated // items. - ty::PredicateKind::Trait(pred, hir::Constness::NotConst) => { + ty::PredicateAtom::Trait(pred, hir::Constness::NotConst) => { if !matches!( trait_predicate_kind(tcx, predicate), Some(TraitSpecializationKind::Marker) @@ -394,20 +392,19 @@ fn trait_predicate_kind<'tcx>( tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tcx>, ) -> Option { - match predicate.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::ForAll(_) => bug!("unexpected predicate: {:?}", predicate), - ty::PredicateKind::Trait(pred, hir::Constness::NotConst) => { + match predicate.skip_binders() { + ty::PredicateAtom::Trait(pred, hir::Constness::NotConst) => { Some(tcx.trait_def(pred.def_id()).specialization_kind) } - ty::PredicateKind::Trait(_, hir::Constness::Const) - | ty::PredicateKind::RegionOutlives(_) - | ty::PredicateKind::TypeOutlives(_) - | ty::PredicateKind::Projection(_) - | ty::PredicateKind::WellFormed(_) - | ty::PredicateKind::Subtype(_) - | ty::PredicateKind::ObjectSafe(_) - | ty::PredicateKind::ClosureKind(..) - | ty::PredicateKind::ConstEvaluatable(..) - | ty::PredicateKind::ConstEquate(..) => None, + ty::PredicateAtom::Trait(_, hir::Constness::Const) + | ty::PredicateAtom::RegionOutlives(_) + | ty::PredicateAtom::TypeOutlives(_) + | ty::PredicateAtom::Projection(_) + | ty::PredicateAtom::WellFormed(_) + | ty::PredicateAtom::Subtype(_) + | ty::PredicateAtom::ObjectSafe(_) + | ty::PredicateAtom::ClosureKind(..) + | ty::PredicateAtom::ConstEvaluatable(..) + | ty::PredicateAtom::ConstEquate(..) => None, } } diff --git a/src/librustc_typeck/outlives/explicit.rs b/src/librustc_typeck/outlives/explicit.rs index bbde0b63cbbba..135960a4c1114 100644 --- a/src/librustc_typeck/outlives/explicit.rs +++ b/src/librustc_typeck/outlives/explicit.rs @@ -29,10 +29,8 @@ impl<'tcx> ExplicitPredicatesMap<'tcx> { // process predicates and convert to `RequiredPredicates` entry, see below for &(predicate, span) in predicates.predicates { - match predicate.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::ForAll(_) => bug!("unepected predicate: {:?}", predicate), - - ty::PredicateKind::TypeOutlives(OutlivesPredicate(ref ty, ref reg)) => { + match predicate.skip_binders() { + ty::PredicateAtom::TypeOutlives(OutlivesPredicate(ref ty, ref reg)) => { insert_outlives_predicate( tcx, (*ty).into(), @@ -42,7 +40,7 @@ impl<'tcx> ExplicitPredicatesMap<'tcx> { ) } - ty::PredicateKind::RegionOutlives(OutlivesPredicate(ref reg1, ref reg2)) => { + ty::PredicateAtom::RegionOutlives(OutlivesPredicate(ref reg1, ref reg2)) => { insert_outlives_predicate( tcx, (*reg1).into(), @@ -52,14 +50,14 @@ impl<'tcx> ExplicitPredicatesMap<'tcx> { ) } - ty::PredicateKind::Trait(..) - | ty::PredicateKind::Projection(..) - | ty::PredicateKind::WellFormed(..) - | ty::PredicateKind::ObjectSafe(..) - | ty::PredicateKind::ClosureKind(..) - | ty::PredicateKind::Subtype(..) - | ty::PredicateKind::ConstEvaluatable(..) - | ty::PredicateKind::ConstEquate(..) => (), + ty::PredicateAtom::Trait(..) + | ty::PredicateAtom::Projection(..) + | ty::PredicateAtom::WellFormed(..) + | ty::PredicateAtom::ObjectSafe(..) + | ty::PredicateAtom::ClosureKind(..) + | ty::PredicateAtom::Subtype(..) + | ty::PredicateAtom::ConstEvaluatable(..) + | ty::PredicateAtom::ConstEquate(..) => (), } } diff --git a/src/librustc_typeck/outlives/mod.rs b/src/librustc_typeck/outlives/mod.rs index 56badb324c73e..823a0235b176d 100644 --- a/src/librustc_typeck/outlives/mod.rs +++ b/src/librustc_typeck/outlives/mod.rs @@ -31,8 +31,12 @@ fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[(ty::Predicate let mut pred: Vec = predicates .iter() .map(|(out_pred, _)| match out_pred.kind() { - ty::PredicateKind::RegionOutlives(p) => p.to_string(), - ty::PredicateKind::TypeOutlives(p) => p.to_string(), + ty::PredicateKind::Atom(ty::PredicateAtom::RegionOutlives(p)) => { + p.to_string() + } + ty::PredicateKind::Atom(ty::PredicateAtom::TypeOutlives(p)) => { + p.to_string() + } err => bug!("unexpected predicate {:?}", err), }) .collect(); @@ -85,13 +89,13 @@ fn inferred_outlives_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> CratePredica |(ty::OutlivesPredicate(kind1, region2), &span)| { match kind1.unpack() { GenericArgKind::Type(ty1) => Some(( - ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty1, region2)) + ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty1, region2)) .to_predicate(tcx) .potentially_quantified(tcx, ty::PredicateKind::ForAll), span, )), GenericArgKind::Lifetime(region1) => Some(( - ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate( + ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate( region1, region2, )) .to_predicate(tcx) diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs index 498fa25836c95..98d8f100b27d9 100644 --- a/src/librustdoc/clean/auto_trait.rs +++ b/src/librustdoc/clean/auto_trait.rs @@ -315,11 +315,11 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> { tcx: TyCtxt<'tcx>, pred: ty::Predicate<'tcx>, ) -> FxHashSet { - let regions = match pred.ignore_quantifiers().skip_binder().kind() { - &ty::PredicateKind::Trait(poly_trait_pred, _) => { + let regions = match pred.skip_binders() { + ty::PredicateAtom::Trait(poly_trait_pred, _) => { tcx.collect_referenced_late_bound_regions(&ty::Binder::bind(poly_trait_pred)) } - &ty::PredicateKind::Projection(poly_proj_pred) => { + ty::PredicateAtom::Projection(poly_proj_pred) => { tcx.collect_referenced_late_bound_regions(&ty::Binder::bind(poly_proj_pred)) } _ => return FxHashSet::default(), @@ -465,8 +465,8 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> { .iter() .filter(|p| { !orig_bounds.contains(p) - || match p.ignore_quantifiers().skip_binder().kind() { - ty::PredicateKind::Trait(pred, _) => pred.def_id() == sized_trait, + || match p.skip_binders() { + ty::PredicateAtom::Trait(pred, _) => pred.def_id() == sized_trait, _ => false, } }) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index a86ee12fa99b1..728453deecff6 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -480,19 +480,18 @@ impl Clean for hir::WherePredicate<'_> { impl<'a> Clean> for ty::Predicate<'a> { fn clean(&self, cx: &DocContext<'_>) -> Option { - match self.ignore_quantifiers().skip_binder().kind() { - &ty::PredicateKind::Trait(pred, _) => Some(ty::Binder::bind(pred).clean(cx)), - &ty::PredicateKind::Subtype(pred) => Some(ty::Binder::bind(pred).clean(cx)), - &ty::PredicateKind::RegionOutlives(pred) => ty::Binder::bind(pred).clean(cx), - &ty::PredicateKind::TypeOutlives(pred) => ty::Binder::bind(pred).clean(cx), - &ty::PredicateKind::Projection(pred) => Some(ty::Binder::bind(pred).clean(cx)), + match self.skip_binders() { + ty::PredicateAtom::Trait(pred, _) => Some(ty::Binder::bind(pred).clean(cx)), + ty::PredicateAtom::Subtype(pred) => Some(ty::Binder::bind(pred).clean(cx)), + ty::PredicateAtom::RegionOutlives(pred) => ty::Binder::bind(pred).clean(cx), + ty::PredicateAtom::TypeOutlives(pred) => ty::Binder::bind(pred).clean(cx), + ty::PredicateAtom::Projection(pred) => Some(ty::Binder::bind(pred).clean(cx)), - ty::PredicateKind::ForAll(_) => panic!("unexpected predicate: {:?}", self), - ty::PredicateKind::WellFormed(..) - | ty::PredicateKind::ObjectSafe(..) - | ty::PredicateKind::ClosureKind(..) - | ty::PredicateKind::ConstEvaluatable(..) - | ty::PredicateKind::ConstEquate(..) => panic!("not user writable"), + ty::PredicateAtom::WellFormed(..) + | ty::PredicateAtom::ObjectSafe(..) + | ty::PredicateAtom::ClosureKind(..) + | ty::PredicateAtom::ConstEvaluatable(..) + | ty::PredicateAtom::ConstEquate(..) => panic!("not user writable"), } } } @@ -755,18 +754,18 @@ impl<'a, 'tcx> Clean for (&'a ty::Generics, ty::GenericPredicates<'tcx .flat_map(|(p, _)| { let mut projection = None; let param_idx = (|| { - match p.ignore_quantifiers().skip_binder().kind() { - &ty::PredicateKind::Trait(pred, _constness) => { + match p.skip_binders() { + ty::PredicateAtom::Trait(pred, _constness) => { if let ty::Param(param) = pred.self_ty().kind { return Some(param.index); } } - &ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, _reg)) => { + ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty, _reg)) => { if let ty::Param(param) = ty.kind { return Some(param.index); } } - &ty::PredicateKind::Projection(p) => { + ty::PredicateAtom::Projection(p) => { if let ty::Param(param) = p.projection_ty.self_ty().kind { projection = Some(ty::Binder::bind(p)); return Some(param.index); @@ -1663,15 +1662,11 @@ impl<'tcx> Clean for Ty<'tcx> { .filter_map(|predicate| { // Note: The substs of opaque types can contain unbound variables, // meaning that we have to use `ignore_quantifiers_with_unbound_vars` here. - let trait_ref = match predicate - .ignore_quantifiers_with_unbound_vars(cx.tcx) - .skip_binder() - .kind() - { - ty::PredicateKind::Trait(tr, _constness) => { + let trait_ref = match predicate.bound_atom(cx.tcx).skip_binder() { + ty::PredicateAtom::Trait(tr, _constness) => { ty::Binder::bind(tr.trait_ref) } - ty::PredicateKind::TypeOutlives(pred) => { + ty::PredicateAtom::TypeOutlives(pred) => { if let Some(r) = pred.1.clean(cx) { regions.push(GenericBound::Outlives(r)); } @@ -1691,10 +1686,9 @@ impl<'tcx> Clean for Ty<'tcx> { .predicates .iter() .filter_map(|pred| { - if let ty::PredicateKind::Projection(proj) = pred - .ignore_quantifiers_with_unbound_vars(cx.tcx) - .skip_binder() - .kind() + // We never rebind `proj`, so `skip_binders_unchecked` is safe here. + if let ty::PredicateAtom::Projection(proj) = + pred.skip_binders_unchecked() { if proj.projection_ty.trait_ref(cx.tcx) == trait_ref.skip_binder() diff --git a/src/librustdoc/clean/simplify.rs b/src/librustdoc/clean/simplify.rs index 98e461fe69563..0f995a60c22fd 100644 --- a/src/librustdoc/clean/simplify.rs +++ b/src/librustdoc/clean/simplify.rs @@ -141,9 +141,7 @@ fn trait_is_same_or_supertrait(cx: &DocContext<'_>, child: DefId, trait_: DefId) .predicates .iter() .filter_map(|(pred, _)| { - if let ty::PredicateKind::Trait(pred, _) = - pred.ignore_quantifiers().skip_binder().kind() - { + if let ty::PredicateAtom::Trait(pred, _) = pred.skip_binders() { if pred.trait_ref.self_ty() == self_ty { Some(pred.def_id()) } else { None } } else { None diff --git a/src/tools/clippy/clippy_lints/src/future_not_send.rs b/src/tools/clippy/clippy_lints/src/future_not_send.rs index 7eeb6a75ea7b0..0fdb5b8c2a48e 100644 --- a/src/tools/clippy/clippy_lints/src/future_not_send.rs +++ b/src/tools/clippy/clippy_lints/src/future_not_send.rs @@ -3,7 +3,7 @@ use rustc_hir::intravisit::FnKind; use rustc_hir::{Body, FnDecl, HirId}; use rustc_infer::infer::TyCtxtInferExt; use rustc_lint::{LateContext, LateLintPass}; -use rustc_middle::ty::{Opaque, PredicateKind::Trait}; +use rustc_middle::ty::{Opaque, PredicateAtom::Trait}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::{sym, Span}; use rustc_trait_selection::traits::error_reporting::suggestions::InferCtxtExt; @@ -91,7 +91,7 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend { cx.tcx.infer_ctxt().enter(|infcx| { for FulfillmentError { obligation, .. } in send_errors { infcx.maybe_note_obligation_cause_for_async_await(db, &obligation); - if let Trait(trait_pred, _) = obligation.predicate.ignore_quantifiers().skip_binder().kind() { + if let Trait(trait_pred, _) = obligation.predicate.skip_binders() { db.note(&format!( "`{}` doesn't implement `{}`", trait_pred.self_ty(), diff --git a/src/tools/clippy/clippy_lints/src/methods/mod.rs b/src/tools/clippy/clippy_lints/src/methods/mod.rs index a450d5f16f8cb..2c70183d87666 100644 --- a/src/tools/clippy/clippy_lints/src/methods/mod.rs +++ b/src/tools/clippy/clippy_lints/src/methods/mod.rs @@ -1559,7 +1559,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods { if let ty::Opaque(def_id, _) = ret_ty.kind { // one of the associated types must be Self for &(predicate, _span) in cx.tcx.predicates_of(def_id).predicates { - if let ty::PredicateKind::Projection(projection_predicate) = predicate.ignore_quantifiers().skip_binder().kind() { + if let ty::PredicateAtom::Projection(projection_predicate) = predicate.skip_binders() { // walk the associated type and check for Self if contains_self_ty(projection_predicate.ty) { return; diff --git a/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs b/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs index e39fb23365a27..0957787774498 100644 --- a/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs +++ b/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs @@ -115,7 +115,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue { .filter(|p| !p.is_global()) .filter_map(|obligation| { // Note that we do not want to deal with qualified predicates here. - if let ty::PredicateKind::Trait(pred, _) = obligation.predicate.kind() { + if let ty::PredicateKind::Atom(ty::PredicateAtom::Trait(pred, _)) = obligation.predicate.kind() { if pred.def_id() == sized_trait { return None; } diff --git a/src/tools/clippy/clippy_lints/src/utils/mod.rs b/src/tools/clippy/clippy_lints/src/utils/mod.rs index c4603418ee3c6..655b1133cf74f 100644 --- a/src/tools/clippy/clippy_lints/src/utils/mod.rs +++ b/src/tools/clippy/clippy_lints/src/utils/mod.rs @@ -1263,7 +1263,7 @@ pub fn is_must_use_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool { ty::Tuple(ref substs) => substs.types().any(|ty| is_must_use_ty(cx, ty)), ty::Opaque(ref def_id, _) => { for (predicate, _) in cx.tcx.predicates_of(*def_id).predicates { - if let ty::PredicateKind::Trait(trait_predicate, _) = predicate.ignore_quantifiers().skip_binder().kind() { + if let ty::PredicateAtom::Trait(trait_predicate, _) = predicate.skip_binders() { if must_use_attr(&cx.tcx.get_attrs(trait_predicate.trait_ref.def_id)).is_some() { return true; } From cd9743b4d49bbc9c8d84f419ecf37c93047d7ec3 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sat, 18 Jul 2020 11:46:38 +0200 Subject: [PATCH 23/28] directly contain `PredicateAtom` in `PredicateKind::ForAll` --- .../infer/canonical/query_response.rs | 2 - src/librustc_middle/ty/flags.rs | 81 +++++++++---------- src/librustc_middle/ty/mod.rs | 64 +++++++-------- src/librustc_middle/ty/print/pretty.rs | 74 ++++++++--------- .../traits/fulfill.rs | 1 + src/librustc_trait_selection/traits/wf.rs | 67 +++++++-------- src/librustc_typeck/check/method/probe.rs | 40 ++++----- src/librustc_typeck/check/mod.rs | 5 +- src/librustc_typeck/collect.rs | 5 +- src/librustc_typeck/outlives/mod.rs | 4 +- 10 files changed, 163 insertions(+), 180 deletions(-) diff --git a/src/librustc_infer/infer/canonical/query_response.rs b/src/librustc_infer/infer/canonical/query_response.rs index 8406eb9bc175b..0dbebac7e36c6 100644 --- a/src/librustc_infer/infer/canonical/query_response.rs +++ b/src/librustc_infer/infer/canonical/query_response.rs @@ -531,11 +531,9 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> { let predicate = match k1.unpack() { GenericArgKind::Lifetime(r1) => { ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(r1, r2)) - .to_predicate(self.tcx) } GenericArgKind::Type(t1) => { ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(t1, r2)) - .to_predicate(self.tcx) } GenericArgKind::Const(..) => { // Consts cannot outlive one another, so we don't expect to diff --git a/src/librustc_middle/ty/flags.rs b/src/librustc_middle/ty/flags.rs index 7452089658feb..27f50c240db67 100644 --- a/src/librustc_middle/ty/flags.rs +++ b/src/librustc_middle/ty/flags.rs @@ -201,55 +201,54 @@ impl FlagComputation { } } - fn add_predicate(&mut self, pred: ty::Predicate<'_>) { - self.add_flags(pred.inner.flags); - self.add_exclusive_binder(pred.inner.outer_exclusive_binder); - } - fn add_predicate_kind(&mut self, kind: &ty::PredicateKind<'_>) { match kind { ty::PredicateKind::ForAll(binder) => { let mut computation = FlagComputation::new(); - computation.add_predicate(binder.skip_binder()); + computation.add_predicate_atom(binder.skip_binder()); self.add_bound_computation(computation); } - &ty::PredicateKind::Atom(atom) => match atom { - ty::PredicateAtom::Trait(trait_pred, _constness) => { - self.add_substs(trait_pred.trait_ref.substs); - } - ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(a, b)) => { - self.add_region(a); - self.add_region(b); - } - ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty, region)) => { - self.add_ty(ty); - self.add_region(region); - } - ty::PredicateAtom::Subtype(ty::SubtypePredicate { a_is_expected: _, a, b }) => { - self.add_ty(a); - self.add_ty(b); - } - ty::PredicateAtom::Projection(ty::ProjectionPredicate { projection_ty, ty }) => { - self.add_projection_ty(projection_ty); - self.add_ty(ty); - } - ty::PredicateAtom::WellFormed(arg) => { - self.add_substs(slice::from_ref(&arg)); - } - ty::PredicateAtom::ObjectSafe(_def_id) => {} - ty::PredicateAtom::ClosureKind(_def_id, substs, _kind) => { - self.add_substs(substs); - } - ty::PredicateAtom::ConstEvaluatable(_def_id, substs) => { - self.add_substs(substs); - } - ty::PredicateAtom::ConstEquate(expected, found) => { - self.add_const(expected); - self.add_const(found); - } - }, + &ty::PredicateKind::Atom(atom) => self.add_predicate_atom(atom), + } + } + + fn add_predicate_atom(&mut self, atom: ty::PredicateAtom<'_>) { + match atom { + ty::PredicateAtom::Trait(trait_pred, _constness) => { + self.add_substs(trait_pred.trait_ref.substs); + } + ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(a, b)) => { + self.add_region(a); + self.add_region(b); + } + ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty, region)) => { + self.add_ty(ty); + self.add_region(region); + } + ty::PredicateAtom::Subtype(ty::SubtypePredicate { a_is_expected: _, a, b }) => { + self.add_ty(a); + self.add_ty(b); + } + ty::PredicateAtom::Projection(ty::ProjectionPredicate { projection_ty, ty }) => { + self.add_projection_ty(projection_ty); + self.add_ty(ty); + } + ty::PredicateAtom::WellFormed(arg) => { + self.add_substs(slice::from_ref(&arg)); + } + ty::PredicateAtom::ObjectSafe(_def_id) => {} + ty::PredicateAtom::ClosureKind(_def_id, substs, _kind) => { + self.add_substs(substs); + } + ty::PredicateAtom::ConstEvaluatable(_def_id, substs) => { + self.add_substs(substs); + } + ty::PredicateAtom::ConstEquate(expected, found) => { + self.add_const(expected); + self.add_const(found); + } } } diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index 0ff475fb288a6..15210c5b21bc2 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -1053,8 +1053,9 @@ impl<'tcx> Predicate<'tcx> { /// /// Note that this method panics in case this predicate has unbound variables. pub fn skip_binders(self) -> PredicateAtom<'tcx> { + // TODO no_escaping_vars match self.kind() { - &PredicateKind::ForAll(binder) => binder.skip_binder().skip_binders(), + &PredicateKind::ForAll(binder) => binder.skip_binder(), &ty::PredicateKind::Atom(atom) => atom, } } @@ -1066,33 +1067,17 @@ impl<'tcx> Predicate<'tcx> { /// 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().skip_binders(), + &PredicateKind::ForAll(binder) => binder.skip_binder(), &ty::PredicateKind::Atom(atom) => atom, } } pub fn bound_atom(self, tcx: TyCtxt<'tcx>) -> Binder> { match self.kind() { - &PredicateKind::ForAll(binder) => binder.map_bound(|inner| match inner.kind() { - ty::PredicateKind::ForAll(_) => bug!("unexpect forall"), - &ty::PredicateKind::Atom(atom) => atom, - }), + &PredicateKind::ForAll(binder) => binder, &ty::PredicateKind::Atom(atom) => Binder::wrap_nonbinding(tcx, atom), } } - - /// 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)).to_predicate(tcx) - } else { - self - } - } } impl<'a, 'tcx> HashStable> for Predicate<'tcx> { @@ -1114,7 +1099,7 @@ impl<'a, 'tcx> HashStable> for Predicate<'tcx> { #[derive(HashStable, TypeFoldable)] pub enum PredicateKind<'tcx> { /// `for<'a>: ...` - ForAll(Binder>), + ForAll(Binder>), Atom(PredicateAtom<'tcx>), } @@ -1162,6 +1147,22 @@ pub enum PredicateAtom<'tcx> { ConstEquate(&'tcx Const<'tcx>, &'tcx Const<'tcx>), } +impl<'tcx> 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>) -> PredicateKind<'tcx>, + ) -> Predicate<'tcx> { + if self.has_escaping_bound_vars() { + qualifier(Binder::bind(self)) + } else { + PredicateKind::Atom(self) + } + .to_predicate(tcx) + } +} + /// The crate outlives map is computed during typeck and contains the /// outlives of every item in the local crate. You should not use it /// directly, because to do so will make your pass dependent on the @@ -1249,11 +1250,7 @@ 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.to_predicate(tcx).potentially_quantified(tcx, PredicateKind::ForAll) - } else { - self - } + if new != pred { new.potentially_quantified(tcx, PredicateKind::ForAll) } else { self } } } @@ -1381,6 +1378,7 @@ impl ToPredicate<'tcx> for PredicateKind<'tcx> { impl ToPredicate<'tcx> for PredicateAtom<'tcx> { #[inline(always)] fn to_predicate(&self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { + debug_assert!(!self.has_escaping_bound_vars(), "excaping bound vars for {:?}", self); tcx.mk_predicate(ty::PredicateKind::Atom(*self)) } } @@ -1408,9 +1406,7 @@ impl<'tcx> ToPredicate<'tcx> for ConstnessAnd> { 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) - }), + self.value.map_bound(|pred| ty::PredicateAtom::Trait(pred, self.constness)), ) .to_predicate(tcx) } @@ -1423,9 +1419,7 @@ impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate<'tcx> { PredicateAtom::RegionOutlives(outlives).to_predicate(tcx) } else { ty::PredicateKind::ForAll( - self.map_bound(|outlives| { - PredicateAtom::RegionOutlives(outlives).to_predicate(tcx) - }), + self.map_bound(|outlives| PredicateAtom::RegionOutlives(outlives)), ) .to_predicate(tcx) } @@ -1438,7 +1432,7 @@ impl<'tcx> ToPredicate<'tcx> for PolyTypeOutlivesPredicate<'tcx> { PredicateAtom::TypeOutlives(outlives).to_predicate(tcx) } else { ty::PredicateKind::ForAll( - self.map_bound(|outlives| PredicateAtom::TypeOutlives(outlives).to_predicate(tcx)), + self.map_bound(|outlives| PredicateAtom::TypeOutlives(outlives)), ) .to_predicate(tcx) } @@ -1450,10 +1444,8 @@ impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'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)), - ) - .to_predicate(tcx) + ty::PredicateKind::ForAll(self.map_bound(|proj| PredicateAtom::Projection(proj))) + .to_predicate(tcx) } } } diff --git a/src/librustc_middle/ty/print/pretty.rs b/src/librustc_middle/ty/print/pretty.rs index b0de57e15cc12..3bb9c20370e8c 100644 --- a/src/librustc_middle/ty/print/pretty.rs +++ b/src/librustc_middle/ty/print/pretty.rs @@ -2013,43 +2013,45 @@ define_print_and_forward_display! { ty::Predicate<'tcx> { match self.kind() { - &ty::PredicateKind::Atom(atom) => match atom { - ty::PredicateAtom::Trait(ref data, constness) => { - if let hir::Constness::Const = constness { - p!(write("const ")); - } - p!(print(data)) - } - ty::PredicateAtom::Subtype(predicate) => p!(print(predicate)), - ty::PredicateAtom::RegionOutlives(predicate) => p!(print(predicate)), - ty::PredicateAtom::TypeOutlives(predicate) => p!(print(predicate)), - ty::PredicateAtom::Projection(predicate) => p!(print(predicate)), - ty::PredicateAtom::WellFormed(arg) => p!(print(arg), write(" well-formed")), - ty::PredicateAtom::ObjectSafe(trait_def_id) => { - p!(write("the trait `"), - print_def_path(trait_def_id, &[]), - write("` is object-safe")) - } - ty::PredicateAtom::ClosureKind(closure_def_id, _closure_substs, kind) => { - p!(write("the closure `"), - print_value_path(closure_def_id, &[]), - write("` implements the trait `{}`", kind)) - } - ty::PredicateAtom::ConstEvaluatable(def, substs) => { - p!(write("the constant `"), - print_value_path(def.did, substs), - write("` can be evaluated")) - } - ty::PredicateAtom::ConstEquate(c1, c2) => { - p!(write("the constant `"), - print(c1), - write("` equals `"), - print(c2), - write("`")) + &ty::PredicateKind::Atom(atom) => p!(print(atom)), + ty::PredicateKind::ForAll(binder) => p!(print(binder)), + } + } + + ty::PredicateAtom<'tcx> { + match *self { + ty::PredicateAtom::Trait(ref data, constness) => { + if let hir::Constness::Const = constness { + p!(write("const ")); } - } - ty::PredicateKind::ForAll(binder) => { - p!(print(binder)) + p!(print(data)) + } + ty::PredicateAtom::Subtype(predicate) => p!(print(predicate)), + ty::PredicateAtom::RegionOutlives(predicate) => p!(print(predicate)), + ty::PredicateAtom::TypeOutlives(predicate) => p!(print(predicate)), + ty::PredicateAtom::Projection(predicate) => p!(print(predicate)), + ty::PredicateAtom::WellFormed(arg) => p!(print(arg), write(" well-formed")), + ty::PredicateAtom::ObjectSafe(trait_def_id) => { + p!(write("the trait `"), + print_def_path(trait_def_id, &[]), + write("` is object-safe")) + } + ty::PredicateAtom::ClosureKind(closure_def_id, _closure_substs, kind) => { + p!(write("the closure `"), + print_value_path(closure_def_id, &[]), + write("` implements the trait `{}`", kind)) + } + ty::PredicateAtom::ConstEvaluatable(def, substs) => { + p!(write("the constant `"), + print_value_path(def.did, substs), + write("` can be evaluated")) + } + ty::PredicateAtom::ConstEquate(c1, c2) => { + p!(write("the constant `"), + print(c1), + write("` equals `"), + print(c2), + write("`")) } } } diff --git a/src/librustc_trait_selection/traits/fulfill.rs b/src/librustc_trait_selection/traits/fulfill.rs index 0f6f362490604..c73ed986317c9 100644 --- a/src/librustc_trait_selection/traits/fulfill.rs +++ b/src/librustc_trait_selection/traits/fulfill.rs @@ -6,6 +6,7 @@ use rustc_errors::ErrorReported; use rustc_infer::traits::{PolyTraitObligation, TraitEngine, TraitEngineExt as _}; use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::ty::error::ExpectedFound; +use rustc_middle::ty::ToPredicate; use rustc_middle::ty::{self, Binder, Const, Ty, TypeFoldable}; use std::marker::PhantomData; diff --git a/src/librustc_trait_selection/traits/wf.rs b/src/librustc_trait_selection/traits/wf.rs index 0ca69c0f76e7a..d225b10834a6b 100644 --- a/src/librustc_trait_selection/traits/wf.rs +++ b/src/librustc_trait_selection/traits/wf.rs @@ -93,45 +93,40 @@ pub fn predicate_obligations<'a, 'tcx>( ) -> Vec> { let mut wf = WfPredicates { infcx, param_env, body_id, span, out: vec![], item: None }; - match predicate.kind() { - ty::PredicateKind::ForAll(binder) => { - // It's ok to skip the binder here because wf code is prepared for it - return predicate_obligations(infcx, param_env, body_id, binder.skip_binder(), span); + // It's ok to skip the binder here because wf code is prepared for it + match predicate.skip_binders() { + ty::PredicateAtom::Trait(t, _) => { + wf.compute_trait_ref(&t.trait_ref, Elaborate::None); } - &ty::PredicateKind::Atom(atom) => match atom { - ty::PredicateAtom::Trait(t, _) => { - wf.compute_trait_ref(&t.trait_ref, Elaborate::None); - } - ty::PredicateAtom::RegionOutlives(..) => {} - ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty, _reg)) => { - wf.compute(ty.into()); - } - ty::PredicateAtom::Projection(t) => { - wf.compute_projection(t.projection_ty); - wf.compute(t.ty.into()); - } - ty::PredicateAtom::WellFormed(arg) => { - wf.compute(arg); - } - ty::PredicateAtom::ObjectSafe(_) => {} - ty::PredicateAtom::ClosureKind(..) => {} - ty::PredicateAtom::Subtype(ty::SubtypePredicate { a, b, a_is_expected: _ }) => { - wf.compute(a.into()); - wf.compute(b.into()); - } - ty::PredicateAtom::ConstEvaluatable(def, substs) => { - let obligations = wf.nominal_obligations(def.did, substs); - wf.out.extend(obligations); + ty::PredicateAtom::RegionOutlives(..) => {} + ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty, _reg)) => { + wf.compute(ty.into()); + } + ty::PredicateAtom::Projection(t) => { + wf.compute_projection(t.projection_ty); + wf.compute(t.ty.into()); + } + ty::PredicateAtom::WellFormed(arg) => { + wf.compute(arg); + } + ty::PredicateAtom::ObjectSafe(_) => {} + ty::PredicateAtom::ClosureKind(..) => {} + ty::PredicateAtom::Subtype(ty::SubtypePredicate { a, b, a_is_expected: _ }) => { + wf.compute(a.into()); + wf.compute(b.into()); + } + ty::PredicateAtom::ConstEvaluatable(def, substs) => { + let obligations = wf.nominal_obligations(def.did, substs); + wf.out.extend(obligations); - for arg in substs.iter() { - wf.compute(arg); - } - } - ty::PredicateAtom::ConstEquate(c1, c2) => { - wf.compute(c1.into()); - wf.compute(c2.into()); + for arg in substs.iter() { + wf.compute(arg); } - }, + } + ty::PredicateAtom::ConstEquate(c1, c2) => { + wf.compute(c1.into()); + wf.compute(c2.into()); + } } wf.normalize() diff --git a/src/librustc_typeck/check/method/probe.rs b/src/librustc_typeck/check/method/probe.rs index e569d1c443a69..106df847a05cf 100644 --- a/src/librustc_typeck/check/method/probe.rs +++ b/src/librustc_typeck/check/method/probe.rs @@ -798,24 +798,28 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { // FIXME: do we want to commit to this behavior for param bounds? debug!("assemble_inherent_candidates_from_param(param_ty={:?})", param_ty); - let bounds = self.param_env.caller_bounds().iter().map(ty::Predicate::skip_binders).filter_map(|predicate| match predicate - { - ty::PredicateAtom::Trait(trait_predicate, _) => { - match trait_predicate.trait_ref.self_ty().kind { - ty::Param(ref p) if *p == param_ty => Some(ty::Binder::bind(trait_predicate.trait_ref)), - _ => None, - } - } - ty::PredicateAtom::Subtype(..) - | ty::PredicateAtom::Projection(..) - | ty::PredicateAtom::RegionOutlives(..) - | ty::PredicateAtom::WellFormed(..) - | ty::PredicateAtom::ObjectSafe(..) - | ty::PredicateAtom::ClosureKind(..) - | ty::PredicateAtom::TypeOutlives(..) - | ty::PredicateAtom::ConstEvaluatable(..) - | ty::PredicateAtom::ConstEquate(..) => None, - }); + let bounds = + self.param_env.caller_bounds().iter().map(ty::Predicate::skip_binders).filter_map( + |predicate| match predicate { + ty::PredicateAtom::Trait(trait_predicate, _) => { + match trait_predicate.trait_ref.self_ty().kind { + ty::Param(ref p) if *p == param_ty => { + Some(ty::Binder::bind(trait_predicate.trait_ref)) + } + _ => None, + } + } + ty::PredicateAtom::Subtype(..) + | ty::PredicateAtom::Projection(..) + | ty::PredicateAtom::RegionOutlives(..) + | ty::PredicateAtom::WellFormed(..) + | ty::PredicateAtom::ObjectSafe(..) + | ty::PredicateAtom::ClosureKind(..) + | ty::PredicateAtom::TypeOutlives(..) + | ty::PredicateAtom::ConstEvaluatable(..) + | ty::PredicateAtom::ConstEquate(..) => None, + }, + ); self.elaborate_bounds(bounds, |this, poly_trait_ref, item| { let trait_ref = this.erase_late_bound_regions(&poly_trait_ref); diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 61520e2923650..6cefc99f7b171 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -2939,9 +2939,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> { predicates: tcx.arena.alloc_from_iter( self.param_env.caller_bounds().iter().filter_map(|predicate| { match predicate.skip_binders() { - ty::PredicateAtom::Trait(data, _) - if data.self_ty().is_param(index) => - { + ty::PredicateAtom::Trait(data, _) if data.self_ty().is_param(index) => { // HACK(eddyb) should get the original `Span`. let span = tcx.def_span(def_id); Some((predicate, span)) @@ -5373,7 +5371,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { projection_ty, ty: expected, }) - .to_predicate(self.tcx) .potentially_quantified(self.tcx, ty::PredicateKind::ForAll); let obligation = traits::Obligation::new(self.misc(sp), self.param_env, predicate); diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index d906c5c05c019..a733ad4fccdc6 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -1961,7 +1961,6 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat let region = AstConv::ast_region_to_region(&icx, lifetime, None); predicates.push(( ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty, region)) - .to_predicate(tcx) .potentially_quantified(tcx, ty::PredicateKind::ForAll), lifetime.span, )) @@ -1979,8 +1978,7 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat } _ => bug!(), }; - let pred = ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(r1, r2)) - .to_predicate(icx.tcx); + let pred = ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(r1, r2)); (pred.potentially_quantified(icx.tcx, ty::PredicateKind::ForAll), span) })) @@ -2111,7 +2109,6 @@ 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)) - .to_predicate(astconv.tcx()) .potentially_quantified(astconv.tcx(), ty::PredicateKind::ForAll); vec![(pred, lifetime.span)] } diff --git a/src/librustc_typeck/outlives/mod.rs b/src/librustc_typeck/outlives/mod.rs index 823a0235b176d..5dc7ac9fa0d4e 100644 --- a/src/librustc_typeck/outlives/mod.rs +++ b/src/librustc_typeck/outlives/mod.rs @@ -3,7 +3,7 @@ use rustc_hir as hir; use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; use rustc_middle::ty::query::Providers; use rustc_middle::ty::subst::GenericArgKind; -use rustc_middle::ty::{self, CratePredicatesMap, ToPredicate, TyCtxt}; +use rustc_middle::ty::{self, CratePredicatesMap, TyCtxt}; use rustc_span::symbol::sym; use rustc_span::Span; @@ -90,7 +90,6 @@ fn inferred_outlives_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> CratePredica match kind1.unpack() { GenericArgKind::Type(ty1) => Some(( ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty1, region2)) - .to_predicate(tcx) .potentially_quantified(tcx, ty::PredicateKind::ForAll), span, )), @@ -98,7 +97,6 @@ fn inferred_outlives_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> CratePredica ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate( region1, region2, )) - .to_predicate(tcx) .potentially_quantified(tcx, ty::PredicateKind::ForAll), span, )), From 825cb5bdc925c4f2462c58c8aae5f7942a52a29f Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sat, 18 Jul 2020 12:06:47 +0200 Subject: [PATCH 24/28] fix rebase --- src/librustc_infer/infer/outlives/mod.rs | 2 +- src/librustc_lint/builtin.rs | 2 - src/librustc_middle/ty/mod.rs | 6 +-- .../traits/fulfill.rs | 48 ++++++++++--------- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/librustc_infer/infer/outlives/mod.rs b/src/librustc_infer/infer/outlives/mod.rs index 6009d4e65793b..a1e7f1fa3e5e7 100644 --- a/src/librustc_infer/infer/outlives/mod.rs +++ b/src/librustc_infer/infer/outlives/mod.rs @@ -16,7 +16,7 @@ pub fn explicit_outlives_bounds<'tcx>( .caller_bounds() .into_iter() .map(ty::Predicate::skip_binders) - .filter(TypeFoldable::has_escaping_bound_vars) + .filter(|atom| !atom.has_escaping_bound_vars()) .filter_map(move |atom| match atom { ty::PredicateAtom::Projection(..) | ty::PredicateAtom::Trait(..) diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index d67be44e4f0c5..a45817beea164 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -1208,8 +1208,6 @@ impl<'tcx> LateLintPass<'tcx> for TrivialConstraints { let def_id = cx.tcx.hir().local_def_id(item.hir_id); let predicates = cx.tcx.predicates_of(def_id); for &(predicate, span) in predicates.predicates { - // We don't actually look inside of the predicate, - // so it is safe to skip this binder here. let predicate_kind_name = match predicate.skip_binders() { Trait(..) => "Trait", TypeOutlives(..) | diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index 15210c5b21bc2..3bae1c1314325 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -1377,14 +1377,14 @@ impl ToPredicate<'tcx> for PredicateKind<'tcx> { impl ToPredicate<'tcx> for PredicateAtom<'tcx> { #[inline(always)] - fn to_predicate(&self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { + fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { debug_assert!(!self.has_escaping_bound_vars(), "excaping bound vars for {:?}", self); - tcx.mk_predicate(ty::PredicateKind::Atom(*self)) + tcx.mk_predicate(ty::PredicateKind::Atom(self)) } } impl<'tcx> ToPredicate<'tcx> for ConstnessAnd> { - fn to_predicate(&self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { + fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { ty::PredicateAtom::Trait(ty::TraitPredicate { trait_ref: self.value }, self.constness) .to_predicate(tcx) } diff --git a/src/librustc_trait_selection/traits/fulfill.rs b/src/librustc_trait_selection/traits/fulfill.rs index c73ed986317c9..25564d03e8373 100644 --- a/src/librustc_trait_selection/traits/fulfill.rs +++ b/src/librustc_trait_selection/traits/fulfill.rs @@ -471,29 +471,31 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { } } - ty::PredicateAtom::ConstEquate(c1, c2) => { - debug!("equating consts: c1={:?} c2={:?}", c1, c2); - - let stalled_on = &mut pending_obligation.stalled_on; - - let mut evaluate = |c: &'tcx Const<'tcx>| { - if let ty::ConstKind::Unevaluated(def, substs, promoted) = c.val { - match self.selcx.infcx().const_eval_resolve( - obligation.param_env, - def, - substs, - promoted, - Some(obligation.cause.span), - ) { - Ok(val) => Ok(Const::from_value(self.selcx.tcx(), val, c.ty)), - Err(ErrorHandled::TooGeneric) => { - stalled_on.append( - &mut substs - .types() - .filter_map(|ty| TyOrConstInferVar::maybe_from_ty(ty)) - .collect(), - ); - Err(ErrorHandled::TooGeneric) + ty::PredicateAtom::ConstEquate(c1, c2) => { + debug!("equating consts: c1={:?} c2={:?}", c1, c2); + + let stalled_on = &mut pending_obligation.stalled_on; + + let mut evaluate = |c: &'tcx Const<'tcx>| { + if let ty::ConstKind::Unevaluated(def, substs, promoted) = c.val { + match self.selcx.infcx().const_eval_resolve( + obligation.param_env, + def, + substs, + promoted, + Some(obligation.cause.span), + ) { + Ok(val) => Ok(Const::from_value(self.selcx.tcx(), val, c.ty)), + Err(ErrorHandled::TooGeneric) => { + stalled_on.append( + &mut substs + .types() + .filter_map(|ty| TyOrConstInferVar::maybe_from_ty(ty)) + .collect(), + ); + Err(ErrorHandled::TooGeneric) + } + Err(err) => Err(err), } } else { Ok(c) From 072cc458393974f9ff38b1448773c013c604c5fd Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sat, 18 Jul 2020 14:37:36 +0200 Subject: [PATCH 25/28] it works again :tada: --- src/librustc_middle/ty/mod.rs | 11 +++- src/librustc_middle/ty/structural_impls.rs | 8 +-- src/librustc_middle/ty/sty.rs | 3 + .../traits/error_reporting/mod.rs | 2 +- .../traits/fulfill.rs | 65 +++++++++---------- 5 files changed, 47 insertions(+), 42 deletions(-) diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index 3bae1c1314325..6f454e15aad76 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -1051,12 +1051,17 @@ impl<'tcx> Predicate<'tcx> { /// Returns the inner `PredicateAtom`. /// + /// The returned atom may contain unbound variables bound to binders skipped in this method. + /// It is safe to reapply binders to the given atom. + /// /// Note that this method panics in case this predicate has unbound variables. pub fn skip_binders(self) -> PredicateAtom<'tcx> { - // TODO no_escaping_vars match self.kind() { &PredicateKind::ForAll(binder) => binder.skip_binder(), - &ty::PredicateKind::Atom(atom) => atom, + &PredicateKind::Atom(atom) => { + debug_assert!(!atom.has_escaping_bound_vars()); + atom + } } } @@ -1378,7 +1383,7 @@ impl ToPredicate<'tcx> for PredicateKind<'tcx> { impl ToPredicate<'tcx> for PredicateAtom<'tcx> { #[inline(always)] fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { - debug_assert!(!self.has_escaping_bound_vars(), "excaping bound vars for {:?}", self); + debug_assert!(!self.has_escaping_bound_vars(), "escaping bound vars for {:?}", self); tcx.mk_predicate(ty::PredicateKind::Atom(self)) } } diff --git a/src/librustc_middle/ty/structural_impls.rs b/src/librustc_middle/ty/structural_impls.rs index cfe076e120702..21b8d7101a304 100644 --- a/src/librustc_middle/ty/structural_impls.rs +++ b/src/librustc_middle/ty/structural_impls.rs @@ -486,11 +486,9 @@ impl<'a, 'tcx> Lift<'tcx> for ty::ExistentialProjection<'a> { impl<'a, 'tcx> Lift<'tcx> for ty::PredicateKind<'a> { type Lifted = ty::PredicateKind<'tcx>; fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option { - match *self { - ty::PredicateKind::ForAll(ref binder) => { - tcx.lift(binder).map(ty::PredicateKind::ForAll) - } - ty::PredicateKind::Atom(ref atom) => tcx.lift(atom).map(ty::PredicateKind::Atom), + match self { + ty::PredicateKind::ForAll(binder) => tcx.lift(binder).map(ty::PredicateKind::ForAll), + ty::PredicateKind::Atom(atom) => tcx.lift(atom).map(ty::PredicateKind::Atom), } } } diff --git a/src/librustc_middle/ty/sty.rs b/src/librustc_middle/ty/sty.rs index 58a89c7fdb110..df8fa4d73ddf1 100644 --- a/src/librustc_middle/ty/sty.rs +++ b/src/librustc_middle/ty/sty.rs @@ -897,6 +897,9 @@ impl Binder { /// Wraps `value` in a binder without actually binding any currently /// unbound variables. + /// + /// Note that this will shift all debrujin indices of escaping bound variables + /// by 1 to avoid accidential captures. pub fn wrap_nonbinding(tcx: TyCtxt<'tcx>, value: T) -> Binder where T: TypeFoldable<'tcx>, diff --git a/src/librustc_trait_selection/traits/error_reporting/mod.rs b/src/librustc_trait_selection/traits/error_reporting/mod.rs index 951e0b2202666..349fa68a4da99 100644 --- a/src/librustc_trait_selection/traits/error_reporting/mod.rs +++ b/src/librustc_trait_selection/traits/error_reporting/mod.rs @@ -1100,7 +1100,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { for obligation in super::elaborate_predicates(self.tcx, std::iter::once(cond)) { if let ty::PredicateAtom::Trait(implication, _) = obligation.predicate.skip_binders() { let error = error.to_poly_trait_ref(); - let implication = ty::Binder::bind(implication).to_poly_trait_ref(); + let implication = ty::Binder::bind(implication.trait_ref); // FIXME: I'm just not taking associated types at all here. // Eventually I'll need to implement param-env-aware // `Γ₁ ⊦ φ₁ => Γ₂ ⊦ φ₂` logic. diff --git a/src/librustc_trait_selection/traits/fulfill.rs b/src/librustc_trait_selection/traits/fulfill.rs index 25564d03e8373..2b9621c9271e4 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::{PolyTraitObligation, TraitEngine, TraitEngineExt as _}; +use rustc_infer::traits::{TraitObligation, TraitEngine, TraitEngineExt as _}; use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::ty::error::ExpectedFound; use rustc_middle::ty::ToPredicate; @@ -320,41 +320,40 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> { let infcx = self.selcx.infcx(); match obligation.predicate.kind() { - ty::PredicateKind::ForAll(binder) => match binder.skip_binder().kind() { - ty::PredicateKind::ForAll(_) => bug!("unexpected forall"), + ty::PredicateKind::ForAll(binder) => match binder.skip_binder() { // Evaluation will discard candidates using the leak check. // This means we need to pass it the bound version of our // predicate. - &ty::PredicateKind::Atom(atom) => match atom { - ty::PredicateAtom::Trait(trait_ref, _constness) => { - let trait_obligation = obligation.with(Binder::bind(trait_ref)); - - self.process_trait_obligation( - obligation, - trait_obligation, - &mut pending_obligation.stalled_on, - ) - } - ty::PredicateAtom::Projection(projection) => { - let project_obligation = obligation.with(Binder::bind(projection)); + ty::PredicateAtom::Trait(trait_ref, _constness) => { + let trait_obligation = obligation.with(Binder::bind(trait_ref)); - self.process_projection_obligation( - project_obligation, - &mut pending_obligation.stalled_on, - ) - } - ty::PredicateAtom::RegionOutlives(_) - | ty::PredicateAtom::TypeOutlives(_) - | ty::PredicateAtom::WellFormed(_) - | ty::PredicateAtom::ObjectSafe(_) - | ty::PredicateAtom::ClosureKind(..) - | ty::PredicateAtom::Subtype(_) - | ty::PredicateAtom::ConstEvaluatable(..) - | ty::PredicateAtom::ConstEquate(..) => { - let (pred, _) = infcx.replace_bound_vars_with_placeholders(binder); - ProcessResult::Changed(mk_pending(vec![obligation.with(pred)])) - } - }, + self.process_trait_obligation( + obligation, + trait_obligation, + &mut pending_obligation.stalled_on, + ) + } + ty::PredicateAtom::Projection(data) => { + let project_obligation = obligation.with(Binder::bind(data)); + + self.process_projection_obligation( + project_obligation, + &mut pending_obligation.stalled_on, + ) + } + ty::PredicateAtom::RegionOutlives(_) + | ty::PredicateAtom::TypeOutlives(_) + | ty::PredicateAtom::WellFormed(_) + | ty::PredicateAtom::ObjectSafe(_) + | ty::PredicateAtom::ClosureKind(..) + | ty::PredicateAtom::Subtype(_) + | ty::PredicateAtom::ConstEvaluatable(..) + | ty::PredicateAtom::ConstEquate(..) => { + let (pred, _) = infcx.replace_bound_vars_with_placeholders(binder); + ProcessResult::Changed(mk_pending(vec![ + obligation.with(pred.to_predicate(self.selcx.tcx())), + ])) + } }, &ty::PredicateKind::Atom(atom) => match atom { ty::PredicateAtom::Trait(ref data, _) => { @@ -560,7 +559,7 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> { fn process_trait_obligation( &mut self, obligation: &PredicateObligation<'tcx>, - trait_obligation: PolyTraitObligation<'tcx>, + trait_obligation: TraitObligation<'tcx>, stalled_on: &mut Vec>, ) -> ProcessResult, FulfillmentErrorCode<'tcx>> { let infcx = self.selcx.infcx(); From 51cbcca2eb1fe9e34742b932aeedbadd2ab745d5 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sat, 18 Jul 2020 18:46:30 +0200 Subject: [PATCH 26/28] fix rustdoc --- src/librustdoc/clean/mod.rs | 38 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 728453deecff6..cc3a60c596ae7 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -482,12 +482,12 @@ impl<'a> Clean> for ty::Predicate<'a> { fn clean(&self, cx: &DocContext<'_>) -> Option { match self.skip_binders() { ty::PredicateAtom::Trait(pred, _) => Some(ty::Binder::bind(pred).clean(cx)), - ty::PredicateAtom::Subtype(pred) => Some(ty::Binder::bind(pred).clean(cx)), - ty::PredicateAtom::RegionOutlives(pred) => ty::Binder::bind(pred).clean(cx), - ty::PredicateAtom::TypeOutlives(pred) => ty::Binder::bind(pred).clean(cx), - ty::PredicateAtom::Projection(pred) => Some(ty::Binder::bind(pred).clean(cx)), + ty::PredicateAtom::RegionOutlives(pred) => pred.clean(cx), + ty::PredicateAtom::TypeOutlives(pred) => pred.clean(cx), + ty::PredicateAtom::Projection(pred) => Some(pred.clean(cx)), - ty::PredicateAtom::WellFormed(..) + ty::PredicateAtom::Subtype(..) + | ty::PredicateAtom::WellFormed(..) | ty::PredicateAtom::ObjectSafe(..) | ty::PredicateAtom::ClosureKind(..) | ty::PredicateAtom::ConstEvaluatable(..) @@ -506,20 +506,11 @@ impl<'a> Clean for ty::PolyTraitPredicate<'a> { } } -impl<'tcx> Clean for ty::PolySubtypePredicate<'tcx> { - fn clean(&self, _cx: &DocContext<'_>) -> WherePredicate { - panic!( - "subtype predicates are an internal rustc artifact \ - and should not be seen by rustdoc" - ) - } -} - impl<'tcx> Clean> - for ty::PolyOutlivesPredicate, ty::Region<'tcx>> + for ty::OutlivesPredicate, ty::Region<'tcx>> { fn clean(&self, cx: &DocContext<'_>) -> Option { - let ty::OutlivesPredicate(a, b) = self.skip_binder(); + let ty::OutlivesPredicate(a, b) = self; if let (ty::ReEmpty(_), ty::ReEmpty(_)) = (a, b) { return None; @@ -532,9 +523,9 @@ impl<'tcx> Clean> } } -impl<'tcx> Clean> for ty::PolyOutlivesPredicate, ty::Region<'tcx>> { +impl<'tcx> Clean> for ty::OutlivesPredicate, ty::Region<'tcx>> { fn clean(&self, cx: &DocContext<'_>) -> Option { - let ty::OutlivesPredicate(ty, lt) = self.skip_binder(); + let ty::OutlivesPredicate(ty, lt) = self; if let ty::ReEmpty(_) = lt { return None; @@ -547,9 +538,9 @@ impl<'tcx> Clean> for ty::PolyOutlivesPredicate, } } -impl<'tcx> Clean for ty::PolyProjectionPredicate<'tcx> { +impl<'tcx> Clean for ty::ProjectionPredicate<'tcx> { fn clean(&self, cx: &DocContext<'_>) -> WherePredicate { - let ty::ProjectionPredicate { projection_ty, ty } = self.skip_binder(); + let ty::ProjectionPredicate { projection_ty, ty } = self; WherePredicate::EqPredicate { lhs: projection_ty.clean(cx), rhs: ty.clean(cx) } } } @@ -1666,8 +1657,8 @@ impl<'tcx> Clean for Ty<'tcx> { ty::PredicateAtom::Trait(tr, _constness) => { ty::Binder::bind(tr.trait_ref) } - ty::PredicateAtom::TypeOutlives(pred) => { - if let Some(r) = pred.1.clean(cx) { + ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(_ty, reg)) => { + if let Some(r) = reg.clean(cx) { regions.push(GenericBound::Outlives(r)); } return None; @@ -1686,9 +1677,8 @@ impl<'tcx> Clean for Ty<'tcx> { .predicates .iter() .filter_map(|pred| { - // We never rebind `proj`, so `skip_binders_unchecked` is safe here. if let ty::PredicateAtom::Projection(proj) = - pred.skip_binders_unchecked() + pred.bound_atom(cx.tcx).skip_binder() { if proj.projection_ty.trait_ref(cx.tcx) == trait_ref.skip_binder() From 833b1d84e85808ba3d770240380970504537b5a0 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Tue, 21 Jul 2020 15:42:18 +0200 Subject: [PATCH 27/28] cleanup --- src/librustc_middle/ty/mod.rs | 66 +++++++------------ src/librustc_mir/monomorphize/polymorphize.rs | 15 ++--- .../traits/fulfill.rs | 2 +- src/librustc_typeck/check/method/suggest.rs | 7 +- src/librustc_typeck/collect.rs | 4 +- 5 files changed, 35 insertions(+), 59 deletions(-) diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index 6f454e15aad76..d1c6d3be5f4bb 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>` even if the given predicate previously + /// contained unbound variables by shifting these variables outwards. pub fn bound_atom(self, tcx: TyCtxt<'tcx>) -> Binder> { 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> for Predicate<'tcx> { pub enum PredicateKind<'tcx> { /// `for<'a>: ...` ForAll(Binder>), - 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, Span)]>, + pub predicates: FxHashMap, 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> { 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> { impl<'tcx> ToPredicate<'tcx> for ConstnessAnd> { 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> type having at least 2-byte + // This relies on the List> 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>>, + caller_bounds: PhantomData<&'tcx List>>, /// 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> { + pub fn caller_bounds(self) -> &'tcx List> { // 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>, + caller_bounds: &'tcx List>, reveal: Reveal, def_id: Option, ) -> Self { diff --git a/src/librustc_mir/monomorphize/polymorphize.rs b/src/librustc_mir/monomorphize/polymorphize.rs index 071b9bb971139..562f512c5dacf 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 2b9621c9271e4..2eda1ce595a7d 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 07cc8332b84cf..ae2cf6daf5350 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 a733ad4fccdc6..76439af79f351 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, )); } From 602ef6bc0edb3292b4ae96515328bb16348950fb Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Mon, 27 Jul 2020 21:17:28 +0200 Subject: [PATCH 28/28] clippy --- .../clippy_lints/src/unit_return_expecting_ord.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/tools/clippy/clippy_lints/src/unit_return_expecting_ord.rs b/src/tools/clippy/clippy_lints/src/unit_return_expecting_ord.rs index ac6f3d125bb42..679aaec9fcd6c 100644 --- a/src/tools/clippy/clippy_lints/src/unit_return_expecting_ord.rs +++ b/src/tools/clippy/clippy_lints/src/unit_return_expecting_ord.rs @@ -4,7 +4,7 @@ use rustc_hir::def_id::DefId; use rustc_hir::{Expr, ExprKind, StmtKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty; -use rustc_middle::ty::{GenericPredicates, PredicateKind, ProjectionPredicate, TraitPredicate}; +use rustc_middle::ty::{GenericPredicates, PredicateAtom, ProjectionPredicate, TraitPredicate}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::{BytePos, Span}; @@ -42,8 +42,8 @@ fn get_trait_predicates_for_trait_id<'tcx>( let mut preds = Vec::new(); for (pred, _) in generics.predicates { if_chain! { - if let PredicateKind::Trait(poly_trait_pred, _) = pred.kind(); - let trait_pred = cx.tcx.erase_late_bound_regions(&poly_trait_pred); + if let PredicateAtom::Trait(poly_trait_pred, _) = pred.skip_binders(); + let trait_pred = cx.tcx.erase_late_bound_regions(&ty::Binder::bind(poly_trait_pred)); if let Some(trait_def_id) = trait_id; if trait_def_id == trait_pred.trait_ref.def_id; then { @@ -60,8 +60,8 @@ fn get_projection_pred<'tcx>( pred: TraitPredicate<'tcx>, ) -> Option> { generics.predicates.iter().find_map(|(proj_pred, _)| { - if let PredicateKind::Projection(proj_pred) = proj_pred.kind() { - let projection_pred = cx.tcx.erase_late_bound_regions(proj_pred); + if let ty::PredicateAtom::Projection(proj_pred) = proj_pred.skip_binders() { + let projection_pred = cx.tcx.erase_late_bound_regions(&ty::Binder::bind(proj_pred)); if projection_pred.projection_ty.substs == pred.trait_ref.substs { return Some(projection_pred); }