1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use rustc_ast_ir::try_visit;
use rustc_ast_ir::visit::VisitorResult;
use std::fmt;

use crate::fold::{FallibleTypeFolder, TypeFoldable};
use crate::visit::{TypeVisitable, TypeVisitor};
use crate::Interner;

/// A clause is something that can appear in where bounds or be inferred
/// by implied bounds.
#[derive(derivative::Derivative)]
#[derivative(Clone(bound = ""), Copy(bound = ""), Hash(bound = ""))]
#[cfg_attr(feature = "nightly", derive(TyEncodable, TyDecodable, HashStable_NoContext))]
pub enum ClauseKind<I: Interner> {
    /// 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.
    Trait(I::TraitPredicate),

    /// `where 'a: 'b`
    RegionOutlives(I::RegionOutlivesPredicate),

    /// `where T: 'a`
    TypeOutlives(I::TypeOutlivesPredicate),

    /// `where <T as TraitRef>::Name == X`, approximately.
    /// See the `ProjectionPredicate` struct for details.
    Projection(I::ProjectionPredicate),

    /// Ensures that a const generic argument to a parameter `const N: u8`
    /// is of type `u8`.
    ConstArgHasType(I::Const, I::Ty),

    /// No syntax: `T` well-formed.
    WellFormed(I::GenericArg),

    /// Constant initializer must evaluate successfully.
    ConstEvaluatable(I::Const),
}

impl<I: Interner> PartialEq for ClauseKind<I> {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Trait(l0), Self::Trait(r0)) => l0 == r0,
            (Self::RegionOutlives(l0), Self::RegionOutlives(r0)) => l0 == r0,
            (Self::TypeOutlives(l0), Self::TypeOutlives(r0)) => l0 == r0,
            (Self::Projection(l0), Self::Projection(r0)) => l0 == r0,
            (Self::ConstArgHasType(l0, l1), Self::ConstArgHasType(r0, r1)) => l0 == r0 && l1 == r1,
            (Self::WellFormed(l0), Self::WellFormed(r0)) => l0 == r0,
            (Self::ConstEvaluatable(l0), Self::ConstEvaluatable(r0)) => l0 == r0,
            _ => false,
        }
    }
}

impl<I: Interner> Eq for ClauseKind<I> {}

impl<I: Interner> TypeFoldable<I> for ClauseKind<I>
where
    I::Ty: TypeFoldable<I>,
    I::Const: TypeFoldable<I>,
    I::GenericArg: TypeFoldable<I>,
    I::TraitPredicate: TypeFoldable<I>,
    I::ProjectionPredicate: TypeFoldable<I>,
    I::TypeOutlivesPredicate: TypeFoldable<I>,
    I::RegionOutlivesPredicate: TypeFoldable<I>,
{
    fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
        Ok(match self {
            ClauseKind::Trait(p) => ClauseKind::Trait(p.try_fold_with(folder)?),
            ClauseKind::RegionOutlives(p) => ClauseKind::RegionOutlives(p.try_fold_with(folder)?),
            ClauseKind::TypeOutlives(p) => ClauseKind::TypeOutlives(p.try_fold_with(folder)?),
            ClauseKind::Projection(p) => ClauseKind::Projection(p.try_fold_with(folder)?),
            ClauseKind::ConstArgHasType(c, t) => {
                ClauseKind::ConstArgHasType(c.try_fold_with(folder)?, t.try_fold_with(folder)?)
            }
            ClauseKind::WellFormed(p) => ClauseKind::WellFormed(p.try_fold_with(folder)?),
            ClauseKind::ConstEvaluatable(p) => {
                ClauseKind::ConstEvaluatable(p.try_fold_with(folder)?)
            }
        })
    }
}

