Skip to main content

rustc_infer/infer/canonical/
query_response.rs

1//! This module contains the code to instantiate a "query result", and
2//! in particular to extract out the resulting region obligations and
3//! encode them therein.
4//!
5//! For an overview of what canonicalization is and how it fits into
6//! rustc, check out the [chapter in the rustc dev guide][c].
7//!
8//! [c]: https://rust-lang.github.io/chalk/book/canonical_queries/canonicalization.html
9
10use std::fmt::Debug;
11use std::iter;
12
13use rustc_index::{Idx, IndexVec};
14use rustc_middle::arena::ArenaAllocatable;
15use rustc_middle::bug;
16use rustc_middle::infer::canonical::{CanonicalVarKind, QueryRegionConstraint};
17use rustc_middle::ty::{self, BoundVar, GenericArg, GenericArgKind, Ty, TyCtxt, TypeFoldable};
18use tracing::{debug, instrument};
19
20use crate::infer::canonical::instantiate::{CanonicalExt, instantiate_value};
21use crate::infer::canonical::{
22    Canonical, CanonicalQueryResponse, CanonicalVarValues, Certainty, OriginalQueryValues,
23    QueryRegionConstraints, QueryResponse,
24};
25use crate::infer::region_constraints::{ConstraintKind, RegionConstraintData};
26use crate::infer::{
27    DefineOpaqueTypes, InferCtxt, InferOk, InferResult, OpaqueTypeStorageEntries, SubregionOrigin,
28    TypeOutlivesConstraint,
29};
30use crate::traits::query::NoSolution;
31use crate::traits::{ObligationCause, PredicateObligations, ScrubbedTraitError, TraitEngine};
32
33impl<'tcx> InferCtxt<'tcx> {
34    /// This method is meant to be invoked as the final step of a canonical query
35    /// implementation. It is given:
36    ///
37    /// - the instantiated variables `inference_vars` created from the query key
38    /// - the result `answer` of the query
39    /// - a fulfillment context `fulfill_cx` that may contain various obligations which
40    ///   have yet to be proven.
41    ///
42    /// Given this, the function will process the obligations pending
43    /// in `fulfill_cx`:
44    ///
45    /// - If all the obligations can be proven successfully, it will
46    ///   package up any resulting region obligations (extracted from
47    ///   `infcx`) along with the fully resolved value `answer` into a
48    ///   query result (which is then itself canonicalized).
49    /// - If some obligations can be neither proven nor disproven, then
50    ///   the same thing happens, but the resulting query is marked as ambiguous.
51    /// - Finally, if any of the obligations result in a hard error,
52    ///   then `Err(NoSolution)` is returned.
53    #[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("make_canonicalized_query_response",
                                    "rustc_infer::infer::canonical::query_response",
                                    ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/canonical/query_response.rs"),
                                    ::tracing_core::__macro_support::Option::Some(53u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::canonical::query_response"),
                                    ::tracing_core::field::FieldSet::new(&[],
                                        ::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,
                        &{ meta.fields().value_set(&[]) })
                } 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:
                    Result<CanonicalQueryResponse<'tcx, T>, NoSolution> =
                loop {};
            return __tracing_attr_fake_return;
        }
        {
            let query_response =
                self.make_query_response(inference_vars, answer, fulfill_cx)?;
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_infer/src/infer/canonical/query_response.rs:65",
                                    "rustc_infer::infer::canonical::query_response",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/canonical/query_response.rs"),
                                    ::tracing_core::__macro_support::Option::Some(65u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::canonical::query_response"),
                                    ::tracing_core::field::FieldSet::new(&["message"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&format_args!("query_response = {0:#?}",
                                                                query_response) as &dyn Value))])
                        });
                } else { ; }
            };
            let canonical_result = self.canonicalize_response(query_response);
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_infer/src/infer/canonical/query_response.rs:67",
                                    "rustc_infer::infer::canonical::query_response",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/canonical/query_response.rs"),
                                    ::tracing_core::__macro_support::Option::Some(67u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::canonical::query_response"),
                                    ::tracing_core::field::FieldSet::new(&["message"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&format_args!("canonical_result = {0:#?}",
                                                                canonical_result) as &dyn Value))])
                        });
                } else { ; }
            };
            Ok(self.tcx.arena.alloc(canonical_result))
        }
    }
}#[instrument(skip(self, inference_vars, answer, fulfill_cx), level = "trace")]
54    pub fn make_canonicalized_query_response<T>(
55        &self,
56        inference_vars: CanonicalVarValues<'tcx>,
57        answer: T,
58        fulfill_cx: &mut dyn TraitEngine<'tcx, ScrubbedTraitError<'tcx>>,
59    ) -> Result<CanonicalQueryResponse<'tcx, T>, NoSolution>
60    where
61        T: Debug + TypeFoldable<TyCtxt<'tcx>>,
62        Canonical<'tcx, QueryResponse<'tcx, T>>: ArenaAllocatable<'tcx>,
63    {
64        let query_response = self.make_query_response(inference_vars, answer, fulfill_cx)?;
65        debug!("query_response = {:#?}", query_response);
66        let canonical_result = self.canonicalize_response(query_response);
67        debug!("canonical_result = {:#?}", canonical_result);
68
69        Ok(self.tcx.arena.alloc(canonical_result))
70    }
71
72    /// A version of `make_canonicalized_query_response` that does
73    /// not pack in obligations, for contexts that want to drop
74    /// pending obligations instead of treating them as an ambiguity (e.g.
75    /// typeck "probing" contexts).
76    ///
77    /// If you DO want to keep track of pending obligations (which
78    /// include all region obligations, so this includes all cases
79    /// that care about regions) with this function, you have to
80    /// do it yourself, by e.g., having them be a part of the answer.
81    pub fn make_query_response_ignoring_pending_obligations<T>(
82        &self,
83        inference_vars: CanonicalVarValues<'tcx>,
84        answer: T,
85        prev_entries: OpaqueTypeStorageEntries,
86    ) -> Canonical<'tcx, QueryResponse<'tcx, T>>
87    where
88        T: Debug + TypeFoldable<TyCtxt<'tcx>>,
89    {
90        // While we ignore region constraints and pending obligations,
91        // we do return constrained opaque types to avoid unconstrained
92        // inference variables in the response. This is important as we want
93        // to check that opaques in deref steps stay unconstrained.
94        //
95        // This doesn't handle the more general case for non-opaques as
96        // ambiguous `Projection` obligations have same the issue.
97        let opaque_types = if self.next_trait_solver() {
98            self.inner
99                .borrow_mut()
100                .opaque_type_storage
101                .opaque_types_added_since(prev_entries)
102                .map(|(k, v)| (k, v.ty))
103                .collect()
104        } else {
105            ::alloc::vec::Vec::new()vec![]
106        };
107
108        self.canonicalize_response(QueryResponse {
109            var_values: inference_vars,
110            region_constraints: QueryRegionConstraints::default(),
111            certainty: Certainty::Proven, // Ambiguities are OK!
112            opaque_types,
113            value: answer,
114        })
115    }
116
117    /// Helper for `make_canonicalized_query_response` that does
118    /// everything up until the final canonicalization.
119    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::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("make_query_response",
                                    "rustc_infer::infer::canonical::query_response",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/canonical/query_response.rs"),
                                    ::tracing_core::__macro_support::Option::Some(119u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::canonical::query_response"),
                                    ::tracing_core::field::FieldSet::new(&["inference_vars",
                                                    "answer"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::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(&inference_vars)
                                                            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(&answer)
                                                            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:
                    Result<QueryResponse<'tcx, T>, NoSolution> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let errors =
                fulfill_cx.evaluate_obligations_error_on_ambiguity(self);
            if errors.iter().any(|e| e.is_true_error()) {
                return Err(NoSolution);
            }
            let region_obligations =
                self.take_registered_region_obligations();
            let region_assumptions =
                self.take_registered_region_assumptions();
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_infer/src/infer/canonical/query_response.rs:139",
                                    "rustc_infer::infer::canonical::query_response",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/canonical/query_response.rs"),
                                    ::tracing_core::__macro_support::Option::Some(139u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::canonical::query_response"),
                                    ::tracing_core::field::FieldSet::new(&["region_obligations"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&region_obligations)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let region_constraints =
                self.with_region_constraints(|region_constraints|
                        {
                            make_query_region_constraints(region_obligations,
                                region_constraints, region_assumptions)
                        });
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_infer/src/infer/canonical/query_response.rs:147",
                                    "rustc_infer::infer::canonical::query_response",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/canonical/query_response.rs"),
                                    ::tracing_core::__macro_support::Option::Some(147u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::canonical::query_response"),
                                    ::tracing_core::field::FieldSet::new(&["region_constraints"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&region_constraints)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let certainty =
                if errors.is_empty() {
                    Certainty::Proven
                } else { Certainty::Ambiguous };
            let opaque_types =
                self.inner.borrow_mut().opaque_type_storage.take_opaque_types().map(|(k,
                                v)| (k, v.ty)).collect();
            Ok(QueryResponse {
                    var_values: inference_vars,
                    region_constraints,
                    certainty,
                    value: answer,
                    opaque_types,
                })
        }
    }
}#[instrument(skip(self, fulfill_cx), level = "debug")]
120    fn make_query_response<T>(
121        &self,
122        inference_vars: CanonicalVarValues<'tcx>,
123        answer: T,
124        fulfill_cx: &mut dyn TraitEngine<'tcx, ScrubbedTraitError<'tcx>>,
125    ) -> Result<QueryResponse<'tcx, T>, NoSolution>
126    where
127        T: Debug + TypeFoldable<TyCtxt<'tcx>>,
128    {
129        // Select everything, returning errors.
130        let errors = fulfill_cx.evaluate_obligations_error_on_ambiguity(self);
131
132        // True error!
133        if errors.iter().any(|e| e.is_true_error()) {
134            return Err(NoSolution);
135        }
136
137        let region_obligations = self.take_registered_region_obligations();
138        let region_assumptions = self.take_registered_region_assumptions();
139        debug!(?region_obligations);
140        let region_constraints = self.with_region_constraints(|region_constraints| {
141            make_query_region_constraints(
142                region_obligations,
143                region_constraints,
144                region_assumptions,
145            )
146        });
147        debug!(?region_constraints);
148
149        let certainty = if errors.is_empty() { Certainty::Proven } else { Certainty::Ambiguous };
150
151        let opaque_types = self
152            .inner
153            .borrow_mut()
154            .opaque_type_storage
155            .take_opaque_types()
156            .map(|(k, v)| (k, v.ty))
157            .collect();
158
159        Ok(QueryResponse {
160            var_values: inference_vars,
161            region_constraints,
162            certainty,
163            value: answer,
164            opaque_types,
165        })
166    }
167
168    /// Given the (canonicalized) result to a canonical query,
169    /// instantiates the result so it can be used, plugging in the
170    /// values from the canonical query. (Note that the result may
171    /// have been ambiguous; you should check the certainty level of
172    /// the query before applying this function.)
173    ///
174    /// To get a good understanding of what is happening here, check
175    /// out the [chapter in the rustc dev guide][c].
176    ///
177    /// [c]: https://rust-lang.github.io/chalk/book/canonical_queries/canonicalization.html#processing-the-canonicalized-query-result
178    pub fn instantiate_query_response_and_region_obligations<R>(
179        &self,
180        cause: &ObligationCause<'tcx>,
181        param_env: ty::ParamEnv<'tcx>,
182        original_values: &OriginalQueryValues<'tcx>,
183        query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
184    ) -> InferResult<'tcx, R>
185    where
186        R: Debug + TypeFoldable<TyCtxt<'tcx>>,
187    {
188        let InferOk { value: result_args, obligations } =
189            self.query_response_instantiation(cause, param_env, original_values, query_response)?;
190
191        for QueryRegionConstraint { constraint, visible_for_leak_check: vis, .. } in
192            &query_response.value.region_constraints.constraints
193        {
194            let constraint = instantiate_value(self.tcx, &result_args, *constraint);
195            match constraint {
196                ty::RegionConstraint::Outlives(predicate) => {
197                    self.register_outlives_constraint(predicate, *vis, cause);
198                }
199                ty::RegionConstraint::Eq(predicate) => {
200                    self.register_region_eq_constraint(predicate, *vis, cause);
201                }
202            }
203        }
204
205        for assumption in &query_response.value.region_constraints.assumptions {
206            let assumption = instantiate_value(self.tcx, &result_args, *assumption);
207            self.register_region_assumption(assumption);
208        }
209
210        let user_result: R =
211            query_response.instantiate_projected(self.tcx, &result_args, |q_r| q_r.value.clone());
212
213        Ok(InferOk { value: user_result, obligations })
214    }
215
216    /// An alternative to
217    /// `instantiate_query_response_and_region_obligations` that is more
218    /// efficient for NLL. NLL is a bit more advanced in the
219    /// "transition to chalk" than the rest of the compiler. During
220    /// the NLL type check, all of the "processing" of types and
221    /// things happens in queries -- the NLL checker itself is only
222    /// interested in the region obligations (`'a: 'b` or `T: 'b`)
223    /// that come out of these queries, which it wants to convert into
224    /// MIR-based constraints and solve. Therefore, it is most
225    /// convenient for the NLL Type Checker to **directly consume**
226    /// the `QueryOutlivesConstraint` values that arise from doing a
227    /// query. This is contrast to other parts of the compiler, which
228    /// would prefer for those `QueryOutlivesConstraint` to be converted
229    /// into the older infcx-style constraints (e.g., calls to
230    /// `sub_regions` or `register_region_obligation`).
231    ///
232    /// Therefore, `instantiate_nll_query_response_and_region_obligations` performs the same
233    /// basic operations as `instantiate_query_response_and_region_obligations` but
234    /// it returns its result differently:
235    ///
236    /// - It creates an instantiation `S` that maps from the original
237    ///   query variables to the values computed in the query
238    ///   result. If any errors arise, they are propagated back as an
239    ///   `Err` result.
240    /// - In the case of a successful instantiation, we will append
241    ///   `QueryOutlivesConstraint` values onto the
242    ///   `output_query_region_constraints` vector for the solver to
243    ///   use (if an error arises, some values may also be pushed, but
244    ///   they should be ignored).
245    /// - It **can happen** (though it rarely does currently) that
246    ///   equating types and things will give rise to subobligations
247    ///   that must be processed. In this case, those subobligations
248    ///   are propagated back in the return value.
249    /// - Finally, the query result (of type `R`) is propagated back,
250    ///   after applying the instantiation `S`.
251    pub fn instantiate_nll_query_response_and_region_obligations<R>(
252        &self,
253        cause: &ObligationCause<'tcx>,
254        param_env: ty::ParamEnv<'tcx>,
255        original_values: &OriginalQueryValues<'tcx>,
256        query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
257        output_query_region_constraints: &mut QueryRegionConstraints<'tcx>,
258    ) -> InferResult<'tcx, R>
259    where
260        R: Debug + TypeFoldable<TyCtxt<'tcx>>,
261    {
262        let InferOk { value: result_args, mut obligations } = self
263            .query_response_instantiation_guess(
264                cause,
265                param_env,
266                original_values,
267                query_response,
268            )?;
269
270        // Compute `QueryOutlivesConstraint` values that unify each of
271        // the original values `v_o` that was canonicalized into a
272        // variable...
273
274        let constraint_category = cause.to_constraint_category();
275
276        for (index, original_value) in original_values.var_values.iter().enumerate() {
277            // ...with the value `v_r` of that variable from the query.
278            let result_value = query_response.instantiate_projected(self.tcx, &result_args, |v| {
279                v.var_values[BoundVar::new(index)]
280            });
281            match (original_value.kind(), result_value.kind()) {
282                (GenericArgKind::Lifetime(re1), GenericArgKind::Lifetime(re2))
283                    if re1.is_erased() && re2.is_erased() =>
284                {
285                    // No action needed.
286                }
287
288                (GenericArgKind::Lifetime(v_o), GenericArgKind::Lifetime(v_r)) => {
289                    if v_o != v_r {
290                        let constraint = QueryRegionConstraint {
291                            constraint: ty::RegionEqPredicate(v_o, v_r).into(),
292                            category: constraint_category,
293                            visible_for_leak_check: ty::VisibleForLeakCheck::Yes,
294                        };
295                        output_query_region_constraints.constraints.push(constraint);
296                    }
297                }
298
299                (GenericArgKind::Type(v1), GenericArgKind::Type(v2)) => {
300                    obligations.extend(
301                        self.at(&cause, param_env)
302                            .eq(DefineOpaqueTypes::Yes, v1, v2)?
303                            .into_obligations(),
304                    );
305                }
306
307                (GenericArgKind::Const(v1), GenericArgKind::Const(v2)) => {
308                    obligations.extend(
309                        self.at(&cause, param_env)
310                            .eq(DefineOpaqueTypes::Yes, v1, v2)?
311                            .into_obligations(),
312                    );
313                }
314
315                _ => {
316                    ::rustc_middle::util::bug::bug_fmt(format_args!("kind mismatch, cannot unify {0:?} and {1:?}",
        original_value, result_value));bug!("kind mismatch, cannot unify {:?} and {:?}", original_value, result_value);
317                }
318            }
319        }
320
321        // ...also include the other query region constraints from the query.
322        output_query_region_constraints.constraints.extend(
323            query_response.value.region_constraints.constraints.iter().filter_map(|&r_c| {
324                let r_c = instantiate_value(self.tcx, &result_args, r_c);
325
326                // Screen out `'a: 'a` or `'a == 'a` cases.
327                if r_c.constraint.is_trivial() { None } else { Some(r_c) }
328            }),
329        );
330
331        // FIXME(higher_ranked_auto): Optimize this to instantiate all assumptions
332        // at once, rather than calling `instantiate_value` repeatedly which may
333        // create more universes.
334        output_query_region_constraints.assumptions.extend(
335            query_response
336                .value
337                .region_constraints
338                .assumptions
339                .iter()
340                .map(|&r_c| instantiate_value(self.tcx, &result_args, r_c)),
341        );
342
343        let user_result: R =
344            query_response.instantiate_projected(self.tcx, &result_args, |q_r| q_r.value.clone());
345
346        Ok(InferOk { value: user_result, obligations })
347    }
348
349    /// Given the original values and the (canonicalized) result from
350    /// computing a query, returns an instantiation that can be applied
351    /// to the query result to convert the result back into the
352    /// original namespace.
353    ///
354    /// The instantiation also comes accompanied with subobligations
355    /// that arose from unification; these might occur if (for
356    /// example) we are doing lazy normalization and the value
357    /// assigned to a type variable is unified with an unnormalized
358    /// projection.
359    fn query_response_instantiation<R>(
360        &self,
361        cause: &ObligationCause<'tcx>,
362        param_env: ty::ParamEnv<'tcx>,
363        original_values: &OriginalQueryValues<'tcx>,
364        query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
365    ) -> InferResult<'tcx, CanonicalVarValues<'tcx>>
366    where
367        R: Debug + TypeFoldable<TyCtxt<'tcx>>,
368    {
369        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_infer/src/infer/canonical/query_response.rs:369",
                        "rustc_infer::infer::canonical::query_response",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/canonical/query_response.rs"),
                        ::tracing_core::__macro_support::Option::Some(369u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::canonical::query_response"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("query_response_instantiation(original_values={0:#?}, query_response={1:#?})",
                                                    original_values, query_response) as &dyn Value))])
            });
    } else { ; }
};debug!(
370            "query_response_instantiation(original_values={:#?}, query_response={:#?})",
371            original_values, query_response,
372        );
373
374        let mut value = self.query_response_instantiation_guess(
375            cause,
376            param_env,
377            original_values,
378            query_response,
379        )?;
380
381        value.obligations.extend(
382            self.unify_query_response_instantiation_guess(
383                cause,
384                param_env,
385                original_values,
386                &value.value,
387                query_response,
388            )?
389            .into_obligations(),
390        );
391
392        Ok(value)
393    }
394
395    /// Given the original values and the (canonicalized) result from
396    /// computing a query, returns a **guess** at an instantiation that
397    /// can be applied to the query result to convert the result back
398    /// into the original namespace. This is called a **guess**
399    /// because it uses a quick heuristic to find the values for each
400    /// canonical variable; if that quick heuristic fails, then we
401    /// will instantiate fresh inference variables for each canonical
402    /// variable instead. Therefore, the result of this method must be
403    /// properly unified
404    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::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("query_response_instantiation_guess",
                                    "rustc_infer::infer::canonical::query_response",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/canonical/query_response.rs"),
                                    ::tracing_core::__macro_support::Option::Some(404u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::canonical::query_response"),
                                    ::tracing_core::field::FieldSet::new(&["cause",
                                                    "original_values", "query_response"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::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(&cause)
                                                            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(&query_response)
                                                            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:
                    InferResult<'tcx, CanonicalVarValues<'tcx>> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let mut universe_map = original_values.universe_map.clone();
            let num_universes_in_query = original_values.universe_map.len();
            let num_universes_in_response =
                query_response.max_universe.as_usize() + 1;
            for _ in num_universes_in_query..num_universes_in_response {
                universe_map.push(self.create_next_universe());
            }
            if !!universe_map.is_empty() {
                ::core::panicking::panic("assertion failed: !universe_map.is_empty()")
            };
            match (&universe_map[ty::UniverseIndex::ROOT.as_usize()],
                    &ty::UniverseIndex::ROOT) {
                (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);
                    }
                }
            };
            let result_values = &query_response.value.var_values;
            match (&original_values.var_values.len(), &result_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);
                    }
                }
            };
            let mut opt_values: IndexVec<BoundVar, Option<GenericArg<'tcx>>> =
                IndexVec::from_elem_n(None, query_response.var_kinds.len());
            for (original_value, result_value) in
                iter::zip(&original_values.var_values, result_values) {
                match result_value.kind() {
                    GenericArgKind::Type(result_value) => {
                        if let ty::Bound(index_kind, b) = *result_value.kind() &&
                                !#[allow(non_exhaustive_omitted_patterns)] match query_response.var_kinds[b.var.as_usize()]
                                        {
                                        CanonicalVarKind::Ty { .. } => true,
                                        _ => false,
                                    } {
                            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)")
                            };
                            opt_values[b.var] = Some(*original_value);
                        }
                    }
                    GenericArgKind::Lifetime(result_value) => {
                        if let ty::ReBound(index_kind, b) = result_value.kind() {
                            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)")
                            };
                            opt_values[b.var] = Some(*original_value);
                        }
                    }
                    GenericArgKind::Const(result_value) => {
                        if let ty::ConstKind::Bound(index_kind, b) =
                                result_value.kind() {
                            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)")
                            };
                            opt_values[b.var] = Some(*original_value);
                        }
                    }
                }
            }
            let tcx = self.tcx;
            let var_kinds = query_response.var_kinds;
            let var_values =
                CanonicalVarValues::instantiate(tcx, var_kinds,
                    |var_values, kind|
                        {
                            if kind.universe() != ty::UniverseIndex::ROOT {
                                self.instantiate_canonical_var(cause.span, kind,
                                    &var_values, |u| { universe_map[u.as_usize()] })
                            } else if kind.is_existential() {
                                match opt_values[BoundVar::new(var_values.len())] {
                                    Some(k) => k,
                                    None =>
                                        self.instantiate_canonical_var(cause.span, kind,
                                            &var_values, |u| { universe_map[u.as_usize()] }),
                                }
                            } else {
                                opt_values[BoundVar::new(var_values.len())].expect("expected placeholder to be unified with itself during response")
                            }
                        });
            let mut obligations = PredicateObligations::new();
            for &(a, b) in &query_response.value.opaque_types {
                let a = instantiate_value(self.tcx, &var_values, a);
                let b = instantiate_value(self.tcx, &var_values, b);
                {
                    use ::tracing::__macro_support::Callsite as _;
                    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                        {
                            static META: ::tracing::Metadata<'static> =
                                {
                                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_infer/src/infer/canonical/query_response.rs:513",
                                        "rustc_infer::infer::canonical::query_response",
                                        ::tracing::Level::DEBUG,
                                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/canonical/query_response.rs"),
                                        ::tracing_core::__macro_support::Option::Some(513u32),
                                        ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::canonical::query_response"),
                                        ::tracing_core::field::FieldSet::new(&["message", "a", "b"],
                                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                        ::tracing::metadata::Kind::EVENT)
                                };
                            ::tracing::callsite::DefaultCallsite::new(&META)
                        };
                    let enabled =
                        ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            {
                                let interest = __CALLSITE.interest();
                                !interest.is_never() &&
                                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                        interest)
                            };
                    if enabled {
                        (|value_set: ::tracing::field::ValueSet|
                                    {
                                        let meta = __CALLSITE.metadata();
                                        ::tracing::Event::dispatch(meta, &value_set);
                                        ;
                                    })({
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = __CALLSITE.metadata().fields().iter();
                                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&format_args!("constrain opaque type")
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&debug(&a) as
                                                            &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&debug(&b) as
                                                            &dyn Value))])
                            });
                    } else { ; }
                };
                obligations.extend(self.at(cause,
                                    param_env).eq(DefineOpaqueTypes::Yes,
                                Ty::new_opaque(self.tcx, a.def_id.to_def_id(), a.args),
                                b)?.obligations);
            }
            Ok(InferOk { value: var_values, obligations })
        }
    }
}#[instrument(level = "debug", skip(self, param_env))]
405    fn query_response_instantiation_guess<R>(
406        &self,
407        cause: &ObligationCause<'tcx>,
408        param_env: ty::ParamEnv<'tcx>,
409        original_values: &OriginalQueryValues<'tcx>,
410        query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
411    ) -> InferResult<'tcx, CanonicalVarValues<'tcx>>
412    where
413        R: Debug + TypeFoldable<TyCtxt<'tcx>>,
414    {
415        // For each new universe created in the query result that did
416        // not appear in the original query, create a local
417        // superuniverse.
418        let mut universe_map = original_values.universe_map.clone();
419        let num_universes_in_query = original_values.universe_map.len();
420        let num_universes_in_response = query_response.max_universe.as_usize() + 1;
421        for _ in num_universes_in_query..num_universes_in_response {
422            universe_map.push(self.create_next_universe());
423        }
424        assert!(!universe_map.is_empty()); // always have the root universe
425        assert_eq!(universe_map[ty::UniverseIndex::ROOT.as_usize()], ty::UniverseIndex::ROOT);
426
427        // Every canonical query result includes values for each of
428        // the inputs to the query. Therefore, we begin by unifying
429        // these values with the original inputs that were
430        // canonicalized.
431        let result_values = &query_response.value.var_values;
432        assert_eq!(original_values.var_values.len(), result_values.len());
433
434        // Quickly try to find initial values for the canonical
435        // variables in the result in terms of the query. We do this
436        // by iterating down the values that the query gave to each of
437        // the canonical inputs. If we find that one of those values
438        // is directly equal to one of the canonical variables in the
439        // result, then we can type the corresponding value from the
440        // input. See the example above.
441        let mut opt_values: IndexVec<BoundVar, Option<GenericArg<'tcx>>> =
442            IndexVec::from_elem_n(None, query_response.var_kinds.len());
443
444        for (original_value, result_value) in iter::zip(&original_values.var_values, result_values)
445        {
446            match result_value.kind() {
447                GenericArgKind::Type(result_value) => {
448                    // We disable the instantiation guess for inference variables
449                    // and only use it for placeholders. We need to handle the
450                    // `sub_root` of type inference variables which would make this
451                    // more involved. They are also a lot rarer than region variables.
452                    if let ty::Bound(index_kind, b) = *result_value.kind()
453                        && !matches!(
454                            query_response.var_kinds[b.var.as_usize()],
455                            CanonicalVarKind::Ty { .. }
456                        )
457                    {
458                        // We only allow a `Canonical` index in generic parameters.
459                        assert!(matches!(index_kind, ty::BoundVarIndexKind::Canonical));
460                        opt_values[b.var] = Some(*original_value);
461                    }
462                }
463                GenericArgKind::Lifetime(result_value) => {
464                    if let ty::ReBound(index_kind, b) = result_value.kind() {
465                        // We only allow a `Canonical` index in generic parameters.
466                        assert!(matches!(index_kind, ty::BoundVarIndexKind::Canonical));
467                        opt_values[b.var] = Some(*original_value);
468                    }
469                }
470                GenericArgKind::Const(result_value) => {
471                    if let ty::ConstKind::Bound(index_kind, b) = result_value.kind() {
472                        // We only allow a `Canonical` index in generic parameters.
473                        assert!(matches!(index_kind, ty::BoundVarIndexKind::Canonical));
474                        opt_values[b.var] = Some(*original_value);
475                    }
476                }
477            }
478        }
479
480        // Create result arguments: if we found a value for a
481        // given variable in the loop above, use that. Otherwise, use
482        // a fresh inference variable.
483        let tcx = self.tcx;
484        let var_kinds = query_response.var_kinds;
485        let var_values = CanonicalVarValues::instantiate(tcx, var_kinds, |var_values, kind| {
486            if kind.universe() != ty::UniverseIndex::ROOT {
487                // A variable from inside a binder of the query. While ideally these shouldn't
488                // exist at all, we have to deal with them for now.
489                self.instantiate_canonical_var(cause.span, kind, &var_values, |u| {
490                    universe_map[u.as_usize()]
491                })
492            } else if kind.is_existential() {
493                match opt_values[BoundVar::new(var_values.len())] {
494                    Some(k) => k,
495                    None => self.instantiate_canonical_var(cause.span, kind, &var_values, |u| {
496                        universe_map[u.as_usize()]
497                    }),
498                }
499            } else {
500                // For placeholders which were already part of the input, we simply map this
501                // universal bound variable back the placeholder of the input.
502                opt_values[BoundVar::new(var_values.len())]
503                    .expect("expected placeholder to be unified with itself during response")
504            }
505        });
506
507        let mut obligations = PredicateObligations::new();
508
509        // Carry all newly resolved opaque types to the caller's scope
510        for &(a, b) in &query_response.value.opaque_types {
511            let a = instantiate_value(self.tcx, &var_values, a);
512            let b = instantiate_value(self.tcx, &var_values, b);
513            debug!(?a, ?b, "constrain opaque type");
514            // We use equate here instead of, for example, just registering the
515            // opaque type's hidden value directly, because the hidden type may have been an inference
516            // variable that got constrained to the opaque type itself. In that case we want to equate
517            // the generic args of the opaque with the generic params of its hidden type version.
518            obligations.extend(
519                self.at(cause, param_env)
520                    .eq(
521                        DefineOpaqueTypes::Yes,
522                        Ty::new_opaque(self.tcx, a.def_id.to_def_id(), a.args),
523                        b,
524                    )?
525                    .obligations,
526            );
527        }
528
529        Ok(InferOk { value: var_values, obligations })
530    }
531
532    /// Given a "guess" at the values for the canonical variables in
533    /// the input, try to unify with the *actual* values found in the
534    /// query result. Often, but not always, this is a no-op, because
535    /// we already found the mapping in the "guessing" step.
536    ///
537    /// See also: [`Self::query_response_instantiation_guess`]
538    fn unify_query_response_instantiation_guess<R>(
539        &self,
540        cause: &ObligationCause<'tcx>,
541        param_env: ty::ParamEnv<'tcx>,
542        original_values: &OriginalQueryValues<'tcx>,
543        result_args: &CanonicalVarValues<'tcx>,
544        query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
545    ) -> InferResult<'tcx, ()>
546    where
547        R: Debug + TypeFoldable<TyCtxt<'tcx>>,
548    {
549        // A closure that yields the result value for the given
550        // canonical variable; this is taken from
551        // `query_response.var_values` after applying the instantiation
552        // by `result_args`.
553        let instantiated_query_response = |index: BoundVar| -> GenericArg<'tcx> {
554            query_response.instantiate_projected(self.tcx, result_args, |v| v.var_values[index])
555        };
556
557        // Unify the original value for each variable with the value
558        // taken from `query_response` (after applying `result_args`).
559        self.unify_canonical_vars(cause, param_env, original_values, instantiated_query_response)
560    }
561
562    /// Given two sets of values for the same set of canonical variables, unify them.
563    /// The second set is produced lazily by supplying indices from the first set.
564    fn unify_canonical_vars(
565        &self,
566        cause: &ObligationCause<'tcx>,
567        param_env: ty::ParamEnv<'tcx>,
568        variables1: &OriginalQueryValues<'tcx>,
569        variables2: impl Fn(BoundVar) -> GenericArg<'tcx>,
570    ) -> InferResult<'tcx, ()> {
571        let mut obligations = PredicateObligations::new();
572        for (index, value1) in variables1.var_values.iter().enumerate() {
573            let value2 = variables2(BoundVar::new(index));
574
575            match (value1.kind(), value2.kind()) {
576                (GenericArgKind::Type(v1), GenericArgKind::Type(v2)) => {
577                    obligations.extend(
578                        self.at(cause, param_env)
579                            .eq(DefineOpaqueTypes::Yes, v1, v2)?
580                            .into_obligations(),
581                    );
582                }
583                (GenericArgKind::Lifetime(re1), GenericArgKind::Lifetime(re2))
584                    if re1.is_erased() && re2.is_erased() =>
585                {
586                    // no action needed
587                }
588                (GenericArgKind::Lifetime(v1), GenericArgKind::Lifetime(v2)) => {
589                    self.inner.borrow_mut().unwrap_region_constraints().make_eqregion(
590                        SubregionOrigin::RelateRegionParamBound(cause.span, None),
591                        v1,
592                        v2,
593                        ty::VisibleForLeakCheck::Yes,
594                    );
595                }
596                (GenericArgKind::Const(v1), GenericArgKind::Const(v2)) => {
597                    let ok = self.at(cause, param_env).eq(DefineOpaqueTypes::Yes, v1, v2)?;
598                    obligations.extend(ok.into_obligations());
599                }
600                _ => {
601                    ::rustc_middle::util::bug::bug_fmt(format_args!("kind mismatch, cannot unify {0:?} and {1:?}",
        value1, value2));bug!("kind mismatch, cannot unify {:?} and {:?}", value1, value2,);
602                }
603            }
604        }
605        Ok(InferOk { value: (), obligations })
606    }
607}
608
609/// Given the region obligations and constraints scraped from the infcx,
610/// creates query region constraints.
611pub fn make_query_region_constraints<'tcx>(
612    outlives_obligations: Vec<TypeOutlivesConstraint<'tcx>>,
613    region_constraints: &RegionConstraintData<'tcx>,
614    assumptions: Vec<ty::ArgOutlivesPredicate<'tcx>>,
615) -> QueryRegionConstraints<'tcx> {
616    let RegionConstraintData { constraints, verifys } = region_constraints;
617
618    if !verifys.is_empty() {
    ::core::panicking::panic("assertion failed: verifys.is_empty()")
};assert!(verifys.is_empty());
619
620    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_infer/src/infer/canonical/query_response.rs:620",
                        "rustc_infer::infer::canonical::query_response",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/canonical/query_response.rs"),
                        ::tracing_core::__macro_support::Option::Some(620u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::canonical::query_response"),
                        ::tracing_core::field::FieldSet::new(&["constraints"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&constraints)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?constraints);
621
622    let constraints: Vec<QueryRegionConstraint<'tcx>> = constraints
623        .iter()
624        .map(|(c, origin)| match c.kind {
625            ConstraintKind::VarSubVar
626            | ConstraintKind::RegSubVar
627            | ConstraintKind::VarSubReg
628            | ConstraintKind::RegSubReg => {
629                // Swap regions because we are going from sub (<=) to outlives (>=).
630                let constraint = ty::OutlivesPredicate(c.sup.into(), c.sub).into();
631                QueryRegionConstraint {
632                    constraint,
633                    category: origin.to_constraint_category(),
634                    visible_for_leak_check: c.visible_for_leak_check,
635                }
636            }
637
638            ConstraintKind::VarEqVar | ConstraintKind::VarEqReg | ConstraintKind::RegEqReg => {
639                let constraint = ty::RegionEqPredicate(c.sup, c.sub).into();
640                QueryRegionConstraint {
641                    constraint,
642                    category: origin.to_constraint_category(),
643                    visible_for_leak_check: c.visible_for_leak_check,
644                }
645            }
646        })
647        .chain(outlives_obligations.into_iter().map(
648            |TypeOutlivesConstraint { sub_region, sup_type, origin }| {
649                QueryRegionConstraint {
650                    constraint: ty::OutlivesPredicate(sup_type.into(), sub_region).into(),
651                    category: origin.to_constraint_category(),
652                    // We don't do leak checks for type outlives
653                    visible_for_leak_check: ty::VisibleForLeakCheck::Unreachable,
654                }
655            },
656        ))
657        .collect();
658
659    QueryRegionConstraints { constraints, assumptions }
660}