Skip to content

Commit a4b5051

Browse files
committed
mir_build: consider privacy when checking for irrefutable patterns
1 parent 66dd2d8 commit a4b5051

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

compiler/rustc_mir_build/src/builder/matches/match_pair.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,11 @@ impl<'tcx> MatchPairTree<'tcx> {
219219

220220
let irrefutable = adt_def.variants().iter_enumerated().all(|(i, v)| {
221221
i == variant_index
222-
|| !v
223-
.inhabited_predicate(cx.tcx, adt_def)
224-
.instantiate(cx.tcx, args)
225-
.apply_ignore_module(cx.tcx, cx.infcx.typing_env(cx.param_env))
222+
|| !v.inhabited_predicate(cx.tcx, adt_def).instantiate(cx.tcx, args).apply(
223+
cx.tcx,
224+
cx.infcx.typing_env(cx.param_env),
225+
cx.def_id.into(),
226+
)
226227
}) && !adt_def.variant_list_has_applicable_non_exhaustive();
227228
if irrefutable {
228229
default_irrefutable()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//@ edition:2024
2+
//@ check-fail
3+
4+
mod m {
5+
enum Void {}
6+
7+
pub struct Internal {
8+
_v: Void,
9+
}
10+
11+
pub enum Test {
12+
A(u32, u32),
13+
B(Internal),
14+
}
15+
}
16+
17+
use m::Test;
18+
19+
pub fn f1(x: &mut Test) {
20+
let r1: &mut u32 = match x {
21+
Test::A(a, _) => a,
22+
_ => todo!(),
23+
};
24+
25+
let r2: &mut u32 = match x { //~ ERROR cannot use `*x` because it was mutably borrowed
26+
Test::A(_, b) => b,
27+
_ => todo!(),
28+
};
29+
30+
let _ = *r1;
31+
let _ = *r2;
32+
}
33+
34+
pub fn f2(x: &mut Test) {
35+
let r = &mut *x;
36+
match x { //~ ERROR cannot use `*x` because it was mutably borrowed
37+
Test::A(_, _) => {}
38+
_ => {}
39+
}
40+
41+
let _ = r;
42+
}
43+
44+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0503]: cannot use `*x` because it was mutably borrowed
2+
--> $DIR/privately-uninhabited-issue-137999.rs:25:30
3+
|
4+
LL | Test::A(a, _) => a,
5+
| - `x.0` is borrowed here
6+
...
7+
LL | let r2: &mut u32 = match x {
8+
| ^ use of borrowed `x.0`
9+
...
10+
LL | let _ = *r1;
11+
| --- borrow later used here
12+
13+
error[E0503]: cannot use `*x` because it was mutably borrowed
14+
--> $DIR/privately-uninhabited-issue-137999.rs:36:11
15+
|
16+
LL | let r = &mut *x;
17+
| ------- `*x` is borrowed here
18+
LL | match x {
19+
| ^ use of borrowed `*x`
20+
...
21+
LL | let _ = r;
22+
| - borrow later used here
23+
24+
error: aborting due to 2 previous errors
25+
26+
For more information about this error, try `rustc --explain E0503`.

0 commit comments

Comments
 (0)