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