Unnamed repository; edit this file 'description' to name the repository.
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
use hir_def::TraitId;
use rustc_type_ir::{TypeFoldable, Upcast, Variance};

use crate::next_solver::{
    Const, DbInterner, ParamEnv, Term, TraitRef, Ty, TypeError,
    fulfill::{FulfillmentCtxt, NextSolverError},
    infer::{
        InferCtxt, InferOk,
        at::ToTrace,
        traits::{Obligation, ObligationCause, PredicateObligation, PredicateObligations},
    },
};

/// Used if you want to have pleasant experience when dealing
/// with obligations outside of hir or mir typeck.
pub struct ObligationCtxt<'a, 'db> {
    pub infcx: &'a InferCtxt<'db>,
    engine: FulfillmentCtxt<'db>,
}

impl<'a, 'db> ObligationCtxt<'a, 'db> {
    pub fn new(infcx: &'a InferCtxt<'db>) -> Self {
        Self { infcx, engine: FulfillmentCtxt::new(infcx) }
    }
}

impl<'a, 'db> ObligationCtxt<'a, 'db> {
    pub fn register_obligation(&mut self, obligation: PredicateObligation<'db>) {
        self.engine.register_predicate_obligation(self.infcx, obligation);
    }

    pub fn register_obligations(
        &mut self,
        obligations: impl IntoIterator<Item = PredicateObligation<'db>>,
    ) {
        self.engine.register_predicate_obligations(self.infcx, obligations);
    }

    pub fn register_infer_ok_obligations<T>(&mut self, infer_ok: InferOk<'db, T>) -> T {
        let InferOk { value, obligations } = infer_ok;
        self.register_obligations(obligations);
        value
    }

    /// Requires that `ty` must implement the trait with `def_id` in
    /// the given environment. This trait must not have any type
    /// parameters (except for `Self`).
    pub fn register_bound(
        &mut self,
        cause: ObligationCause,
        param_env: ParamEnv<'db>,
        ty: Ty<'db>,
        def_id: TraitId,
    ) {
        let trait_ref = TraitRef::new(self.infcx.interner, def_id.into(), [ty]);
        self.register_obligation(Obligation {
            cause,
            recursion_depth: 0,
            param_env,
            predicate: trait_ref.upcast(self.infcx.interner),
        });
    }

    pub fn eq<T: ToTrace<'db>>(
        &mut self,
        cause: &ObligationCause,
        param_env: ParamEnv<'db>,
        expected: T,
        actual: T,
    ) -> Result<(), TypeError<'db>> {
        self.infcx
            .at(cause, param_env)
            .eq(expected, actual)
            .map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
    }

    /// Checks whether `expected` is a subtype of `actual`: `expected <: actual`.
    pub fn sub<T: ToTrace<'db>>(
        &mut self,
        cause: &ObligationCause,
        param_env: ParamEnv<'db>,
        expected: T,
        actual: T,
    ) -> Result<(), TypeError<'db>> {
        self.infcx
            .at(cause, param_env)
            .sub(expected, actual)
            .map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
    }

    pub fn relate<T: ToTrace<'db>>(
        &mut self,
        cause: &ObligationCause,
        param_env: ParamEnv<'db>,
        variance: Variance,
        expected: T,
        actual: T,
    ) -> Result<(), TypeError<'db>> {
        self.infcx
            .at(cause, param_env)
            .relate(expected, variance, actual)
            .map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
    }

    /// Checks whether `expected` is a supertype of `actual`: `expected :> actual`.
    pub fn sup<T: ToTrace<'db>>(
        &mut self,
        cause: &ObligationCause,
        param_env: ParamEnv<'db>,
        expected: T,
        actual: T,
    ) -> Result<(), TypeError<'db>> {
        self.infcx
            .at(cause, param_env)
            .sup(expected, actual)
            .map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
    }

    /// Computes the least-upper-bound, or mutual supertype, of two values.
    pub fn lub<T: ToTrace<'db>>(
        &mut self,
        cause: &ObligationCause,
        param_env: ParamEnv<'db>,
        expected: T,
        actual: T,
    ) -> Result<T, TypeError<'db>> {
        self.infcx
            .at(cause, param_env)
            .lub(expected, actual)
            .map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
    }

    #[must_use]
    pub fn try_evaluate_obligations(&mut self) -> Vec<NextSolverError<'db>> {
        self.engine.try_evaluate_obligations(self.infcx)
    }

    #[must_use]
    pub fn evaluate_obligations_error_on_ambiguity(&mut self) -> Vec<NextSolverError<'db>> {
        self.engine.evaluate_obligations_error_on_ambiguity(self.infcx)
    }

    /// Returns the not-yet-processed and stalled obligations from the
    /// `ObligationCtxt`.
    ///
    /// Takes ownership of the context as doing operations such as
    /// [`ObligationCtxt::eq`] afterwards will result in other obligations
    /// getting ignored. You can make a new `ObligationCtxt` if this
    /// needs to be done in a loop, for example.
    #[must_use]
    pub fn into_pending_obligations(self) -> PredicateObligations<'db> {
        self.engine.pending_obligations()
    }

    pub fn deeply_normalize<T: TypeFoldable<DbInterner<'db>>>(
        &self,
        cause: &ObligationCause,
        param_env: ParamEnv<'db>,
        value: T,
    ) -> Result<T, Vec<NextSolverError<'db>>> {
        self.infcx.at(cause, param_env).deeply_normalize(value)
    }

    pub fn structurally_normalize_ty(
        &mut self,
        cause: &ObligationCause,
        param_env: ParamEnv<'db>,
        value: Ty<'db>,
    ) -> Result<Ty<'db>, Vec<NextSolverError<'db>>> {
        self.infcx.at(cause, param_env).structurally_normalize_ty(value, &mut self.engine)
    }

    pub fn structurally_normalize_const(
        &mut self,
        cause: &ObligationCause,
        param_env: ParamEnv<'db>,
        value: Const<'db>,
    ) -> Result<Const<'db>, Vec<NextSolverError<'db>>> {
        self.infcx.at(cause, param_env).structurally_normalize_const(value, &mut self.engine)
    }

    pub fn structurally_normalize_term(
        &mut self,
        cause: &ObligationCause,
        param_env: ParamEnv<'db>,
        value: Term<'db>,
    ) -> Result<Term<'db>, Vec<NextSolverError<'db>>> {
        self.infcx.at(cause, param_env).structurally_normalize_term(value, &mut self.engine)
    }
}