fix: panic on extra fields in a pattern

This commit is contained in:
Dawer 2021-05-24 17:03:36 +05:00
parent 4899ac8c05
commit 31b6a750f8

View file

@ -25,6 +25,7 @@ pub(crate) enum PatternError {
Unimplemented,
UnresolvedVariant,
MissingField,
ExtraFields,
}
#[derive(Clone, Debug, PartialEq)]
@ -182,6 +183,11 @@ impl<'a> PatCtxt<'a> {
expected_len: usize,
ellipsis: Option<usize>,
) -> Vec<FieldPat> {
if pats.len() > expected_len {
self.errors.push(PatternError::ExtraFields);
return Vec::new();
}
pats.iter()
.enumerate_and_adjust(expected_len, ellipsis)
.map(|(i, &subpattern)| FieldPat {
@ -702,6 +708,25 @@ fn main() {
);
}
#[test]
fn malformed_match_arm_extra_fields() {
check_diagnostics(
r#"
enum A { B(isize, isize), C }
fn main() {
match A::B(1, 2) {
A::B(_, _, _) => (),
// ^^^^^^^^^^^^^ Internal: match check bailed out
}
match A::B(1, 2) {
A::C(_) => (),
// ^^^^^^^ Internal: match check bailed out
}
}
"#,
);
}
#[test]
fn expr_diverges() {
check_diagnostics(