From 80fe03877d39348aa00208f3d7f335ea8427e688 Mon Sep 17 00:00:00 2001 From: Jesse Bakker Date: Tue, 30 Mar 2021 14:58:00 +0200 Subject: Fix expansion of OR-patterns in match check --- crates/hir_ty/src/diagnostics/match_check.rs | 40 +++++++++++----------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/crates/hir_ty/src/diagnostics/match_check.rs b/crates/hir_ty/src/diagnostics/match_check.rs index 5a5cdcbf3..9cb472b51 100644 --- a/crates/hir_ty/src/diagnostics/match_check.rs +++ b/crates/hir_ty/src/diagnostics/match_check.rs @@ -539,7 +539,7 @@ impl Matrix { if let Some(Pat::Or(pat_ids)) = row.get_head().map(|pat_id| pat_id.as_pat(cx)) { // Or patterns are expanded here for pat_id in pat_ids { - self.0.push(PatStack::from_pattern(pat_id)); + self.0.push(row.replace_head_with([pat_id].iter())); } } else { self.0.push(row); @@ -1084,6 +1084,20 @@ fn main() { ); } + #[test] + fn or_pattern_no_diagnostic() { + check_diagnostics( + r#" +enum Either {A, B} + +fn main() { + match (Either::A, Either::B) { + (Either::A | Either::B, _) => (), + } +}"#, + ) + } + #[test] fn mismatched_types() { // Match statements with arms that don't match the @@ -1335,30 +1349,6 @@ fn bang(never: !) { ); } - #[test] - fn or_pattern_panic() { - check_diagnostics( - r#" -pub enum Category { Infinity, Zero } - -fn panic(a: Category, b: Category) { - match (a, b) { - (Category::Zero | Category::Infinity, _) => (), - (_, Category::Zero | Category::Infinity) => (), - } - - // FIXME: This is a false positive, but the code used to cause a panic in the match checker, - // so this acts as a regression test for that. - match (a, b) { - //^^^^^^ Missing match arm - (Category::Infinity, Category::Infinity) | (Category::Zero, Category::Zero) => (), - (Category::Infinity | Category::Zero, _) => (), - } -} -"#, - ); - } - #[test] fn unknown_type() { check_diagnostics( -- cgit v1.2.3