Skip to content

Commit eea8bac

Browse files
committed
Use NormalizesTo instead of AliasRelate for normalization
1 parent 39602ee commit eea8bac

26 files changed

Lines changed: 97 additions & 113 deletions

compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,13 +1219,12 @@ where
12191219

12201220
fn fold_ty(&mut self, ty: Ty<I>) -> Ty<I> {
12211221
match ty.kind() {
1222-
ty::Alias(..) if !ty.has_escaping_bound_vars() => {
1222+
ty::Alias(_, alias) if !ty.has_escaping_bound_vars() => {
12231223
let infer_ty = self.ecx.next_ty_infer();
1224-
let normalizes_to = ty::PredicateKind::AliasRelate(
1225-
ty.into(),
1226-
infer_ty.into(),
1227-
ty::AliasRelationDirection::Equate,
1228-
);
1224+
let normalizes_to = ty::PredicateKind::NormalizesTo(ty::NormalizesTo {
1225+
alias: ty::AliasTerm::new_from_args(self.cx(), alias.def_id, alias.args),
1226+
term: infer_ty.into(),
1227+
});
12291228
self.ecx.add_goal(
12301229
self.normalization_goal_source,
12311230
Goal::new(self.cx(), self.param_env, normalizes_to),
@@ -1248,13 +1247,12 @@ where
12481247

12491248
fn fold_const(&mut self, ct: I::Const) -> I::Const {
12501249
match ct.kind() {
1251-
ty::ConstKind::Unevaluated(..) if !ct.has_escaping_bound_vars() => {
1250+
ty::ConstKind::Unevaluated(uc) if !ct.has_escaping_bound_vars() => {
12521251
let infer_ct = self.ecx.next_const_infer();
1253-
let normalizes_to = ty::PredicateKind::AliasRelate(
1254-
ct.into(),
1255-
infer_ct.into(),
1256-
ty::AliasRelationDirection::Equate,
1257-
);
1252+
let normalizes_to = ty::PredicateKind::NormalizesTo(ty::NormalizesTo {
1253+
alias: ty::AliasTerm::new_from_args(self.cx(), uc.def.into(), uc.args),
1254+
term: infer_ct.into(),
1255+
});
12581256
self.ecx.add_goal(
12591257
self.normalization_goal_source,
12601258
Goal::new(self.cx(), self.param_env, normalizes_to),

compiler/rustc_next_trait_solver/src/solve/mod.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -344,20 +344,16 @@ where
344344
param_env: I::ParamEnv,
345345
term: I::Term,
346346
) -> Result<I::Term, NoSolution> {
347-
if let Some(_) = term.to_alias_term() {
347+
if let Some(alias) = term.to_alias_term() {
348348
let normalized_term = self.next_term_infer_of_kind(term);
349-
let alias_relate_goal = Goal::new(
349+
let normalizes_to_goal = Goal::new(
350350
self.cx(),
351351
param_env,
352-
ty::PredicateKind::AliasRelate(
353-
term,
354-
normalized_term,
355-
ty::AliasRelationDirection::Equate,
356-
),
352+
ty::PredicateKind::NormalizesTo(ty::NormalizesTo { alias, term: normalized_term }),
357353
);
358354
// We normalize the self type to be able to relate it with
359355
// types from candidates.
360-
self.add_goal(GoalSource::TypeRelating, alias_relate_goal);
356+
self.add_goal(GoalSource::TypeRelating, normalizes_to_goal);
361357
self.try_evaluate_added_goals()?;
362358
Ok(self.resolve_vars_if_possible(normalized_term))
363359
} else {

compiler/rustc_next_trait_solver/src/solve/normalizes_to/anon_const.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ where
3232
goal.predicate.alias.args,
3333
),
3434
) {
35-
self.instantiate_normalizes_to_term(goal, normalized_const.into());
35+
self.eq(goal.param_env, goal.predicate.term, normalized_const.into())?;
3636
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
3737
} else {
3838
self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)

compiler/rustc_next_trait_solver/src/solve/normalizes_to/free_alias.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ where
3535
cx.const_of_item(free_alias.def_id).instantiate(cx, free_alias.args).into()
3636
};
3737

38-
self.instantiate_normalizes_to_term(goal, actual);
38+
self.eq(goal.param_env, goal.predicate.term, actual)?;
3939
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
4040
}
4141
}

compiler/rustc_next_trait_solver/src/solve/normalizes_to/inherent.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ where
5656
} else {
5757
cx.const_of_item(inherent.def_id).instantiate(cx, inherent_args).into()
5858
};
59-
self.instantiate_normalizes_to_term(goal, normalized);
59+
self.eq(goal.param_env, goal.predicate.term, normalized)?;
6060
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
6161
}
6262
}

compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ where
3939
ecx.compute_trait_goal(trait_goal)
4040
})?;
4141

42-
let term = self.next_term_infer_of_kind(goal.predicate.term);
43-
self.eq(goal.param_env, goal.predicate.term, term)?;
44-
4542
let (probed_term, certainty) =
4643
self.probe_with_unconstrained_projection_term(goal, |ecx, goal| {
4744
ecx.assemble_and_merge_candidates(
@@ -93,7 +90,11 @@ where
9390
)
9491
})?;
9592

96-
self.eq_structurally_relating_aliases(goal.param_env, term, probed_term)?;
93+
self.eq_structurally_relating_aliases(
94+
goal.param_env,
95+
goal.predicate.term,
96+
probed_term,
97+
)?;
9798

9899
self.evaluate_added_goals_and_make_canonical_response(certainty)
99100
}
@@ -113,18 +114,14 @@ where
113114
/// We know `term` to always be a fully unconstrained inference variable, so
114115
/// `eq` should never fail here. However, in case `term` contains aliases, we
115116
/// emit nested `AliasRelate` goals to structurally normalize the alias.
116-
pub fn instantiate_normalizes_to_term(
117-
&mut self,
118-
goal: Goal<I, NormalizesTo<I>>,
119-
term: I::Term,
120-
) {
117+
fn instantiate_normalizes_to_term(&mut self, goal: Goal<I, NormalizesTo<I>>, term: I::Term) {
121118
self.eq(goal.param_env, goal.predicate.term, term)
122119
.expect("expected goal term to be fully unconstrained");
123120
}
124121

125122
/// Unlike `instantiate_normalizes_to_term` this instantiates the expected term
126123
/// with a rigid alias. Using this is pretty much always wrong.
127-
pub fn structurally_instantiate_normalizes_to_term(
124+
fn structurally_instantiate_normalizes_to_term(
128125
&mut self,
129126
goal: Goal<I, NormalizesTo<I>>,
130127
term: ty::AliasTerm<I>,

compiler/rustc_next_trait_solver/src/solve/normalizes_to/opaque_types.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ where
4848
.filter(|&def_id| defining_opaque_types.contains(&def_id))
4949
else {
5050
// If we're not in the defining scope, treat the alias as rigid.
51-
self.structurally_instantiate_normalizes_to_term(goal, goal.predicate.alias);
51+
self.relate_rigid_alias_non_alias(
52+
goal.param_env,
53+
goal.predicate.alias,
54+
ty::Invariant,
55+
goal.predicate.term,
56+
)?;
5257
return self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes);
5358
};
5459

@@ -111,7 +116,12 @@ where
111116
.as_local()
112117
.filter(|&def_id| defined_opaque_types.contains(&def_id))
113118
else {
114-
self.structurally_instantiate_normalizes_to_term(goal, goal.predicate.alias);
119+
self.relate_rigid_alias_non_alias(
120+
goal.param_env,
121+
goal.predicate.alias,
122+
ty::Invariant,
123+
goal.predicate.term,
124+
)?;
115125
return self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes);
116126
};
117127

compiler/rustc_trait_selection/src/solve/fulfill/derive_errors.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,13 @@ impl<'tcx> BestObligation<'tcx> {
292292
) -> ControlFlow<PredicateObligation<'tcx>> {
293293
assert!(!self.consider_ambiguities);
294294
let tcx = goal.infcx().tcx;
295-
if let ty::Alias(..) = self_ty.kind() {
295+
let term: ty::Term<'tcx> = self_ty.into();
296+
if let Some(alias) = term.to_alias_term() {
296297
let infer_term = goal.infcx().next_ty_var(self.obligation.cause.span);
297-
let pred = ty::PredicateKind::AliasRelate(
298-
self_ty.into(),
299-
infer_term.into(),
300-
ty::AliasRelationDirection::Equate,
301-
);
298+
let pred = ty::PredicateKind::NormalizesTo(ty::NormalizesTo {
299+
alias,
300+
term: infer_term.into(),
301+
});
302302
let obligation =
303303
Obligation::new(tcx, self.obligation.cause.clone(), goal.goal().param_env, pred);
304304
self.with_derived_obligation(obligation, |this| {

compiler/rustc_trait_selection/src/solve/normalize.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,10 @@ where
102102
let infcx = self.at.infcx;
103103
let tcx = infcx.tcx;
104104
let recursion_limit = tcx.recursion_limit();
105+
let alias = alias_term.to_alias_term().unwrap();
105106
if !recursion_limit.value_within_limit(self.depth) {
106-
let term = alias_term.to_alias_term().unwrap();
107-
108107
self.at.infcx.err_ctxt().report_overflow_error(
109-
OverflowCause::DeeplyNormalize(term),
108+
OverflowCause::DeeplyNormalize(alias),
110109
self.at.cause.span,
111110
true,
112111
|_| {},
@@ -120,11 +119,7 @@ where
120119
tcx,
121120
self.at.cause.clone(),
122121
self.at.param_env,
123-
ty::PredicateKind::AliasRelate(
124-
alias_term.into(),
125-
infer_term.into(),
126-
ty::AliasRelationDirection::Equate,
127-
),
122+
ty::PredicateKind::NormalizesTo(ty::NormalizesTo { alias, term: infer_term }),
128123
);
129124

130125
self.fulfill_cx.register_predicate_obligation(infcx, obligation);

compiler/rustc_trait_selection/src/traits/structural_normalize.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ impl<'tcx> At<'_, 'tcx> {
3535
assert!(!term.is_infer(), "should have resolved vars before calling");
3636

3737
if self.infcx.next_trait_solver() {
38-
if let None = term.to_alias_term() {
38+
let Some(alias) = term.to_alias_term() else {
3939
return Ok(term);
40-
}
40+
};
4141

4242
let new_infer = self.infcx.next_term_var_of_kind(term, self.cause.span);
4343

@@ -48,7 +48,7 @@ impl<'tcx> At<'_, 'tcx> {
4848
self.infcx.tcx,
4949
self.cause.clone(),
5050
self.param_env,
51-
ty::PredicateKind::AliasRelate(term, new_infer, ty::AliasRelationDirection::Equate),
51+
ty::PredicateKind::NormalizesTo(ty::NormalizesTo { alias, term: new_infer }),
5252
);
5353

5454
fulfill_cx.register_predicate_obligation(self.infcx, obligation);

0 commit comments

Comments
 (0)