aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDawer <[email protected]>2021-05-24 13:03:36 +0100
committerDawer <[email protected]>2021-05-31 20:49:44 +0100
commit31b6a750f8e37d011060a17ffd816d721d087844 (patch)
tree249bff6e47d0fdcd5ebc02e655c246b74e1f897e
parent4899ac8c053ef782c25c87b12b54dbcbfdb5e0fb (diff)
fix: panic on extra fields in a pattern
-rw-r--r--crates/hir_ty/src/diagnostics/match_check.rs25
1 files changed, 25 insertions, 0 deletions
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 {
25 Unimplemented, 25 Unimplemented,
26 UnresolvedVariant, 26 UnresolvedVariant,
27 MissingField, 27 MissingField,
28 ExtraFields,
28} 29}
29 30
30#[derive(Clone, Debug, PartialEq)] 31#[derive(Clone, Debug, PartialEq)]
@@ -182,6 +183,11 @@ impl<'a> PatCtxt<'a> {
182 expected_len: usize, 183 expected_len: usize,
183 ellipsis: Option<usize>, 184 ellipsis: Option<usize>,
184 ) -> Vec<FieldPat> { 185 ) -> Vec<FieldPat> {
186 if pats.len() > expected_len {
187 self.errors.push(PatternError::ExtraFields);
188 return Vec::new();
189 }
190
185 pats.iter() 191 pats.iter()
186 .enumerate_and_adjust(expected_len, ellipsis) 192 .enumerate_and_adjust(expected_len, ellipsis)
187 .map(|(i, &subpattern)| FieldPat { 193 .map(|(i, &subpattern)| FieldPat {
@@ -703,6 +709,25 @@ fn main() {
703 } 709 }
704 710
705 #[test] 711 #[test]
712 fn malformed_match_arm_extra_fields() {
713 check_diagnostics(
714 r#"
715enum A { B(isize, isize), C }
716fn 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
730 #[test]
706 fn expr_diverges() { 731 fn expr_diverges() {
707 check_diagnostics( 732 check_diagnostics(
708 r#" 733 r#"