impl<I: Interner> TypeVisitable<I> for ClauseKind<I>
where
    I::Ty: TypeVisitable<I>,
    I::Const: TypeVisitable<I>,
    I::GenericArg: TypeVisitable<I>,
    I::TraitPredicate: TypeVisitable<I>,
    I::ProjectionPredicate: TypeVisitable<I>,
    I::TypeOutlivesPredicate: TypeVisitable<I>,
    I::RegionOutlivesPredicate: TypeVisitable<I>,
{
    fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> V::Result {
        match self {
            ClauseKind::Trait(p) => p.visit_with(visitor),
            ClauseKind::RegionOutlives(p) => p.visit_with(visitor),
            ClauseKind::TypeOutlives(p) => p.visit_with(visitor),
            ClauseKind::Projection(p) => p.visit_with(visitor),
            ClauseKind::ConstArgHasType(c, t) => {
                try_visit!(c.visit_with(visitor));
                t.visit_with(visitor)
            }
            ClauseKind::WellFormed(p) => p.visit_with(visitor),
            ClauseKind::ConstEvaluatable(p) => p.visit_with(visitor),
        }
    }
}

#[derive(derivative::Derivative)]
#[derivative(
    Clone(bound = ""),
    Copy(bound = ""),
    Hash(bound = ""),
    PartialEq(bound = ""),
    Eq(bound = "")
)]
#[cfg_attr(feature = "nightly", derive(TyEncodable, TyDecodable, HashStable_NoContext))]
pub enum PredicateKind<I: Interner> {
    /// Prove a clause
    Clause(ClauseKind<I>),

    /// Trait must be object-safe.
    ObjectSafe(I::DefId),

    /// `T1 <: T2`
    ///
    /// This obligation is created most often when we have two
    /// unresolved type variables and hence don't have enough
    /// information to process the subtyping obligation yet.
    Subtype(I::SubtypePredicate),

    /// `T1` coerced to `T2`
    ///
    /// Like a subtyping obligation, this is created most often
    /// when we have two unresolved type variables and hence
    /// don't have enough information to process the coercion
    /// obligation yet. At the moment, we actually process coercions
    /// very much like subtyping and don't handle the full coercion
    /// logic.
    Coerce(I::CoercePredicate),

    /// Constants must be equal. The first component is the const that is expected.
    ConstEquate(I::Const, I::Const),

    /// A marker predicate that is always ambiguous.
    /// Used for coherence to mark opaque types as possibly equal to each other but ambiguous.
    Ambiguous,

    /// This should only be used inside of the new solver for `AliasRelate` and expects
    /// the `term` to be an unconstrained inference variable.
    ///
    /// The alias normalizes to `term`. Unlike `Projection`, this always fails if the
    /// alias cannot be normalized in the current context. For the rigid alias
    /// `T as Trait>::Assoc`, `Projection(<T as Trait>::Assoc, ?x)` constrains `?x`
    /// to `<T as Trait>::Assoc` while `NormalizesTo(<T as Trait>::Assoc, ?x)`
    /// results in `NoSolution`.
    NormalizesTo(I::NormalizesTo),

    /// Separate from `ClauseKind::Projection` which is used for normalization in new solver.
    /// This predicate requires two terms to be equal to eachother.
    ///
    /// Only used for new solver.
    AliasRelate(I::Term, I::Term, AliasRelationDirection),
}

impl<I: Interner> TypeFoldable<I> for PredicateKind<I>
where
    I::DefId: TypeFoldable<I>,
    I::Const: TypeFoldable<I>,
    I::GenericArgs: TypeFoldable<I>,
    I::Term: TypeFoldable<I>,
    I::CoercePredicate: TypeFoldable<I>,
    I::SubtypePredicate: TypeFoldable<I>,
    I::NormalizesTo: TypeFoldable<I>,
    ClauseKind<I>: TypeFoldable<I>,
{
    fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
        Ok(match self {
            PredicateKind::Clause(c) => PredicateKind::Clause(c.try_fold_with(folder)?),
            PredicateKind::ObjectSafe(d) => PredicateKind::ObjectSafe(d.try_fold_with(folder)?),
            PredicateKind::Subtype(s) => PredicateKind::Subtype(s.try_fold_with(folder)?),
            PredicateKind::Coerce(s) => PredicateKind::Coerce(s.try_fold_with(folder)?),
            PredicateKind::ConstEquate(a, b) => {
                PredicateKind::ConstEquate(a.try_fold_with(folder)?, b.try_fold_with(folder)?)
            }
            PredicateKind::Ambiguous => PredicateKind::Ambiguous,
            PredicateKind::NormalizesTo(p) => PredicateKind::NormalizesTo(p.try_fold_with(folder)?),
            PredicateKind::AliasRelate(a, b, d) => PredicateKind::AliasRelate(
                a.try_fold_with(folder)?,
                b.try_fold_with(folder)?,
                d.try_fold_with(folder)?,
            ),
        })
    }
}

