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, 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 an
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.
60 /// #![feature(never_type)]
65 /// pub struct SecretlyUninhabited {
73 /// pub struct AlsoSecretlyUninhabited {
81 /// x: a::b::SecretlyUninhabited,
82 /// y: c::AlsoSecretlyUninhabited,
85 /// In this code, the type `Foo` will only be visibly uninhabited inside the
86 /// modules b, c and d. This effects pattern-matching on `Foo` or types that
90 /// ```ignore (illustrative)
91 /// let foo_result: Result<T, Foo> = ... ;
92 /// let Ok(t) = foo_result;
94 /// This code should only compile in modules where the uninhabitedness of Foo is
96 pub fn is_ty_uninhabited_from(
100 param_env: ty::ParamEnv<'tcx>,
102 // To check whether this type is uninhabited at all (not just from the
103 // given node), you could check whether the forest is empty.
107 ty.uninhabited_from(self, param_env).contains(self, module)
111 impl<'tcx> AdtDef<'tcx> {
112 /// Calculates the forest of `DefId`s from which this ADT is visibly uninhabited.
116 substs: SubstsRef<'tcx>,
117 param_env: ty::ParamEnv<'tcx>,
118 ) -> DefIdForest<'tcx> {
119 // Non-exhaustive ADTs from other crates are always considered inhabited.
120 if self.is_variant_list_non_exhaustive() && !self.did().is_local() {
123 DefIdForest::intersection(
127 .map(|v| v.uninhabited_from(tcx, substs, self.adt_kind(), param_env)),
133 impl<'tcx> VariantDef {
134 /// Calculates the forest of `DefId`s from which this variant is visibly uninhabited.
135 pub fn uninhabited_from(
138 substs: SubstsRef<'tcx>,
140 param_env: ty::ParamEnv<'tcx>,
141 ) -> DefIdForest<'tcx> {
142 let is_enum = match adt_kind {
143 // For now, `union`s are never considered uninhabited.
144 // The precise semantics of inhabitedness with respect to unions is currently undecided.
145 AdtKind::Union => return DefIdForest::empty(),
146 AdtKind::Enum => true,
147 AdtKind::Struct => false,
149 // Non-exhaustive variants from other crates are always considered inhabited.
150 if self.is_field_list_non_exhaustive() && !self.def_id.is_local() {
155 self.fields.iter().map(|f| f.uninhabited_from(tcx, substs, is_enum, param_env)),
161 impl<'tcx> FieldDef {
162 /// Calculates the forest of `DefId`s from which this field is visibly uninhabited.
166 substs: SubstsRef<'tcx>,
168 param_env: ty::ParamEnv<'tcx>,
169 ) -> DefIdForest<'tcx> {
170 let data_uninhabitedness = move || self.ty(tcx, substs).uninhabited_from(tcx, param_env);
171 // FIXME(canndrew): Currently enum fields are (incorrectly) stored with
172 // `Visibility::Invisible` so we need to override `self.vis` if we're
173 // dealing with an enum.
175 data_uninhabitedness()
178 Visibility::Invisible => DefIdForest::empty(),
179 Visibility::Restricted(from) => {
180 let forest = DefIdForest::from_id(from);
181 let iter = Some(forest).into_iter().chain(Some(data_uninhabitedness()));
182 DefIdForest::intersection(tcx, iter)
184 Visibility::Public => data_uninhabitedness(),
190 impl<'tcx> Ty<'tcx> {
191 /// Calculates the forest of `DefId`s from which this type is visibly uninhabited.
195 param_env: ty::ParamEnv<'tcx>,
196 ) -> DefIdForest<'tcx> {
197 tcx.type_uninhabited_from(param_env.and(self))
201 // Query provider for `type_uninhabited_from`.
202 pub(crate) fn type_uninhabited_from<'tcx>(
204 key: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
205 ) -> DefIdForest<'tcx> {
207 let param_env = key.param_env;
209 Adt(def, substs) => def.uninhabited_from(tcx, substs, param_env),
211 Never => DefIdForest::full(),
214 DefIdForest::union(tcx, tys.iter().map(|ty| ty.uninhabited_from(tcx, param_env)))
217 Array(ty, len) => match len.try_eval_usize(tcx, param_env) {
218 Some(0) | None => DefIdForest::empty(),
219 // If the array is definitely non-empty, it's uninhabited if
220 // the type of its elements is uninhabited.
221 Some(1..) => ty.uninhabited_from(tcx, param_env),
224 // References to uninitialised memory are valid for any type, including
225 // uninhabited types, in unsafe code, so we treat all references as
227 // The precise semantics of inhabitedness with respect to references is currently
229 Ref(..) => DefIdForest::empty(),
231 _ => DefIdForest::empty(),