Skip to content

Commit 31b6a75

Browse files
committed
fix: panic on extra fields in a pattern
1 parent 4899ac8 commit 31b6a75

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

crates/hir_ty/src/diagnostics/match_check.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub(crate) enum PatternError {
2525
Unimplemented,
2626
UnresolvedVariant,
2727
MissingField,
28+
ExtraFields,
2829
}
2930

3031
#[derive(Clone, Debug, PartialEq)]
@@ -182,6 +183,11 @@ impl<'a> PatCtxt<'a> {
182183
expected_len: usize,
183184
ellipsis: Option<usize>,
184185
) -> Vec<FieldPat> {
186+
if pats.len() > expected_len {
187+
self.errors.push(PatternError::ExtraFields);
188+
return Vec::new();
189+
}
190+
185191
pats.iter()
186192
.enumerate_and_adjust(expected_len, ellipsis)
187193
.map(|(i, &subpattern)| FieldPat {
@@ -702,6 +708,25 @@ fn main() {
702708
);
703709
}
704710

711+
#[test]
712+
fn malformed_match_arm_extra_fields() {
713+
check_diagnostics(
714+
r#"
715+
enum A { B(isize, isize), C }
716+
fn main() {
717+
match A::B(1, 2) {
718+
A::B(_, _, _) => (),
719+
// ^^^^^^^^^^^^^ Internal: match check bailed out
720+
}
721+
match A::B(1, 2) {
722+
A::C(_) => (),
723+
// ^^^^^^^ Internal: match check bailed out
724+
}
725+
}
726+
"#,
727+
);
728+
}
729+
705730
#[test]
706731
fn expr_diverges() {
707732
check_diagnostics(

0 commit comments

Comments
 (0)