impl<I: Interner> TypeVisitable<I> for PredicateKind<I>
where
    I::DefId: TypeVisitable<I>,
    I::Const: TypeVisitable<I>,
    I::GenericArgs: TypeVisitable<I>,
    I::Term: TypeVisitable<I>,
    I::CoercePredicate: TypeVisitable<I>,
    I::SubtypePredicate: TypeVisitable<I>,
    I::NormalizesTo: TypeVisitable<I>,
    ClauseKind<I>: TypeVisitable<I>,
{
    fn visit_with<V: TypeVisitor<I>>(&self, visitor: &mut V) -> V::Result {
        match self {
            PredicateKind::Clause(p) => p.visit_with(visitor),
            PredicateKind::ObjectSafe(d) => d.visit_with(visitor),
            PredicateKind::Subtype(s) => s.visit_with(visitor),
            PredicateKind::Coerce(s) => s.visit_with(visitor),
            PredicateKind::ConstEquate(a, b) => {
                try_visit!(a.visit_with(visitor));
                b.visit_with(visitor)
            }
            PredicateKind::Ambiguous => V::Result::output(),
            PredicateKind::NormalizesTo(p) => p.visit_with(visitor),
            PredicateKind::AliasRelate(a, b, d) => {
                try_visit!(a.visit_with(visitor));
                try_visit!(b.visit_with(visitor));
                d.visit_with(visitor)
            }
        }
    }
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)]
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext, Encodable, Decodable))]
pub enum AliasRelationDirection {
    Equate,
    Subtype,
}

impl std::fmt::Display for AliasRelationDirection {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AliasRelationDirection::Equate => write!(f, "=="),
            AliasRelationDirection::Subtype => write!(f, "<:"),
        }
    }
}

// FIXME: Convert to DebugWithInfcx impl
impl<I: Interner> fmt::Debug for ClauseKind<I> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ClauseKind::ConstArgHasType(ct, ty) => write!(f, "ConstArgHasType({ct:?}, {ty:?})"),
            ClauseKind::Trait(a) => a.fmt(f),
            ClauseKind::RegionOutlives(pair) => pair.fmt(f),
            ClauseKind::TypeOutlives(pair) => pair.fmt(f),
            ClauseKind::Projection(pair) => pair.fmt(f),
            ClauseKind::WellFormed(data) => write!(f, "WellFormed({data:?})"),
            ClauseKind::ConstEvaluatable(ct) => {
                write!(f, "ConstEvaluatable({ct:?})")
            }
        }
    }
}

// FIXME: Convert to DebugWithInfcx impl
impl<I: Interner> fmt::Debug for PredicateKind<I> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PredicateKind::Clause(a) => a.fmt(f),
            PredicateKind::Subtype(pair) => pair.fmt(f),
            PredicateKind::Coerce(pair) => pair.fmt(f),
            PredicateKind::ObjectSafe(trait_def_id) => {
                write!(f, "ObjectSafe({trait_def_id:?})")
            }
            PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({c1:?}, {c2:?})"),
            PredicateKind::Ambiguous => write!(f, "Ambiguous"),
            PredicateKind::NormalizesTo(p) => p.fmt(f),
            PredicateKind::AliasRelate(t1, t2, dir) => {
                write!(f, "AliasRelate({t1:?}, {dir:?}, {t2:?})")
            }
        }
    }
}