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