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