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 guide][c].
8 //! [c]: https://rust-lang.github.io/rustc-guide/traits/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::region_constraints::{Constraint, RegionConstraintData};
16 use crate::infer::InferCtxtBuilder;
17 use crate::infer::{InferCtxt, InferOk, InferResult};
18 use crate::traits::query::{Fallible, NoSolution};
19 use crate::traits::TraitEngine;
20 use crate::traits::{Obligation, ObligationCause, PredicateObligation};
21 use rustc::arena::ArenaAllocatable;
22 use rustc::ty::fold::TypeFoldable;
23 use rustc::ty::subst::{GenericArg, GenericArgKind};
24 use rustc::ty::{self, BoundVar, Ty, TyCtxt};
25 use rustc_data_structures::captures::Captures;
26 use rustc_index::vec::Idx;
27 use rustc_index::vec::IndexVec;
28 use rustc_span::DUMMY_SP;
31 impl<'tcx> InferCtxtBuilder<'tcx> {
32 /// The "main method" for a canonicalized trait query. Given the
33 /// canonical key `canonical_key`, this method will create a new
34 /// inference context, instantiate the key, and run your operation
35 /// `op`. The operation should yield up a result (of type `R`) as
36 /// well as a set of trait obligations that must be fully
37 /// satisfied. These obligations will be processed and the
38 /// canonical result created.
40 /// Returns `NoSolution` in the event of any error.
42 /// (It might be mildly nicer to implement this on `TyCtxt`, and
43 /// not `InferCtxtBuilder`, but that is a bit tricky right now.
44 /// In part because we would need a `for<'tcx>` sort of
45 /// bound for the closure and in part because it is convenient to
46 /// have `'tcx` be free on this function so that we can talk about
47 /// `K: TypeFoldable<'tcx>`.)
48 pub fn enter_canonical_trait_query<K, R>(
50 canonical_key: &Canonical<'tcx, K>,
51 operation: impl FnOnce(&InferCtxt<'_, 'tcx>, &mut dyn TraitEngine<'tcx>, K) -> Fallible<R>,
52 ) -> Fallible<CanonicalizedQueryResponse<'tcx, R>>
54 K: TypeFoldable<'tcx>,
55 R: Debug + TypeFoldable<'tcx>,
56 Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable,
58 self.enter_with_canonical(
61 |ref infcx, key, canonical_inference_vars| {
62 let mut fulfill_cx = TraitEngine::new(infcx.tcx);
63 let value = operation(infcx, &mut *fulfill_cx, key)?;
64 infcx.make_canonicalized_query_response(
65 canonical_inference_vars,
74 impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
75 /// This method is meant to be invoked as the final step of a canonical query
76 /// implementation. It is given:
78 /// - the instantiated variables `inference_vars` created from the query key
79 /// - the result `answer` of the query
80 /// - a fulfillment context `fulfill_cx` that may contain various obligations which
81 /// have yet to be proven.
83 /// Given this, the function will process the obligations pending
86 /// - If all the obligations can be proven successfully, it will
87 /// package up any resulting region obligations (extracted from
88 /// `infcx`) along with the fully resolved value `answer` into a
89 /// query result (which is then itself canonicalized).
90 /// - If some obligations can be neither proven nor disproven, then
91 /// the same thing happens, but the resulting query is marked as ambiguous.
92 /// - Finally, if any of the obligations result in a hard error,
93 /// then `Err(NoSolution)` is returned.
94 pub fn make_canonicalized_query_response<T>(
96 inference_vars: CanonicalVarValues<'tcx>,
98 fulfill_cx: &mut dyn TraitEngine<'tcx>,
99 ) -> Fallible<CanonicalizedQueryResponse<'tcx, T>>
101 T: Debug + TypeFoldable<'tcx>,
102 Canonical<'tcx, QueryResponse<'tcx, T>>: ArenaAllocatable,
104 let query_response = self.make_query_response(inference_vars, answer, fulfill_cx)?;
105 let canonical_result = self.canonicalize_response(&query_response);
107 debug!("make_canonicalized_query_response: canonical_result = {:#?}", canonical_result);
109 Ok(self.tcx.arena.alloc(canonical_result))
112 /// A version of `make_canonicalized_query_response` that does
113 /// not pack in obligations, for contexts that want to drop
114 /// pending obligations instead of treating them as an ambiguity (e.g.
115 /// typeck "probing" contexts).
117 /// If you DO want to keep track of pending obligations (which
118 /// include all region obligations, so this includes all cases
119 /// that care about regions) with this function, you have to
120 /// do it yourself, by e.g., having them be a part of the answer.
121 pub fn make_query_response_ignoring_pending_obligations<T>(
123 inference_vars: CanonicalVarValues<'tcx>,
125 ) -> Canonical<'tcx, QueryResponse<'tcx, T>>
127 T: Debug + TypeFoldable<'tcx>,
129 self.canonicalize_response(&QueryResponse {
130 var_values: inference_vars,
131 region_constraints: QueryRegionConstraints::default(),
132 certainty: Certainty::Proven, // Ambiguities are OK!
137 /// Helper for `make_canonicalized_query_response` that does
138 /// everything up until the final canonicalization.
139 fn make_query_response<T>(
141 inference_vars: CanonicalVarValues<'tcx>,
143 fulfill_cx: &mut dyn TraitEngine<'tcx>,
144 ) -> Result<QueryResponse<'tcx, T>, NoSolution>
146 T: Debug + TypeFoldable<'tcx>,
151 "make_query_response(\
152 inference_vars={:?}, \
154 inference_vars, answer,
157 // Select everything, returning errors.
158 let true_errors = fulfill_cx.select_where_possible(self).err().unwrap_or_else(Vec::new);
159 debug!("true_errors = {:#?}", true_errors);
161 if !true_errors.is_empty() {
162 // FIXME -- we don't indicate *why* we failed to solve
163 debug!("make_query_response: true_errors={:#?}", true_errors);
164 return Err(NoSolution);
167 // Anything left unselected *now* must be an ambiguity.
168 let ambig_errors = fulfill_cx.select_all_or_error(self).err().unwrap_or_else(Vec::new);
169 debug!("ambig_errors = {:#?}", ambig_errors);
171 let region_obligations = self.take_registered_region_obligations();
172 let region_constraints = self.with_region_constraints(|region_constraints| {
173 make_query_region_constraints(
175 region_obligations.iter().map(|(_, r_o)| (r_o.sup_type, r_o.sub_region)),
181 if ambig_errors.is_empty() { Certainty::Proven } else { Certainty::Ambiguous };
184 var_values: inference_vars,
191 /// Given the (canonicalized) result to a canonical query,
192 /// instantiates the result so it can be used, plugging in the
193 /// values from the canonical query. (Note that the result may
194 /// have been ambiguous; you should check the certainty level of
195 /// the query before applying this function.)
197 /// To get a good understanding of what is happening here, check
198 /// out the [chapter in the rustc guide][c].
200 /// [c]: https://rust-lang.github.io/rustc-guide/traits/canonicalization.html#processing-the-canonicalized-query-result
201 pub fn instantiate_query_response_and_region_obligations<R>(
203 cause: &ObligationCause<'tcx>,
204 param_env: ty::ParamEnv<'tcx>,
205 original_values: &OriginalQueryValues<'tcx>,
206 query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
207 ) -> InferResult<'tcx, R>
209 R: Debug + TypeFoldable<'tcx>,
211 let InferOk { value: result_subst, mut obligations } =
212 self.query_response_substitution(cause, param_env, original_values, query_response)?;
214 obligations.extend(self.query_outlives_constraints_into_obligations(
217 &query_response.value.region_constraints.outlives,
222 query_response.substitute_projected(self.tcx, &result_subst, |q_r| &q_r.value);
224 Ok(InferOk { value: user_result, obligations })
227 /// An alternative to
228 /// `instantiate_query_response_and_region_obligations` that is more
229 /// efficient for NLL. NLL is a bit more advanced in the
230 /// "transition to chalk" than the rest of the compiler. During
231 /// the NLL type check, all of the "processing" of types and
232 /// things happens in queries -- the NLL checker itself is only
233 /// interested in the region obligations (`'a: 'b` or `T: 'b`)
234 /// that come out of these queries, which it wants to convert into
235 /// MIR-based constraints and solve. Therefore, it is most
236 /// convenient for the NLL Type Checker to **directly consume**
237 /// the `QueryOutlivesConstraint` values that arise from doing a
238 /// query. This is contrast to other parts of the compiler, which
239 /// would prefer for those `QueryOutlivesConstraint` to be converted
240 /// into the older infcx-style constraints (e.g., calls to
241 /// `sub_regions` or `register_region_obligation`).
243 /// Therefore, `instantiate_nll_query_response_and_region_obligations` performs the same
244 /// basic operations as `instantiate_query_response_and_region_obligations` but
245 /// it returns its result differently:
247 /// - It creates a substitution `S` that maps from the original
248 /// query variables to the values computed in the query
249 /// result. If any errors arise, they are propagated back as an
251 /// - In the case of a successful substitution, we will append
252 /// `QueryOutlivesConstraint` values onto the
253 /// `output_query_region_constraints` vector for the solver to
254 /// use (if an error arises, some values may also be pushed, but
255 /// they should be ignored).
256 /// - It **can happen** (though it rarely does currently) that
257 /// equating types and things will give rise to subobligations
258 /// that must be processed. In this case, those subobligations
259 /// are propagated back in the return value.
260 /// - Finally, the query result (of type `R`) is propagated back,
261 /// after applying the substitution `S`.
262 pub fn instantiate_nll_query_response_and_region_obligations<R>(
264 cause: &ObligationCause<'tcx>,
265 param_env: ty::ParamEnv<'tcx>,
266 original_values: &OriginalQueryValues<'tcx>,
267 query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
268 output_query_region_constraints: &mut QueryRegionConstraints<'tcx>,
269 ) -> InferResult<'tcx, R>
271 R: Debug + TypeFoldable<'tcx>,
274 self.query_response_substitution_guess(cause, original_values, query_response);
276 // Compute `QueryOutlivesConstraint` values that unify each of
277 // the original values `v_o` that was canonicalized into a
279 let mut obligations = vec![];
281 for (index, original_value) in original_values.var_values.iter().enumerate() {
282 // ...with the value `v_r` of that variable from the query.
283 let result_value = query_response.substitute_projected(self.tcx, &result_subst, |v| {
284 &v.var_values[BoundVar::new(index)]
286 match (original_value.unpack(), result_value.unpack()) {
288 GenericArgKind::Lifetime(ty::ReErased),
289 GenericArgKind::Lifetime(ty::ReErased),
294 (GenericArgKind::Lifetime(v_o), GenericArgKind::Lifetime(v_r)) => {
295 // To make `v_o = v_r`, we emit `v_o: v_r` and `v_r: v_o`.
297 output_query_region_constraints
299 .push(ty::Binder::dummy(ty::OutlivesPredicate(v_o.into(), v_r)));
300 output_query_region_constraints
302 .push(ty::Binder::dummy(ty::OutlivesPredicate(v_r.into(), v_o)));
306 (GenericArgKind::Type(v1), GenericArgKind::Type(v2)) => {
307 let ok = self.at(cause, param_env).eq(v1, v2)?;
308 obligations.extend(ok.into_obligations());
311 (GenericArgKind::Const(v1), GenericArgKind::Const(v2)) => {
312 let ok = self.at(cause, param_env).eq(v1, v2)?;
313 obligations.extend(ok.into_obligations());
317 bug!("kind mismatch, cannot unify {:?} and {:?}", original_value, result_value);
322 // ...also include the other query region constraints from the query.
323 output_query_region_constraints.outlives.extend(
324 query_response.value.region_constraints.outlives.iter().filter_map(|r_c| {
325 let r_c = substitute_value(self.tcx, &result_subst, r_c);
327 // Screen out `'a: 'a` cases -- we skip the binder here but
328 // only compare the inner values to one another, so they are still at
329 // consistent binding levels.
330 let &ty::OutlivesPredicate(k1, r2) = r_c.skip_binder();
331 if k1 != r2.into() { Some(r_c) } else { None }
335 // ...also include the query member constraints.
336 output_query_region_constraints.member_constraints.extend(
342 .map(|p_c| substitute_value(self.tcx, &result_subst, p_c)),
346 query_response.substitute_projected(self.tcx, &result_subst, |q_r| &q_r.value);
348 Ok(InferOk { value: user_result, obligations })
351 /// Given the original values and the (canonicalized) result from
352 /// computing a query, returns a substitution that can be applied
353 /// to the query result to convert the result back into the
354 /// original namespace.
356 /// The substitution also comes accompanied with subobligations
357 /// that arose from unification; these might occur if (for
358 /// example) we are doing lazy normalization and the value
359 /// assigned to a type variable is unified with an unnormalized
361 fn query_response_substitution<R>(
363 cause: &ObligationCause<'tcx>,
364 param_env: ty::ParamEnv<'tcx>,
365 original_values: &OriginalQueryValues<'tcx>,
366 query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
367 ) -> InferResult<'tcx, CanonicalVarValues<'tcx>>
369 R: Debug + TypeFoldable<'tcx>,
372 "query_response_substitution(original_values={:#?}, query_response={:#?})",
373 original_values, query_response,
377 self.query_response_substitution_guess(cause, original_values, query_response);
379 let obligations = self
380 .unify_query_response_substitution_guess(
389 Ok(InferOk { value: result_subst, obligations })
392 /// Given the original values and the (canonicalized) result from
393 /// computing a query, returns a **guess** at a substitution that
394 /// can be applied to the query result to convert the result back
395 /// into the original namespace. This is called a **guess**
396 /// because it uses a quick heuristic to find the values for each
397 /// canonical variable; if that quick heuristic fails, then we
398 /// will instantiate fresh inference variables for each canonical
399 /// variable instead. Therefore, the result of this method must be
401 fn query_response_substitution_guess<R>(
403 cause: &ObligationCause<'tcx>,
404 original_values: &OriginalQueryValues<'tcx>,
405 query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
406 ) -> CanonicalVarValues<'tcx>
408 R: Debug + TypeFoldable<'tcx>,
411 "query_response_substitution_guess(original_values={:#?}, query_response={:#?})",
412 original_values, query_response,
415 // For each new universe created in the query result that did
416 // not appear in the original query, create a local
418 let mut universe_map = original_values.universe_map.clone();
419 let num_universes_in_query = original_values.universe_map.len();
420 let num_universes_in_response = query_response.max_universe.as_usize() + 1;
421 for _ in num_universes_in_query..num_universes_in_response {
422 universe_map.push(self.create_next_universe());
424 assert!(!universe_map.is_empty()); // always have the root universe
425 assert_eq!(universe_map[ty::UniverseIndex::ROOT.as_usize()], ty::UniverseIndex::ROOT);
427 // Every canonical query result includes values for each of
428 // the inputs to the query. Therefore, we begin by unifying
429 // these values with the original inputs that were
431 let result_values = &query_response.value.var_values;
432 assert_eq!(original_values.var_values.len(), result_values.len());
434 // Quickly try to find initial values for the canonical
435 // variables in the result in terms of the query. We do this
436 // by iterating down the values that the query gave to each of
437 // the canonical inputs. If we find that one of those values
438 // is directly equal to one of the canonical variables in the
439 // result, then we can type the corresponding value from the
440 // input. See the example above.
441 let mut opt_values: IndexVec<BoundVar, Option<GenericArg<'tcx>>> =
442 IndexVec::from_elem_n(None, query_response.variables.len());
444 // In terms of our example above, we are iterating over pairs like:
445 // [(?A, Vec<?0>), ('static, '?1), (?B, ?0)]
446 for (original_value, result_value) in original_values.var_values.iter().zip(result_values) {
447 match result_value.unpack() {
448 GenericArgKind::Type(result_value) => {
449 // e.g., here `result_value` might be `?0` in the example above...
450 if let ty::Bound(debruijn, b) = result_value.kind {
451 // ...in which case we would set `canonical_vars[0]` to `Some(?U)`.
453 // We only allow a `ty::INNERMOST` index in substitutions.
454 assert_eq!(debruijn, ty::INNERMOST);
455 opt_values[b.var] = Some(*original_value);
458 GenericArgKind::Lifetime(result_value) => {
459 // e.g., here `result_value` might be `'?1` in the example above...
460 if let &ty::RegionKind::ReLateBound(debruijn, br) = result_value {
461 // ... in which case we would set `canonical_vars[0]` to `Some('static)`.
463 // We only allow a `ty::INNERMOST` index in substitutions.
464 assert_eq!(debruijn, ty::INNERMOST);
465 opt_values[br.assert_bound_var()] = Some(*original_value);
468 GenericArgKind::Const(result_value) => {
469 if let ty::Const { val: ty::ConstKind::Bound(debrujin, b), .. } = result_value {
470 // ...in which case we would set `canonical_vars[0]` to `Some(const X)`.
472 // We only allow a `ty::INNERMOST` index in substitutions.
473 assert_eq!(*debrujin, ty::INNERMOST);
474 opt_values[*b] = Some(*original_value);
480 // Create a result substitution: if we found a value for a
481 // given variable in the loop above, use that. Otherwise, use
482 // a fresh inference variable.
483 let result_subst = CanonicalVarValues {
484 var_values: query_response
488 .map(|(index, info)| {
489 if info.is_existential() {
490 match opt_values[BoundVar::new(index)] {
492 None => self.instantiate_canonical_var(cause.span, *info, |u| {
493 universe_map[u.as_usize()]
497 self.instantiate_canonical_var(cause.span, *info, |u| {
498 universe_map[u.as_usize()]
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.
513 /// See also: `query_response_substitution_guess`
514 fn unify_query_response_substitution_guess<R>(
516 cause: &ObligationCause<'tcx>,
517 param_env: ty::ParamEnv<'tcx>,
518 original_values: &OriginalQueryValues<'tcx>,
519 result_subst: &CanonicalVarValues<'tcx>,
520 query_response: &Canonical<'tcx, QueryResponse<'tcx, R>>,
521 ) -> InferResult<'tcx, ()>
523 R: Debug + TypeFoldable<'tcx>,
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 substitution
529 let substituted_query_response = |index: BoundVar| -> GenericArg<'tcx> {
530 query_response.substitute_projected(self.tcx, &result_subst, |v| &v.var_values[index])
533 // Unify the original value for each variable with the value
534 // taken from `query_response` (after applying `result_subst`).
535 Ok(self.unify_canonical_vars(
539 substituted_query_response,
543 /// Converts the region constraints resulting from a query into an
544 /// iterator of obligations.
545 fn query_outlives_constraints_into_obligations<'a>(
547 cause: &'a ObligationCause<'tcx>,
548 param_env: ty::ParamEnv<'tcx>,
549 unsubstituted_region_constraints: &'a [QueryOutlivesConstraint<'tcx>],
550 result_subst: &'a CanonicalVarValues<'tcx>,
551 ) -> impl Iterator<Item = PredicateObligation<'tcx>> + 'a + Captures<'tcx> {
552 unsubstituted_region_constraints.iter().map(move |constraint| {
553 let constraint = substitute_value(self.tcx, result_subst, constraint);
554 let &ty::OutlivesPredicate(k1, r2) = constraint.skip_binder(); // restored below
560 GenericArgKind::Lifetime(r1) => ty::Predicate::RegionOutlives(
561 ty::Binder::bind(ty::OutlivesPredicate(r1, r2)),
563 GenericArgKind::Type(t1) => {
564 ty::Predicate::TypeOutlives(ty::Binder::bind(ty::OutlivesPredicate(t1, r2)))
566 GenericArgKind::Const(..) => {
567 // Consts cannot outlive one another, so we don't expect to
568 // ecounter this branch.
569 span_bug!(cause.span, "unexpected const outlives {:?}", constraint);
576 /// Given two sets of values for the same set of canonical variables, unify them.
577 /// The second set is produced lazily by supplying indices from the first set.
578 fn unify_canonical_vars(
580 cause: &ObligationCause<'tcx>,
581 param_env: ty::ParamEnv<'tcx>,
582 variables1: &OriginalQueryValues<'tcx>,
583 variables2: impl Fn(BoundVar) -> GenericArg<'tcx>,
584 ) -> InferResult<'tcx, ()> {
585 self.commit_if_ok(|_| {
586 let mut obligations = vec![];
587 for (index, value1) in variables1.var_values.iter().enumerate() {
588 let value2 = variables2(BoundVar::new(index));
590 match (value1.unpack(), value2.unpack()) {
591 (GenericArgKind::Type(v1), GenericArgKind::Type(v2)) => {
593 .extend(self.at(cause, param_env).eq(v1, v2)?.into_obligations());
596 GenericArgKind::Lifetime(ty::ReErased),
597 GenericArgKind::Lifetime(ty::ReErased),
601 (GenericArgKind::Lifetime(v1), GenericArgKind::Lifetime(v2)) => {
603 .extend(self.at(cause, param_env).eq(v1, v2)?.into_obligations());
605 (GenericArgKind::Const(v1), GenericArgKind::Const(v2)) => {
606 let ok = self.at(cause, param_env).eq(v1, v2)?;
607 obligations.extend(ok.into_obligations());
610 bug!("kind mismatch, cannot unify {:?} and {:?}", value1, value2,);
614 Ok(InferOk { value: (), obligations })
619 /// Given the region obligations and constraints scraped from the infcx,
620 /// creates query region constraints.
621 pub fn make_query_region_constraints<'tcx>(
623 outlives_obligations: impl Iterator<Item = (Ty<'tcx>, ty::Region<'tcx>)>,
624 region_constraints: &RegionConstraintData<'tcx>,
625 ) -> QueryRegionConstraints<'tcx> {
626 let RegionConstraintData { constraints, verifys, givens, member_constraints } =
629 assert!(verifys.is_empty());
630 assert!(givens.is_empty());
632 let outlives: Vec<_> = constraints
634 .map(|(k, _)| match *k {
635 // Swap regions because we are going from sub (<=) to outlives
637 Constraint::VarSubVar(v1, v2) => ty::OutlivesPredicate(
638 tcx.mk_region(ty::ReVar(v2)).into(),
639 tcx.mk_region(ty::ReVar(v1)),
641 Constraint::VarSubReg(v1, r2) => {
642 ty::OutlivesPredicate(r2.into(), tcx.mk_region(ty::ReVar(v1)))
644 Constraint::RegSubVar(r1, v2) => {
645 ty::OutlivesPredicate(tcx.mk_region(ty::ReVar(v2)).into(), r1)
647 Constraint::RegSubReg(r1, r2) => ty::OutlivesPredicate(r2.into(), r1),
649 .map(ty::Binder::dummy) // no bound vars in the code above
652 .map(|(ty, r)| ty::OutlivesPredicate(ty.into(), r))
653 .map(ty::Binder::dummy), // no bound vars in the code above
657 QueryRegionConstraints { outlives, member_constraints: member_constraints.clone() }