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