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