rustc_type_ir/
predicate_kind.rs

1use std::fmt;
2
3use derive_where::derive_where;
4#[cfg(feature = "nightly")]
5use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext};
6use rustc_type_ir_macros::{TypeFoldable_Generic, TypeVisitable_Generic};
7
8use crate::{self as ty, Interner};
9
10/// A clause is something that can appear in where bounds or be inferred
11/// by implied bounds.
12#[derive_where(Clone, Copy, Hash, PartialEq; I: Interner)]
13#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
14#[cfg_attr(
15    feature = "nightly",
16    derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
17)]
18pub enum ClauseKind<I: Interner> {
19    /// Corresponds to `where Foo: Bar<A, B, C>`. `Foo` here would be
20    /// the `Self` type of the trait reference and `A`, `B`, and `C`
21    /// would be the type parameters.
22    Trait(ty::TraitPredicate<I>),
23
24    /// `where 'a: 'r`
25    RegionOutlives(ty::OutlivesPredicate<I, I::Region>),
26
27    /// `where T: 'r`
28    TypeOutlives(ty::OutlivesPredicate<I, I::Ty>),
29
30    /// `where <T as TraitRef>::Name == X`, approximately.
31    /// See the `ProjectionPredicate` struct for details.
32    Projection(ty::ProjectionPredicate<I>),
33
34    /// Ensures that a const generic argument to a parameter `const N: u8`
35    /// is of type `u8`.
36    ConstArgHasType(I::Const, I::Ty),
37
38    /// No syntax: `T` well-formed.
39    WellFormed(I::Term),
40
41    /// Constant initializer must evaluate successfully.
42    ConstEvaluatable(I::Const),
43
44    /// Enforces the constness of the predicate we're calling. Like a projection
45    /// goal from a where clause, it's always going to be paired with a
46    /// corresponding trait clause; this just enforces the *constness* of that
47    /// implementation.
48    HostEffect(ty::HostEffectPredicate<I>),
49
50    /// Support marking impl as unstable.
51    UnstableFeature(
52        #[type_foldable(identity)]
53        #[type_visitable(ignore)]
54        I::Symbol,
55    ),
56}
57
58impl<I: Interner> Eq for ClauseKind<I> {}
59
60#[derive_where(Clone, Copy, Hash, PartialEq; I: Interner)]
61#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
62#[cfg_attr(
63    feature = "nightly",
64    derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
65)]
66pub enum PredicateKind<I: Interner> {
67    /// Prove a clause
68    Clause(ClauseKind<I>),
69
70    /// Trait must be dyn-compatible.
71    DynCompatible(I::TraitId),
72
73    /// `T1 <: T2`
74    ///
75    /// This obligation is created most often when we have two
76    /// unresolved type variables and hence don't have enough
77    /// information to process the subtyping obligation yet.
78    Subtype(ty::SubtypePredicate<I>),
79
80    /// `T1` coerced to `T2`
81    ///
82    /// Like a subtyping obligation, this is created most often
83    /// when we have two unresolved type variables and hence
84    /// don't have enough information to process the coercion
85    /// obligation yet. At the moment, we actually process coercions
86    /// very much like subtyping and don't handle the full coercion
87    /// logic.
88    Coerce(ty::CoercePredicate<I>),
89
90    /// Constants must be equal. The first component is the const that is expected.
91    ConstEquate(I::Const, I::Const),
92
93    /// A marker predicate that is always ambiguous.
94    /// Used for coherence to mark opaque types as possibly equal to each other but ambiguous.
95    Ambiguous,
96
97    /// This should only be used inside of the new solver for `AliasRelate` and expects
98    /// the `term` to be always be an unconstrained inference variable. It is used to
99    /// normalize `alias` as much as possible. In case the alias is rigid - i.e. it cannot
100    /// be normalized in the current environment - this constrains `term` to be equal to
101    /// the alias itself.
102    ///
103    /// It is likely more useful to think of this as a function `normalizes_to(alias)`,
104    /// whose return value is written into `term`.
105    NormalizesTo(ty::NormalizesTo<I>),
106
107    /// Separate from `ClauseKind::Projection` which is used for normalization in new solver.
108    /// This predicate requires two terms to be equal to eachother.
109    ///
110    /// Only used for new solver.
111    AliasRelate(I::Term, I::Term, AliasRelationDirection),
112}
113
114impl<I: Interner> Eq for PredicateKind<I> {}
115
116#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)]
117#[cfg_attr(
118    feature = "nightly",
119    derive(HashStable_NoContext, Encodable_NoContext, Decodable_NoContext)
120)]
121pub enum AliasRelationDirection {
122    Equate,
123    Subtype,
124}
125
126impl std::fmt::Display for AliasRelationDirection {
127    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
128        match self {
129            AliasRelationDirection::Equate => write!(f, "=="),
130            AliasRelationDirection::Subtype => write!(f, "<:"),
131        }
132    }
133}
134
135impl<I: Interner> fmt::Debug for ClauseKind<I> {
136    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137        match self {
138            ClauseKind::ConstArgHasType(ct, ty) => write!(f, "ConstArgHasType({ct:?}, {ty:?})"),
139            ClauseKind::HostEffect(data) => data.fmt(f),
140            ClauseKind::Trait(a) => a.fmt(f),
141            ClauseKind::RegionOutlives(pair) => pair.fmt(f),
142            ClauseKind::TypeOutlives(pair) => pair.fmt(f),
143            ClauseKind::Projection(pair) => pair.fmt(f),
144            ClauseKind::WellFormed(data) => write!(f, "WellFormed({data:?})"),
145            ClauseKind::ConstEvaluatable(ct) => {
146                write!(f, "ConstEvaluatable({ct:?})")
147            }
148            ClauseKind::UnstableFeature(feature_name) => {
149                write!(f, "UnstableFeature({feature_name:?})")
150            }
151        }
152    }
153}
154
155impl<I: Interner> fmt::Debug for PredicateKind<I> {
156    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157        match self {
158            PredicateKind::Clause(a) => a.fmt(f),
159            PredicateKind::Subtype(pair) => pair.fmt(f),
160            PredicateKind::Coerce(pair) => pair.fmt(f),
161            PredicateKind::DynCompatible(trait_def_id) => {
162                write!(f, "DynCompatible({trait_def_id:?})")
163            }
164            PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({c1:?}, {c2:?})"),
165            PredicateKind::Ambiguous => write!(f, "Ambiguous"),
166            PredicateKind::NormalizesTo(p) => p.fmt(f),
167            PredicateKind::AliasRelate(t1, t2, dir) => {
168                write!(f, "AliasRelate({t1:?}, {dir:?}, {t2:?})")
169            }
170        }
171    }
172}