rustc_middle/ty/inhabitedness/mod.rs
1//! This module contains logic for determining whether a type is inhabited or
2//! uninhabited. The [`InhabitedPredicate`] type captures the minimum
3//! information needed to determine whether a type is inhabited given a
4//! `ParamEnv` and module ID.
5//!
6//! # Example
7//! ```rust
8//! #![feature(never_type)]
9//! mod a {
10//! pub mod b {
11//! pub struct SecretlyUninhabited {
12//! _priv: !,
13//! }
14//! }
15//! }
16//!
17//! mod c {
18//! enum Void {}
19//! pub struct AlsoSecretlyUninhabited {
20//! _priv: Void,
21//! }
22//! mod d {
23//! }
24//! }
25//!
26//! struct Foo {
27//! x: a::b::SecretlyUninhabited,
28//! y: c::AlsoSecretlyUninhabited,
29//! }
30//! ```
31//! In this code, the type `Foo` will only be visibly uninhabited inside the
32//! modules `b`, `c` and `d`. Calling `inhabited_predicate` on `Foo` will
33//! return `NotInModule(b) AND NotInModule(c)`.
34//!
35//! We need this information for pattern-matching on `Foo` or types that contain
36//! `Foo`.
37//!
38//! # Example
39//! ```ignore(illustrative)
40//! let foo_result: Result<T, Foo> = ... ;
41//! let Ok(t) = foo_result;
42//! ```
43//! This code should only compile in modules where the uninhabitedness of `Foo`
44//! is visible.
45
46use rustc_type_ir::TyKind::*;
47use tracing::instrument;
48
49use crate::query::Providers;
50use crate::ty::{self, DefId, Ty, TyCtxt, TypeVisitableExt, VariantDef, Visibility};
51
52pub mod inhabited_predicate;
53
54pub use inhabited_predicate::InhabitedPredicate;
55
56pub(crate) fn provide(providers: &mut Providers) {
57 *providers = Providers { inhabited_predicate_adt, inhabited_predicate_type, ..*providers };
58}
59
60/// Returns an `InhabitedPredicate` that is generic over type parameters and
61/// requires calling [`InhabitedPredicate::instantiate`]
62fn inhabited_predicate_adt(tcx: TyCtxt<'_>, def_id: DefId) -> InhabitedPredicate<'_> {
63 if let Some(def_id) = def_id.as_local() {
64 tcx.ensure_ok().check_representability(def_id);
65 }
66
67 let adt = tcx.adt_def(def_id);
68 InhabitedPredicate::any(
69 tcx,
70 adt.variants().iter().map(|variant| variant.inhabited_predicate(tcx, adt)),
71 )
72}
73
74impl<'tcx> VariantDef {
75 /// Calculates the forest of `DefId`s from which this variant is visibly uninhabited.
76 pub fn inhabited_predicate(
77 &self,
78 tcx: TyCtxt<'tcx>,
79 adt: ty::AdtDef<'_>,
80 ) -> InhabitedPredicate<'tcx> {
81 debug_assert!(!adt.is_union());
82 InhabitedPredicate::all(
83 tcx,
84 self.fields.iter().map(|field| {
85 let pred = tcx
86 .type_of(field.did)
87 .instantiate_identity()
88 .skip_norm_wip()
89 .inhabited_predicate(tcx);
90 if adt.is_enum() {
91 return pred;
92 }
93 match field.vis {
94 Visibility::Public => pred,
95 Visibility::Restricted(from) => {
96 InhabitedPredicate::NotInModule(from).or(tcx, pred)
97 }
98 }
99 }),
100 )
101 }
102}
103
104impl<'tcx> Ty<'tcx> {
105 #[instrument(level = "debug", skip(tcx), ret)]
106 pub fn inhabited_predicate(self, tcx: TyCtxt<'tcx>) -> InhabitedPredicate<'tcx> {
107 debug_assert!(!self.has_infer());
108 match self.kind() {
109 // For now, unions are always considered inhabited
110 Adt(adt, _) if adt.is_union() => InhabitedPredicate::True,
111 // Non-exhaustive ADTs from other crates are always considered inhabited
112 Adt(adt, _) if adt.variant_list_has_applicable_non_exhaustive() => {
113 InhabitedPredicate::True
114 }
115 Never => InhabitedPredicate::False,
116 // FIXME(#155345): This should only encounter rigid aliases with the new solver.
117 Param(_)
118 | Alias(
119 _,
120 ty::AliasTy {
121 kind: ty::Inherent { .. } | ty::Projection { .. } | ty::Free { .. },
122 ..
123 },
124 ) => InhabitedPredicate::GenericType(self),
125 &Alias(_, ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) => {
126 match def_id.as_local() {
127 // Foreign opaque is considered inhabited.
128 None => InhabitedPredicate::True,
129 // Local opaque type may possibly be revealed.
130 Some(local_def_id) => {
131 let key = ty::OpaqueTypeKey { def_id: local_def_id, args };
132 InhabitedPredicate::OpaqueType(key)
133 }
134 }
135 }
136 Tuple(tys) if tys.is_empty() => InhabitedPredicate::True,
137 // use a query for more complex cases
138 Adt(..) | Array(..) | Tuple(_) => tcx.inhabited_predicate_type(self),
139 // references and other types are inhabited
140 _ => InhabitedPredicate::True,
141 }
142 }
143
144 /// Checks whether a type is visibly uninhabited from a particular module.
145 ///
146 /// # Example
147 /// ```
148 /// #![feature(never_type)]
149 /// # fn main() {}
150 /// enum Void {}
151 /// mod a {
152 /// pub mod b {
153 /// pub struct SecretlyUninhabited {
154 /// _priv: !,
155 /// }
156 /// }
157 /// }
158 ///
159 /// mod c {
160 /// use super::Void;
161 /// pub struct AlsoSecretlyUninhabited {
162 /// _priv: Void,
163 /// }
164 /// mod d {
165 /// }
166 /// }
167 ///
168 /// struct Foo {
169 /// x: a::b::SecretlyUninhabited,
170 /// y: c::AlsoSecretlyUninhabited,
171 /// }
172 /// ```
173 /// In this code, the type `Foo` will only be visibly uninhabited inside the
174 /// modules b, c and d. This effects pattern-matching on `Foo` or types that
175 /// contain `Foo`.
176 ///
177 /// # Example
178 /// ```ignore (illustrative)
179 /// let foo_result: Result<T, Foo> = ... ;
180 /// let Ok(t) = foo_result;
181 /// ```
182 /// This code should only compile in modules where the uninhabitedness of Foo is
183 /// visible.
184 pub fn is_inhabited_from(
185 self,
186 tcx: TyCtxt<'tcx>,
187 module: DefId,
188 typing_env: ty::TypingEnv<'tcx>,
189 ) -> bool {
190 self.inhabited_predicate(tcx).apply(tcx, typing_env, module)
191 }
192
193 /// Returns true if the type is uninhabited without regard to visibility
194 pub fn is_privately_uninhabited(
195 self,
196 tcx: TyCtxt<'tcx>,
197 typing_env: ty::TypingEnv<'tcx>,
198 ) -> bool {
199 !self.inhabited_predicate(tcx).apply_ignore_module(tcx, typing_env)
200 }
201}
202
203/// N.B. this query should only be called through `Ty::inhabited_predicate`
204fn inhabited_predicate_type<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> InhabitedPredicate<'tcx> {
205 match *ty.kind() {
206 Adt(adt, args) => tcx.inhabited_predicate_adt(adt.did()).instantiate(tcx, args),
207
208 Tuple(tys) => {
209 InhabitedPredicate::all(tcx, tys.iter().map(|ty| ty.inhabited_predicate(tcx)))
210 }
211
212 // If we can evaluate the array length before having a `ParamEnv`, then
213 // we can simplify the predicate. This is an optimization.
214 Array(ty, len) => match len.try_to_target_usize(tcx) {
215 Some(0) => InhabitedPredicate::True,
216 Some(1..) => ty.inhabited_predicate(tcx),
217 None => ty.inhabited_predicate(tcx).or(tcx, InhabitedPredicate::ConstIsZero(len)),
218 },
219
220 _ => bug!("unexpected TyKind, use `Ty::inhabited_predicate`"),
221 }
222}