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, Eq; 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::GenericArg),
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
51#[derive_where(Clone, Copy, Hash, PartialEq, Eq; I: Interner)]
52#[derive(TypeVisitable_Generic, TypeFoldable_Generic)]
53#[cfg_attr(
54    feature = "nightly",
55    derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
56)]
57pub enum PredicateKind<I: Interner> {
58    /// Prove a clause
59    Clause(ClauseKind<I>),
60
61    /// Trait must be dyn-compatible.
62    DynCompatible(I::DefId),
63
64    /// `T1 <: T2`
65    ///
66    /// This obligation is created most often when we have two
67    /// unresolved type variables and hence don't have enough
68    /// information to process the subtyping obligation yet.
69    Subtype(ty::SubtypePredicate<I>),
70
71    /// `T1` coerced to `T2`
72    ///
73    /// Like a subtyping obligation, this is created most often
74    /// when we have two unresolved type variables and hence
75    /// don't have enough information to process the coercion
76    /// obligation yet. At the moment, we actually process coercions
77    /// very much like subtyping and don't handle the full coercion
78    /// logic.
79    Coerce(ty::CoercePredicate<I>),
80
81    /// Constants must be equal. The first component is the const that is expected.
82    ConstEquate(I::Const, I::Const),
83
84    /// A marker predicate that is always ambiguous.
85    /// Used for coherence to mark opaque types as possibly equal to each other but ambiguous.
86    Ambiguous,
87
88    /// This should only be used inside of the new solver for `AliasRelate` and expects
89    /// the `term` to be always be an unconstrained inference variable. It is used to
90    /// normalize `alias` as much as possible. In case the alias is rigid - i.e. it cannot
91    /// be normalized in the current environment - this constrains `term` to be equal to
92    /// the alias itself.
93    ///
94    /// It is likely more useful to think of this as a function `normalizes_to(alias)`,
95    /// whose return value is written into `term`.
96    NormalizesTo(ty::NormalizesTo<I>),
97
98    /// Separate from `ClauseKind::Projection` which is used for normalization in new solver.
99    /// This predicate requires two terms to be equal to eachother.
100    ///
101    /// Only used for new solver.
102    AliasRelate(I::Term, I::Term, AliasRelationDirection),
103}
104
105#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)]
106#[cfg_attr(
107    feature = "nightly",
108    derive(HashStable_NoContext, Encodable_NoContext, Decodable_NoContext)
109)]
110pub enum AliasRelationDirection {
111    Equate,
112    Subtype,
113}
114
115impl std::fmt::Display for AliasRelationDirection {
116    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117        match self {
118            AliasRelationDirection::Equate => write!(f, "=="),
119            AliasRelationDirection::Subtype => write!(f, "<:"),
120        }
121    }
122}
123
124impl<I: Interner> fmt::Debug for ClauseKind<I> {
125    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126        match self {
127            ClauseKind::ConstArgHasType(ct, ty) => write!(f, "ConstArgHasType({ct:?}, {ty:?})"),
128            ClauseKind::HostEffect(data) => data.fmt(f),
129            ClauseKind::Trait(a) => a.fmt(f),
130            ClauseKind::RegionOutlives(pair) => pair.fmt(f),
131            ClauseKind::TypeOutlives(pair) => pair.fmt(f),
132            ClauseKind::Projection(pair) => pair.fmt(f),
133            ClauseKind::WellFormed(data) => write!(f, "WellFormed({data:?})"),
134            ClauseKind::ConstEvaluatable(ct) => {
135                write!(f, "ConstEvaluatable({ct:?})")
136            }
137        }
138    }
139}
140
141impl<I: Interner> fmt::Debug for PredicateKind<I> {
142    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143        match self {
144            PredicateKind::Clause(a) => a.fmt(f),
145            PredicateKind::Subtype(pair) => pair.fmt(f),
146            PredicateKind::Coerce(pair) => pair.fmt(f),
147            PredicateKind::DynCompatible(trait_def_id) => {
148                write!(f, "DynCompatible({trait_def_id:?})")
149            }
150            PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({c1:?}, {c2:?})"),
151            PredicateKind::Ambiguous => write!(f, "Ambiguous"),
152            PredicateKind::NormalizesTo(p) => p.fmt(f),
153            PredicateKind::AliasRelate(t1, t2, dir) => {
154                write!(f, "AliasRelate({t1:?}, {dir:?}, {t2:?})")
155            }
156        }
157    }
158}