1//! Canonicalization is used to separate some goal from its context,
2//! throwing away unnecessary information in the process.
3//!
4//! This is necessary to cache goals containing inference variables
5//! and placeholders without restricting them to the current `InferCtxt`.
6//!
7//! Canonicalization is fairly involved, for more details see the relevant
8//! section of the [rustc-dev-guide][c].
9//!
10//! [c]: https://rustc-dev-guide.rust-lang.org/solve/canonicalization.html
1112use std::iter;
1314use rustc_index::IndexVec;
15use rustc_type_ir::fold::TypeFoldable;
16use rustc_type_ir::inherent::*;
17use rustc_type_ir::relate::solver_relating::RelateExt;
18use rustc_type_ir::{selfas ty, Canonical, CanonicalVarValues, InferCtxtLike, Interner};
19use tracing::{debug, instrument, trace};
2021use crate::canonicalizer::Canonicalizer;
22use crate::delegate::SolverDelegate;
23use crate::resolve::EagerResolver;
24use crate::solve::eval_ctxt::NestedGoals;
25use crate::solve::{
26CanonicalInput, CanonicalResponse, Certainty, EvalCtxt, ExternalConstraintsData, Goal,
27MaybeCause, NestedNormalizationGoals, NoSolution, PredefinedOpaquesData, QueryInput,
28QueryResult, Response, inspect, response_no_constraints_raw,
29};
3031trait ResponseT<I: Interner> {
32fn var_values(&self) -> CanonicalVarValues<I>;
33}
3435impl<I: Interner> ResponseT<I> for Response<I> {
36fn var_values(&self) -> CanonicalVarValues<I> {
37self.var_values
38 }
39}
4041impl<I: Interner, T> ResponseT<I> for inspect::State<I, T> {
42fn var_values(&self) -> CanonicalVarValues<I> {
43self.var_values
44 }
45}
4647impl<D, I> EvalCtxt<'_, D>
48where
49D: SolverDelegate<Interner = I>,
50 I: Interner,
51{
52/// Canonicalizes the goal remembering the original values
53 /// for each bound variable.
54pub(super) fn canonicalize_goal<T: TypeFoldable<I>>(
55&self,
56 goal: Goal<I, T>,
57 ) -> (Vec<I::GenericArg>, CanonicalInput<I, T>) {
58let opaque_types = self.delegate.clone_opaque_types_for_query_response();
59let (goal, opaque_types) =
60 (goal, opaque_types).fold_with(&mut EagerResolver::new(self.delegate));
6162let mut orig_values = Default::default();
63let canonical = Canonicalizer::canonicalize_input(
64self.delegate,
65&mut orig_values,
66QueryInput {
67goal,
68 predefined_opaques_in_body: self69.cx()
70 .mk_predefined_opaques_in_body(PredefinedOpaquesData { opaque_types }),
71 },
72 );
73let query_input = ty::CanonicalQueryInput { canonical, typing_mode: self.typing_mode() };
74 (orig_values, query_input)
75 }
7677/// To return the constraints of a canonical query to the caller, we canonicalize:
78 ///
79 /// - `var_values`: a map from bound variables in the canonical goal to
80 /// the values inferred while solving the instantiated goal.
81 /// - `external_constraints`: additional constraints which aren't expressible
82 /// using simple unification of inference variables.
83#[instrument(level = "trace", skip(self), ret)]
84pub(in crate::solve) fn evaluate_added_goals_and_make_canonical_response(
85&mut self,
86 certainty: Certainty,
87 ) -> QueryResult<I> {
88self.inspect.make_canonical_response(certainty);
8990let goals_certainty = self.try_evaluate_added_goals()?;
91assert_eq!(
92self.tainted,
93Ok(()),
94"EvalCtxt is tainted -- nested goals may have been dropped in a \
95 previous call to `try_evaluate_added_goals!`"
96);
9798// We only check for leaks from universes which were entered inside
99 // of the query.
100self.delegate.leak_check(self.max_input_universe).map_err(|NoSolution| {
101trace!("failed the leak check");
102 NoSolution
103 })?;
104105// When normalizing, we've replaced the expected term with an unconstrained
106 // inference variable. This means that we dropped information which could
107 // have been important. We handle this by instead returning the nested goals
108 // to the caller, where they are then handled.
109 //
110 // As we return all ambiguous nested goals, we can ignore the certainty returned
111 // by `try_evaluate_added_goals()`.
112let (certainty, normalization_nested_goals) = if self.is_normalizes_to_goal {
113let NestedGoals { normalizes_to_goals, goals } = std::mem::take(&mut self.nested_goals);
114if cfg!(debug_assertions) {
115assert!(normalizes_to_goals.is_empty());
116if goals.is_empty() {
117assert!(matches!(goals_certainty, Certainty::Yes));
118 }
119 }
120 (certainty, NestedNormalizationGoals(goals))
121 } else {
122let certainty = certainty.unify_with(goals_certainty);
123 (certainty, NestedNormalizationGoals::empty())
124 };
125126if let Certainty::Maybe(cause @ MaybeCause::Overflow { .. }) = certainty {
127// If we have overflow, it's probable that we're substituting a type
128 // into itself infinitely and any partial substitutions in the query
129 // response are probably not useful anyways, so just return an empty
130 // query response.
131 //
132 // This may prevent us from potentially useful inference, e.g.
133 // 2 candidates, one ambiguous and one overflow, which both
134 // have the same inference constraints.
135 //
136 // Changing this to retain some constraints in the future
137 // won't be a breaking change, so this is good enough for now.
138return Ok(self.make_ambiguous_response_no_constraints(cause));
139 }
140141let external_constraints =
142self.compute_external_query_constraints(certainty, normalization_nested_goals);
143let (var_values, mut external_constraints) = (self.var_values, external_constraints)
144 .fold_with(&mut EagerResolver::new(self.delegate));
145// Remove any trivial region constraints once we've resolved regions
146external_constraints
147 .region_constraints
148 .retain(|outlives| outlives.0.as_region().is_none_or(|re| re != outlives.1));
149150let canonical = Canonicalizer::canonicalize_response(
151self.delegate,
152self.max_input_universe,
153&mut Default::default(),
154 Response {
155 var_values,
156 certainty,
157 external_constraints: self.cx().mk_external_constraints(external_constraints),
158 },
159 );
160161// HACK: We bail with overflow if the response would have too many non-region
162 // inference variables. This tends to only happen if we encounter a lot of
163 // ambiguous alias types which get replaced with fresh inference variables
164 // during generalization. This prevents hangs caused by an exponential blowup,
165 // see tests/ui/traits/next-solver/coherence-alias-hang.rs.
166 //
167 // We don't do so for `NormalizesTo` goals as we erased the expected term and
168 // bailing with overflow here would prevent us from detecting a type-mismatch,
169 // causing a coherence error in diesel, see #131969. We still bail with overflow
170 // when later returning from the parent AliasRelate goal.
171if !self.is_normalizes_to_goal {
172let num_non_region_vars =
173 canonical.variables.iter().filter(|c| !c.is_region() && c.is_existential()).count();
174if num_non_region_vars > self.cx().recursion_limit() {
175debug!(?num_non_region_vars, "too many inference variables -> overflow");
176return Ok(self.make_ambiguous_response_no_constraints(MaybeCause::Overflow {
177 suggest_increasing_limit: true,
178 }));
179 }
180 }
181182Ok(canonical)
183 }
184185/// Constructs a totally unconstrained, ambiguous response to a goal.
186 ///
187 /// Take care when using this, since often it's useful to respond with
188 /// ambiguity but return constrained variables to guide inference.
189pub(in crate::solve) fn make_ambiguous_response_no_constraints(
190&self,
191 maybe_cause: MaybeCause,
192 ) -> CanonicalResponse<I> {
193response_no_constraints_raw(
194self.cx(),
195self.max_input_universe,
196self.variables,
197Certainty::Maybe(maybe_cause),
198 )
199 }
200201/// Computes the region constraints and *new* opaque types registered when
202 /// proving a goal.
203 ///
204 /// If an opaque was already constrained before proving this goal, then the
205 /// external constraints do not need to record that opaque, since if it is
206 /// further constrained by inference, that will be passed back in the var
207 /// values.
208#[instrument(level = "trace", skip(self), ret)]
209fn compute_external_query_constraints(
210&self,
211 certainty: Certainty,
212 normalization_nested_goals: NestedNormalizationGoals<I>,
213 ) -> ExternalConstraintsData<I> {
214// We only return region constraints once the certainty is `Yes`. This
215 // is necessary as we may drop nested goals on ambiguity, which may result
216 // in unconstrained inference variables in the region constraints. It also
217 // prevents us from emitting duplicate region constraints, avoiding some
218 // unnecessary work. This slightly weakens the leak check in case it uses
219 // region constraints from an ambiguous nested goal. This is tested in both
220 // `tests/ui/higher-ranked/leak-check/leak-check-in-selection-5-ambig.rs` and
221 // `tests/ui/higher-ranked/leak-check/leak-check-in-selection-6-ambig-unify.rs`.
222let region_constraints = if certainty == Certainty::Yes {
223self.delegate.make_deduplicated_outlives_constraints()
224 } else {
225 Default::default()
226 };
227228 ExternalConstraintsData {
229 region_constraints,
230 opaque_types: self
231.delegate
232 .clone_opaque_types_for_query_response()
233 .into_iter()
234// Only return *newly defined* opaque types.
235.filter(|(a, _)| {
236self.predefined_opaques_in_body.opaque_types.iter().all(|(pa, _)| pa != a)
237 })
238 .collect(),
239 normalization_nested_goals,
240 }
241 }
242243/// After calling a canonical query, we apply the constraints returned
244 /// by the query using this function.
245 ///
246 /// This happens in three steps:
247 /// - we instantiate the bound variables of the query response
248 /// - we unify the `var_values` of the response with the `original_values`
249 /// - we apply the `external_constraints` returned by the query, returning
250 /// the `normalization_nested_goals`
251pub(super) fn instantiate_and_apply_query_response(
252&mut self,
253 param_env: I::ParamEnv,
254 original_values: Vec<I::GenericArg>,
255 response: CanonicalResponse<I>,
256 ) -> (NestedNormalizationGoals<I>, Certainty) {
257let instantiation = Self::compute_query_response_instantiation_values(
258self.delegate,
259&original_values,
260&response,
261self.origin_span,
262 );
263264let Response { var_values, external_constraints, certainty } =
265self.delegate.instantiate_canonical(response, instantiation);
266267Self::unify_query_var_values(
268self.delegate,
269param_env,
270&original_values,
271var_values,
272self.origin_span,
273 );
274275let ExternalConstraintsData {
276 region_constraints,
277 opaque_types,
278 normalization_nested_goals,
279 } = &*external_constraints;
280281self.register_region_constraints(region_constraints);
282self.register_new_opaque_types(opaque_types);
283284 (normalization_nested_goals.clone(), certainty)
285 }
286287/// This returns the canonical variable values to instantiate the bound variables of
288 /// the canonical response. This depends on the `original_values` for the
289 /// bound variables.
290fn compute_query_response_instantiation_values<T: ResponseT<I>>(
291 delegate: &D,
292 original_values: &[I::GenericArg],
293 response: &Canonical<I, T>,
294 span: I::Span,
295 ) -> CanonicalVarValues<I> {
296// FIXME: Longterm canonical queries should deal with all placeholders
297 // created inside of the query directly instead of returning them to the
298 // caller.
299let prev_universe = delegate.universe();
300let universes_created_in_query = response.max_universe.index();
301for _ in 0..universes_created_in_query {
302 delegate.create_next_universe();
303 }
304305let var_values = response.value.var_values();
306assert_eq!(original_values.len(), var_values.len());
307308// If the query did not make progress with constraining inference variables,
309 // we would normally create a new inference variables for bound existential variables
310 // only then unify this new inference variable with the inference variable from
311 // the input.
312 //
313 // We therefore instantiate the existential variable in the canonical response with the
314 // inference variable of the input right away, which is more performant.
315let mut opt_values = IndexVec::from_elem_n(None, response.variables.len());
316for (original_value, result_value) in
317iter::zip(original_values, var_values.var_values.iter())
318 {
319match result_value.kind() {
320 ty::GenericArgKind::Type(t) => {
321if let ty::Bound(debruijn, b) = t.kind() {
322assert_eq!(debruijn, ty::INNERMOST);
323 opt_values[b.var()] = Some(*original_value);
324 }
325 }
326 ty::GenericArgKind::Lifetime(r) => {
327if let ty::ReBound(debruijn, br) = r.kind() {
328assert_eq!(debruijn, ty::INNERMOST);
329 opt_values[br.var()] = Some(*original_value);
330 }
331 }
332 ty::GenericArgKind::Const(c) => {
333if let ty::ConstKind::Bound(debruijn, bv) = c.kind() {
334assert_eq!(debruijn, ty::INNERMOST);
335 opt_values[bv.var()] = Some(*original_value);
336 }
337 }
338 }
339 }
340341let var_values = delegate.cx().mk_args_from_iter(
342response.variables.iter().enumerate().map(|(index, info)| {
343if info.universe() != ty::UniverseIndex::ROOT {
344// A variable from inside a binder of the query. While ideally these shouldn't
345 // exist at all (see the FIXME at the start of this method), we have to deal with
346 // them for now.
347delegate.instantiate_canonical_var_with_infer(info, span, |idx| {
348ty::UniverseIndex::from(prev_universe.index() + idx.index())
349 })
350 } else if info.is_existential() {
351// As an optimization we sometimes avoid creating a new inference variable here.
352 //
353 // All new inference variables we create start out in the current universe of the caller.
354 // This is conceptually wrong as these inference variables would be able to name
355 // more placeholders then they should be able to. However the inference variables have
356 // to "come from somewhere", so by equating them with the original values of the caller
357 // later on, we pull them down into their correct universe again.
358if let Some(v) = opt_values[ty::BoundVar::from_usize(index)] {
359v360 } else {
361delegate.instantiate_canonical_var_with_infer(info, span, |_| prev_universe)
362 }
363 } else {
364// For placeholders which were already part of the input, we simply map this
365 // universal bound variable back the placeholder of the input.
366original_values[info.expect_placeholder_index()]
367 }
368 }),
369 );
370371CanonicalVarValues { var_values }
372 }
373374/// Unify the `original_values` with the `var_values` returned by the canonical query..
375 ///
376 /// This assumes that this unification will always succeed. This is the case when
377 /// applying a query response right away. However, calling a canonical query, doing any
378 /// other kind of trait solving, and only then instantiating the result of the query
379 /// can cause the instantiation to fail. This is not supported and we ICE in this case.
380 ///
381 /// We always structurally instantiate aliases. Relating aliases needs to be different
382 /// depending on whether the alias is *rigid* or not. We're only really able to tell
383 /// whether an alias is rigid by using the trait solver. When instantiating a response
384 /// from the solver we assume that the solver correctly handled aliases and therefore
385 /// always relate them structurally here.
386#[instrument(level = "trace", skip(delegate))]
387fn unify_query_var_values(
388 delegate: &D,
389 param_env: I::ParamEnv,
390 original_values: &[I::GenericArg],
391 var_values: CanonicalVarValues<I>,
392 span: I::Span,
393 ) {
394assert_eq!(original_values.len(), var_values.len());
395396for (&orig, response) in iter::zip(original_values, var_values.var_values.iter()) {
397let goals =
398 delegate.eq_structurally_relating_aliases(param_env, orig, response, span).unwrap();
399assert!(goals.is_empty());
400 }
401 }
402403fn register_region_constraints(
404&mut self,
405 outlives: &[ty::OutlivesPredicate<I, I::GenericArg>],
406 ) {
407for &ty::OutlivesPredicate(lhs, rhs) in outlives {
408match lhs.kind() {
409 ty::GenericArgKind::Lifetime(lhs) => self.register_region_outlives(lhs, rhs),
410 ty::GenericArgKind::Type(lhs) => self.register_ty_outlives(lhs, rhs),
411 ty::GenericArgKind::Const(_) => panic!("const outlives: {lhs:?}: {rhs:?}"),
412 }
413 }
414 }
415416fn register_new_opaque_types(&mut self, opaque_types: &[(ty::OpaqueTypeKey<I>, I::Ty)]) {
417for &(key, ty) in opaque_types {
418self.delegate.inject_new_hidden_type_unchecked(key, ty, self.origin_span);
419 }
420 }
421}
422423/// Used by proof trees to be able to recompute intermediate actions while
424/// evaluating a goal. The `var_values` not only include the bound variables
425/// of the query input, but also contain all unconstrained inference vars
426/// created while evaluating this goal.
427pub(in crate::solve) fn make_canonical_state<D, T, I>(
428 delegate: &D,
429 var_values: &[I::GenericArg],
430 max_input_universe: ty::UniverseIndex,
431 data: T,
432) -> inspect::CanonicalState<I, T>
433where
434D: SolverDelegate<Interner = I>,
435 I: Interner,
436 T: TypeFoldable<I>,
437{
438let var_values = CanonicalVarValues { var_values: delegate.cx().mk_args(var_values) };
439let state = inspect::State { var_values, data };
440let state = state.fold_with(&mut EagerResolver::new(delegate));
441Canonicalizer::canonicalize_response(delegate, max_input_universe, &mut vec![], state)
442}
443444// FIXME: needs to be pub to be accessed by downstream
445// `rustc_trait_selection::solve::inspect::analyse`.
446pub fn instantiate_canonical_state<D, I, T: TypeFoldable<I>>(
447 delegate: &D,
448 span: I::Span,
449 param_env: I::ParamEnv,
450 orig_values: &mut Vec<I::GenericArg>,
451 state: inspect::CanonicalState<I, T>,
452) -> T
453where
454D: SolverDelegate<Interner = I>,
455 I: Interner,
456{
457// In case any fresh inference variables have been created between `state`
458 // and the previous instantiation, extend `orig_values` for it.
459orig_values.extend(
460state.value.var_values.var_values.as_slice()[orig_values.len()..]
461 .iter()
462 .map(|&arg| delegate.fresh_var_for_kind_with_span(arg, span)),
463 );
464465let instantiation =
466EvalCtxt::compute_query_response_instantiation_values(delegate, orig_values, &state, span);
467468let inspect::State { var_values, data } = delegate.instantiate_canonical(state, instantiation);
469470EvalCtxt::unify_query_var_values(delegate, param_env, orig_values, var_values, span);
471data472}