Skip to content

Commit f6107ca

Browse files
Consider fields to be inhabited if they are unstable
1 parent 75530e9 commit f6107ca

File tree

6 files changed

+95
-1
lines changed

6 files changed

+95
-1
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ impl<'tcx> VariantDef {
8484
InhabitedPredicate::all(
8585
tcx,
8686
self.fields.iter().map(|field| {
87+
// Unstable fields are always considered to be inhabited. In the future,
88+
// this could be extended to be conditional on the field being unstable
89+
// only within the module that's querying the inhabitedness, like:
90+
// `let pred = pred.or(InhabitedPredicate::IsUnstable(field.did));`
91+
// but this is unnecessary for now, since it would only affect nightly-only
92+
// code or code within the standard library itself.
93+
if tcx.lookup_stability(field.did).is_some_and(|stab| stab.is_unstable()) {
94+
return InhabitedPredicate::True;
95+
}
8796
let pred = tcx.type_of(field.did).instantiate_identity().inhabited_predicate(tcx);
8897
if adt.is_enum() {
8998
return pred;

compiler/rustc_pattern_analysis/src/rustc.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,11 @@ 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 skip = is_uninhabited && !is_visible;
235+
let is_unstable = cx
236+
.tcx
237+
.lookup_stability(field.did)
238+
.is_some_and(|stab| stab.is_unstable());
239+
let skip = is_uninhabited && (!is_visible || is_unstable);
236240
(ty, PrivateUninhabitedField(skip))
237241
});
238242
cx.dropless_arena.alloc_from_iter(tys)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![feature(staged_api)]
2+
#![stable(feature = "stable", since = "1.0.0")]
3+
4+
#[stable(feature = "stable", since = "1.0.0")]
5+
pub struct Foo<T> {
6+
#[unstable(feature = "unstable", issue = "none")]
7+
pub field: T,
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0004]: non-exhaustive patterns: type `Foo<Void>` is non-empty
2+
--> $DIR/uninhabited-unstable-field.rs:13:11
3+
|
4+
LL | match x {}
5+
| ^
6+
|
7+
note: `Foo<Void>` defined here
8+
--> $DIR/auxiliary/staged-api.rs:5:1
9+
|
10+
LL | pub struct Foo<T> {
11+
| ^^^^^^^^^^^^^^^^^
12+
= note: the matched value is of type `Foo<Void>`
13+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
14+
|
15+
LL ~ match x {
16+
LL + _ => todo!(),
17+
LL + }
18+
|
19+
20+
error: aborting due to 1 previous error
21+
22+
For more information about this error, try `rustc --explain E0004`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0004]: non-exhaustive patterns: type `Foo<Void>` is non-empty
2+
--> $DIR/uninhabited-unstable-field.rs:13:11
3+
|
4+
LL | match x {}
5+
| ^
6+
|
7+
note: `Foo<Void>` defined here
8+
--> $DIR/auxiliary/staged-api.rs:5:1
9+
|
10+
LL | pub struct Foo<T> {
11+
| ^^^^^^^^^^^^^^^^^
12+
= note: the matched value is of type `Foo<Void>`
13+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
14+
|
15+
LL ~ match x {
16+
LL + _ => todo!(),
17+
LL ~ }
18+
|
19+
20+
error: aborting due to 1 previous error
21+
22+
For more information about this error, try `rustc --explain E0004`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@ aux-build: staged-api.rs
2+
//@ revisions: current exhaustive
3+
4+
#![feature(exhaustive_patterns)]
5+
6+
extern crate staged_api;
7+
8+
use staged_api::Foo;
9+
10+
enum Void {}
11+
12+
fn demo(x: Foo<Void>) {
13+
match x {}
14+
//~^ ERROR non-exhaustive patterns
15+
}
16+
17+
// Ensure that the pattern is not considered unreachable.
18+
fn demo2(x: Foo<Void>) {
19+
match x {
20+
Foo { .. } => {}
21+
}
22+
}
23+
24+
// Same as above, but for wildcard.
25+
fn demo3(x: Foo<Void>) {
26+
match x { _ => {} }
27+
}
28+
29+
fn main() {}

0 commit comments

Comments
 (0)