Skip to content

Commit 4a886ad

Browse files
committed
Fix ICE in pattern matching with generic const array length errors
1 parent 2f7620a commit 4a886ad

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

compiler/rustc_mir_build/src/builder/matches/match_pair.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,23 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4343
) {
4444
let tcx = self.tcx;
4545
let (min_length, exact_size) = if let Some(place_resolved) = place.try_to_place(self) {
46-
match place_resolved.ty(&self.local_decls, tcx).ty.kind() {
47-
ty::Array(_, length) => (
48-
length
49-
.try_to_target_usize(tcx)
50-
.expect("expected len of array pat to be definite"),
51-
true,
52-
),
46+
let place_ty = place_resolved.ty(&self.local_decls, tcx).ty;
47+
match place_ty.kind() {
48+
ty::Array(_, length) => {
49+
if let Some(length) = length.try_to_target_usize(tcx) {
50+
(length, true)
51+
} else {
52+
// This can happen when the array length is a generic const
53+
// expression that couldn't be evaluated (e.g., due to an error).
54+
// Since there's already a compilation error, we use a fallback
55+
// to avoid an ICE.
56+
tcx.dcx().span_delayed_bug(
57+
tcx.def_span(self.def_id),
58+
"array length in pattern couldn't be evaluated",
59+
);
60+
((prefix.len() + suffix.len()).try_into().unwrap(), false)
61+
}
62+
}
5363
_ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
5464
}
5565
} else {

tests/crashes/139815.rs renamed to tests/ui/const-generics/generic-const-array-pattern-ice-139815.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
//@ known-bug: #139815
2-
1+
//@ compile-flags: --crate-type=lib
2+
#![allow(incomplete_features)]
33
#![feature(generic_const_exprs)]
4+
45
fn is_123<const N: usize>(
56
x: [u32; {
7+
//~^ ERROR overly complex generic constant
68
N + 1;
79
5
810
}],
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: overly complex generic constant
2+
--> $DIR/generic-const-array-pattern-ice-139815.rs:6:14
3+
|
4+
LL | x: [u32; {
5+
| ______________^
6+
LL | |
7+
LL | | N + 1;
8+
LL | | 5
9+
LL | | }],
10+
| |_____^ blocks are not supported in generic constants
11+
|
12+
= help: consider moving this anonymous constant into a `const` function
13+
= note: this operation may be supported in the future
14+
15+
error: aborting due to 1 previous error
16+

0 commit comments

Comments
 (0)