Rollup merge of #147724 - chenyukang:yukang-fix-139815-ice, r=jdonszelmann

Fix ICE in pattern matching with generic const array length errors

Fixes rust-lang/rust#139815
This commit is contained in:
Matthias Krüger
2025-10-18 23:54:45 +02:00
committed by GitHub
3 changed files with 37 additions and 9 deletions
@@ -43,13 +43,23 @@ fn prefix_slice_suffix(
) {
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
.try_to_target_usize(tcx)
.expect("expected len of array pat to be definite"),
true,
),
let place_ty = place_resolved.ty(&self.local_decls, tcx).ty;
match place_ty.kind() {
ty::Array(_, length) => {
if let Some(length) = length.try_to_target_usize(tcx) {
(length, true)
} else {
// This can happen when the array length is a generic const
// expression that couldn't be evaluated (e.g., due to an error).
// Since there's already a compilation error, we use a fallback
// to avoid an ICE.
tcx.dcx().span_delayed_bug(
tcx.def_span(self.def_id),
"array length in pattern couldn't be evaluated",
);
((prefix.len() + suffix.len()).try_into().unwrap(), false)
}
}
_ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
}
} else {
@@ -1,8 +1,10 @@
//@ known-bug: #139815
//@ compile-flags: --crate-type=lib
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
fn is_123<const N: usize>(
x: [u32; {
//~^ ERROR overly complex generic constant
N + 1;
5
}],
@@ -0,0 +1,16 @@
error: overly complex generic constant
--> $DIR/generic-const-array-pattern-ice-139815.rs:6:14
|
LL | x: [u32; {
| ______________^
LL | |
LL | | N + 1;
LL | | 5
LL | | }],
| |_____^ blocks are not supported in generic constants
|
= help: consider moving this anonymous constant into a `const` function
= note: this operation may be supported in the future
error: aborting due to 1 previous error