rustc_next_trait_solver/canonical/
mod.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 canonicalizer::Canonicalizer;
15use rustc_index::IndexVec;
16use rustc_type_ir::inherent::*;
17use rustc_type_ir::relate::solver_relating::RelateExt;
18use rustc_type_ir::{
19    self as ty, Canonical, CanonicalVarKind, CanonicalVarValues, InferCtxtLike, Interner,
20    TypeFoldable,
21};
22use tracing::instrument;
23
24use crate::delegate::SolverDelegate;
25use crate::resolve::eager_resolve_vars;
26use crate::solve::{
27    CanonicalInput, CanonicalResponse, Certainty, ExternalConstraintsData, Goal,
28    NestedNormalizationGoals, PredefinedOpaquesData, QueryInput, Response, inspect,
29};
30
31pub mod canonicalizer;
32
33trait ResponseT<I: Interner> {
34    fn var_values(&self) -> CanonicalVarValues<I>;
35}
36
37impl<I: Interner> ResponseT<I> for Response<I> {
38    fn var_values(&self) -> CanonicalVarValues<I> {
39        self.var_values
40    }
41}
42
43impl<I: Interner, T> ResponseT<I> for inspect::State<I, T> {
44    fn var_values(&self) -> CanonicalVarValues<I> {
45        self.var_values
46    }
47}
48
49/// Canonicalizes the goal remembering the original values
50/// for each bound variable.
51///
52/// This expects `goal` and `opaque_types` to be eager resolved.
53pub(super) fn canonicalize_goal<D, I>(
54    delegate: &D,
55    goal: Goal<I, I::Predicate>,
56    opaque_types: Vec<(ty::OpaqueTypeKey<I>, I::Ty)>,
57) -> (Vec<I::GenericArg>, CanonicalInput<I, I::Predicate>)
58where
59    D: SolverDelegate<Interner = I>,
60    I: Interner,
61{
62    let mut orig_values = Default::default();
63    let canonical = Canonicalizer::canonicalize_input(
64        delegate,
65        &mut orig_values,
66        QueryInput {
67            goal,
68            predefined_opaques_in_body: delegate
69                .cx()
70                .mk_predefined_opaques_in_body(PredefinedOpaquesData { opaque_types }),
71        },
72    );
73    let query_input = ty::CanonicalQueryInput { canonical, typing_mode: delegate.typing_mode() };
74    (orig_values, query_input)
75}
76
77pub(super) fn canonicalize_response<D, I, T>(
78    delegate: &D,
79    max_input_universe: ty::UniverseIndex,
80    value: T,
81) -> ty::Canonical<I, T>
82where
83    D: SolverDelegate<Interner = I>,
84    I: Interner,
85    T: TypeFoldable<I>,
86{
87    let mut orig_values = Default::default();
88    let canonical =
89        Canonicalizer::canonicalize_response(delegate, max_input_universe, &mut orig_values, value);
90    canonical
91}
92
93/// After calling a canonical query, we apply the constraints returned
94/// by the query using this function.
95///
96/// This happens in three steps:
97/// - we instantiate the bound variables of the query response
98/// - we unify the `var_values` of the response with the `original_values`
99/// - we apply the `external_constraints` returned by the query, returning
100///   the `normalization_nested_goals`
101pub(super) fn instantiate_and_apply_query_response<D, I>(
102    delegate: &D,
103    param_env: I::ParamEnv,
104    original_values: &[I::GenericArg],
105    response: CanonicalResponse<I>,
106    span: I::Span,
107) -> (NestedNormalizationGoals<I>, Certainty)
108where
109    D: SolverDelegate<Interner = I>,
110    I: Interner,
111{
112    let instantiation =
113        compute_query_response_instantiation_values(delegate, &original_values, &response, span);
114
115    let Response { var_values, external_constraints, certainty } =
116        delegate.instantiate_canonical(response, instantiation);
117
118    unify_query_var_values(delegate, param_env, &original_values, var_values, span);
119
120    let ExternalConstraintsData { region_constraints, opaque_types, normalization_nested_goals } =
121        &*external_constraints;
122
123    register_region_constraints(delegate, region_constraints, span);
124    register_new_opaque_types(delegate, opaque_types, span);
125
126    (normalization_nested_goals.clone(), certainty)
127}
128
129/// This returns the canonical variable values to instantiate the bound variables of
130/// the canonical response. This depends on the `original_values` for the
131/// bound variables.
132fn compute_query_response_instantiation_values<D, I, T>(
133    delegate: &D,
134    original_values: &[I::GenericArg],
135    response: &Canonical<I, T>,
136    span: I::Span,
137) -> CanonicalVarValues<I>
138where
139    D: SolverDelegate<Interner = I>,
140    I: Interner,
141    T: ResponseT<I>,
142{
143    // FIXME: Longterm canonical queries should deal with all placeholders
144    // created inside of the query directly instead of returning them to the
145    // caller.
146    let prev_universe = delegate.universe();
147    let universes_created_in_query = response.max_universe.index();
148    for _ in 0..universes_created_in_query {
149        delegate.create_next_universe();
150    }
151
152    let var_values = response.value.var_values();
153    assert_eq!(original_values.len(), var_values.len());
154
155    // If the query did not make progress with constraining inference variables,
156    // we would normally create a new inference variables for bound existential variables
157    // only then unify this new inference variable with the inference variable from
158    // the input.
159    //
160    // We therefore instantiate the existential variable in the canonical response with the
161    // inference variable of the input right away, which is more performant.
162    let mut opt_values = IndexVec::from_elem_n(None, response.variables.len());
163    for (original_value, result_value) in iter::zip(original_values, var_values.var_values.iter()) {
164        match result_value.kind() {
165            ty::GenericArgKind::Type(t) => {
166                // We disable the instantiation guess for inference variables
167                // and only use it for placeholders. We need to handle the
168                // `sub_root` of type inference variables which would make this
169                // more involved. They are also a lot rarer than region variables.
170                if let ty::Bound(debruijn, b) = t.kind()
171                    && !matches!(
172                        response.variables.get(b.var().as_usize()).unwrap(),
173                        CanonicalVarKind::Ty { .. }
174                    )
175                {
176                    assert_eq!(debruijn, ty::INNERMOST);
177                    opt_values[b.var()] = Some(*original_value);
178                }
179            }
180            ty::GenericArgKind::Lifetime(r) => {
181                if let ty::ReBound(debruijn, br) = r.kind() {
182                    assert_eq!(debruijn, ty::INNERMOST);
183                    opt_values[br.var()] = Some(*original_value);
184                }
185            }
186            ty::GenericArgKind::Const(c) => {
187                if let ty::ConstKind::Bound(debruijn, bv) = c.kind() {
188                    assert_eq!(debruijn, ty::INNERMOST);
189                    opt_values[bv.var()] = Some(*original_value);
190                }
191            }
192        }
193    }
194    CanonicalVarValues::instantiate(delegate.cx(), response.variables, |var_values, kind| {
195        if kind.universe() != ty::UniverseIndex::ROOT {
196            // A variable from inside a binder of the query. While ideally these shouldn't
197            // exist at all (see the FIXME at the start of this method), we have to deal with
198            // them for now.
199            delegate.instantiate_canonical_var(kind, span, &var_values, |idx| {
200                prev_universe + idx.index()
201            })
202        } else if kind.is_existential() {
203            // As an optimization we sometimes avoid creating a new inference variable here.
204            //
205            // All new inference variables we create start out in the current universe of the caller.
206            // This is conceptually wrong as these inference variables would be able to name
207            // more placeholders then they should be able to. However the inference variables have
208            // to "come from somewhere", so by equating them with the original values of the caller
209            // later on, we pull them down into their correct universe again.
210            if let Some(v) = opt_values[ty::BoundVar::from_usize(var_values.len())] {
211                v
212            } else {
213                delegate.instantiate_canonical_var(kind, span, &var_values, |_| prev_universe)
214            }
215        } else {
216            // For placeholders which were already part of the input, we simply map this
217            // universal bound variable back the placeholder of the input.
218            original_values[kind.expect_placeholder_index()]
219        }
220    })
221}
222
223/// Unify the `original_values` with the `var_values` returned by the canonical query..
224///
225/// This assumes that this unification will always succeed. This is the case when
226/// applying a query response right away. However, calling a canonical query, doing any
227/// other kind of trait solving, and only then instantiating the result of the query
228/// can cause the instantiation to fail. This is not supported and we ICE in this case.
229///
230/// We always structurally instantiate aliases. Relating aliases needs to be different
231/// depending on whether the alias is *rigid* or not. We're only really able to tell
232/// whether an alias is rigid by using the trait solver. When instantiating a response
233/// from the solver we assume that the solver correctly handled aliases and therefore
234/// always relate them structurally here.
235#[instrument(level = "trace", skip(delegate))]
236fn unify_query_var_values<D, I>(
237    delegate: &D,
238    param_env: I::ParamEnv,
239    original_values: &[I::GenericArg],
240    var_values: CanonicalVarValues<I>,
241    span: I::Span,
242) where
243    D: SolverDelegate<Interner = I>,
244    I: Interner,
245{
246    assert_eq!(original_values.len(), var_values.len());
247
248    for (&orig, response) in iter::zip(original_values, var_values.var_values.iter()) {
249        let goals =
250            delegate.eq_structurally_relating_aliases(param_env, orig, response, span).unwrap();
251        assert!(goals.is_empty());
252    }
253}
254
255fn register_region_constraints<D, I>(
256    delegate: &D,
257    outlives: &[ty::OutlivesPredicate<I, I::GenericArg>],
258    span: I::Span,
259) where
260    D: SolverDelegate<Interner = I>,
261    I: Interner,
262{
263    for &ty::OutlivesPredicate(lhs, rhs) in outlives {
264        match lhs.kind() {
265            ty::GenericArgKind::Lifetime(lhs) => delegate.sub_regions(rhs, lhs, span),
266            ty::GenericArgKind::Type(lhs) => delegate.register_ty_outlives(lhs, rhs, span),
267            ty::GenericArgKind::Const(_) => panic!("const outlives: {lhs:?}: {rhs:?}"),
268        }
269    }
270}
271
272fn register_new_opaque_types<D, I>(
273    delegate: &D,
274    opaque_types: &[(ty::OpaqueTypeKey<I>, I::Ty)],
275    span: I::Span,
276) where
277    D: SolverDelegate<Interner = I>,
278    I: Interner,
279{
280    for &(key, ty) in opaque_types {
281        let prev = delegate.register_hidden_type_in_storage(key, ty, span);
282        // We eagerly resolve inference variables when computing the query response.
283        // This can cause previously distinct opaque type keys to now be structurally equal.
284        //
285        // To handle this, we store any duplicate entries in a separate list to check them
286        // at the end of typeck/borrowck. We could alternatively eagerly equate the hidden
287        // types here. However, doing so is difficult as it may result in nested goals and
288        // any errors may make it harder to track the control flow for diagnostics.
289        if let Some(prev) = prev {
290            delegate.add_duplicate_opaque_type(key, prev, span);
291        }
292    }
293}
294
295/// Used by proof trees to be able to recompute intermediate actions while
296/// evaluating a goal. The `var_values` not only include the bound variables
297/// of the query input, but also contain all unconstrained inference vars
298/// created while evaluating this goal.
299pub fn make_canonical_state<D, I, T>(
300    delegate: &D,
301    var_values: &[I::GenericArg],
302    max_input_universe: ty::UniverseIndex,
303    data: T,
304) -> inspect::CanonicalState<I, T>
305where
306    D: SolverDelegate<Interner = I>,
307    I: Interner,
308    T: TypeFoldable<I>,
309{
310    let var_values = CanonicalVarValues { var_values: delegate.cx().mk_args(var_values) };
311    let state = inspect::State { var_values, data };
312    let state = eager_resolve_vars(delegate, state);
313    Canonicalizer::canonicalize_response(delegate, max_input_universe, &mut vec![], state)
314}
315
316// FIXME: needs to be pub to be accessed by downstream
317// `rustc_trait_selection::solve::inspect::analyse`.
318pub fn instantiate_canonical_state<D, I, T>(
319    delegate: &D,
320    span: I::Span,
321    param_env: I::ParamEnv,
322    orig_values: &mut Vec<I::GenericArg>,
323    state: inspect::CanonicalState<I, T>,
324) -> T
325where
326    D: SolverDelegate<Interner = I>,
327    I: Interner,
328    T: TypeFoldable<I>,
329{
330    // In case any fresh inference variables have been created between `state`
331    // and the previous instantiation, extend `orig_values` for it.
332    orig_values.extend(
333        state.value.var_values.var_values.as_slice()[orig_values.len()..]
334            .iter()
335            .map(|&arg| delegate.fresh_var_for_kind_with_span(arg, span)),
336    );
337
338    let instantiation =
339        compute_query_response_instantiation_values(delegate, orig_values, &state, span);
340
341    let inspect::State { var_values, data } = delegate.instantiate_canonical(state, instantiation);
342
343    unify_query_var_values(delegate, param_env, orig_values, var_values, span);
344    data
345}
346
347pub fn response_no_constraints_raw<I: Interner>(
348    cx: I,
349    max_universe: ty::UniverseIndex,
350    variables: I::CanonicalVarKinds,
351    certainty: Certainty,
352) -> CanonicalResponse<I> {
353    ty::Canonical {
354        max_universe,
355        variables,
356        value: Response {
357            var_values: ty::CanonicalVarValues::make_identity(cx, variables),
358            // FIXME: maybe we should store the "no response" version in cx, like
359            // we do for cx.types and stuff.
360            external_constraints: cx.mk_external_constraints(ExternalConstraintsData::default()),
361            certainty,
362        },
363    }
364}