about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_mir_build/src/builder/matches/match_pair.rs9
-rw-r--r--tests/ui/match/privately-uninhabited-issue-137999.rs44
-rw-r--r--tests/ui/match/privately-uninhabited-issue-137999.stderr26
3 files changed, 75 insertions, 4 deletions
diff --git a/compiler/rustc_mir_build/src/builder/matches/match_pair.rs b/compiler/rustc_mir_build/src/builder/matches/match_pair.rs
index c41538a2817..f2ce0c46dd3 100644
--- a/compiler/rustc_mir_build/src/builder/matches/match_pair.rs
+++ b/compiler/rustc_mir_build/src/builder/matches/match_pair.rs
@@ -273,10 +273,11 @@ impl<'tcx> MatchPairTree<'tcx> {
 
                 let irrefutable = adt_def.variants().iter_enumerated().all(|(i, v)| {
                     i == variant_index
-                        || !v
-                            .inhabited_predicate(cx.tcx, adt_def)
-                            .instantiate(cx.tcx, args)
-                            .apply_ignore_module(cx.tcx, cx.infcx.typing_env(cx.param_env))
+                        || !v.inhabited_predicate(cx.tcx, adt_def).instantiate(cx.tcx, args).apply(
+                            cx.tcx,
+                            cx.infcx.typing_env(cx.param_env),
+                            cx.def_id.into(),
+                        )
                 }) && !adt_def.variant_list_has_applicable_non_exhaustive();
                 if irrefutable { None } else { Some(TestCase::Variant { adt_def, variant_index }) }
             }
diff --git a/tests/ui/match/privately-uninhabited-issue-137999.rs b/tests/ui/match/privately-uninhabited-issue-137999.rs
new file mode 100644
index 00000000000..918393a0c6a
--- /dev/null
+++ b/tests/ui/match/privately-uninhabited-issue-137999.rs
@@ -0,0 +1,44 @@
+//@ edition:2024
+//@ check-fail
+
+mod m {
+    enum Void {}
+
+    pub struct Internal {
+        _v: Void,
+    }
+
+    pub enum Test {
+        A(u32, u32),
+        B(Internal),
+    }
+}
+
+use m::Test;
+
+pub fn f1(x: &mut Test) {
+    let r1: &mut u32 = match x {
+        Test::A(a, _) => a,
+        _ => todo!(),
+    };
+
+    let r2: &mut u32 = match x { //~ ERROR cannot use `*x` because it was mutably borrowed
+        Test::A(_, b) => b,
+        _ => todo!(),
+    };
+
+    let _ = *r1;
+    let _ = *r2;
+}
+
+pub fn f2(x: &mut Test) {
+    let r = &mut *x;
+    match x { //~ ERROR cannot use `*x` because it was mutably borrowed
+        Test::A(_, _) => {}
+        _ => {}
+    }
+
+    let _ = r;
+}
+
+fn main() {}
diff --git a/tests/ui/match/privately-uninhabited-issue-137999.stderr b/tests/ui/match/privately-uninhabited-issue-137999.stderr
new file mode 100644
index 00000000000..6f74a75375e
--- /dev/null
+++ b/tests/ui/match/privately-uninhabited-issue-137999.stderr
@@ -0,0 +1,26 @@
+error[E0503]: cannot use `*x` because it was mutably borrowed
+  --> $DIR/privately-uninhabited-issue-137999.rs:25:30
+   |
+LL |         Test::A(a, _) => a,
+   |                 - `x.0` is borrowed here
+...
+LL |     let r2: &mut u32 = match x {
+   |                              ^ use of borrowed `x.0`
+...
+LL |     let _ = *r1;
+   |             --- borrow later used here
+
+error[E0503]: cannot use `*x` because it was mutably borrowed
+  --> $DIR/privately-uninhabited-issue-137999.rs:36:11
+   |
+LL |     let r = &mut *x;
+   |             ------- `*x` is borrowed here
+LL |     match x {
+   |           ^ use of borrowed `*x`
+...
+LL |     let _ = r;
+   |             - borrow later used here
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0503`.