2023-07-24 08:37:40 -07:00
|
|
|
//! Implements the `AliasRelate` goal, which is used when unifying aliases.
|
|
|
|
|
//! Doing this via a separate goal is called "deferred alias relation" and part
|
|
|
|
|
//! of our more general approach to "lazy normalization".
|
2023-07-23 12:30:52 -07:00
|
|
|
//!
|
2024-04-02 15:12:20 +02:00
|
|
|
//! This is done by first structurally normalizing both sides of the goal, ending
|
|
|
|
|
//! up in either a concrete type, rigid alias, or an infer variable.
|
2024-01-10 16:22:26 +00:00
|
|
|
//! These are related further according to the rules below:
|
|
|
|
|
//!
|
2024-01-29 17:09:17 +01:00
|
|
|
//! (1.) If we end up with two rigid aliases, then we relate them structurally.
|
2024-01-10 16:22:26 +00:00
|
|
|
//!
|
2024-02-26 10:17:43 +01:00
|
|
|
//! (2.) If we end up with an infer var and a rigid alias, then we instantiate
|
|
|
|
|
//! the infer var with the constructor of the alias and then recursively relate
|
|
|
|
|
//! the terms.
|
2024-01-10 16:22:26 +00:00
|
|
|
//!
|
2024-01-29 17:09:17 +01:00
|
|
|
//! (3.) Otherwise, if we end with two rigid (non-projection) or infer types,
|
2024-01-10 16:22:26 +00:00
|
|
|
//! relate them structurally.
|
|
|
|
|
|
2024-03-18 12:08:06 +01:00
|
|
|
use super::EvalCtxt;
|
2023-05-27 19:05:09 +00:00
|
|
|
use rustc_middle::traits::solve::{Certainty, Goal, QueryResult};
|
2024-04-02 15:12:20 +02:00
|
|
|
use rustc_middle::ty;
|
2023-05-27 19:05:09 +00:00
|
|
|
|
|
|
|
|
impl<'tcx> EvalCtxt<'_, 'tcx> {
|
|
|
|
|
#[instrument(level = "debug", skip(self), ret)]
|
|
|
|
|
pub(super) fn compute_alias_relate_goal(
|
|
|
|
|
&mut self,
|
|
|
|
|
goal: Goal<'tcx, (ty::Term<'tcx>, ty::Term<'tcx>, ty::AliasRelationDirection)>,
|
|
|
|
|
) -> QueryResult<'tcx> {
|
|
|
|
|
let tcx = self.tcx();
|
|
|
|
|
let Goal { param_env, predicate: (lhs, rhs, direction) } = goal;
|
|
|
|
|
|
2024-04-02 15:12:20 +02:00
|
|
|
// Structurally normalize the lhs.
|
|
|
|
|
let lhs = if let Some(alias) = lhs.to_alias_ty(self.tcx()) {
|
|
|
|
|
let term = self.next_term_infer_of_kind(lhs);
|
|
|
|
|
self.add_normalizes_to_goal(goal.with(tcx, ty::NormalizesTo { alias, term }));
|
|
|
|
|
term
|
|
|
|
|
} else {
|
|
|
|
|
lhs
|
2023-11-09 11:06:48 +01:00
|
|
|
};
|
2023-05-27 19:05:09 +00:00
|
|
|
|
2024-04-02 15:12:20 +02:00
|
|
|
// Structurally normalize the rhs.
|
|
|
|
|
let rhs = if let Some(alias) = rhs.to_alias_ty(self.tcx()) {
|
|
|
|
|
let term = self.next_term_infer_of_kind(rhs);
|
|
|
|
|
self.add_normalizes_to_goal(goal.with(tcx, ty::NormalizesTo { alias, term }));
|
|
|
|
|
term
|
|
|
|
|
} else {
|
|
|
|
|
rhs
|
2023-11-09 11:06:48 +01:00
|
|
|
};
|
2023-05-27 19:05:09 +00:00
|
|
|
|
2024-04-02 15:12:20 +02:00
|
|
|
// Apply the constraints.
|
|
|
|
|
self.try_evaluate_added_goals()?;
|
|
|
|
|
let lhs = self.resolve_vars_if_possible(lhs);
|
|
|
|
|
let rhs = self.resolve_vars_if_possible(rhs);
|
|
|
|
|
debug!(?lhs, ?rhs);
|
|
|
|
|
|
2023-11-09 11:06:48 +01:00
|
|
|
let variance = match direction {
|
|
|
|
|
ty::AliasRelationDirection::Equate => ty::Variance::Invariant,
|
|
|
|
|
ty::AliasRelationDirection::Subtype => ty::Variance::Covariant,
|
|
|
|
|
};
|
|
|
|
|
match (lhs.to_alias_ty(tcx), rhs.to_alias_ty(tcx)) {
|
|
|
|
|
(None, None) => {
|
|
|
|
|
self.relate(param_env, lhs, variance, rhs)?;
|
|
|
|
|
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
|
|
|
|
|
}
|
2023-05-27 19:05:09 +00:00
|
|
|
|
2024-02-26 10:17:43 +01:00
|
|
|
(Some(alias), None) => {
|
2024-04-02 15:12:20 +02:00
|
|
|
self.relate_rigid_alias_non_alias(param_env, alias, variance, rhs)?;
|
|
|
|
|
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
|
|
|
|
|
}
|
|
|
|
|
(None, Some(alias)) => {
|
|
|
|
|
self.relate_rigid_alias_non_alias(
|
|
|
|
|
param_env,
|
|
|
|
|
alias,
|
|
|
|
|
variance.xform(ty::Variance::Contravariant),
|
|
|
|
|
lhs,
|
|
|
|
|
)?;
|
|
|
|
|
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
|
2023-11-09 11:06:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(Some(alias_lhs), Some(alias_rhs)) => {
|
2024-01-29 17:09:17 +01:00
|
|
|
self.relate(param_env, alias_lhs, variance, alias_rhs)?;
|
|
|
|
|
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
|
2023-11-09 11:06:48 +01:00
|
|
|
}
|
2023-05-27 19:05:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|