Skip to content

Commit 238d9ea

Browse files
Consider fields to be inhabited if they are unstable
1 parent 617aad8 commit 238d9ea

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
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;
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,14 @@
1+
//@ aux-build: staged-api.rs
2+
3+
extern crate staged_api;
4+
5+
use staged_api::Foo;
6+
7+
enum Void {}
8+
9+
fn demo(x: Foo<Void>) {
10+
match x {}
11+
//~^ ERROR non-exhaustive patterns
12+
}
13+
14+
fn main() {}
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:10: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`.

0 commit comments

Comments
 (0)