1//! Implements the `AliasRelate` goal, which is used when unifying aliases.
2//! Doing this via a separate goal is called "deferred alias relation" and part
3//! of our more general approach to "lazy normalization".
4//!
5//! This is done by first structurally normalizing both sides of the goal, ending
6//! up in either a concrete type, rigid alias, or an infer variable.
7//! These are related further according to the rules below:
8//!
9//! (1.) If we end up with two rigid aliases, then we relate them structurally.
10//!
11//! (2.) If we end up with an infer var and a rigid alias, then we instantiate
12//! the infer var with the constructor of the alias and then recursively relate
13//! the terms.
14//!
15//! (3.) Otherwise, if we end with two rigid (non-projection) or infer types,
16//! relate them structurally.
1718use rustc_type_ir::inherent::*;
19use rustc_type_ir::{selfas ty, Interner};
20use tracing::{instrument, trace};
2122use crate::delegate::SolverDelegate;
23use crate::solve::{Certainty, EvalCtxt, Goal, QueryResult};
2425impl<D, I> EvalCtxt<'_, D>
26where
27D: SolverDelegate<Interner = I>,
28 I: Interner,
29{
30#[instrument(level = "trace", skip(self), ret)]
31pub(super) fn compute_alias_relate_goal(
32&mut self,
33 goal: Goal<I, (I::Term, I::Term, ty::AliasRelationDirection)>,
34 ) -> QueryResult<I> {
35let cx = self.cx();
36let Goal { param_env, predicate: (lhs, rhs, direction) } = goal;
37debug_assert!(lhs.to_alias_term().is_some() || rhs.to_alias_term().is_some());
3839// Structurally normalize the lhs.
40let lhs = if let Some(alias) = lhs.to_alias_term() {
41let term = self.next_term_infer_of_kind(lhs);
42self.add_normalizes_to_goal(goal.with(cx, ty::NormalizesTo { alias, term }));
43 term
44 } else {
45 lhs
46 };
4748// Structurally normalize the rhs.
49let rhs = if let Some(alias) = rhs.to_alias_term() {
50let term = self.next_term_infer_of_kind(rhs);
51self.add_normalizes_to_goal(goal.with(cx, ty::NormalizesTo { alias, term }));
52 term
53 } else {
54 rhs
55 };
5657// Add a `make_canonical_response` probe step so that we treat this as
58 // a candidate, even if `try_evaluate_added_goals` bails due to an error.
59 // It's `Certainty::AMBIGUOUS` because this candidate is not "finished",
60 // since equating the normalized terms will lead to additional constraints.
61self.inspect.make_canonical_response(Certainty::AMBIGUOUS);
6263// Apply the constraints.
64self.try_evaluate_added_goals()?;
65let lhs = self.resolve_vars_if_possible(lhs);
66let rhs = self.resolve_vars_if_possible(rhs);
67trace!(?lhs, ?rhs);
6869let variance = match direction {
70 ty::AliasRelationDirection::Equate => ty::Invariant,
71 ty::AliasRelationDirection::Subtype => ty::Covariant,
72 };
73match (lhs.to_alias_term(), rhs.to_alias_term()) {
74 (None, None) => {
75self.relate(param_env, lhs, variance, rhs)?;
76self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
77 }
7879 (Some(alias), None) => {
80self.relate_rigid_alias_non_alias(param_env, alias, variance, rhs)?;
81self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
82 }
83 (None, Some(alias)) => {
84self.relate_rigid_alias_non_alias(
85 param_env,
86 alias,
87 variance.xform(ty::Contravariant),
88 lhs,
89 )?;
90self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
91 }
9293 (Some(alias_lhs), Some(alias_rhs)) => {
94self.relate(param_env, alias_lhs, variance, alias_rhs)?;
95self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
96 }
97 }
98 }
99}