From 8593132a434216f03d759811a626b42f319192de Mon Sep 17 00:00:00 2001 From: hkalbasi Date: Thu, 9 Mar 2023 22:30:27 +0330 Subject: [PATCH] fix block with no termination in or patterns --- crates/hir-ty/src/mir/lower.rs | 6 +++++- .../src/handlers/mutability_errors.rs | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/crates/hir-ty/src/mir/lower.rs b/crates/hir-ty/src/mir/lower.rs index afa5275ac6b..8638e1d920e 100644 --- a/crates/hir-ty/src/mir/lower.rs +++ b/crates/hir-ty/src/mir/lower.rs @@ -1039,7 +1039,11 @@ fn pattern_match( } } } - (then_target, (!finished).then_some(current)) + if !finished { + let ce = *current_else.get_or_insert_with(|| self.new_basic_block()); + self.set_goto(current, ce); + } + (then_target, current_else) } Pat::Record { .. } => not_supported!("record pattern"), Pat::Range { .. } => not_supported!("range pattern"), diff --git a/crates/ide-diagnostics/src/handlers/mutability_errors.rs b/crates/ide-diagnostics/src/handlers/mutability_errors.rs index 9c79ceba01e..f73d1302bf2 100644 --- a/crates/ide-diagnostics/src/handlers/mutability_errors.rs +++ b/crates/ide-diagnostics/src/handlers/mutability_errors.rs @@ -574,6 +574,27 @@ fn main() { ); } + #[test] + fn or_pattern_no_terminator() { + check_diagnostics( + r#" +enum Foo { + A, B, C, D +} + +use Foo::*; + +fn f(inp: (Foo, Foo, Foo, Foo)) { + let ((A, B, _, x) | (B, C | D, x, _)) = inp else { + return; + }; + x = B; + //^^^^^ 💡 error: cannot mutate immutable variable `x` +} +"#, + ); + } + #[test] fn respect_allow_unused_mut() { // FIXME: respect