Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider fields to be inhabited if they are unstable #133889

Merged
merged 2 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions compiler/rustc_middle/src/ty/inhabitedness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
//! This code should only compile in modules where the uninhabitedness of `Foo`
//! is visible.

use rustc_span::sym;
use rustc_type_ir::TyKind::*;
use tracing::instrument;

Expand Down Expand Up @@ -84,6 +85,21 @@ impl<'tcx> VariantDef {
InhabitedPredicate::all(
tcx,
self.fields.iter().map(|field| {
// Unstable fields are always considered to be inhabited. In the future,
// this could be extended to be conditional on the field being unstable
// only within the module that's querying the inhabitedness, like:
// `let pred = pred.or(InhabitedPredicate::IsUnstable(field.did));`
// but this is unnecessary for now, since it would only affect nightly-only
// code or code within the standard library itself.
// HACK: We filter out `rustc_private` fields since with the flag
// `-Zforce-unstable-if-unmarked` we consider all unmarked fields to be
// unstable when building the compiler.
if tcx
.lookup_stability(field.did)
.is_some_and(|stab| stab.is_unstable() && stab.feature != sym::rustc_private)
{
return InhabitedPredicate::True;
}
let pred = tcx.type_of(field.did).instantiate_identity().inhabited_predicate(tcx);
if adt.is_enum() {
return pred;
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_pattern_analysis/src/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rustc_middle::ty::{
};
use rustc_middle::{bug, span_bug};
use rustc_session::lint;
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, sym};

use crate::constructor::Constructor::*;
use crate::constructor::{
Expand Down Expand Up @@ -232,7 +232,11 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
let is_visible =
adt.is_enum() || field.vis.is_accessible_from(cx.module, cx.tcx);
let is_uninhabited = cx.is_uninhabited(*ty);
let skip = is_uninhabited && !is_visible;
let is_unstable =
cx.tcx.lookup_stability(field.did).is_some_and(|stab| {
stab.is_unstable() && stab.feature != sym::rustc_private
});
let skip = is_uninhabited && (!is_visible || is_unstable);
(ty, PrivateUninhabitedField(skip))
});
cx.dropless_arena.alloc_from_iter(tys)
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/uninhabited/auxiliary/staged-api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(staged_api)]
#![stable(feature = "stable", since = "1.0.0")]

#[stable(feature = "stable", since = "1.0.0")]
pub struct Foo<T> {
#[unstable(feature = "unstable", issue = "none")]
pub field: T,
}
10 changes: 10 additions & 0 deletions tests/ui/uninhabited/uninhabited-pin-field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::pin::Pin;

enum Void {}

fn demo(x: Pin<Void>) {
match x {}
//~^ ERROR non-exhaustive patterns
}

fn main() {}
19 changes: 19 additions & 0 deletions tests/ui/uninhabited/uninhabited-pin-field.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0004]: non-exhaustive patterns: type `Pin<Void>` is non-empty
--> $DIR/uninhabited-pin-field.rs:6:11
|
LL | match x {}
| ^
|
note: `Pin<Void>` defined here
--> $SRC_DIR/core/src/pin.rs:LL:COL
= note: the matched value is of type `Pin<Void>`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
|
LL ~ match x {
LL + _ => todo!(),
LL ~ }
|

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0004`.
22 changes: 22 additions & 0 deletions tests/ui/uninhabited/uninhabited-unstable-field.current.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0004]: non-exhaustive patterns: type `Foo<Void>` is non-empty
--> $DIR/uninhabited-unstable-field.rs:13:11
|
LL | match x {}
| ^
|
note: `Foo<Void>` defined here
--> $DIR/auxiliary/staged-api.rs:5:1
|
LL | pub struct Foo<T> {
| ^^^^^^^^^^^^^^^^^
= note: the matched value is of type `Foo<Void>`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
|
LL ~ match x {
LL + _ => todo!(),
LL ~ }
|

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0004`.
22 changes: 22 additions & 0 deletions tests/ui/uninhabited/uninhabited-unstable-field.exhaustive.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0004]: non-exhaustive patterns: type `Foo<Void>` is non-empty
--> $DIR/uninhabited-unstable-field.rs:13:11
|
LL | match x {}
| ^
|
note: `Foo<Void>` defined here
--> $DIR/auxiliary/staged-api.rs:5:1
|
LL | pub struct Foo<T> {
| ^^^^^^^^^^^^^^^^^
= note: the matched value is of type `Foo<Void>`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
|
LL ~ match x {
LL + _ => todo!(),
LL ~ }
|

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0004`.
29 changes: 29 additions & 0 deletions tests/ui/uninhabited/uninhabited-unstable-field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//@ aux-build: staged-api.rs
//@ revisions: current exhaustive

#![feature(exhaustive_patterns)]

extern crate staged_api;

use staged_api::Foo;

enum Void {}

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

// Ensure that the pattern is not considered unreachable.
fn demo2(x: Foo<Void>) {
match x {
Foo { .. } => {}
}
}

// Same as above, but for wildcard.
fn demo3(x: Foo<Void>) {
match x { _ => {} }
}

fn main() {}
Loading