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