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