Skip to content

Commit d8e44b7

Browse files
committedMar 20, 2025·
Auto merge of #133889 - compiler-errors:inh-unstable, r=Nadrieril
Consider fields to be inhabited if they are unstable Fixes #133885 with a simple heuristic r? Nadrieril Not totally certain if this needs T-lang approval or a crater run.
2 parents 87e60a7 + 0a6a0e4 commit d8e44b7

File tree

8 files changed

+132
-2
lines changed

8 files changed

+132
-2
lines changed
 

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

+16
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

@@ -84,6 +85,21 @@ impl<'tcx> VariantDef {
8485
InhabitedPredicate::all(
8586
tcx,
8687
self.fields.iter().map(|field| {
88+
// Unstable fields are always considered to be inhabited. In the future,
89+
// this could be extended to be conditional on the field being unstable
90+
// only within the module that's querying the inhabitedness, like:
91+
// `let pred = pred.or(InhabitedPredicate::IsUnstable(field.did));`
92+
// but this is unnecessary for now, since it would only affect nightly-only
93+
// code or code within the standard library itself.
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+
{
101+
return InhabitedPredicate::True;
102+
}
87103
let pred = tcx.type_of(field.did).instantiate_identity().inhabited_predicate(tcx);
88104
if adt.is_enum() {
89105
return pred;

‎compiler/rustc_pattern_analysis/src/rustc.rs

+6-2
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::{
@@ -230,7 +230,11 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
230230
let is_visible =
231231
adt.is_enum() || field.vis.is_accessible_from(cx.module, cx.tcx);
232232
let is_uninhabited = cx.is_uninhabited(*ty);
233-
let skip = is_uninhabited && !is_visible;
233+
let is_unstable =
234+
cx.tcx.lookup_stability(field.did).is_some_and(|stab| {
235+
stab.is_unstable() && stab.feature != sym::rustc_private
236+
});
237+
let skip = is_uninhabited && (!is_visible || is_unstable);
234238
(ty, PrivateUninhabitedField(skip))
235239
});
236240
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,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`.
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)
Please sign in to comment.