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::inherent::*;
16use rustc_type_ir::relate::solver_relating::RelateExt;
17use rustc_type_ir::{
18selfas ty, Canonical, CanonicalVarValues, InferCtxtLike, Interner, TypeFoldable,
19};
20use tracing::{debug, instrument, trace};
2122use crate::canonicalizer::Canonicalizer;
23use crate::delegate::SolverDelegate;
24use crate::resolve::EagerResolver;
25use crate::solve::eval_ctxt::{CurrentGoalKind, NestedGoals};
26use crate::solve::{
27CanonicalInput, CanonicalResponse, Certainty, EvalCtxt, ExternalConstraintsData, Goal,
28MaybeCause, NestedNormalizationGoals, NoSolution, PredefinedOpaquesData, QueryInput,
29QueryResult, Response, inspect, response_no_constraints_raw,
30};
3132trait ResponseT<I: Interner> {
33fn var_values(&self) -> CanonicalVarValues<I>;
34}
3536impl<I: Interner> ResponseT<I> for Response<I> {
37fn var_values(&self) -> CanonicalVarValues<I> {
38self.var_values
39 }
40}
4142impl<I: Interner, T> ResponseT<I> for inspect::State<I, T> {
43fn var_values(&self) -> CanonicalVarValues<I> {
44self.var_values
45 }
46}
4748impl<D, I> EvalCtxt<'_, D>
49where
50D: SolverDelegate<Interner = I>,
51 I: Interner,
52{
53/// Canonicalizes the goal remembering the original values
54 /// for each bound variable.
55pub(super) fn canonicalize_goal<T: TypeFoldable<I>>(
56&self,
57 goal: Goal<I, T>,
58 ) -> (Vec<I::GenericArg>, CanonicalInput<I, T>) {
59let opaque_types = self.delegate.clone_opaque_types_for_query_response();
60let (goal, opaque_types) =
61 (goal, opaque_types).fold_with(&mut EagerResolver::new(self.delegate));
6263let mut orig_values = Default::default();
64let canonical = Canonicalizer::canonicalize_input(
65self.delegate,
66&mut orig_values,
67QueryInput {
68goal,
69 predefined_opaques_in_body: self70.cx()
71 .mk_predefined_opaques_in_body(PredefinedOpaquesData { opaque_types }),
72 },
73 );
74let query_input = ty::CanonicalQueryInput { canonical, typing_mode: self.typing_mode() };
75 (orig_values, query_input)
76 }
7778/// To return the constraints of a canonical query to the caller, we canonicalize:
79 ///
80 /// - `var_values`: a map from bound variables in the canonical goal to
81 /// the values inferred while solving the instantiated goal.
82 /// - `external_constraints`: additional constraints which aren't expressible
83 /// using simple unification of inference variables.
84#[instrument(level = "trace", skip(self), ret)]
85pub(in crate::solve) fn evaluate_added_goals_and_make_canonical_response(
86&mut self,
87 certainty: Certainty,
88 ) -> QueryResult<I> {
89self.inspect.make_canonical_response(certainty);
9091let goals_certainty = self.try_evaluate_added_goals()?;
92assert_eq!(
93self.tainted,
94Ok(()),
95"EvalCtxt is tainted -- nested goals may have been dropped in a \
96 previous call to `try_evaluate_added_goals!`"
97);
9899// We only check for leaks from universes which were entered inside
100 // of the query.
101self.delegate.leak_check(self.max_input_universe).map_err(|NoSolution| {
102trace!("failed the leak check");
103 NoSolution
104 })?;
105106// When normalizing, we've replaced the expected term with an unconstrained
107 // inference variable. This means that we dropped information which could
108 // have been important. We handle this by instead returning the nested goals
109 // to the caller, where they are then handled.
110 //
111 // As we return all ambiguous nested goals, we can ignore the certainty returned
112 // by `try_evaluate_added_goals()`.
113let (certainty, normalization_nested_goals) = match self.current_goal_kind {
114 CurrentGoalKind::NormalizesTo => {
115let NestedGoals { normalizes_to_goals, goals } =
116 std::mem::take(&mut self.nested_goals);
117if cfg!(debug_assertions) {
118assert!(normalizes_to_goals.is_empty());
119if goals.is_empty() {
120assert!(matches!(goals_certainty, Certainty::Yes));
121 }
122 }
123 (certainty, NestedNormalizationGoals(goals))
124 }
125 CurrentGoalKind::Misc | CurrentGoalKind::CoinductiveTrait => {
126let certainty = certainty.unify_with(goals_certainty);
127 (certainty, NestedNormalizationGoals::empty())
128 }
129 };
130131if let Certainty::Maybe(cause @ MaybeCause::Overflow { .. }) = certainty {
132// If we have overflow, it's probable that we're substituting a type
133 // into itself infinitely and any partial substitutions in the query
134 // response are probably not useful anyways, so just return an empty
135 // query response.
136 //
137 // This may prevent us from potentially useful inference, e.g.
138 // 2 candidates, one ambiguous and one overflow, which both
139 // have the same inference constraints.
140 //
141 // Changing this to retain some constraints in the future
142 // won't be a breaking change, so this is good enough for now.
143return Ok(self.make_ambiguous_response_no_constraints(cause));
144 }
145146let external_constraints =
147self.compute_external_query_constraints(certainty, normalization_nested_goals);
148let (var_values, mut external_constraints) = (self.var_values, external_constraints)
149 .fold_with(&mut EagerResolver::new(self.delegate));
150// Remove any trivial region constraints once we've resolved regions
151external_constraints
152 .region_constraints
153 .retain(|outlives| outlives.0.as_region().is_none_or(|re| re != outlives.1));
154155let canonical = Canonicalizer::canonicalize_response(
156self.delegate,
157self.max_input_universe,
158&mut Default::default(),
159 Response {
160 var_values,
161 certainty,
162 external_constraints: self.cx().mk_external_constraints(external_constraints),
163 },
164 );
165166// HACK: We bail with overflow if the response would have too many non-region
167 // inference variables. This tends to only happen if we encounter a lot of
168 // ambiguous alias types which get replaced with fresh inference variables
169 // during generalization. This prevents hangs caused by an exponential blowup,
170 // see tests/ui/traits/next-solver/coherence-alias-hang.rs.
171match self.current_goal_kind {
172// We don't do so for `NormalizesTo` goals as we erased the expected term and
173 // bailing with overflow here would prevent us from detecting a type-mismatch,
174 // causing a coherence error in diesel, see #131969. We still bail with overflow
175 // when later returning from the parent AliasRelate goal.
176CurrentGoalKind::NormalizesTo => {}
177 CurrentGoalKind::Misc | CurrentGoalKind::CoinductiveTrait => {
178let num_non_region_vars = canonical
179 .variables
180 .iter()
181 .filter(|c| !c.is_region() && c.is_existential())
182 .count();
183if num_non_region_vars > self.cx().recursion_limit() {
184debug!(?num_non_region_vars, "too many inference variables -> overflow");
185return Ok(self.make_ambiguous_response_no_constraints(MaybeCause::Overflow {
186 suggest_increasing_limit: true,
187 }));
188 }
189 }
190 }
191192Ok(canonical)
193 }
194195/// Constructs a totally unconstrained, ambiguous response to a goal.
196 ///
197 /// Take care when using this, since often it's useful to respond with
198 /// ambiguity but return constrained variables to guide inference.
199pub(in crate::solve) fn make_ambiguous_response_no_constraints(
200&self,
201 maybe_cause: MaybeCause,
202 ) -> CanonicalResponse<I> {
203response_no_constraints_raw(
204self.cx(),
205self.max_input_universe,
206self.variables,
207Certainty::Maybe(maybe_cause),
208 )
209 }
210211/// Computes the region constraints and *new* opaque types registered when
212 /// proving a goal.
213 ///
214 /// If an opaque was already constrained before proving this goal, then the
215 /// external constraints do not need to record that opaque, since if it is
216 /// further constrained by inference, that will be passed back in the var
217 /// values.
218#[instrument(level = "trace", skip(self), ret)]
219fn compute_external_query_constraints(
220&self,
221 certainty: Certainty,
222 normalization_nested_goals: NestedNormalizationGoals<I>,
223 ) -> ExternalConstraintsData<I> {
224// We only return region constraints once the certainty is `Yes`. This
225 // is necessary as we may drop nested goals on ambiguity, which may result
226 // in unconstrained inference variables in the region constraints. It also
227 // prevents us from emitting duplicate region constraints, avoiding some
228 // unnecessary work. This slightly weakens the leak check in case it uses
229 // region constraints from an ambiguous nested goal. This is tested in both
230 // `tests/ui/higher-ranked/leak-check/leak-check-in-selection-5-ambig.rs` and
231 // `tests/ui/higher-ranked/leak-check/leak-check-in-selection-6-ambig-unify.rs`.
232let region_constraints = if certainty == Certainty::Yes {
233self.delegate.make_deduplicated_outlives_constraints()
234 } else {
235 Default::default()
236 };
237238 ExternalConstraintsData {
239 region_constraints,
240 opaque_types: self
241.delegate
242 .clone_opaque_types_for_query_response()
243 .into_iter()
244// Only return *newly defined* opaque types.
245.filter(|(a, _)| {
246self.predefined_opaques_in_body.opaque_types.iter().all(|(pa, _)| pa != a)
247 })
248 .collect(),
249 normalization_nested_goals,
250 }
251 }
252253/// After calling a canonical query, we apply the constraints returned
254 /// by the query using this function.
255 ///
256 /// This happens in three steps:
257 /// - we instantiate the bound variables of the query response
258 /// - we unify the `var_values` of the response with the `original_values`
259 /// - we apply the `external_constraints` returned by the query, returning
260 /// the `normalization_nested_goals`
261pub(super) fn instantiate_and_apply_query_response(
262&mut self,
263 param_env: I::ParamEnv,
264 original_values: Vec<I::GenericArg>,
265 response: CanonicalResponse<I>,
266 ) -> (NestedNormalizationGoals<I>, Certainty) {
267let instantiation = Self::compute_query_response_instantiation_values(
268self.delegate,
269&original_values,
270&response,
271self.origin_span,
272 );
273274let Response { var_values, external_constraints, certainty } =
275self.delegate.instantiate_canonical(response, instantiation);
276277Self::unify_query_var_values(
278self.delegate,
279param_env,
280&original_values,
281var_values,
282self.origin_span,
283 );
284285let ExternalConstraintsData {
286 region_constraints,
287 opaque_types,
288 normalization_nested_goals,
289 } = &*external_constraints;
290291self.register_region_constraints(region_constraints);
292self.register_new_opaque_types(opaque_types);
293294 (normalization_nested_goals.clone(), certainty)
295 }
296297/// This returns the canonical variable values to instantiate the bound variables of
298 /// the canonical response. This depends on the `original_values` for the
299 /// bound variables.
300fn compute_query_response_instantiation_values<T: ResponseT<I>>(
301 delegate: &D,
302 original_values: &[I::GenericArg],
303 response: &Canonical<I, T>,
304 span: I::Span,
305 ) -> CanonicalVarValues<I> {
306// FIXME: Longterm canonical queries should deal with all placeholders
307 // created inside of the query directly instead of returning them to the
308 // caller.
309let prev_universe = delegate.universe();
310let universes_created_in_query = response.max_universe.index();
311for _ in 0..universes_created_in_query {
312 delegate.create_next_universe();
313 }
314315let var_values = response.value.var_values();
316assert_eq!(original_values.len(), var_values.len());
317318// If the query did not make progress with constraining inference variables,
319 // we would normally create a new inference variables for bound existential variables
320 // only then unify this new inference variable with the inference variable from
321 // the input.
322 //
323 // We therefore instantiate the existential variable in the canonical response with the
324 // inference variable of the input right away, which is more performant.
325let mut opt_values = IndexVec::from_elem_n(None, response.variables.len());
326for (original_value, result_value) in
327iter::zip(original_values, var_values.var_values.iter())
328 {
329match result_value.kind() {
330 ty::GenericArgKind::Type(t) => {
331if let ty::Bound(debruijn, b) = t.kind() {
332assert_eq!(debruijn, ty::INNERMOST);
333 opt_values[b.var()] = Some(*original_value);
334 }
335 }
336 ty::GenericArgKind::Lifetime(r) => {
337if let ty::ReBound(debruijn, br) = r.kind() {
338assert_eq!(debruijn, ty::INNERMOST);
339 opt_values[br.var()] = Some(*original_value);
340 }
341 }
342 ty::GenericArgKind::Const(c) => {
343if let ty::ConstKind::Bound(debruijn, bv) = c.kind() {
344assert_eq!(debruijn, ty::INNERMOST);
345 opt_values[bv.var()] = Some(*original_value);
346 }
347 }
348 }
349 }
350351let var_values = delegate.cx().mk_args_from_iter(
352response.variables.iter().enumerate().map(|(index, info)| {
353if info.universe() != ty::UniverseIndex::ROOT {
354// A variable from inside a binder of the query. While ideally these shouldn't
355 // exist at all (see the FIXME at the start of this method), we have to deal with
356 // them for now.
357delegate.instantiate_canonical_var_with_infer(info, span, |idx| {
358ty::UniverseIndex::from(prev_universe.index() + idx.index())
359 })
360 } else if info.is_existential() {
361// As an optimization we sometimes avoid creating a new inference variable here.
362 //
363 // All new inference variables we create start out in the current universe of the caller.
364 // This is conceptually wrong as these inference variables would be able to name
365 // more placeholders then they should be able to. However the inference variables have
366 // to "come from somewhere", so by equating them with the original values of the caller
367 // later on, we pull them down into their correct universe again.
368if let Some(v) = opt_values[ty::BoundVar::from_usize(index)] {
369v370 } else {
371delegate.instantiate_canonical_var_with_infer(info, span, |_| prev_universe)
372 }
373 } else {
374// For placeholders which were already part of the input, we simply map this
375 // universal bound variable back the placeholder of the input.
376original_values[info.expect_placeholder_index()]
377 }
378 }),
379 );
380381CanonicalVarValues { var_values }
382 }
383384/// Unify the `original_values` with the `var_values` returned by the canonical query..
385 ///
386 /// This assumes that this unification will always succeed. This is the case when
387 /// applying a query response right away. However, calling a canonical query, doing any
388 /// other kind of trait solving, and only then instantiating the result of the query
389 /// can cause the instantiation to fail. This is not supported and we ICE in this case.
390 ///
391 /// We always structurally instantiate aliases. Relating aliases needs to be different
392 /// depending on whether the alias is *rigid* or not. We're only really able to tell
393 /// whether an alias is rigid by using the trait solver. When instantiating a response
394 /// from the solver we assume that the solver correctly handled aliases and therefore
395 /// always relate them structurally here.
396#[instrument(level = "trace", skip(delegate))]
397fn unify_query_var_values(
398 delegate: &D,
399 param_env: I::ParamEnv,
400 original_values: &[I::GenericArg],
401 var_values: CanonicalVarValues<I>,
402 span: I::Span,
403 ) {
404assert_eq!(original_values.len(), var_values.len());
405406for (&orig, response) in iter::zip(original_values, var_values.var_values.iter()) {
407let goals =
408 delegate.eq_structurally_relating_aliases(param_env, orig, response, span).unwrap();
409assert!(goals.is_empty());
410 }
411 }
412413fn register_region_constraints(
414&mut self,
415 outlives: &[ty::OutlivesPredicate<I, I::GenericArg>],
416 ) {
417for &ty::OutlivesPredicate(lhs, rhs) in outlives {
418match lhs.kind() {
419 ty::GenericArgKind::Lifetime(lhs) => self.register_region_outlives(lhs, rhs),
420 ty::GenericArgKind::Type(lhs) => self.register_ty_outlives(lhs, rhs),
421 ty::GenericArgKind::Const(_) => panic!("const outlives: {lhs:?}: {rhs:?}"),
422 }
423 }
424 }
425426fn register_new_opaque_types(&mut self, opaque_types: &[(ty::OpaqueTypeKey<I>, I::Ty)]) {
427for &(key, ty) in opaque_types {
428self.delegate.inject_new_hidden_type_unchecked(key, ty, self.origin_span);
429 }
430 }
431}
432433/// Used by proof trees to be able to recompute intermediate actions while
434/// evaluating a goal. The `var_values` not only include the bound variables
435/// of the query input, but also contain all unconstrained inference vars
436/// created while evaluating this goal.
437pub(in crate::solve) fn make_canonical_state<D, T, I>(
438 delegate: &D,
439 var_values: &[I::GenericArg],
440 max_input_universe: ty::UniverseIndex,
441 data: T,
442) -> inspect::CanonicalState<I, T>
443where
444D: SolverDelegate<Interner = I>,
445 I: Interner,
446 T: TypeFoldable<I>,
447{
448let var_values = CanonicalVarValues { var_values: delegate.cx().mk_args(var_values) };
449let state = inspect::State { var_values, data };
450let state = state.fold_with(&mut EagerResolver::new(delegate));
451Canonicalizer::canonicalize_response(delegate, max_input_universe, &mut vec![], state)
452}
453454// FIXME: needs to be pub to be accessed by downstream
455// `rustc_trait_selection::solve::inspect::analyse`.
456pub fn instantiate_canonical_state<D, I, T: TypeFoldable<I>>(
457 delegate: &D,
458 span: I::Span,
459 param_env: I::ParamEnv,
460 orig_values: &mut Vec<I::GenericArg>,
461 state: inspect::CanonicalState<I, T>,
462) -> T
463where
464D: SolverDelegate<Interner = I>,
465 I: Interner,
466{
467// In case any fresh inference variables have been created between `state`
468 // and the previous instantiation, extend `orig_values` for it.
469orig_values.extend(
470state.value.var_values.var_values.as_slice()[orig_values.len()..]
471 .iter()
472 .map(|&arg| delegate.fresh_var_for_kind_with_span(arg, span)),
473 );
474475let instantiation =
476EvalCtxt::compute_query_response_instantiation_values(delegate, orig_values, &state, span);
477478let inspect::State { var_values, data } = delegate.instantiate_canonical(state, instantiation);
479480EvalCtxt::unify_query_var_values(delegate, param_env, orig_values, var_values, span);
481data482}