From 4a886add5c472aaa8d68c572b32566b57ee51315 Mon Sep 17 00:00:00 2001 From: yukang Date: Wed, 15 Oct 2025 10:00:15 +0800 Subject: [PATCH] Fix ICE in pattern matching with generic const array length errors --- .../src/builder/matches/match_pair.rs | 24 +++++++++++++------ ...generic-const-array-pattern-ice-139815.rs} | 6 +++-- ...eric-const-array-pattern-ice-139815.stderr | 16 +++++++++++++ 3 files changed, 37 insertions(+), 9 deletions(-) rename tests/{crashes/139815.rs => ui/const-generics/generic-const-array-pattern-ice-139815.rs} (61%) create mode 100644 tests/ui/const-generics/generic-const-array-pattern-ice-139815.stderr diff --git a/compiler/rustc_mir_build/src/builder/matches/match_pair.rs b/compiler/rustc_mir_build/src/builder/matches/match_pair.rs index 82c769eb3d584..c3028b9b5c4ea 100644 --- a/compiler/rustc_mir_build/src/builder/matches/match_pair.rs +++ b/compiler/rustc_mir_build/src/builder/matches/match_pair.rs @@ -43,13 +43,23 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { ) { 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 { diff --git a/tests/crashes/139815.rs b/tests/ui/const-generics/generic-const-array-pattern-ice-139815.rs similarity index 61% rename from tests/crashes/139815.rs rename to tests/ui/const-generics/generic-const-array-pattern-ice-139815.rs index 9094acdc94b21..52f4b246d069f 100644 --- a/tests/crashes/139815.rs +++ b/tests/ui/const-generics/generic-const-array-pattern-ice-139815.rs @@ -1,8 +1,10 @@ -//@ known-bug: #139815 - +//@ compile-flags: --crate-type=lib +#![allow(incomplete_features)] #![feature(generic_const_exprs)] + fn is_123( x: [u32; { + //~^ ERROR overly complex generic constant N + 1; 5 }], diff --git a/tests/ui/const-generics/generic-const-array-pattern-ice-139815.stderr b/tests/ui/const-generics/generic-const-array-pattern-ice-139815.stderr new file mode 100644 index 0000000000000..9da973ef3a04e --- /dev/null +++ b/tests/ui/const-generics/generic-const-array-pattern-ice-139815.stderr @@ -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 +