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<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, '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_all_modules(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)
109 impl<'a, 'gcx, 'tcx> AdtDef {
110 /// Calculate the forest of DefIds from which this adt is visibly uninhabited.
113 tcx: TyCtxt<'a, 'gcx, 'tcx>,
114 substs: SubstsRef<'tcx>) -> DefIdForest
116 DefIdForest::intersection(tcx, self.variants.iter().map(|v| {
117 v.uninhabited_from(tcx, substs, self.adt_kind())
122 impl<'a, 'gcx, 'tcx> VariantDef {
123 /// Calculate the forest of DefIds from which this variant is visibly uninhabited.
124 pub fn uninhabited_from(
126 tcx: TyCtxt<'a, 'gcx, 'tcx>,
127 substs: SubstsRef<'tcx>,
128 adt_kind: AdtKind) -> DefIdForest
130 let is_enum = match adt_kind {
131 // For now, `union`s are never considered uninhabited.
132 // The precise semantics of inhabitedness with respect to unions is currently undecided.
133 AdtKind::Union => return DefIdForest::empty(),
134 AdtKind::Enum => true,
135 AdtKind::Struct => false,
137 DefIdForest::union(tcx, self.fields.iter().map(|f| {
138 f.uninhabited_from(tcx, substs, is_enum)
143 impl<'a, 'gcx, 'tcx> FieldDef {
144 /// Calculate the forest of DefIds from which this field is visibly uninhabited.
147 tcx: TyCtxt<'a, 'gcx, 'tcx>,
148 substs: SubstsRef<'tcx>,
151 let data_uninhabitedness = move || {
152 self.ty(tcx, substs).uninhabited_from(tcx)
154 // FIXME(canndrew): Currently enum fields are (incorrectly) stored with
155 // Visibility::Invisible so we need to override self.vis if we're
156 // dealing with an enum.
158 data_uninhabitedness()
161 Visibility::Invisible => DefIdForest::empty(),
162 Visibility::Restricted(from) => {
163 let forest = DefIdForest::from_id(from);
164 let iter = Some(forest).into_iter().chain(Some(data_uninhabitedness()));
165 DefIdForest::intersection(tcx, iter)
167 Visibility::Public => data_uninhabitedness(),
173 impl<'a, 'gcx, 'tcx> TyS<'tcx> {
174 /// Calculate the forest of DefIds from which this type is visibly uninhabited.
175 fn uninhabited_from(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> DefIdForest
178 Adt(def, substs) => def.uninhabited_from(tcx, substs),
180 Never => DefIdForest::full(tcx),
183 DefIdForest::union(tcx, tys.iter().map(|ty| {
184 ty.expect_ty().uninhabited_from(tcx)
188 Array(ty, len) => match len.assert_usize(tcx) {
189 // If the array is definitely non-empty, it's uninhabited if
190 // the type of its elements is uninhabited.
191 Some(n) if n != 0 => ty.uninhabited_from(tcx),
192 _ => DefIdForest::empty()
195 // References to uninitialised memory is valid for any type, including
196 // uninhabited types, in unsafe code, so we treat all references as
198 // The precise semantics of inhabitedness with respect to references is currently
200 Ref(..) => DefIdForest::empty(),
202 _ => DefIdForest::empty(),