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.
5 //! For an overview of what canonicaliation is and how it fits into
6 //! rustc, check out the [chapter in the rustc dev guide][c].
8 //! [c]: https://rust-lang.github.io/chalk/book/canonical_queries/canonicalization.html
10 use crate::infer::canonical::substitute::{substitute_value, CanonicalExt};
11 use crate::infer::canonical::{
12 Canonical, CanonicalVarValues, CanonicalizedQueryResponse, Certainty, OriginalQueryValues,
13 QueryOutlivesConstraint, QueryRegionConstraints, QueryResponse,
15 use crate::infer::nll_relate::{NormalizationStrategy, TypeRelating, TypeRelatingDelegate};
16 use crate::infer::region_constraints::{Constraint, RegionConstraintData};
17 use crate::infer::{InferCtxt, InferOk, InferResult, NllRegionVariableOrigin};
18 use crate::traits::query::{Fallible, NoSolution};
19 use crate::traits::TraitEngine;
20 use crate::traits::{Obligation, ObligationCause, PredicateObligation};
21 use rustc_data_structures::captures::Captures;
22 use rustc_index::vec::Idx;
23 use rustc_index::vec::IndexVec;
24 use rustc_middle::arena::ArenaAllocatable;
25 use rustc_middle::ty::fold::TypeFoldable;
26 use rustc_middle::ty::relate::TypeRelation;
27 use rustc_middle::ty::subst::{GenericArg, GenericArgKind};
28 use rustc_middle::ty::{self, BoundVar, Const, ToPredicate, Ty, TyCtxt};
32 impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
33 /// This method is meant to be invoked as the final step of a canonical query
34 /// implementation. It is given:
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.
41 /// Given this, the function will process the obligations pending
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 #[instrument(skip(self, inference_vars, answer, fulfill_cx), level = "trace")]
53 pub fn make_canonicalized_query_response<T>(
55 inference_vars: CanonicalVarValues<'tcx>,
57 fulfill_cx: &mut dyn TraitEngine<'tcx>,
58 ) -> Fallible<CanonicalizedQueryResponse<'tcx, T>>
60 T: Debug + TypeFoldable<'tcx>,
61 Canonical<'tcx, QueryResponse<'tcx, T>>: ArenaAllocatable<'tcx>,
63 let query_response = self.make_query_response(inference_vars, answer, fulfill_cx)?;
64 let canonical_result = self.canonicalize_response(query_response);
66 debug!("canonical_result = {:#?}", canonical_result);
68 Ok(self.tcx.arena.alloc(canonical_result))
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).
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>(
82 inference_vars: CanonicalVarValues<'tcx>,
84 ) -> Canonical<'tcx, QueryResponse<'tcx, T>>
86 T: Debug + TypeFoldable<'tcx>,
88 self.canonicalize_response(QueryResponse {
89 var_values: inference_vars,
90 region_constraints: QueryRegionConstraints::default(),
91 certainty: Certainty::Proven, // Ambiguities are OK!
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>(
101 inference_vars: CanonicalVarValues<'tcx>,
103 fulfill_cx: &mut dyn TraitEngine<'tcx>,
104 ) -> Result<QueryResponse<'tcx, T>, NoSolution>
106 T: Debug + TypeFoldable<'tcx>,
110 // Select everything, returning errors.
111 let true_errors = fulfill_cx.select_where_possible(self);
112 debug!("true_errors = {:#?}", true_errors);
114 if !true_errors.is_empty() {
115 // FIXME -- we don't indicate *why* we failed to solve
116 debug!("make_query_response: true_errors={:#?}", true_errors);
117 return Err(NoSolution);
120 // Anything left unselected *now* must be an ambiguity.
121 let ambig_errors = fulfill_cx.select_all_or_error(self);
122 debug!("ambig_errors = {:#?}", ambig_errors);
124 let region_obligations = self.take_registered_region_obligations();
125 let region_constraints = self.with_region_constraints(|region_constraints| {
126 make_query_region_constraints(
128 region_obligations.iter().map(|(_, r_o)| (r_o.sup_type, r_o.sub_region)),
134 if ambig_errors.is_empty() { Certainty::Proven } else { Certainty::Ambiguous };
137 var_values: inference_vars,
144 /// Given the (canonicalized) result to a canonical query,
145 /// instantiates the result so it can be used, plugging in the
146 /// values from the canonical query. (Note that the result may
147 /// have been ambiguous; you should check the certainty level of
148 /// the query before applying this function.)
150 /// To get a good understanding of what is happening here, check
151 /// out the [chapter in the rustc dev guide][c].
153 /// [c]: https://rust-lang.github.io/chalk/book/canonical_queries/canonicalization.html#processing-the-canonicalized-query-result
154 pub fn instantiate_query_response_and_region_obligations<R>(
156 cause: &ObligationCause<'tcx>,
157 param_env: ty::ParamEnv<'tcx>,
158 original_values: &OriginalQueryValues<'tcx>,
159 query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
160 ) -> InferResult<'tcx, R>
162 R: Debug + TypeFoldable<'tcx>,
164 let InferOk { value: result_subst, mut obligations } =
165 self.query_response_substitution(cause, param_env, original_values, query_response)?;
167 obligations.extend(self.query_outlives_constraints_into_obligations(
170 &query_response.value.region_constraints.outlives,
175 query_response.substitute_projected(self.tcx, &result_subst, |q_r| q_r.value.clone());
177 Ok(InferOk { value: user_result, obligations })
180 /// An alternative to
181 /// `instantiate_query_response_and_region_obligations` that is more
182 /// efficient for NLL. NLL is a bit more advanced in the
183 /// "transition to chalk" than the rest of the compiler. During
184 /// the NLL type check, all of the "processing" of types and
185 /// things happens in queries -- the NLL checker itself is only
186 /// interested in the region obligations (`'a: 'b` or `T: 'b`)
187 /// that come out of these queries, which it wants to convert into
188 /// MIR-based constraints and solve. Therefore, it is most
189 /// convenient for the NLL Type Checker to **directly consume**
190 /// the `QueryOutlivesConstraint` values that arise from doing a
191 /// query. This is contrast to other parts of the compiler, which
192 /// would prefer for those `QueryOutlivesConstraint` to be converted
193 /// into the older infcx-style constraints (e.g., calls to
194 /// `sub_regions` or `register_region_obligation`).
196 /// Therefore, `instantiate_nll_query_response_and_region_obligations` performs the same
197 /// basic operations as `instantiate_query_response_and_region_obligations` but
198 /// it returns its result differently:
200 /// - It creates a substitution `S` that maps from the original
201 /// query variables to the values computed in the query
202 /// result. If any errors arise, they are propagated back as an
204 /// - In the case of a successful substitution, we will append
205 /// `QueryOutlivesConstraint` values onto the
206 /// `output_query_region_constraints` vector for the solver to
207 /// use (if an error arises, some values may also be pushed, but
208 /// they should be ignored).
209 /// - It **can happen** (though it rarely does currently) that
210 /// equating types and things will give rise to subobligations
211 /// that must be processed. In this case, those subobligations
212 /// are propagated back in the return value.
213 /// - Finally, the query result (of type `R`) is propagated back,
214 /// after applying the substitution `S`.
215 pub fn instantiate_nll_query_response_and_region_obligations<R>(
217 cause: &ObligationCause<'tcx>,
218 param_env: ty::ParamEnv<'tcx>,
219 original_values: &OriginalQueryValues<'tcx>,
220 query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
221 output_query_region_constraints: &mut QueryRegionConstraints<'tcx>,
222 ) -> InferResult<'tcx, R>
224 R: Debug + TypeFoldable<'tcx>,
227 self.query_response_substitution_guess(cause, original_values, query_response);
229 // Compute `QueryOutlivesConstraint` values that unify each of
230 // the original values `v_o` that was canonicalized into a
232 let mut obligations = vec![];
234 for (index, original_value) in original_values.var_values.iter().enumerate() {
235 // ...with the value `v_r` of that variable from the query.
236 let result_value = query_response.substitute_projected(self.tcx, &result_subst, |v| {
237 v.var_values[BoundVar::new(index)]
239 match (original_value.unpack(), result_value.unpack()) {
240 (GenericArgKind::Lifetime(re1), GenericArgKind::Lifetime(re2))
241 if re1.is_erased() && re2.is_erased() =>
246 (GenericArgKind::Lifetime(v_o), GenericArgKind::Lifetime(v_r)) => {
247 // To make `v_o = v_r`, we emit `v_o: v_r` and `v_r: v_o`.
249 output_query_region_constraints
251 .push(ty::Binder::dummy(ty::OutlivesPredicate(v_o.into(), v_r)));
252 output_query_region_constraints
254 .push(ty::Binder::dummy(ty::OutlivesPredicate(v_r.into(), v_o)));
258 (GenericArgKind::Type(v1), GenericArgKind::Type(v2)) => {
261 QueryTypeRelatingDelegate {
265 obligations: &mut obligations,
267 ty::Variance::Invariant,
272 (GenericArgKind::Const(v1), GenericArgKind::Const(v2)) => {
275 QueryTypeRelatingDelegate {
279 obligations: &mut obligations,
281 ty::Variance::Invariant,
287 bug!("kind mismatch, cannot unify {:?} and {:?}", original_value, result_value);
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 = substitute_value(self.tcx, &result_subst, r_c);
297 // Screen out `'a: 'a` cases -- we skip the binder here but
298 // only compare the inner values to one another, so they are still at
299 // consistent binding levels.
300 let ty::OutlivesPredicate(k1, r2) = r_c.skip_binder();
301 if k1 != r2.into() { Some(r_c) } else { None }
305 // ...also include the query member constraints.
306 output_query_region_constraints.member_constraints.extend(
312 .map(|p_c| substitute_value(self.tcx, &result_subst, p_c.clone())),
316 query_response.substitute_projected(self.tcx, &result_subst, |q_r| q_r.value.clone());
318 Ok(InferOk { value: user_result, obligations })
321 /// Given the original values and the (canonicalized) result from
322 /// computing a query, returns a substitution that can be applied
323 /// to the query result to convert the result back into the
324 /// original namespace.
326 /// The substitution 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
331 fn query_response_substitution<R>(
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>>
339 R: Debug + TypeFoldable<'tcx>,
342 "query_response_substitution(original_values={:#?}, query_response={:#?})",
343 original_values, query_response,
347 self.query_response_substitution_guess(cause, original_values, query_response);
349 let obligations = self
350 .unify_query_response_substitution_guess(
359 Ok(InferOk { value: result_subst, obligations })
362 /// Given the original values and the (canonicalized) result from
363 /// computing a query, returns a **guess** at a substitution that
364 /// can be applied to the query result to convert the result back
365 /// into the original namespace. This is called a **guess**
366 /// because it uses a quick heuristic to find the values for each
367 /// canonical variable; if that quick heuristic fails, then we
368 /// will instantiate fresh inference variables for each canonical
369 /// variable instead. Therefore, the result of this method must be
371 fn query_response_substitution_guess<R>(
373 cause: &ObligationCause<'tcx>,
374 original_values: &OriginalQueryValues<'tcx>,
375 query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
376 ) -> CanonicalVarValues<'tcx>
378 R: Debug + TypeFoldable<'tcx>,
381 "query_response_substitution_guess(original_values={:#?}, query_response={:#?})",
382 original_values, query_response,
385 // For each new universe created in the query result that did
386 // not appear in the original query, create a local
388 let mut universe_map = original_values.universe_map.clone();
389 let num_universes_in_query = original_values.universe_map.len();
390 let num_universes_in_response = query_response.max_universe.as_usize() + 1;
391 for _ in num_universes_in_query..num_universes_in_response {
392 universe_map.push(self.create_next_universe());
394 assert!(!universe_map.is_empty()); // always have the root universe
395 assert_eq!(universe_map[ty::UniverseIndex::ROOT.as_usize()], ty::UniverseIndex::ROOT);
397 // Every canonical query result includes values for each of
398 // the inputs to the query. Therefore, we begin by unifying
399 // these values with the original inputs that were
401 let result_values = &query_response.value.var_values;
402 assert_eq!(original_values.var_values.len(), result_values.len());
404 // Quickly try to find initial values for the canonical
405 // variables in the result in terms of the query. We do this
406 // by iterating down the values that the query gave to each of
407 // the canonical inputs. If we find that one of those values
408 // is directly equal to one of the canonical variables in the
409 // result, then we can type the corresponding value from the
410 // input. See the example above.
411 let mut opt_values: IndexVec<BoundVar, Option<GenericArg<'tcx>>> =
412 IndexVec::from_elem_n(None, query_response.variables.len());
414 // In terms of our example above, we are iterating over pairs like:
415 // [(?A, Vec<?0>), ('static, '?1), (?B, ?0)]
416 for (original_value, result_value) in iter::zip(&original_values.var_values, result_values)
418 match result_value.unpack() {
419 GenericArgKind::Type(result_value) => {
420 // e.g., here `result_value` might be `?0` in the example above...
421 if let ty::Bound(debruijn, b) = *result_value.kind() {
422 // ...in which case we would set `canonical_vars[0]` to `Some(?U)`.
424 // We only allow a `ty::INNERMOST` index in substitutions.
425 assert_eq!(debruijn, ty::INNERMOST);
426 opt_values[b.var] = Some(*original_value);
429 GenericArgKind::Lifetime(result_value) => {
430 // e.g., here `result_value` might be `'?1` in the example above...
431 if let ty::ReLateBound(debruijn, br) = *result_value {
432 // ... in which case we would set `canonical_vars[0]` to `Some('static)`.
434 // We only allow a `ty::INNERMOST` index in substitutions.
435 assert_eq!(debruijn, ty::INNERMOST);
436 opt_values[br.var] = Some(*original_value);
439 GenericArgKind::Const(result_value) => {
440 if let ty::ConstKind::Bound(debrujin, b) = result_value.val() {
441 // ...in which case we would set `canonical_vars[0]` to `Some(const X)`.
443 // We only allow a `ty::INNERMOST` index in substitutions.
444 assert_eq!(debrujin, ty::INNERMOST);
445 opt_values[b] = Some(*original_value);
451 // Create a result substitution: if we found a value for a
452 // given variable in the loop above, use that. Otherwise, use
453 // a fresh inference variable.
454 let result_subst = CanonicalVarValues {
455 var_values: query_response
459 .map(|(index, info)| {
460 if info.is_existential() {
461 match opt_values[BoundVar::new(index)] {
463 None => self.instantiate_canonical_var(cause.span, info, |u| {
464 universe_map[u.as_usize()]
468 self.instantiate_canonical_var(cause.span, info, |u| {
469 universe_map[u.as_usize()]
479 /// Given a "guess" at the values for the canonical variables in
480 /// the input, try to unify with the *actual* values found in the
481 /// query result. Often, but not always, this is a no-op, because
482 /// we already found the mapping in the "guessing" step.
484 /// See also: `query_response_substitution_guess`
485 fn unify_query_response_substitution_guess<R>(
487 cause: &ObligationCause<'tcx>,
488 param_env: ty::ParamEnv<'tcx>,
489 original_values: &OriginalQueryValues<'tcx>,
490 result_subst: &CanonicalVarValues<'tcx>,
491 query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
492 ) -> InferResult<'tcx, ()>
494 R: Debug + TypeFoldable<'tcx>,
496 // A closure that yields the result value for the given
497 // canonical variable; this is taken from
498 // `query_response.var_values` after applying the substitution
500 let substituted_query_response = |index: BoundVar| -> GenericArg<'tcx> {
501 query_response.substitute_projected(self.tcx, &result_subst, |v| v.var_values[index])
504 // Unify the original value for each variable with the value
505 // taken from `query_response` (after applying `result_subst`).
506 self.unify_canonical_vars(cause, param_env, original_values, substituted_query_response)
509 /// Converts the region constraints resulting from a query into an
510 /// iterator of obligations.
511 fn query_outlives_constraints_into_obligations<'a>(
513 cause: &'a ObligationCause<'tcx>,
514 param_env: ty::ParamEnv<'tcx>,
515 unsubstituted_region_constraints: &'a [QueryOutlivesConstraint<'tcx>],
516 result_subst: &'a CanonicalVarValues<'tcx>,
517 ) -> impl Iterator<Item = PredicateObligation<'tcx>> + 'a + Captures<'tcx> {
518 unsubstituted_region_constraints.iter().map(move |&constraint| {
519 let predicate = substitute_value(self.tcx, result_subst, constraint);
520 let ty::OutlivesPredicate(k1, r2) = predicate.skip_binder();
522 let atom = match k1.unpack() {
523 GenericArgKind::Lifetime(r1) => {
524 ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(r1, r2))
526 GenericArgKind::Type(t1) => {
527 ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(t1, r2))
529 GenericArgKind::Const(..) => {
530 // Consts cannot outlive one another, so we don't expect to
531 // encounter this branch.
532 span_bug!(cause.span, "unexpected const outlives {:?}", constraint);
535 let predicate = predicate.rebind(atom).to_predicate(self.tcx);
537 Obligation::new(cause.clone(), param_env, predicate)
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(
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 self.commit_if_ok(|_| {
551 let mut obligations = vec![];
552 for (index, value1) in variables1.var_values.iter().enumerate() {
553 let value2 = variables2(BoundVar::new(index));
555 match (value1.unpack(), value2.unpack()) {
556 (GenericArgKind::Type(v1), GenericArgKind::Type(v2)) => {
558 .extend(self.at(cause, param_env).eq(v1, v2)?.into_obligations());
560 (GenericArgKind::Lifetime(re1), GenericArgKind::Lifetime(re2))
561 if re1.is_erased() && re2.is_erased() =>
565 (GenericArgKind::Lifetime(v1), GenericArgKind::Lifetime(v2)) => {
567 .extend(self.at(cause, param_env).eq(v1, v2)?.into_obligations());
569 (GenericArgKind::Const(v1), GenericArgKind::Const(v2)) => {
570 let ok = self.at(cause, param_env).eq(v1, v2)?;
571 obligations.extend(ok.into_obligations());
574 bug!("kind mismatch, cannot unify {:?} and {:?}", value1, value2,);
578 Ok(InferOk { value: (), obligations })
583 /// Given the region obligations and constraints scraped from the infcx,
584 /// creates query region constraints.
585 pub fn make_query_region_constraints<'tcx>(
587 outlives_obligations: impl Iterator<Item = (Ty<'tcx>, ty::Region<'tcx>)>,
588 region_constraints: &RegionConstraintData<'tcx>,
589 ) -> QueryRegionConstraints<'tcx> {
590 let RegionConstraintData { constraints, verifys, givens, member_constraints } =
593 assert!(verifys.is_empty());
594 assert!(givens.is_empty());
596 let outlives: Vec<_> = constraints
598 .map(|(k, _)| match *k {
599 // Swap regions because we are going from sub (<=) to outlives
601 Constraint::VarSubVar(v1, v2) => ty::OutlivesPredicate(
602 tcx.mk_region(ty::ReVar(v2)).into(),
603 tcx.mk_region(ty::ReVar(v1)),
605 Constraint::VarSubReg(v1, r2) => {
606 ty::OutlivesPredicate(r2.into(), tcx.mk_region(ty::ReVar(v1)))
608 Constraint::RegSubVar(r1, v2) => {
609 ty::OutlivesPredicate(tcx.mk_region(ty::ReVar(v2)).into(), r1)
611 Constraint::RegSubReg(r1, r2) => ty::OutlivesPredicate(r2.into(), r1),
613 .map(ty::Binder::dummy) // no bound vars in the code above
616 .map(|(ty, r)| ty::OutlivesPredicate(ty.into(), r))
617 .map(ty::Binder::dummy), // no bound vars in the code above
621 QueryRegionConstraints { outlives, member_constraints: member_constraints.clone() }
624 struct QueryTypeRelatingDelegate<'a, 'tcx> {
625 infcx: &'a InferCtxt<'a, 'tcx>,
626 obligations: &'a mut Vec<PredicateObligation<'tcx>>,
627 param_env: ty::ParamEnv<'tcx>,
628 cause: &'a ObligationCause<'tcx>,
631 impl<'tcx> TypeRelatingDelegate<'tcx> for QueryTypeRelatingDelegate<'_, 'tcx> {
632 fn param_env(&self) -> ty::ParamEnv<'tcx> {
636 fn create_next_universe(&mut self) -> ty::UniverseIndex {
637 self.infcx.create_next_universe()
640 fn next_existential_region_var(&mut self, from_forall: bool) -> ty::Region<'tcx> {
641 let origin = NllRegionVariableOrigin::Existential { from_forall };
642 self.infcx.next_nll_region_var(origin)
645 fn next_placeholder_region(&mut self, placeholder: ty::PlaceholderRegion) -> ty::Region<'tcx> {
646 self.infcx.tcx.mk_region(ty::RePlaceholder(placeholder))
649 fn generalize_existential(&mut self, universe: ty::UniverseIndex) -> ty::Region<'tcx> {
650 self.infcx.next_nll_region_var_in_universe(
651 NllRegionVariableOrigin::Existential { from_forall: false },
658 sup: ty::Region<'tcx>,
659 sub: ty::Region<'tcx>,
660 _info: ty::VarianceDiagInfo<'tcx>,
662 self.obligations.push(Obligation {
663 cause: self.cause.clone(),
664 param_env: self.param_env,
665 predicate: ty::Binder::dummy(ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(
668 .to_predicate(self.infcx.tcx),
673 fn const_equate(&mut self, _a: Const<'tcx>, _b: Const<'tcx>) {
675 self.cause.span(self.infcx.tcx),
676 "generic_const_exprs: unreachable `const_equate`"
680 fn normalization() -> NormalizationStrategy {
681 NormalizationStrategy::Eager
684 fn forbid_inference_vars() -> bool {