Skip to content

Commit ecd4929

Browse files
Unify normalization of terms in deeply normalize
1 parent 7efd90a commit ecd4929

File tree

4 files changed

+36
-70
lines changed

4 files changed

+36
-70
lines changed

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,13 @@ impl<'tcx> InferCtxt<'tcx> {
823823
ty::Region::new_var(self.tcx, region_var)
824824
}
825825

826+
pub fn next_term_var_of_kind(&self, term: ty::Term<'tcx>, span: Span) -> ty::Term<'tcx> {
827+
match term.kind() {
828+
ty::TermKind::Ty(_) => self.next_ty_var(span).into(),
829+
ty::TermKind::Const(_) => self.next_const_var(span).into(),
830+
}
831+
}
832+
826833
/// Return the universe that the region `r` was created in. For
827834
/// most regions (e.g., `'static`, named regions from the user,
828835
/// etc) this is the root universe U0. For inference variables or

compiler/rustc_trait_selection/src/solve/inspect/analyse.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,7 @@ impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
206206
let infcx = self.goal.infcx;
207207
match goal.predicate.kind().no_bound_vars() {
208208
Some(ty::PredicateKind::NormalizesTo(ty::NormalizesTo { alias, term })) => {
209-
let unconstrained_term = match term.kind() {
210-
ty::TermKind::Ty(_) => infcx.next_ty_var(span).into(),
211-
ty::TermKind::Const(_) => infcx.next_const_var(span).into(),
212-
};
209+
let unconstrained_term = infcx.next_term_var_of_kind(term, span);
213210
let goal =
214211
goal.with(infcx.tcx, ty::NormalizesTo { alias, term: unconstrained_term });
215212
// We have to use a `probe` here as evaluating a `NormalizesTo` can constrain the

compiler/rustc_trait_selection/src/solve/normalize.rs

Lines changed: 27 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::assert_matches::assert_matches;
21
use std::fmt::Debug;
32

43
use rustc_data_structures::stack::ensure_sufficient_stack;
@@ -96,66 +95,27 @@ impl<'tcx, E> NormalizationFolder<'_, 'tcx, E>
9695
where
9796
E: FromSolverError<'tcx, NextSolverError<'tcx>>,
9897
{
99-
fn normalize_alias_ty(&mut self, alias_ty: Ty<'tcx>) -> Result<Ty<'tcx>, Vec<E>> {
100-
assert_matches!(alias_ty.kind(), ty::Alias(..));
101-
102-
let infcx = self.at.infcx;
103-
let tcx = infcx.tcx;
104-
let recursion_limit = tcx.recursion_limit();
105-
if !recursion_limit.value_within_limit(self.depth) {
106-
let ty::Alias(_, data) = *alias_ty.kind() else {
107-
unreachable!();
108-
};
109-
110-
self.at.infcx.err_ctxt().report_overflow_error(
111-
OverflowCause::DeeplyNormalize(data.into()),
112-
self.at.cause.span,
113-
true,
114-
|_| {},
115-
);
116-
}
117-
118-
self.depth += 1;
119-
120-
let new_infer_ty = infcx.next_ty_var(self.at.cause.span);
121-
let obligation = Obligation::new(
122-
tcx,
123-
self.at.cause.clone(),
124-
self.at.param_env,
125-
ty::PredicateKind::AliasRelate(
126-
alias_ty.into(),
127-
new_infer_ty.into(),
128-
ty::AliasRelationDirection::Equate,
129-
),
130-
);
131-
132-
self.fulfill_cx.register_predicate_obligation(infcx, obligation);
133-
self.select_all_and_stall_coroutine_predicates()?;
134-
135-
// Alias is guaranteed to be fully structurally resolved,
136-
// so we can super fold here.
137-
let ty = infcx.resolve_vars_if_possible(new_infer_ty);
138-
let result = ty.try_super_fold_with(self)?;
139-
self.depth -= 1;
140-
Ok(result)
141-
}
142-
143-
fn normalize_unevaluated_const(
98+
fn normalize_alias_term(
14499
&mut self,
145-
alias_ct: ty::Const<'tcx>,
146-
) -> Result<ty::Const<'tcx>, Vec<E>> {
147-
assert_matches!(alias_ct.kind(), ty::ConstKind::Unevaluated(..));
148-
100+
alias_term: ty::Term<'tcx>,
101+
) -> Result<ty::Term<'tcx>, Vec<E>> {
149102
let infcx = self.at.infcx;
150103
let tcx = infcx.tcx;
151104
let recursion_limit = tcx.recursion_limit();
152105
if !recursion_limit.value_within_limit(self.depth) {
153-
let ty::ConstKind::Unevaluated(uv) = alias_ct.kind() else {
154-
unreachable!();
106+
let term = match alias_term.kind() {
107+
ty::TermKind::Ty(ty) => match *ty.kind() {
108+
ty::Alias(_, alias) => alias.into(),
109+
_ => unreachable!(),
110+
},
111+
ty::TermKind::Const(ct) => match ct.kind() {
112+
ty::ConstKind::Unevaluated(alias) => alias.into(),
113+
_ => unreachable!(),
114+
},
155115
};
156116

157117
self.at.infcx.err_ctxt().report_overflow_error(
158-
OverflowCause::DeeplyNormalize(uv.into()),
118+
OverflowCause::DeeplyNormalize(term),
159119
self.at.cause.span,
160120
true,
161121
|_| {},
@@ -164,14 +124,14 @@ where
164124

165125
self.depth += 1;
166126

167-
let new_infer_ct = infcx.next_const_var(self.at.cause.span);
127+
let infer_term = infcx.next_term_var_of_kind(alias_term, self.at.cause.span);
168128
let obligation = Obligation::new(
169129
tcx,
170130
self.at.cause.clone(),
171131
self.at.param_env,
172132
ty::PredicateKind::AliasRelate(
173-
alias_ct.into(),
174-
new_infer_ct.into(),
133+
alias_term.into(),
134+
infer_term.into(),
175135
ty::AliasRelationDirection::Equate,
176136
),
177137
);
@@ -181,8 +141,11 @@ where
181141

182142
// Alias is guaranteed to be fully structurally resolved,
183143
// so we can super fold here.
184-
let ct = infcx.resolve_vars_if_possible(new_infer_ct);
185-
let result = ct.try_super_fold_with(self)?;
144+
let term = infcx.resolve_vars_if_possible(infer_term);
145+
let result = match term.kind() {
146+
ty::TermKind::Ty(ty) => ty.try_super_fold_with(self)?.into(),
147+
ty::TermKind::Const(ct) => ct.try_super_fold_with(self)?.into(),
148+
};
186149
self.depth -= 1;
187150
Ok(result)
188151
}
@@ -242,7 +205,8 @@ where
242205
if ty.has_escaping_bound_vars() {
243206
let (ty, mapped_regions, mapped_types, mapped_consts) =
244207
BoundVarReplacer::replace_bound_vars(infcx, &mut self.universes, ty);
245-
let result = ensure_sufficient_stack(|| self.normalize_alias_ty(ty))?;
208+
let result =
209+
ensure_sufficient_stack(|| self.normalize_alias_term(ty.into()))?.expect_type();
246210
Ok(PlaceholderReplacer::replace_placeholders(
247211
infcx,
248212
mapped_regions,
@@ -252,7 +216,7 @@ where
252216
result,
253217
))
254218
} else {
255-
ensure_sufficient_stack(|| self.normalize_alias_ty(ty))
219+
Ok(ensure_sufficient_stack(|| self.normalize_alias_term(ty.into()))?.expect_type())
256220
}
257221
}
258222

@@ -269,7 +233,8 @@ where
269233
if ct.has_escaping_bound_vars() {
270234
let (ct, mapped_regions, mapped_types, mapped_consts) =
271235
BoundVarReplacer::replace_bound_vars(infcx, &mut self.universes, ct);
272-
let result = ensure_sufficient_stack(|| self.normalize_unevaluated_const(ct))?;
236+
let result =
237+
ensure_sufficient_stack(|| self.normalize_alias_term(ct.into()))?.expect_const();
273238
Ok(PlaceholderReplacer::replace_placeholders(
274239
infcx,
275240
mapped_regions,
@@ -279,7 +244,7 @@ where
279244
result,
280245
))
281246
} else {
282-
ensure_sufficient_stack(|| self.normalize_unevaluated_const(ct))
247+
Ok(ensure_sufficient_stack(|| self.normalize_alias_term(ct.into()))?.expect_const())
283248
}
284249
}
285250
}

compiler/rustc_trait_selection/src/traits/structural_normalize.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ impl<'tcx> At<'_, 'tcx> {
3939
return Ok(term);
4040
}
4141

42-
let new_infer = match term.kind() {
43-
ty::TermKind::Ty(_) => self.infcx.next_ty_var(self.cause.span).into(),
44-
ty::TermKind::Const(_) => self.infcx.next_const_var(self.cause.span).into(),
45-
};
42+
let new_infer = self.infcx.next_term_var_of_kind(term, self.cause.span);
4643

4744
// We simply emit an `alias-eq` goal here, since that will take care of
4845
// normalizing the LHS of the projection until it is a rigid projection

0 commit comments

Comments
 (0)