about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-07-09 21:33:42 +0000
committerbors <bors@rust-lang.org>2023-07-09 21:33:42 +0000
commitff15634831f4a3cdb8abf5690a9848a6fdf48432 (patch)
treeee69271765127318d7029c6a29ca122e224e1012
parent5f11c9a1c3c213405f99c2bd369b679c08615554 (diff)
parent42d35f8af91e2788b342d13a64b9966d7f0e41e1 (diff)
downloadrust-ff15634831f4a3cdb8abf5690a9848a6fdf48432.tar.gz
rust-ff15634831f4a3cdb8abf5690a9848a6fdf48432.zip
Auto merge of #15245 - HKalbasi:mir, r=HKalbasi
Fix missing terminator in pattern matching of consts

fix #15238
-rw-r--r--crates/hir-ty/src/mir/lower/pattern_matching.rs7
-rw-r--r--crates/ide-diagnostics/src/handlers/mutability_errors.rs16
2 files changed, 22 insertions, 1 deletions
diff --git a/crates/hir-ty/src/mir/lower/pattern_matching.rs b/crates/hir-ty/src/mir/lower/pattern_matching.rs
index ff43c64a9e6..3354cbd76a0 100644
--- a/crates/hir-ty/src/mir/lower/pattern_matching.rs
+++ b/crates/hir-ty/src/mir/lower/pattern_matching.rs
@@ -307,6 +307,11 @@ impl MirLowerCtx<'_> {
                     mode,
                 )?,
                 None => {
+                    // The path is not a variant, so it is a const
+                    if mode != MatchingMode::Check {
+                        // A const don't bind anything. Only needs check.
+                        return Ok((current, current_else));
+                    }
                     let unresolved_name = || MirLowerError::unresolved_path(self.db, p);
                     let resolver = self.owner.resolver(self.db.upcast());
                     let pr = resolver
@@ -362,8 +367,8 @@ impl MirLowerCtx<'_> {
             },
             Pat::Lit(l) => match &self.body.exprs[*l] {
                 Expr::Literal(l) => {
-                    let c = self.lower_literal_to_operand(self.infer[pattern].clone(), l)?;
                     if mode == MatchingMode::Check {
+                        let c = self.lower_literal_to_operand(self.infer[pattern].clone(), l)?;
                         self.pattern_match_const(current_else, current, c, cond_place, pattern)?
                     } else {
                         (current, current_else)
diff --git a/crates/ide-diagnostics/src/handlers/mutability_errors.rs b/crates/ide-diagnostics/src/handlers/mutability_errors.rs
index 0a004c0647d..935f12a3969 100644
--- a/crates/ide-diagnostics/src/handlers/mutability_errors.rs
+++ b/crates/ide-diagnostics/src/handlers/mutability_errors.rs
@@ -797,6 +797,22 @@ fn main() {
 }
 "#,
         );
+        check_diagnostics(
+            r#"
+struct Foo(i32);
+
+const X: Foo = Foo(5);
+const Y: Foo = Foo(12);
+
+const fn f(mut a: Foo) -> bool {
+         //^^^^^ 💡 warn: variable does not need to be mutable
+    match a {
+        X | Y => true,
+        _ => false,
+    }
+}
+"#,
+        );
     }
 
     #[test]