about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-03-20 14:31:34 +0000
committerbors <bors@rust-lang.org>2025-03-20 14:31:34 +0000
commitd8e44b722a93e55cbc9a8188dfbfe3faf1226096 (patch)
tree813918a15fdb2dc6bb9118f328afc4fc5c83b711 /compiler
parent87e60a7d285f8ea560189024f85f2b194d1f168f (diff)
parent0a6a0e47d2efb207ad0e95c3eb67b543c8cbb2e6 (diff)
downloadrust-d8e44b722a93e55cbc9a8188dfbfe3faf1226096.tar.gz
rust-d8e44b722a93e55cbc9a8188dfbfe3faf1226096.zip
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.
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_middle/src/ty/inhabitedness/mod.rs16
-rw-r--r--compiler/rustc_pattern_analysis/src/rustc.rs8
2 files changed, 22 insertions, 2 deletions
diff --git a/compiler/rustc_middle/src/ty/inhabitedness/mod.rs b/compiler/rustc_middle/src/ty/inhabitedness/mod.rs
index 855e5043e84..32988965a35 100644
--- a/compiler/rustc_middle/src/ty/inhabitedness/mod.rs
+++ b/compiler/rustc_middle/src/ty/inhabitedness/mod.rs
@@ -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;
 
@@ -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;
diff --git a/compiler/rustc_pattern_analysis/src/rustc.rs b/compiler/rustc_pattern_analysis/src/rustc.rs
index fec525c9e8c..a25a80cd45f 100644
--- a/compiler/rustc_pattern_analysis/src/rustc.rs
+++ b/compiler/rustc_pattern_analysis/src/rustc.rs
@@ -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::{
@@ -230,7 +230,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)