Skip to content

Commit 41ea610

Browse files
committed
Don't consider unstable fields always-inhabited
This reverts the hack in #133889 now that `Pin`'s field is no longer public.
1 parent e4cc9b3 commit 41ea610

File tree

6 files changed

+51
-115
lines changed

6 files changed

+51
-115
lines changed

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
//! This code should only compile in modules where the uninhabitedness of `Foo`
4444
//! is visible.
4545
46-
use rustc_span::sym;
4746
use rustc_type_ir::TyKind::*;
4847
use tracing::instrument;
4948

@@ -85,21 +84,6 @@ impl<'tcx> VariantDef {
8584
InhabitedPredicate::all(
8685
tcx,
8786
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-
}
10387
let pred = tcx.type_of(field.did).instantiate_identity().inhabited_predicate(tcx);
10488
if adt.is_enum() {
10589
return pred;

compiler/rustc_pattern_analysis/src/rustc.rs

Lines changed: 2 additions & 5 deletions
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, sym};
18+
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
1919

2020
use crate::constructor::Constructor::*;
2121
use crate::constructor::{
@@ -227,10 +227,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
227227
let is_visible =
228228
adt.is_enum() || field.vis.is_accessible_from(cx.module, cx.tcx);
229229
let is_uninhabited = cx.is_uninhabited(*ty);
230-
let is_unstable = cx.tcx.lookup_stability(field.did).is_some_and(|stab| {
231-
stab.is_unstable() && stab.feature != sym::rustc_private
232-
});
233-
let skip = is_uninhabited && (!is_visible || is_unstable);
230+
let skip = is_uninhabited && !is_visible;
234231
(ty, PrivateUninhabitedField(skip))
235232
});
236233
cx.dropless_arena.alloc_from_iter(tys)

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

Lines changed: 0 additions & 43 deletions
This file was deleted.

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

Lines changed: 0 additions & 43 deletions
This file was deleted.

tests/ui/uninhabited/uninhabited-unstable-field.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//@ aux-build: staged-api.rs
2-
//@ revisions: current exhaustive
3-
#![cfg_attr(exhaustive, feature(exhaustive_patterns))]
2+
//! The field of `Pin` used to be public, which would cause `Pin<Void>` to be uninhabited. To remedy
3+
//! this, we temporarily made it so unstable fields are always considered uninhabited. This has now
4+
//! been reverted, and this file ensures that we don't special-case unstable fields wrt
5+
//! inhabitedness anymore.
6+
#![feature(exhaustive_patterns)]
47
#![feature(never_type)]
58
#![feature(my_coro_state)] // Custom feature from `staged-api.rs`
69
#![deny(unreachable_patterns)]
@@ -13,31 +16,29 @@ enum Void {}
1316

1417
fn demo(x: Foo<Void>) {
1518
match x {}
16-
//~^ ERROR non-exhaustive patterns
1719
}
1820

19-
// Ensure that the pattern is not considered unreachable.
21+
// Ensure that the pattern is considered unreachable.
2022
fn demo2(x: Foo<Void>) {
2123
match x {
22-
Foo { .. } => {}
24+
Foo { .. } => {} //~ ERROR unreachable
2325
}
2426
}
2527

2628
// Same as above, but for wildcard.
2729
fn demo3(x: Foo<Void>) {
2830
match x {
29-
_ => {}
31+
_ => {} //~ ERROR unreachable
3032
}
3133
}
3234

3335
fn unstable_enum(x: MyCoroutineState<i32, !>) {
3436
match x {
35-
//~^ ERROR non-exhaustive patterns
3637
MyCoroutineState::Yielded(_) => {}
3738
}
3839
match x {
3940
MyCoroutineState::Yielded(_) => {}
40-
MyCoroutineState::Complete(_) => {}
41+
MyCoroutineState::Complete(_) => {} //~ ERROR unreachable
4142
}
4243
}
4344

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error: unreachable pattern
2+
--> $DIR/uninhabited-unstable-field.rs:20:9
3+
|
4+
LL | Foo { .. } => {}
5+
| ^^^^^^^^^^------
6+
| |
7+
| matches no values because `Foo<Void>` is uninhabited
8+
| help: remove the match arm
9+
|
10+
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
11+
note: the lint level is defined here
12+
--> $DIR/uninhabited-unstable-field.rs:5:9
13+
|
14+
LL | #![deny(unreachable_patterns)]
15+
| ^^^^^^^^^^^^^^^^^^^^
16+
17+
error: unreachable pattern
18+
--> $DIR/uninhabited-unstable-field.rs:27:9
19+
|
20+
LL | _ => {}
21+
| ^------
22+
| |
23+
| matches no values because `Foo<Void>` is uninhabited
24+
| help: remove the match arm
25+
|
26+
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
27+
28+
error: unreachable pattern
29+
--> $DIR/uninhabited-unstable-field.rs:37:9
30+
|
31+
LL | MyCoroutineState::Complete(_) => {}
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------
33+
| |
34+
| matches no values because `!` is uninhabited
35+
| help: remove the match arm
36+
|
37+
= note: to learn more about uninhabited types, see https://doc.rust-lang.org/nomicon/exotic-sizes.html#empty-types
38+
39+
error: aborting due to 3 previous errors
40+

0 commit comments

Comments
 (0)