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