rustc_next_trait_solver/solve/
alias_relate.rs

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.
17
18use rustc_type_ir::inherent::*;
19use rustc_type_ir::{self as ty, Interner};
20use tracing::{instrument, trace};
21
22use crate::delegate::SolverDelegate;
23use crate::solve::{Certainty, EvalCtxt, Goal, QueryResult};
24
25impl<D, I> EvalCtxt<'_, D>
26where
27    D: SolverDelegate<Interner = I>,
28    I: Interner,
29{
30    #[instrument(level = "trace", skip(self), ret)]
31    pub(super) fn compute_alias_relate_goal(
32        &mut self,
33        goal: Goal<I, (I::Term, I::Term, ty::AliasRelationDirection)>,
34    ) -> QueryResult<I> {
35        let cx = self.cx();
36        let Goal { param_env, predicate: (lhs, rhs, direction) } = goal;
37        debug_assert!(lhs.to_alias_term().is_some() || rhs.to_alias_term().is_some());
38
39        // Structurally normalize the lhs.
40        let lhs = if let Some(alias) = lhs.to_alias_term() {
41            let term = self.next_term_infer_of_kind(lhs);
42            self.add_normalizes_to_goal(goal.with(cx, ty::NormalizesTo { alias, term }));
43            term
44        } else {
45            lhs
46        };
47
48        // Structurally normalize the rhs.
49        let rhs = if let Some(alias) = rhs.to_alias_term() {
50            let term = self.next_term_infer_of_kind(rhs);
51            self.add_normalizes_to_goal(goal.with(cx, ty::NormalizesTo { alias, term }));
52            term
53        } else {
54            rhs
55        };
56
57        // 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.
61        self.inspect.make_canonical_response(Certainty::AMBIGUOUS);
62
63        // Apply the constraints.
64        self.try_evaluate_added_goals()?;
65        let lhs = self.resolve_vars_if_possible(lhs);
66        let rhs = self.resolve_vars_if_possible(rhs);
67        trace!(?lhs, ?rhs);
68
69        let variance = match direction {
70            ty::AliasRelationDirection::Equate => ty::Invariant,
71            ty::AliasRelationDirection::Subtype => ty::Covariant,
72        };
73        match (lhs.to_alias_term(), rhs.to_alias_term()) {
74            (None, None) => {
75                self.relate(param_env, lhs, variance, rhs)?;
76                self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
77            }
78
79            (Some(alias), None) => {
80                self.relate_rigid_alias_non_alias(param_env, alias, variance, rhs)?;
81                self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
82            }
83            (None, Some(alias)) => {
84                self.relate_rigid_alias_non_alias(
85                    param_env,
86                    alias,
87                    variance.xform(ty::Contravariant),
88                    lhs,
89                )?;
90                self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
91            }
92
93            (Some(alias_lhs), Some(alias_rhs)) => {
94                self.relate(param_env, alias_lhs, variance, alias_rhs)?;
95                self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
96            }
97        }
98    }
99}