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.
8 //! #![feature(never_type)]
11 //! pub struct SecretlyUninhabited {
19 //! pub struct AlsoSecretlyUninhabited {
27 //! x: a::b::SecretlyUninhabited,
28 //! y: c::AlsoSecretlyUninhabited,
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)`.
35 //! We need this information for pattern-matching on `Foo` or types that contain
39 //! ```ignore(illustrative)
40 //! let foo_result: Result<T, Foo> = ... ;
41 //! let Ok(t) = foo_result;
43 //! This code should only compile in modules where the uninhabitedness of `Foo`
46 use crate::ty::context::TyCtxt;
47 use crate::ty::{self, DefId, Ty, VariantDef, Visibility};
49 use rustc_type_ir::sty::TyKind::*;
51 pub mod inhabited_predicate;
53 pub use inhabited_predicate::InhabitedPredicate;
55 pub(crate) fn provide(providers: &mut ty::query::Providers) {
57 ty::query::Providers { inhabited_predicate_adt, inhabited_predicate_type, ..*providers };
60 /// Returns an `InhabitedPredicate` that is generic over type parameters and
61 /// requires calling [`InhabitedPredicate::subst`]
62 fn inhabited_predicate_adt(tcx: TyCtxt<'_>, def_id: DefId) -> InhabitedPredicate<'_> {
63 if let Some(def_id) = def_id.as_local() {
64 if matches!(tcx.representability(def_id), ty::Representability::Infinite) {
65 return InhabitedPredicate::True;
68 let adt = tcx.adt_def(def_id);
69 InhabitedPredicate::any(
71 adt.variants().iter().map(|variant| variant.inhabited_predicate(tcx, adt)),
75 impl<'tcx> VariantDef {
76 /// Calculates the forest of `DefId`s from which this variant is visibly uninhabited.
77 pub fn inhabited_predicate(
81 ) -> InhabitedPredicate<'tcx> {
82 debug_assert!(!adt.is_union());
83 if self.is_field_list_non_exhaustive() && !self.def_id.is_local() {
84 // Non-exhaustive variants from other crates are always considered inhabited.
85 return InhabitedPredicate::True;
87 InhabitedPredicate::all(
89 self.fields.iter().map(|field| {
90 let pred = tcx.type_of(field.did).inhabited_predicate(tcx);
95 Visibility::Public => pred,
96 Visibility::Restricted(from) => {
97 pred.or(tcx, InhabitedPredicate::NotInModule(from))
105 impl<'tcx> Ty<'tcx> {
106 pub fn inhabited_predicate(self, tcx: TyCtxt<'tcx>) -> InhabitedPredicate<'tcx> {
108 // For now, union`s are always considered inhabited
109 Adt(adt, _) if adt.is_union() => InhabitedPredicate::True,
110 // Non-exhaustive ADTs from other crates are always considered inhabited
111 Adt(adt, _) if adt.is_variant_list_non_exhaustive() && !adt.did().is_local() => {
112 InhabitedPredicate::True
114 Never => InhabitedPredicate::False,
115 Param(_) | Alias(ty::Projection, _) => InhabitedPredicate::GenericType(self),
116 Tuple(tys) if tys.is_empty() => InhabitedPredicate::True,
117 // use a query for more complex cases
118 Adt(..) | Array(..) | Tuple(_) => tcx.inhabited_predicate_type(self),
119 // references and other types are inhabited
120 _ => InhabitedPredicate::True,
124 /// Checks whether a type is visibly uninhabited from a particular module.
128 /// #![feature(never_type)]
133 /// pub struct SecretlyUninhabited {
141 /// pub struct AlsoSecretlyUninhabited {
149 /// x: a::b::SecretlyUninhabited,
150 /// y: c::AlsoSecretlyUninhabited,
153 /// In this code, the type `Foo` will only be visibly uninhabited inside the
154 /// modules b, c and d. This effects pattern-matching on `Foo` or types that
158 /// ```ignore (illustrative)
159 /// let foo_result: Result<T, Foo> = ... ;
160 /// let Ok(t) = foo_result;
162 /// This code should only compile in modules where the uninhabitedness of Foo is
164 pub fn is_inhabited_from(
168 param_env: ty::ParamEnv<'tcx>,
170 self.inhabited_predicate(tcx).apply(tcx, param_env, module)
173 /// Returns true if the type is uninhabited without regard to visibility
174 pub fn is_privately_uninhabited(
177 param_env: ty::ParamEnv<'tcx>,
179 !self.inhabited_predicate(tcx).apply_ignore_module(tcx, param_env)
183 /// N.B. this query should only be called through `Ty::inhabited_predicate`
184 fn inhabited_predicate_type<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> InhabitedPredicate<'tcx> {
186 Adt(adt, substs) => tcx.inhabited_predicate_adt(adt.did()).subst(tcx, substs),
189 InhabitedPredicate::all(tcx, tys.iter().map(|ty| ty.inhabited_predicate(tcx)))
192 // If we can evaluate the array length before having a `ParamEnv`, then
193 // we can simplify the predicate. This is an optimization.
194 Array(ty, len) => match len.kind().try_to_machine_usize(tcx) {
195 Some(0) => InhabitedPredicate::True,
196 Some(1..) => ty.inhabited_predicate(tcx),
197 None => ty.inhabited_predicate(tcx).or(tcx, InhabitedPredicate::ConstIsZero(len)),
200 _ => bug!("unexpected TyKind, use `Ty::inhabited_predicate`"),