Skip to content

Commit 0a6a0e4

Browse files
Dont consider fields that are forced unstable due to -Zforce-unstable-if-unmarked to be uninhabited
1 parent f6107ca commit 0a6a0e4

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

compiler/rustc_middle/src/ty/inhabitedness/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
//! This code should only compile in modules where the uninhabitedness of `Foo`
4444
//! is visible.
4545
46+
use rustc_span::sym;
4647
use rustc_type_ir::TyKind::*;
4748
use tracing::instrument;
4849

@@ -90,7 +91,13 @@ impl<'tcx> VariantDef {
9091
// `let pred = pred.or(InhabitedPredicate::IsUnstable(field.did));`
9192
// but this is unnecessary for now, since it would only affect nightly-only
9293
// code or code within the standard library itself.
93-
if tcx.lookup_stability(field.did).is_some_and(|stab| stab.is_unstable()) {
94+
// HACK: We filter out `rustc_private` fields since with the flag
95+
// `-Zforce-unstable-if-unmarked` we consider all unmarked fields to be
96+
// unstable when building the compiler.
97+
if tcx
98+
.lookup_stability(field.did)
99+
.is_some_and(|stab| stab.is_unstable() && stab.feature != sym::rustc_private)
100+
{
94101
return InhabitedPredicate::True;
95102
}
96103
let pred = tcx.type_of(field.did).instantiate_identity().inhabited_predicate(tcx);

compiler/rustc_pattern_analysis/src/rustc.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_middle::ty::{
1515
};
1616
use rustc_middle::{bug, span_bug};
1717
use rustc_session::lint;
18-
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
18+
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, sym};
1919

2020
use crate::constructor::Constructor::*;
2121
use crate::constructor::{
@@ -232,10 +232,10 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
232232
let is_visible =
233233
adt.is_enum() || field.vis.is_accessible_from(cx.module, cx.tcx);
234234
let is_uninhabited = cx.is_uninhabited(*ty);
235-
let is_unstable = cx
236-
.tcx
237-
.lookup_stability(field.did)
238-
.is_some_and(|stab| stab.is_unstable());
235+
let is_unstable =
236+
cx.tcx.lookup_stability(field.did).is_some_and(|stab| {
237+
stab.is_unstable() && stab.feature != sym::rustc_private
238+
});
239239
let skip = is_uninhabited && (!is_visible || is_unstable);
240240
(ty, PrivateUninhabitedField(skip))
241241
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use std::pin::Pin;
2+
3+
enum Void {}
4+
5+
fn demo(x: Pin<Void>) {
6+
match x {}
7+
//~^ ERROR non-exhaustive patterns
8+
}
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0004]: non-exhaustive patterns: type `Pin<Void>` is non-empty
2+
--> $DIR/uninhabited-pin-field.rs:6:11
3+
|
4+
LL | match x {}
5+
| ^
6+
|
7+
note: `Pin<Void>` defined here
8+
--> $SRC_DIR/core/src/pin.rs:LL:COL
9+
= note: the matched value is of type `Pin<Void>`
10+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
11+
|
12+
LL ~ match x {
13+
LL + _ => todo!(),
14+
LL ~ }
15+
|
16+
17+
error: aborting due to 1 previous error
18+
19+
For more information about this error, try `rustc --explain E0004`.

tests/ui/uninhabited/uninhabited-unstable-field.current.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ help: ensure that all possible cases are being handled by adding a match arm wit
1414
|
1515
LL ~ match x {
1616
LL + _ => todo!(),
17-
LL + }
17+
LL ~ }
1818
|
1919

2020
error: aborting due to 1 previous error

0 commit comments

Comments
 (0)