1 use crate::ty::context::TyCtxt;
2 use crate::ty::{AdtDef, VariantDef, FieldDef, Ty, TyS};
3 use crate::ty::{DefId, SubstsRef};
4 use crate::ty::{AdtKind, Visibility};
5 use crate::ty::TyKind::*;
7 pub use self::def_id_forest::DefIdForest;
11 // The methods in this module calculate DefIdForests of modules in which a
12 // AdtDef/VariantDef/FieldDef is visibly uninhabited.
19 // pub struct SecretlyUninhabited {
26 // pub struct AlsoSecretlyUninhabited {
34 // x: a::b::SecretlyUninhabited,
35 // y: c::AlsoSecretlyUninhabited,
38 // In this code, the type Foo will only be visibly uninhabited inside the
39 // modules b, c and d. Calling uninhabited_from on Foo or its AdtDef will
40 // return the forest of modules {b, c->d} (represented in a DefIdForest by the
43 // We need this information for pattern-matching on Foo or types that contain
48 // let foo_result: Result<T, Foo> = ... ;
49 // let Ok(t) = foo_result;
51 // This code should only compile in modules where the uninhabitedness of Foo is
54 impl<'tcx> TyCtxt<'tcx> {
55 /// Checks whether a type is visibly uninhabited from a particular module.
61 /// pub struct SecretlyUninhabited {
68 /// pub struct AlsoSecretlyUninhabited {
76 /// x: a::b::SecretlyUninhabited,
77 /// y: c::AlsoSecretlyUninhabited,
80 /// In this code, the type `Foo` will only be visibly uninhabited inside the
81 /// modules b, c and d. This effects pattern-matching on `Foo` or types that
86 /// let foo_result: Result<T, Foo> = ... ;
87 /// let Ok(t) = foo_result;
89 /// This code should only compile in modules where the uninhabitedness of Foo is
91 pub fn is_ty_uninhabited_from(self, module: DefId, ty: Ty<'tcx>) -> bool {
92 // To check whether this type is uninhabited at all (not just from the
93 // given node) you could check whether the forest is empty.
97 self.ty_inhabitedness_forest(ty).contains(self, module)
100 pub fn is_ty_uninhabited_from_any_module(self, ty: Ty<'tcx>) -> bool {
101 !self.ty_inhabitedness_forest(ty).is_empty()
104 fn ty_inhabitedness_forest(self, ty: Ty<'tcx>) -> DefIdForest {
105 ty.uninhabited_from(self)
110 /// Calculate the forest of DefIds from which this adt is visibly uninhabited.
111 fn uninhabited_from(&self, tcx: TyCtxt<'tcx>, substs: SubstsRef<'tcx>) -> DefIdForest {
112 // Non-exhaustive ADTs from other crates are always considered inhabited.
113 if self.is_variant_list_non_exhaustive() && !self.did.is_local() {
116 DefIdForest::intersection(tcx, self.variants.iter().map(|v| {
117 v.uninhabited_from(tcx, substs, self.adt_kind())
123 impl<'tcx> VariantDef {
124 /// Calculate the forest of DefIds from which this variant is visibly uninhabited.
125 pub fn uninhabited_from(
128 substs: SubstsRef<'tcx>,
131 let is_enum = match adt_kind {
132 // For now, `union`s are never considered uninhabited.
133 // The precise semantics of inhabitedness with respect to unions is currently undecided.
134 AdtKind::Union => return DefIdForest::empty(),
135 AdtKind::Enum => true,
136 AdtKind::Struct => false,
138 // Non-exhaustive variants from other crates are always considered inhabited.
139 if self.is_field_list_non_exhaustive() && !self.def_id.is_local() {
142 DefIdForest::union(tcx, self.fields.iter().map(|f| {
143 f.uninhabited_from(tcx, substs, is_enum)
149 impl<'tcx> FieldDef {
150 /// Calculate the forest of DefIds from which this field is visibly uninhabited.
154 substs: SubstsRef<'tcx>,
157 let data_uninhabitedness = move || {
158 self.ty(tcx, substs).uninhabited_from(tcx)
160 // FIXME(canndrew): Currently enum fields are (incorrectly) stored with
161 // Visibility::Invisible so we need to override self.vis if we're
162 // dealing with an enum.
164 data_uninhabitedness()
167 Visibility::Invisible => DefIdForest::empty(),
168 Visibility::Restricted(from) => {
169 let forest = DefIdForest::from_id(from);
170 let iter = Some(forest).into_iter().chain(Some(data_uninhabitedness()));
171 DefIdForest::intersection(tcx, iter)
173 Visibility::Public => data_uninhabitedness(),
179 impl<'tcx> TyS<'tcx> {
180 /// Calculate the forest of DefIds from which this type is visibly uninhabited.
181 fn uninhabited_from(&self, tcx: TyCtxt<'tcx>) -> DefIdForest {
183 Adt(def, substs) => def.uninhabited_from(tcx, substs),
185 Never => DefIdForest::full(tcx),
188 DefIdForest::union(tcx, tys.iter().map(|ty| {
189 ty.expect_ty().uninhabited_from(tcx)
193 Array(ty, len) => match len.assert_usize(tcx) {
194 // If the array is definitely non-empty, it's uninhabited if
195 // the type of its elements is uninhabited.
196 Some(n) if n != 0 => ty.uninhabited_from(tcx),
197 _ => DefIdForest::empty()
200 // References to uninitialised memory is valid for any type, including
201 // uninhabited types, in unsafe code, so we treat all references as
203 // The precise semantics of inhabitedness with respect to references is currently
205 Ref(..) => DefIdForest::empty(),
207 _ => DefIdForest::empty(),