1
Fork 0

Rollup merge of #130714 - compiler-errors:try-structurally-resolve-const, r=BoxyUwU

Introduce `structurally_normalize_const`, use it in `rustc_hir_typeck`

Introduces `structurally_normalize_const` to typecking to separate the "eval a const" step from the "try to turn a valtree into a target usize" in HIR typeck, where we may still have infer vars and stuff around.

I also changed `check_expr_repeat` to move a double evaluation of a const into a single one. I'll leave inline comments.

r? ```@BoxyUwU```

I hesitated to really test this on the new solver where it probably matters for unevaluated consts. If you're worried about the side-effects, I'd be happy to craft some more tests 😄
This commit is contained in:
Matthias Krüger 2024-09-23 06:45:36 +02:00 committed by GitHub
commit 2bca5c4fc1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 122 additions and 39 deletions

View file

@ -46,4 +46,44 @@ impl<'tcx> At<'_, 'tcx> {
Ok(self.normalize(ty).into_value_registering_obligations(self.infcx, fulfill_cx))
}
}
fn structurally_normalize_const<E: 'tcx>(
&self,
ct: ty::Const<'tcx>,
fulfill_cx: &mut dyn TraitEngine<'tcx, E>,
) -> Result<ty::Const<'tcx>, Vec<E>> {
assert!(!ct.is_ct_infer(), "should have resolved vars before calling");
if self.infcx.next_trait_solver() {
let ty::ConstKind::Unevaluated(..) = ct.kind() else {
return Ok(ct);
};
let new_infer_ct = self.infcx.next_const_var(self.cause.span);
// We simply emit an `alias-eq` goal here, since that will take care of
// normalizing the LHS of the projection until it is a rigid projection
// (or a not-yet-defined opaque in scope).
let obligation = Obligation::new(
self.infcx.tcx,
self.cause.clone(),
self.param_env,
ty::PredicateKind::AliasRelate(
ct.into(),
new_infer_ct.into(),
ty::AliasRelationDirection::Equate,
),
);
fulfill_cx.register_predicate_obligation(self.infcx, obligation);
let errors = fulfill_cx.select_where_possible(self.infcx);
if !errors.is_empty() {
return Err(errors);
}
Ok(self.infcx.resolve_vars_if_possible(new_infer_ct))
} else {
Ok(self.normalize(ct).into_value_registering_obligations(self.infcx, fulfill_cx))
}
}
}