1 pub use self::def_id_forest::DefIdForest;
4 use crate::ty::context::TyCtxt;
5 use crate::ty::TyKind::*;
6 use crate::ty::{AdtDef, FieldDef, Ty, TyS, VariantDef};
7 use crate::ty::{AdtKind, Visibility};
8 use crate::ty::{DefId, SubstsRef};
12 // The methods in this module calculate `DefIdForest`s of modules in which a
13 // `AdtDef`/`VariantDef`/`FieldDef` is visibly uninhabited.
20 // pub struct SecretlyUninhabited {
27 // pub struct AlsoSecretlyUninhabited {
35 // x: a::b::SecretlyUninhabited,
36 // y: c::AlsoSecretlyUninhabited,
39 // In this code, the type `Foo` will only be visibly uninhabited inside the
40 // modules `b`, `c` and `d`. Calling `uninhabited_from` on `Foo` or its `AdtDef` will
41 // return the forest of modules {`b`, `c`->`d`} (represented in a `DefIdForest` by the
44 // We need this information for pattern-matching on `Foo` or types that contain
49 // let foo_result: Result<T, Foo> = ... ;
50 // let Ok(t) = foo_result;
52 // This code should only compile in modules where the uninhabitedness of `Foo` is
55 impl<'tcx> TyCtxt<'tcx> {
56 /// Checks whether a type is visibly uninhabited from a particular module.
63 /// pub struct SecretlyUninhabited {
70 /// pub struct AlsoSecretlyUninhabited {
78 /// x: a::b::SecretlyUninhabited,
79 /// y: c::AlsoSecretlyUninhabited,
82 /// In this code, the type `Foo` will only be visibly uninhabited inside the
83 /// modules b, c and d. This effects pattern-matching on `Foo` or types that
88 /// let foo_result: Result<T, Foo> = ... ;
89 /// let Ok(t) = foo_result;
91 /// This code should only compile in modules where the uninhabitedness of Foo is
93 pub fn is_ty_uninhabited_from(
97 param_env: ty::ParamEnv<'tcx>,
99 // To check whether this type is uninhabited at all (not just from the
100 // given node), you could check whether the forest is empty.
104 ty.uninhabited_from(self, param_env).contains(self, module)
109 /// Calculates the forest of `DefId`s from which this ADT is visibly uninhabited.
113 substs: SubstsRef<'tcx>,
114 param_env: ty::ParamEnv<'tcx>,
116 // Non-exhaustive ADTs from other crates are always considered inhabited.
117 if self.is_variant_list_non_exhaustive() && !self.did.is_local() {
120 DefIdForest::intersection(
124 .map(|v| v.uninhabited_from(tcx, substs, self.adt_kind(), param_env)),
130 impl<'tcx> VariantDef {
131 /// Calculates the forest of `DefId`s from which this variant is visibly uninhabited.
132 pub fn uninhabited_from(
135 substs: SubstsRef<'tcx>,
137 param_env: ty::ParamEnv<'tcx>,
139 let is_enum = match adt_kind {
140 // For now, `union`s are never considered uninhabited.
141 // The precise semantics of inhabitedness with respect to unions is currently undecided.
142 AdtKind::Union => return DefIdForest::empty(),
143 AdtKind::Enum => true,
144 AdtKind::Struct => false,
146 // Non-exhaustive variants from other crates are always considered inhabited.
147 if self.is_field_list_non_exhaustive() && !self.def_id.is_local() {
152 self.fields.iter().map(|f| f.uninhabited_from(tcx, substs, is_enum, param_env)),
158 impl<'tcx> FieldDef {
159 /// Calculates the forest of `DefId`s from which this field is visibly uninhabited.
163 substs: SubstsRef<'tcx>,
165 param_env: ty::ParamEnv<'tcx>,
167 let data_uninhabitedness = move || self.ty(tcx, substs).uninhabited_from(tcx, param_env);
168 // FIXME(canndrew): Currently enum fields are (incorrectly) stored with
169 // `Visibility::Invisible` so we need to override `self.vis` if we're
170 // dealing with an enum.
172 data_uninhabitedness()
175 Visibility::Invisible => DefIdForest::empty(),
176 Visibility::Restricted(from) => {
177 let forest = DefIdForest::from_id(from);
178 let iter = Some(forest).into_iter().chain(Some(data_uninhabitedness()));
179 DefIdForest::intersection(tcx, iter)
181 Visibility::Public => data_uninhabitedness(),
187 impl<'tcx> TyS<'tcx> {
188 /// Calculates the forest of `DefId`s from which this type is visibly uninhabited.
192 param_env: ty::ParamEnv<'tcx>,
194 tcx.type_uninhabited_from(param_env.and(self))
198 // Query provider for `type_uninhabited_from`.
199 pub(crate) fn type_uninhabited_from<'tcx>(
201 key: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
204 let param_env = key.param_env;
206 Adt(def, substs) => def.uninhabited_from(tcx, substs, param_env),
208 Never => DefIdForest::full(tcx),
210 Tuple(ref tys) => DefIdForest::union(
212 tys.iter().map(|ty| ty.expect_ty().uninhabited_from(tcx, param_env)),
215 Array(ty, len) => match len.try_eval_usize(tcx, param_env) {
216 Some(0) | None => DefIdForest::empty(),
217 // If the array is definitely non-empty, it's uninhabited if
218 // the type of its elements is uninhabited.
219 Some(1..) => ty.uninhabited_from(tcx, param_env),
222 // References to uninitialised memory are valid for any type, including
223 // uninhabited types, in unsafe code, so we treat all references as
225 // The precise semantics of inhabitedness with respect to references is currently
227 Ref(..) => DefIdForest::empty(),
229 _ => DefIdForest::empty(),