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