diff options
| author | Santiago Pastorino <spastorino@gmail.com> | 2022-11-24 18:14:58 -0300 |
|---|---|---|
| committer | Santiago Pastorino <spastorino@gmail.com> | 2022-11-25 00:04:54 -0300 |
| commit | 974e2837bb643a1213caeea4025f2f5681bd5f3e (patch) | |
| tree | ef25aad9b89c8cb4f8f618f153c04f3dd2aa8b87 /compiler/rustc_middle | |
| parent | 42cc8e8f4e5fe3864a9aca4f342ee2aff2ba696a (diff) | |
Introduce PredicateKind::Clause
Diffstat (limited to 'compiler/rustc_middle')
| -rw-r--r-- | compiler/rustc_middle/src/ty/context.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/flags.rs | 14 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/mod.rs | 73 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/print/pretty.rs | 14 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/structural_impls.rs | 16 |
5 files changed, 75 insertions, 48 deletions
diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index b5327ad0cec..bede0ec142e 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -2296,7 +2296,7 @@ impl<'tcx> TyCtxt<'tcx> { let future_trait = self.require_lang_item(LangItem::Future, None); self.explicit_item_bounds(def_id).iter().any(|(predicate, _)| { - let ty::PredicateKind::Trait(trait_predicate) = predicate.kind().skip_binder() else { + let ty::PredicateKind::Clause(ty::Clause::Trait(trait_predicate)) = predicate.kind().skip_binder() else { return false; }; trait_predicate.trait_ref.def_id == future_trait @@ -2319,7 +2319,9 @@ impl<'tcx> TyCtxt<'tcx> { let generic_predicates = self.super_predicates_of(trait_did); for (predicate, _) in generic_predicates.predicates { - if let ty::PredicateKind::Trait(data) = predicate.kind().skip_binder() { + if let ty::PredicateKind::Clause(ty::Clause::Trait(data)) = + predicate.kind().skip_binder() + { if set.insert(data.def_id()) { stack.push(data.def_id()); } diff --git a/compiler/rustc_middle/src/ty/flags.rs b/compiler/rustc_middle/src/ty/flags.rs index ee4b8f91c54..61f3a0685e7 100644 --- a/compiler/rustc_middle/src/ty/flags.rs +++ b/compiler/rustc_middle/src/ty/flags.rs @@ -216,14 +216,17 @@ impl FlagComputation { fn add_predicate_atom(&mut self, atom: ty::PredicateKind<'_>) { match atom { - ty::PredicateKind::Trait(trait_pred) => { + ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) => { self.add_substs(trait_pred.trait_ref.substs); } - ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => { + ty::PredicateKind::Clause(ty::Clause::RegionOutlives(ty::OutlivesPredicate(a, b))) => { self.add_region(a); self.add_region(b); } - ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty, region)) => { + ty::PredicateKind::Clause(ty::Clause::TypeOutlives(ty::OutlivesPredicate( + ty, + region, + ))) => { self.add_ty(ty); self.add_region(region); } @@ -235,7 +238,10 @@ impl FlagComputation { self.add_ty(a); self.add_ty(b); } - ty::PredicateKind::Projection(ty::ProjectionPredicate { projection_ty, term }) => { + ty::PredicateKind::Clause(ty::Clause::Projection(ty::ProjectionPredicate { + projection_ty, + term, + })) => { self.add_projection_ty(projection_ty); match term.unpack() { ty::TermKind::Ty(ty) => self.add_ty(ty), diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 8e03cba15ef..f20fa3c32ad 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -574,13 +574,15 @@ impl<'tcx> Predicate<'tcx> { let kind = self .kind() .map_bound(|kind| match kind { - PredicateKind::Trait(TraitPredicate { trait_ref, constness, polarity }) => { - Some(PredicateKind::Trait(TraitPredicate { - trait_ref, - constness, - polarity: polarity.flip()?, - })) - } + PredicateKind::Clause(Clause::Trait(TraitPredicate { + trait_ref, + constness, + polarity, + })) => Some(PredicateKind::Clause(Clause::Trait(TraitPredicate { + trait_ref, + constness, + polarity: polarity.flip()?, + }))), _ => None, }) @@ -590,14 +592,14 @@ impl<'tcx> Predicate<'tcx> { } pub fn without_const(mut self, tcx: TyCtxt<'tcx>) -> Self { - if let PredicateKind::Trait(TraitPredicate { trait_ref, constness, polarity }) = self.kind().skip_binder() + if let PredicateKind::Clause(Clause::Trait(TraitPredicate { trait_ref, constness, polarity })) = self.kind().skip_binder() && constness != BoundConstness::NotConst { - self = tcx.mk_predicate(self.kind().rebind(PredicateKind::Trait(TraitPredicate { + self = tcx.mk_predicate(self.kind().rebind(PredicateKind::Clause(Clause::Trait(TraitPredicate { trait_ref, constness: BoundConstness::NotConst, polarity, - }))); + })))); } self } @@ -611,10 +613,10 @@ impl<'tcx> Predicate<'tcx> { pub fn allow_normalization(self) -> bool { match self.kind().skip_binder() { PredicateKind::WellFormed(_) => false, - PredicateKind::Trait(_) - | PredicateKind::RegionOutlives(_) - | PredicateKind::TypeOutlives(_) - | PredicateKind::Projection(_) + PredicateKind::Clause(Clause::Trait(_)) + | PredicateKind::Clause(Clause::RegionOutlives(_)) + | PredicateKind::Clause(Clause::TypeOutlives(_)) + | PredicateKind::Clause(Clause::Projection(_)) | PredicateKind::ObjectSafe(_) | PredicateKind::ClosureKind(_, _, _) | PredicateKind::Subtype(_) @@ -650,7 +652,9 @@ impl rustc_errors::IntoDiagnosticArg for Predicate<'_> { #[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable)] #[derive(HashStable, TypeFoldable, TypeVisitable, Lift)] -pub enum PredicateKind<'tcx> { +/// A clause is something that can appear in where bounds or be inferred +/// by implied bounds. +pub enum Clause<'tcx> { /// Corresponds to `where Foo: Bar<A, B, C>`. `Foo` here would be /// the `Self` type of the trait reference and `A`, `B`, and `C` /// would be the type parameters. @@ -665,6 +669,13 @@ pub enum PredicateKind<'tcx> { /// `where <T as TraitRef>::Name == X`, approximately. /// See the `ProjectionPredicate` struct for details. Projection(ProjectionPredicate<'tcx>), +} + +#[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable)] +#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)] +pub enum PredicateKind<'tcx> { + /// Prove a clause + Clause(Clause<'tcx>), /// No syntax: `T` well-formed. WellFormed(GenericArg<'tcx>), @@ -1174,25 +1185,25 @@ impl<'tcx> ToPredicate<'tcx, PolyTraitPredicate<'tcx>> for Binder<'tcx, TraitRef impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for PolyTraitPredicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { - self.map_bound(PredicateKind::Trait).to_predicate(tcx) + self.map_bound(|p| PredicateKind::Clause(Clause::Trait(p))).to_predicate(tcx) } } impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for PolyRegionOutlivesPredicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { - self.map_bound(PredicateKind::RegionOutlives).to_predicate(tcx) + self.map_bound(|p| PredicateKind::Clause(Clause::RegionOutlives(p))).to_predicate(tcx) } } impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for PolyTypeOutlivesPredicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { - self.map_bound(PredicateKind::TypeOutlives).to_predicate(tcx) + self.map_bound(|p| PredicateKind::Clause(Clause::TypeOutlives(p))).to_predicate(tcx) } } impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for PolyProjectionPredicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { - self.map_bound(PredicateKind::Projection).to_predicate(tcx) + self.map_bound(|p| PredicateKind::Clause(Clause::Projection(p))).to_predicate(tcx) } } @@ -1200,15 +1211,15 @@ impl<'tcx> Predicate<'tcx> { pub fn to_opt_poly_trait_pred(self) -> Option<PolyTraitPredicate<'tcx>> { let predicate = self.kind(); match predicate.skip_binder() { - PredicateKind::Trait(t) => Some(predicate.rebind(t)), - PredicateKind::Projection(..) + PredicateKind::Clause(Clause::Trait(t)) => Some(predicate.rebind(t)), + PredicateKind::Clause(Clause::Projection(..)) | PredicateKind::Subtype(..) | PredicateKind::Coerce(..) - | PredicateKind::RegionOutlives(..) + | PredicateKind::Clause(Clause::RegionOutlives(..)) | PredicateKind::WellFormed(..) | PredicateKind::ObjectSafe(..) | PredicateKind::ClosureKind(..) - | PredicateKind::TypeOutlives(..) + | PredicateKind::Clause(Clause::TypeOutlives(..)) | PredicateKind::ConstEvaluatable(..) | PredicateKind::ConstEquate(..) | PredicateKind::Ambiguous @@ -1219,15 +1230,15 @@ impl<'tcx> Predicate<'tcx> { pub fn to_opt_poly_projection_pred(self) -> Option<PolyProjectionPredicate<'tcx>> { let predicate = self.kind(); match predicate.skip_binder() { - PredicateKind::Projection(t) => Some(predicate.rebind(t)), - PredicateKind::Trait(..) + PredicateKind::Clause(Clause::Projection(t)) => Some(predicate.rebind(t)), + PredicateKind::Clause(Clause::Trait(..)) | PredicateKind::Subtype(..) | PredicateKind::Coerce(..) - | PredicateKind::RegionOutlives(..) + | PredicateKind::Clause(Clause::RegionOutlives(..)) | PredicateKind::WellFormed(..) | PredicateKind::ObjectSafe(..) | PredicateKind::ClosureKind(..) - | PredicateKind::TypeOutlives(..) + | PredicateKind::Clause(Clause::TypeOutlives(..)) | PredicateKind::ConstEvaluatable(..) | PredicateKind::ConstEquate(..) | PredicateKind::Ambiguous @@ -1238,12 +1249,12 @@ impl<'tcx> Predicate<'tcx> { pub fn to_opt_type_outlives(self) -> Option<PolyTypeOutlivesPredicate<'tcx>> { let predicate = self.kind(); match predicate.skip_binder() { - PredicateKind::TypeOutlives(data) => Some(predicate.rebind(data)), - PredicateKind::Trait(..) - | PredicateKind::Projection(..) + PredicateKind::Clause(Clause::TypeOutlives(data)) => Some(predicate.rebind(data)), + PredicateKind::Clause(Clause::Trait(..)) + | PredicateKind::Clause(Clause::Projection(..)) | PredicateKind::Subtype(..) | PredicateKind::Coerce(..) - | PredicateKind::RegionOutlives(..) + | PredicateKind::Clause(Clause::RegionOutlives(..)) | PredicateKind::WellFormed(..) | PredicateKind::ObjectSafe(..) | PredicateKind::ClosureKind(..) diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index c54edf10c2d..a90b63cc05b 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -814,7 +814,7 @@ pub trait PrettyPrinter<'tcx>: let bound_predicate = predicate.kind(); match bound_predicate.skip_binder() { - ty::PredicateKind::Trait(pred) => { + ty::PredicateKind::Clause(ty::Clause::Trait(pred)) => { let trait_ref = bound_predicate.rebind(pred.trait_ref); // Don't print + Sized, but rather + ?Sized if absent. @@ -825,7 +825,7 @@ pub trait PrettyPrinter<'tcx>: self.insert_trait_and_projection(trait_ref, None, &mut traits, &mut fn_traits); } - ty::PredicateKind::Projection(pred) => { + ty::PredicateKind::Clause(ty::Clause::Projection(pred)) => { let proj_ref = bound_predicate.rebind(pred); let trait_ref = proj_ref.required_poly_trait_ref(tcx); @@ -839,7 +839,7 @@ pub trait PrettyPrinter<'tcx>: &mut fn_traits, ); } - ty::PredicateKind::TypeOutlives(outlives) => { + ty::PredicateKind::Clause(ty::Clause::TypeOutlives(outlives)) => { lifetimes.push(outlives.1); } _ => {} @@ -2689,14 +2689,14 @@ define_print_and_forward_display! { ty::PredicateKind<'tcx> { match *self { - ty::PredicateKind::Trait(ref data) => { + ty::PredicateKind::Clause(ty::Clause::Trait(ref data)) => { p!(print(data)) } ty::PredicateKind::Subtype(predicate) => p!(print(predicate)), ty::PredicateKind::Coerce(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::Clause(ty::Clause::RegionOutlives(predicate)) => p!(print(predicate)), + ty::PredicateKind::Clause(ty::Clause::TypeOutlives(predicate)) => p!(print(predicate)), + ty::PredicateKind::Clause(ty::Clause::Projection(predicate)) => p!(print(predicate)), ty::PredicateKind::WellFormed(arg) => p!(print(arg), " well-formed"), ty::PredicateKind::ObjectSafe(trait_def_id) => { p!("the trait `", print_def_path(trait_def_id, &[]), "` is object-safe") diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index 64b4fd11762..c8baac1ec20 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -150,15 +150,23 @@ impl<'tcx> fmt::Debug for ty::Predicate<'tcx> { } } +impl<'tcx> fmt::Debug for ty::Clause<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + ty::Clause::Trait(ref a) => a.fmt(f), + ty::Clause::RegionOutlives(ref pair) => pair.fmt(f), + ty::Clause::TypeOutlives(ref pair) => pair.fmt(f), + ty::Clause::Projection(ref pair) => pair.fmt(f), + } + } +} + impl<'tcx> fmt::Debug for ty::PredicateKind<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { - ty::PredicateKind::Trait(ref a) => a.fmt(f), + ty::PredicateKind::Clause(ref a) => a.fmt(f), ty::PredicateKind::Subtype(ref pair) => pair.fmt(f), ty::PredicateKind::Coerce(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) => { write!(f, "ObjectSafe({:?})", trait_def_id) |
