Skip to main content

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, TypingModeEqWrapper,
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, VisibleForLeakCheck, 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 (orig_values, canonical) = Canonicalizer::canonicalize_input(
63        delegate,
64        QueryInput {
65            goal,
66            predefined_opaques_in_body: delegate.cx().mk_predefined_opaques_in_body(opaque_types),
67        },
68    );
69    let query_input = ty::CanonicalQueryInput {
70        canonical,
71        typing_mode: TypingModeEqWrapper(delegate.typing_mode()),
72    };
73    (orig_values, query_input)
74}
75
76pub(super) fn canonicalize_response<D, I, T>(
77    delegate: &D,
78    max_input_universe: ty::UniverseIndex,
79    value: T,
80) -> ty::Canonical<I, T>
81where
82    D: SolverDelegate<Interner = I>,
83    I: Interner,
84    T: TypeFoldable<I>,
85{
86    Canonicalizer::canonicalize_response(delegate, max_input_universe, value)
87}
88
89/// After calling a canonical query, we apply the constraints returned
90/// by the query using this function.
91///
92/// This happens in three steps:
93/// - we instantiate the bound variables of the query response
94/// - we unify the `var_values` of the response with the `original_values`
95/// - we apply the `external_constraints` returned by the query, returning
96///   the `normalization_nested_goals`
97pub(super) fn instantiate_and_apply_query_response<D, I>(
98    delegate: &D,
99    param_env: I::ParamEnv,
100    original_values: &[I::GenericArg],
101    response: CanonicalResponse<I>,
102    visible_for_leak_check: VisibleForLeakCheck,
103    span: I::Span,
104) -> (NestedNormalizationGoals<I>, Certainty)
105where
106    D: SolverDelegate<Interner = I>,
107    I: Interner,
108{
109    let instantiation =
110        compute_query_response_instantiation_values(delegate, &original_values, &response, span);
111
112    let Response { var_values, external_constraints, certainty } =
113        delegate.instantiate_canonical(response, instantiation);
114
115    unify_query_var_values(delegate, param_env, &original_values, var_values, span);
116
117    let ExternalConstraintsData { region_constraints, opaque_types, normalization_nested_goals } =
118        &*external_constraints;
119
120    register_region_constraints(
121        delegate,
122        region_constraints.iter().map(|(c, vis)| (*c, vis.and(visible_for_leak_check))),
123        span,
124    );
125    register_new_opaque_types(delegate, opaque_types, span);
126
127    (normalization_nested_goals.clone(), certainty)
128}
129
130/// This returns the canonical variable values to instantiate the bound variables of
131/// the canonical response. This depends on the `original_values` for the
132/// bound variables.
133fn compute_query_response_instantiation_values<D, I, T>(
134    delegate: &D,
135    original_values: &[I::GenericArg],
136    response: &Canonical<I, T>,
137    span: I::Span,
138) -> CanonicalVarValues<I>
139where
140    D: SolverDelegate<Interner = I>,
141    I: Interner,
142    T: ResponseT<I>,
143{
144    // FIXME: Longterm canonical queries should deal with all placeholders
145    // created inside of the query directly instead of returning them to the
146    // caller.
147    let prev_universe = delegate.universe();
148    let universes_created_in_query = response.max_universe.index();
149    for _ in 0..universes_created_in_query {
150        delegate.create_next_universe();
151    }
152
153    let var_values = response.value.var_values();
154    match (&original_values.len(), &var_values.len()) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(original_values.len(), var_values.len());
155
156    // If the query did not make progress with constraining inference variables,
157    // we would normally create a new inference variables for bound existential variables
158    // only then unify this new inference variable with the inference variable from
159    // the input.
160    //
161    // We therefore instantiate the existential variable in the canonical response with the
162    // inference variable of the input right away, which is more performant.
163    let mut opt_values = IndexVec::from_elem_n(None, response.var_kinds.len());
164    for (original_value, result_value) in iter::zip(original_values, var_values.var_values.iter()) {
165        match result_value.kind() {
166            ty::GenericArgKind::Type(t) => {
167                // We disable the instantiation guess for inference variables
168                // and only use it for placeholders. We need to handle the
169                // `sub_root` of type inference variables which would make this
170                // more involved. They are also a lot rarer than region variables.
171                if let ty::Bound(index_kind, b) = t.kind()
172                    && !#[allow(non_exhaustive_omitted_patterns)] match response.var_kinds.get(b.var().as_usize()).unwrap()
    {
    CanonicalVarKind::Ty { .. } => true,
    _ => false,
}matches!(
173                        response.var_kinds.get(b.var().as_usize()).unwrap(),
174                        CanonicalVarKind::Ty { .. }
175                    )
176                {
177                    if !#[allow(non_exhaustive_omitted_patterns)] match index_kind {
            ty::BoundVarIndexKind::Canonical => true,
            _ => false,
        } {
    ::core::panicking::panic("assertion failed: matches!(index_kind, ty::BoundVarIndexKind::Canonical)")
};assert!(matches!(index_kind, ty::BoundVarIndexKind::Canonical));
178                    opt_values[b.var()] = Some(*original_value);
179                }
180            }
181            ty::GenericArgKind::Lifetime(r) => {
182                if let ty::ReBound(index_kind, br) = r.kind() {
183                    if !#[allow(non_exhaustive_omitted_patterns)] match index_kind {
            ty::BoundVarIndexKind::Canonical => true,
            _ => false,
        } {
    ::core::panicking::panic("assertion failed: matches!(index_kind, ty::BoundVarIndexKind::Canonical)")
};assert!(matches!(index_kind, ty::BoundVarIndexKind::Canonical));
184                    opt_values[br.var()] = Some(*original_value);
185                }
186            }
187            ty::GenericArgKind::Const(c) => {
188                if let ty::ConstKind::Bound(index_kind, bc) = c.kind() {
189                    if !#[allow(non_exhaustive_omitted_patterns)] match index_kind {
            ty::BoundVarIndexKind::Canonical => true,
            _ => false,
        } {
    ::core::panicking::panic("assertion failed: matches!(index_kind, ty::BoundVarIndexKind::Canonical)")
};assert!(matches!(index_kind, ty::BoundVarIndexKind::Canonical));
190                    opt_values[bc.var()] = Some(*original_value);
191                }
192            }
193        }
194    }
195    CanonicalVarValues::instantiate(delegate.cx(), response.var_kinds, |var_values, kind| {
196        if kind.universe() != ty::UniverseIndex::ROOT {
197            // A variable from inside a binder of the query. While ideally these shouldn't
198            // exist at all (see the FIXME at the start of this method), we have to deal with
199            // them for now.
200            delegate.instantiate_canonical_var(kind, span, &var_values, |idx| {
201                prev_universe + idx.index()
202            })
203        } else if kind.is_existential() {
204            // As an optimization we sometimes avoid creating a new inference variable here.
205            //
206            // All new inference variables we create start out in the current universe of the caller.
207            // This is conceptually wrong as these inference variables would be able to name
208            // more placeholders then they should be able to. However the inference variables have
209            // to "come from somewhere", so by equating them with the original values of the caller
210            // later on, we pull them down into their correct universe again.
211            if let Some(v) = opt_values[ty::BoundVar::from_usize(var_values.len())] {
212                v
213            } else {
214                delegate.instantiate_canonical_var(kind, span, &var_values, |_| prev_universe)
215            }
216        } else {
217            // For placeholders which were already part of the input, we simply map this
218            // universal bound variable back the placeholder of the input.
219            //
220            // For `CanonicalVarKind::PlaceholderRegion`, this differs slightly: we
221            // canonicalize all free regions from the input into placeholders. This is
222            // unlike types or consts, where only input placeholders remain placeholders
223            // in the canonical form.
224            //
225            // We can still map these back to the original input regions, as we
226            // just instantiate the canonical variable with its corresponding
227            // `original_value`.
228            //
229            // For more information on why we canonicalize all input regions as
230            // placeholders, see the comment in `Canonicalizer::fold_region`.
231            original_values[kind.expect_placeholder_index()]
232        }
233    })
234}
235
236/// Unify the `original_values` with the `var_values` returned by the canonical query..
237///
238/// This assumes that this unification will always succeed. This is the case when
239/// applying a query response right away. However, calling a canonical query, doing any
240/// other kind of trait solving, and only then instantiating the result of the query
241/// can cause the instantiation to fail. This is not supported and we ICE in this case.
242///
243/// We always structurally instantiate aliases. Relating aliases needs to be different
244/// depending on whether the alias is *rigid* or not. We're only really able to tell
245/// whether an alias is rigid by using the trait solver. When instantiating a response
246/// from the solver we assume that the solver correctly handled aliases and therefore
247/// always relate them structurally here.
248#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("unify_query_var_values",
                                    "rustc_next_trait_solver::canonical",
                                    ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/canonical/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(248u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::canonical"),
                                    ::tracing_core::field::FieldSet::new(&["param_env",
                                                    "original_values", "var_values", "span"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&param_env)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&original_values)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&var_values)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            match (&original_values.len(), &var_values.len()) {
                (left_val, right_val) => {
                    if !(*left_val == *right_val) {
                        let kind = ::core::panicking::AssertKind::Eq;
                        ::core::panicking::assert_failed(kind, &*left_val,
                            &*right_val, ::core::option::Option::None);
                    }
                }
            };
            for (&orig, response) in
                iter::zip(original_values, var_values.var_values.iter()) {
                let goals =
                    delegate.eq_structurally_relating_aliases(param_env, orig,
                            response, span).unwrap();
                if !goals.is_empty() {
                    ::core::panicking::panic("assertion failed: goals.is_empty()")
                };
            }
        }
    }
}#[instrument(level = "trace", skip(delegate))]
249fn unify_query_var_values<D, I>(
250    delegate: &D,
251    param_env: I::ParamEnv,
252    original_values: &[I::GenericArg],
253    var_values: CanonicalVarValues<I>,
254    span: I::Span,
255) where
256    D: SolverDelegate<Interner = I>,
257    I: Interner,
258{
259    assert_eq!(original_values.len(), var_values.len());
260
261    for (&orig, response) in iter::zip(original_values, var_values.var_values.iter()) {
262        let goals =
263            delegate.eq_structurally_relating_aliases(param_env, orig, response, span).unwrap();
264        assert!(goals.is_empty());
265    }
266}
267
268fn register_region_constraints<D, I>(
269    delegate: &D,
270    constraints: impl IntoIterator<Item = (ty::RegionConstraint<I>, VisibleForLeakCheck)>,
271    span: I::Span,
272) where
273    D: SolverDelegate<Interner = I>,
274    I: Interner,
275{
276    for (constraint, vis) in constraints {
277        match constraint {
278            ty::RegionConstraint::Outlives(ty::OutlivesPredicate(lhs, rhs)) => match lhs.kind() {
279                ty::GenericArgKind::Lifetime(lhs) => delegate.sub_regions(rhs, lhs, vis, span),
280                ty::GenericArgKind::Type(lhs) => delegate.register_ty_outlives(lhs, rhs, span),
281                ty::GenericArgKind::Const(_) => {
    ::core::panicking::panic_fmt(format_args!("const outlives: {0:?}: {1:?}",
            lhs, rhs));
}panic!("const outlives: {lhs:?}: {rhs:?}"),
282            },
283            ty::RegionConstraint::Eq(ty::RegionEqPredicate(lhs, rhs)) => {
284                delegate.equate_regions(lhs, rhs, vis, span)
285            }
286        }
287    }
288}
289
290fn register_new_opaque_types<D, I>(
291    delegate: &D,
292    opaque_types: &[(ty::OpaqueTypeKey<I>, I::Ty)],
293    span: I::Span,
294) where
295    D: SolverDelegate<Interner = I>,
296    I: Interner,
297{
298    for &(key, ty) in opaque_types {
299        let prev = delegate.register_hidden_type_in_storage(key, ty, span);
300        // We eagerly resolve inference variables when computing the query response.
301        // This can cause previously distinct opaque type keys to now be structurally equal.
302        //
303        // To handle this, we store any duplicate entries in a separate list to check them
304        // at the end of typeck/borrowck. We could alternatively eagerly equate the hidden
305        // types here. However, doing so is difficult as it may result in nested goals and
306        // any errors may make it harder to track the control flow for diagnostics.
307        if let Some(prev) = prev {
308            delegate.add_duplicate_opaque_type(key, prev, span);
309        }
310    }
311}
312
313/// Used by proof trees to be able to recompute intermediate actions while
314/// evaluating a goal. The `var_values` not only include the bound variables
315/// of the query input, but also contain all unconstrained inference vars
316/// created while evaluating this goal.
317pub fn make_canonical_state<D, I, T>(
318    delegate: &D,
319    var_values: &[I::GenericArg],
320    max_input_universe: ty::UniverseIndex,
321    data: T,
322) -> inspect::CanonicalState<I, T>
323where
324    D: SolverDelegate<Interner = I>,
325    I: Interner,
326    T: TypeFoldable<I>,
327{
328    let var_values = CanonicalVarValues { var_values: delegate.cx().mk_args(var_values) };
329    let state = inspect::State { var_values, data };
330    let state = eager_resolve_vars(delegate, state);
331    Canonicalizer::canonicalize_response(delegate, max_input_universe, state)
332}
333
334// FIXME: needs to be pub to be accessed by downstream
335// `rustc_trait_selection::solve::inspect::analyse`.
336pub fn instantiate_canonical_state<D, I, T>(
337    delegate: &D,
338    span: I::Span,
339    param_env: I::ParamEnv,
340    orig_values: &mut Vec<I::GenericArg>,
341    state: inspect::CanonicalState<I, T>,
342) -> T
343where
344    D: SolverDelegate<Interner = I>,
345    I: Interner,
346    T: TypeFoldable<I>,
347{
348    // In case any fresh inference variables have been created between `state`
349    // and the previous instantiation, extend `orig_values` for it.
350    orig_values.extend(
351        state.value.var_values.var_values.as_slice()[orig_values.len()..]
352            .iter()
353            .map(|&arg| delegate.fresh_var_for_kind_with_span(arg, span)),
354    );
355
356    let instantiation =
357        compute_query_response_instantiation_values(delegate, orig_values, &state, span);
358
359    let inspect::State { var_values, data } = delegate.instantiate_canonical(state, instantiation);
360
361    unify_query_var_values(delegate, param_env, orig_values, var_values, span);
362    data
363}
364
365pub fn response_no_constraints_raw<I: Interner>(
366    cx: I,
367    max_universe: ty::UniverseIndex,
368    var_kinds: I::CanonicalVarKinds,
369    certainty: Certainty,
370) -> CanonicalResponse<I> {
371    ty::Canonical {
372        max_universe,
373        var_kinds,
374        value: Response {
375            var_values: ty::CanonicalVarValues::make_identity(cx, var_kinds),
376            // FIXME: maybe we should store the "no response" version in cx, like
377            // we do for cx.types and stuff.
378            external_constraints: cx.mk_external_constraints(ExternalConstraintsData::default()),
379            certainty,
380        },
381    }
382}