Skip to content

Commit c9f0231

Browse files
committed
Auto merge of rust-lang#133889 - compiler-errors:inh-unstable, r=<try>
Consider fields to be inhabited if they are unstable Fixes rust-lang#133885 with a simple heuristic r? Nadrieril Not totally certain if this needs T-lang approval or a crater run.
2 parents acabb52 + cf82347 commit c9f0231

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
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;
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)