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#[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    Trait(ty::TraitPredicate<I>),
23
24    RegionOutlives(ty::OutlivesPredicate<I, I::Region>),
26
27    TypeOutlives(ty::OutlivesPredicate<I, I::Ty>),
29
30    Projection(ty::ProjectionPredicate<I>),
33
34    ConstArgHasType(I::Const, I::Ty),
37
38    WellFormed(I::Term),
40
41    ConstEvaluatable(I::Const),
43
44    HostEffect(ty::HostEffectPredicate<I>),
49
50    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    Clause(ClauseKind<I>),
69
70    DynCompatible(I::TraitId),
72
73    Subtype(ty::SubtypePredicate<I>),
79
80    Coerce(ty::CoercePredicate<I>),
89
90    ConstEquate(I::Const, I::Const),
92
93    Ambiguous,
96
97    NormalizesTo(ty::NormalizesTo<I>),
106
107    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}