From 31b6a750f8e37d011060a17ffd816d721d087844 Mon Sep 17 00:00:00 2001 From: Dawer <7803845+iDawer@users.noreply.github.com> Date: Mon, 24 May 2021 17:03:36 +0500 Subject: fix: panic on extra fields in a pattern --- crates/hir_ty/src/diagnostics/match_check.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'crates') diff --git a/crates/hir_ty/src/diagnostics/match_check.rs b/crates/hir_ty/src/diagnostics/match_check.rs index 1d250413d..a9a99f57a 100644 --- a/crates/hir_ty/src/diagnostics/match_check.rs +++ b/crates/hir_ty/src/diagnostics/match_check.rs @@ -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, ) -> Vec { + 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( -- cgit v1.2.3