rustc_next_trait_solver/solve/eval_ctxt/
canonical.rs

1//! Canonicalization is used to separate some goal from its context,
2//! throwing away unnecessary information in the process.
3//!
4//! This is necessary to cache goals containing inference variables
5//! and placeholders without restricting them to the current `InferCtxt`.
6//!
7//! Canonicalization is fairly involved, for more details see the relevant
8//! section of the [rustc-dev-guide][c].
9//!
10//! [c]: https://rustc-dev-guide.rust-lang.org/solve/canonicalization.html
11
12use std::iter;
13
14use rustc_index::IndexVec;
15use rustc_type_ir::fold::TypeFoldable;
16use rustc_type_ir::inherent::*;
17use rustc_type_ir::relate::solver_relating::RelateExt;
18use rustc_type_ir::{self as ty, Canonical, CanonicalVarValues, InferCtxtLike, Interner};
19use tracing::{debug, instrument, trace};
20
21use crate::canonicalizer::Canonicalizer;
22use crate::delegate::SolverDelegate;
23use crate::resolve::EagerResolver;
24use crate::solve::eval_ctxt::NestedGoals;
25use crate::solve::{
26    CanonicalInput, CanonicalResponse, Certainty, EvalCtxt, ExternalConstraintsData, Goal,
27    MaybeCause, NestedNormalizationGoals, NoSolution, PredefinedOpaquesData, QueryInput,
28    QueryResult, Response, inspect, response_no_constraints_raw,
29};
30
31trait ResponseT<I: Interner> {
32    fn var_values(&self) -> CanonicalVarValues<I>;
33}
34
35impl<I: Interner> ResponseT<I> for Response<I> {
36    fn var_values(&self) -> CanonicalVarValues<I> {
37        self.var_values
38    }
39}
40
41impl<I: Interner, T> ResponseT<I> for inspect::State<I, T> {
42    fn var_values(&self) -> CanonicalVarValues<I> {
43        self.var_values
44    }
45}
46
47impl<D, I> EvalCtxt<'_, D>
48where
49    D: SolverDelegate<Interner = I>,
50    I: Interner,
51{
52    /// Canonicalizes the goal remembering the original values
53    /// for each bound variable.
54    pub(super) fn canonicalize_goal<T: TypeFoldable<I>>(
55        &self,
56        goal: Goal<I, T>,
57    ) -> (Vec<I::GenericArg>, CanonicalInput<I, T>) {
58        let opaque_types = self.delegate.clone_opaque_types_for_query_response();
59        let (goal, opaque_types) =
60            (goal, opaque_types).fold_with(&mut EagerResolver::new(self.delegate));
61
62        let mut orig_values = Default::default();
63        let canonical = Canonicalizer::canonicalize_input(
64            self.delegate,
65            &mut orig_values,
66            QueryInput {
67                goal,
68                predefined_opaques_in_body: self
69                    .cx()
70                    .mk_predefined_opaques_in_body(PredefinedOpaquesData { opaque_types }),
71            },
72        );
73        let query_input = ty::CanonicalQueryInput { canonical, typing_mode: self.typing_mode() };
74        (orig_values, query_input)
75    }
76
77    /// To return the constraints of a canonical query to the caller, we canonicalize:
78    ///
79    /// - `var_values`: a map from bound variables in the canonical goal to
80    ///   the values inferred while solving the instantiated goal.
81    /// - `external_constraints`: additional constraints which aren't expressible
82    ///   using simple unification of inference variables.
83    #[instrument(level = "trace", skip(self), ret)]
84    pub(in crate::solve) fn evaluate_added_goals_and_make_canonical_response(
85        &mut self,
86        certainty: Certainty,
87    ) -> QueryResult<I> {
88        self.inspect.make_canonical_response(certainty);
89
90        let goals_certainty = self.try_evaluate_added_goals()?;
91        assert_eq!(
92            self.tainted,
93            Ok(()),
94            "EvalCtxt is tainted -- nested goals may have been dropped in a \
95            previous call to `try_evaluate_added_goals!`"
96        );
97
98        // We only check for leaks from universes which were entered inside
99        // of the query.
100        self.delegate.leak_check(self.max_input_universe).map_err(|NoSolution| {
101            trace!("failed the leak check");
102            NoSolution
103        })?;
104
105        // When normalizing, we've replaced the expected term with an unconstrained
106        // inference variable. This means that we dropped information which could
107        // have been important. We handle this by instead returning the nested goals
108        // to the caller, where they are then handled.
109        //
110        // As we return all ambiguous nested goals, we can ignore the certainty returned
111        // by `try_evaluate_added_goals()`.
112        let (certainty, normalization_nested_goals) = if self.is_normalizes_to_goal {
113            let NestedGoals { normalizes_to_goals, goals } = std::mem::take(&mut self.nested_goals);
114            if cfg!(debug_assertions) {
115                assert!(normalizes_to_goals.is_empty());
116                if goals.is_empty() {
117                    assert!(matches!(goals_certainty, Certainty::Yes));
118                }
119            }
120            (certainty, NestedNormalizationGoals(goals))
121        } else {
122            let certainty = certainty.unify_with(goals_certainty);
123            (certainty, NestedNormalizationGoals::empty())
124        };
125
126        if let Certainty::Maybe(cause @ MaybeCause::Overflow { .. }) = certainty {
127            // If we have overflow, it's probable that we're substituting a type
128            // into itself infinitely and any partial substitutions in the query
129            // response are probably not useful anyways, so just return an empty
130            // query response.
131            //
132            // This may prevent us from potentially useful inference, e.g.
133            // 2 candidates, one ambiguous and one overflow, which both
134            // have the same inference constraints.
135            //
136            // Changing this to retain some constraints in the future
137            // won't be a breaking change, so this is good enough for now.
138            return Ok(self.make_ambiguous_response_no_constraints(cause));
139        }
140
141        let external_constraints =
142            self.compute_external_query_constraints(certainty, normalization_nested_goals);
143        let (var_values, mut external_constraints) = (self.var_values, external_constraints)
144            .fold_with(&mut EagerResolver::new(self.delegate));
145        // Remove any trivial region constraints once we've resolved regions
146        external_constraints
147            .region_constraints
148            .retain(|outlives| outlives.0.as_region().is_none_or(|re| re != outlives.1));
149
150        let canonical = Canonicalizer::canonicalize_response(
151            self.delegate,
152            self.max_input_universe,
153            &mut Default::default(),
154            Response {
155                var_values,
156                certainty,
157                external_constraints: self.cx().mk_external_constraints(external_constraints),
158            },
159        );
160
161        // HACK: We bail with overflow if the response would have too many non-region
162        // inference variables. This tends to only happen if we encounter a lot of
163        // ambiguous alias types which get replaced with fresh inference variables
164        // during generalization. This prevents hangs caused by an exponential blowup,
165        // see tests/ui/traits/next-solver/coherence-alias-hang.rs.
166        //
167        // We don't do so for `NormalizesTo` goals as we erased the expected term and
168        // bailing with overflow here would prevent us from detecting a type-mismatch,
169        // causing a coherence error in diesel, see #131969. We still bail with overflow
170        // when later returning from the parent AliasRelate goal.
171        if !self.is_normalizes_to_goal {
172            let num_non_region_vars =
173                canonical.variables.iter().filter(|c| !c.is_region() && c.is_existential()).count();
174            if num_non_region_vars > self.cx().recursion_limit() {
175                debug!(?num_non_region_vars, "too many inference variables -> overflow");
176                return Ok(self.make_ambiguous_response_no_constraints(MaybeCause::Overflow {
177                    suggest_increasing_limit: true,
178                }));
179            }
180        }
181
182        Ok(canonical)
183    }
184
185    /// Constructs a totally unconstrained, ambiguous response to a goal.
186    ///
187    /// Take care when using this, since often it's useful to respond with
188    /// ambiguity but return constrained variables to guide inference.
189    pub(in crate::solve) fn make_ambiguous_response_no_constraints(
190        &self,
191        maybe_cause: MaybeCause,
192    ) -> CanonicalResponse<I> {
193        response_no_constraints_raw(
194            self.cx(),
195            self.max_input_universe,
196            self.variables,
197            Certainty::Maybe(maybe_cause),
198        )
199    }
200
201    /// Computes the region constraints and *new* opaque types registered when
202    /// proving a goal.
203    ///
204    /// If an opaque was already constrained before proving this goal, then the
205    /// external constraints do not need to record that opaque, since if it is
206    /// further constrained by inference, that will be passed back in the var
207    /// values.
208    #[instrument(level = "trace", skip(self), ret)]
209    fn compute_external_query_constraints(
210        &self,
211        certainty: Certainty,
212        normalization_nested_goals: NestedNormalizationGoals<I>,
213    ) -> ExternalConstraintsData<I> {
214        // We only return region constraints once the certainty is `Yes`. This
215        // is necessary as we may drop nested goals on ambiguity, which may result
216        // in unconstrained inference variables in the region constraints. It also
217        // prevents us from emitting duplicate region constraints, avoiding some
218        // unnecessary work. This slightly weakens the leak check in case it uses
219        // region constraints from an ambiguous nested goal. This is tested in both
220        // `tests/ui/higher-ranked/leak-check/leak-check-in-selection-5-ambig.rs` and
221        // `tests/ui/higher-ranked/leak-check/leak-check-in-selection-6-ambig-unify.rs`.
222        let region_constraints = if certainty == Certainty::Yes {
223            self.delegate.make_deduplicated_outlives_constraints()
224        } else {
225            Default::default()
226        };
227
228        ExternalConstraintsData {
229            region_constraints,
230            opaque_types: self
231                .delegate
232                .clone_opaque_types_for_query_response()
233                .into_iter()
234                // Only return *newly defined* opaque types.
235                .filter(|(a, _)| {
236                    self.predefined_opaques_in_body.opaque_types.iter().all(|(pa, _)| pa != a)
237                })
238                .collect(),
239            normalization_nested_goals,
240        }
241    }
242
243    /// After calling a canonical query, we apply the constraints returned
244    /// by the query using this function.
245    ///
246    /// This happens in three steps:
247    /// - we instantiate the bound variables of the query response
248    /// - we unify the `var_values` of the response with the `original_values`
249    /// - we apply the `external_constraints` returned by the query, returning
250    ///   the `normalization_nested_goals`
251    pub(super) fn instantiate_and_apply_query_response(
252        &mut self,
253        param_env: I::ParamEnv,
254        original_values: Vec<I::GenericArg>,
255        response: CanonicalResponse<I>,
256    ) -> (NestedNormalizationGoals<I>, Certainty) {
257        let instantiation = Self::compute_query_response_instantiation_values(
258            self.delegate,
259            &original_values,
260            &response,
261            self.origin_span,
262        );
263
264        let Response { var_values, external_constraints, certainty } =
265            self.delegate.instantiate_canonical(response, instantiation);
266
267        Self::unify_query_var_values(
268            self.delegate,
269            param_env,
270            &original_values,
271            var_values,
272            self.origin_span,
273        );
274
275        let ExternalConstraintsData {
276            region_constraints,
277            opaque_types,
278            normalization_nested_goals,
279        } = &*external_constraints;
280
281        self.register_region_constraints(region_constraints);
282        self.register_new_opaque_types(opaque_types);
283
284        (normalization_nested_goals.clone(), certainty)
285    }
286
287    /// This returns the canonical variable values to instantiate the bound variables of
288    /// the canonical response. This depends on the `original_values` for the
289    /// bound variables.
290    fn compute_query_response_instantiation_values<T: ResponseT<I>>(
291        delegate: &D,
292        original_values: &[I::GenericArg],
293        response: &Canonical<I, T>,
294        span: I::Span,
295    ) -> CanonicalVarValues<I> {
296        // FIXME: Longterm canonical queries should deal with all placeholders
297        // created inside of the query directly instead of returning them to the
298        // caller.
299        let prev_universe = delegate.universe();
300        let universes_created_in_query = response.max_universe.index();
301        for _ in 0..universes_created_in_query {
302            delegate.create_next_universe();
303        }
304
305        let var_values = response.value.var_values();
306        assert_eq!(original_values.len(), var_values.len());
307
308        // If the query did not make progress with constraining inference variables,
309        // we would normally create a new inference variables for bound existential variables
310        // only then unify this new inference variable with the inference variable from
311        // the input.
312        //
313        // We therefore instantiate the existential variable in the canonical response with the
314        // inference variable of the input right away, which is more performant.
315        let mut opt_values = IndexVec::from_elem_n(None, response.variables.len());
316        for (original_value, result_value) in
317            iter::zip(original_values, var_values.var_values.iter())
318        {
319            match result_value.kind() {
320                ty::GenericArgKind::Type(t) => {
321                    if let ty::Bound(debruijn, b) = t.kind() {
322                        assert_eq!(debruijn, ty::INNERMOST);
323                        opt_values[b.var()] = Some(*original_value);
324                    }
325                }
326                ty::GenericArgKind::Lifetime(r) => {
327                    if let ty::ReBound(debruijn, br) = r.kind() {
328                        assert_eq!(debruijn, ty::INNERMOST);
329                        opt_values[br.var()] = Some(*original_value);
330                    }
331                }
332                ty::GenericArgKind::Const(c) => {
333                    if let ty::ConstKind::Bound(debruijn, bv) = c.kind() {
334                        assert_eq!(debruijn, ty::INNERMOST);
335                        opt_values[bv.var()] = Some(*original_value);
336                    }
337                }
338            }
339        }
340
341        let var_values = delegate.cx().mk_args_from_iter(
342            response.variables.iter().enumerate().map(|(index, info)| {
343                if info.universe() != ty::UniverseIndex::ROOT {
344                    // A variable from inside a binder of the query. While ideally these shouldn't
345                    // exist at all (see the FIXME at the start of this method), we have to deal with
346                    // them for now.
347                    delegate.instantiate_canonical_var_with_infer(info, span, |idx| {
348                        ty::UniverseIndex::from(prev_universe.index() + idx.index())
349                    })
350                } else if info.is_existential() {
351                    // As an optimization we sometimes avoid creating a new inference variable here.
352                    //
353                    // All new inference variables we create start out in the current universe of the caller.
354                    // This is conceptually wrong as these inference variables would be able to name
355                    // more placeholders then they should be able to. However the inference variables have
356                    // to "come from somewhere", so by equating them with the original values of the caller
357                    // later on, we pull them down into their correct universe again.
358                    if let Some(v) = opt_values[ty::BoundVar::from_usize(index)] {
359                        v
360                    } else {
361                        delegate.instantiate_canonical_var_with_infer(info, span, |_| prev_universe)
362                    }
363                } else {
364                    // For placeholders which were already part of the input, we simply map this
365                    // universal bound variable back the placeholder of the input.
366                    original_values[info.expect_placeholder_index()]
367                }
368            }),
369        );
370
371        CanonicalVarValues { var_values }
372    }
373
374    /// Unify the `original_values` with the `var_values` returned by the canonical query..
375    ///
376    /// This assumes that this unification will always succeed. This is the case when
377    /// applying a query response right away. However, calling a canonical query, doing any
378    /// other kind of trait solving, and only then instantiating the result of the query
379    /// can cause the instantiation to fail. This is not supported and we ICE in this case.
380    ///
381    /// We always structurally instantiate aliases. Relating aliases needs to be different
382    /// depending on whether the alias is *rigid* or not. We're only really able to tell
383    /// whether an alias is rigid by using the trait solver. When instantiating a response
384    /// from the solver we assume that the solver correctly handled aliases and therefore
385    /// always relate them structurally here.
386    #[instrument(level = "trace", skip(delegate))]
387    fn unify_query_var_values(
388        delegate: &D,
389        param_env: I::ParamEnv,
390        original_values: &[I::GenericArg],
391        var_values: CanonicalVarValues<I>,
392        span: I::Span,
393    ) {
394        assert_eq!(original_values.len(), var_values.len());
395
396        for (&orig, response) in iter::zip(original_values, var_values.var_values.iter()) {
397            let goals =
398                delegate.eq_structurally_relating_aliases(param_env, orig, response, span).unwrap();
399            assert!(goals.is_empty());
400        }
401    }
402
403    fn register_region_constraints(
404        &mut self,
405        outlives: &[ty::OutlivesPredicate<I, I::GenericArg>],
406    ) {
407        for &ty::OutlivesPredicate(lhs, rhs) in outlives {
408            match lhs.kind() {
409                ty::GenericArgKind::Lifetime(lhs) => self.register_region_outlives(lhs, rhs),
410                ty::GenericArgKind::Type(lhs) => self.register_ty_outlives(lhs, rhs),
411                ty::GenericArgKind::Const(_) => panic!("const outlives: {lhs:?}: {rhs:?}"),
412            }
413        }
414    }
415
416    fn register_new_opaque_types(&mut self, opaque_types: &[(ty::OpaqueTypeKey<I>, I::Ty)]) {
417        for &(key, ty) in opaque_types {
418            self.delegate.inject_new_hidden_type_unchecked(key, ty, self.origin_span);
419        }
420    }
421}
422
423/// Used by proof trees to be able to recompute intermediate actions while
424/// evaluating a goal. The `var_values` not only include the bound variables
425/// of the query input, but also contain all unconstrained inference vars
426/// created while evaluating this goal.
427pub(in crate::solve) fn make_canonical_state<D, T, I>(
428    delegate: &D,
429    var_values: &[I::GenericArg],
430    max_input_universe: ty::UniverseIndex,
431    data: T,
432) -> inspect::CanonicalState<I, T>
433where
434    D: SolverDelegate<Interner = I>,
435    I: Interner,
436    T: TypeFoldable<I>,
437{
438    let var_values = CanonicalVarValues { var_values: delegate.cx().mk_args(var_values) };
439    let state = inspect::State { var_values, data };
440    let state = state.fold_with(&mut EagerResolver::new(delegate));
441    Canonicalizer::canonicalize_response(delegate, max_input_universe, &mut vec![], state)
442}
443
444// FIXME: needs to be pub to be accessed by downstream
445// `rustc_trait_selection::solve::inspect::analyse`.
446pub fn instantiate_canonical_state<D, I, T: TypeFoldable<I>>(
447    delegate: &D,
448    span: I::Span,
449    param_env: I::ParamEnv,
450    orig_values: &mut Vec<I::GenericArg>,
451    state: inspect::CanonicalState<I, T>,
452) -> T
453where
454    D: SolverDelegate<Interner = I>,
455    I: Interner,
456{
457    // In case any fresh inference variables have been created between `state`
458    // and the previous instantiation, extend `orig_values` for it.
459    orig_values.extend(
460        state.value.var_values.var_values.as_slice()[orig_values.len()..]
461            .iter()
462            .map(|&arg| delegate.fresh_var_for_kind_with_span(arg, span)),
463    );
464
465    let instantiation =
466        EvalCtxt::compute_query_response_instantiation_values(delegate, orig_values, &state, span);
467
468    let inspect::State { var_values, data } = delegate.instantiate_canonical(state, instantiation);
469
470    EvalCtxt::unify_query_var_values(delegate, param_env, orig_values, var_values, span);
471    data
472}