Rollup merge of #130715 - compiler-errors:mir-build-const-eval, r=BoxyUwU

Replace calls to `ty::Const::{try_}eval` in mir build/pattern analysis

We normalize consts in writeback: #130645. This means that consts are gonna be as normalized as they're ever gonna get in MIR building and pattern analysis. Therefore we can just use `try_to_target_usize` rather than calling `eval_target_usize`.

Regarding the `.expect` calls, I'm not totally certain whether they're correct given rigid unevaluated consts. But this PR shouldn't make *more* ICEs occur; we may have to squash these ICEs when mGCE comes around, tho 😺
This commit is contained in:
Matthias Krüger 2024-09-23 06:45:36 +02:00 committed by GitHub
commit 0e08d7002b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 17 additions and 7 deletions

View File

@ -57,7 +57,7 @@ pub(crate) fn as_rvalue(
this.in_scope(region_scope, lint_level, |this| this.as_rvalue(block, scope, value))
}
ExprKind::Repeat { value, count } => {
if Some(0) == count.try_eval_target_usize(this.tcx, this.param_env) {
if Some(0) == count.try_to_target_usize(this.tcx) {
this.build_zero_repeat(block, value, scope, source_info)
} else {
let value_operand = unpack!(

View File

@ -42,7 +42,12 @@ fn prefix_slice_suffix<'pat>(
let tcx = self.tcx;
let (min_length, exact_size) = if let Some(place_resolved) = place.try_to_place(self) {
match place_resolved.ty(&self.local_decls, tcx).ty.kind() {
ty::Array(_, length) => (length.eval_target_usize(tcx, self.param_env), true),
ty::Array(_, length) => (
length
.try_to_target_usize(tcx)
.expect("expected len of array pat to be definite"),
true,
),
_ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
}
} else {

View File

@ -441,7 +441,9 @@ fn slice_or_array_pattern(
ty::Slice(..) => PatKind::Slice { prefix, slice, suffix },
// Fixed-length array, `[T; len]`.
ty::Array(_, len) => {
let len = len.eval_target_usize(self.tcx, self.param_env);
let len = len
.try_to_target_usize(self.tcx)
.expect("expected len of array pat to be definite");
assert!(len >= prefix.len() as u64 + suffix.len() as u64);
PatKind::Array { prefix, slice, suffix }
}

View File

@ -352,7 +352,7 @@ pub fn ctors_for_ty(
ty::Array(sub_ty, len) => {
// We treat arrays of a constant but unknown length like slices.
ConstructorSet::Slice {
array_len: len.try_eval_target_usize(cx.tcx, cx.param_env).map(|l| l as usize),
array_len: len.try_to_target_usize(cx.tcx).map(|l| l as usize),
subtype_is_empty: cx.is_uninhabited(*sub_ty),
}
}
@ -685,9 +685,12 @@ pub fn lower_pat(&self, pat: &'p Pat<'tcx>) -> DeconstructedPat<'p, 'tcx> {
}
PatKind::Array { prefix, slice, suffix } | PatKind::Slice { prefix, slice, suffix } => {
let array_len = match ty.kind() {
ty::Array(_, length) => {
Some(length.eval_target_usize(cx.tcx, cx.param_env) as usize)
}
ty::Array(_, length) => Some(
length
.try_to_target_usize(cx.tcx)
.expect("expected len of array pat to be definite")
as usize,
),
ty::Slice(_) => None,
_ => span_bug!(pat.span, "bad ty {} for slice pattern", ty.inner()),
};