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)
108 pub fn is_enum_variant_uninhabited_from(self,
110 variant: &'tcx VariantDef,
111 substs: SubstsRef<'tcx>)
114 self.variant_inhabitedness_forest(variant, substs).contains(self, module)
117 pub fn is_variant_uninhabited_from_all_modules(self,
118 variant: &'tcx VariantDef,
119 substs: SubstsRef<'tcx>)
122 !self.variant_inhabitedness_forest(variant, substs).is_empty()
125 fn variant_inhabitedness_forest(self, variant: &'tcx VariantDef, substs: SubstsRef<'tcx>)
127 // Determine the ADT kind:
128 let adt_def_id = self.adt_def_id_of_variant(variant);
129 let adt_kind = self.adt_def(adt_def_id).adt_kind();
131 // Compute inhabitedness forest:
132 variant.uninhabited_from(self, substs, adt_kind)
136 impl<'a, 'gcx, 'tcx> AdtDef {
137 /// Calculate the forest of DefIds from which this adt is visibly uninhabited.
140 tcx: TyCtxt<'a, 'gcx, 'tcx>,
141 substs: SubstsRef<'tcx>) -> DefIdForest
143 DefIdForest::intersection(tcx, self.variants.iter().map(|v| {
144 v.uninhabited_from(tcx, substs, self.adt_kind())
149 impl<'a, 'gcx, 'tcx> VariantDef {
150 /// Calculate the forest of DefIds from which this variant is visibly uninhabited.
153 tcx: TyCtxt<'a, 'gcx, 'tcx>,
154 substs: SubstsRef<'tcx>,
155 adt_kind: AdtKind) -> DefIdForest
157 let is_enum = match adt_kind {
158 // For now, `union`s are never considered uninhabited.
159 // The precise semantics of inhabitedness with respect to unions is currently undecided.
160 AdtKind::Union => return DefIdForest::empty(),
161 AdtKind::Enum => true,
162 AdtKind::Struct => false,
164 DefIdForest::union(tcx, self.fields.iter().map(|f| {
165 f.uninhabited_from(tcx, substs, is_enum)
170 impl<'a, 'gcx, 'tcx> FieldDef {
171 /// Calculate the forest of DefIds from which this field is visibly uninhabited.
174 tcx: TyCtxt<'a, 'gcx, 'tcx>,
175 substs: SubstsRef<'tcx>,
178 let data_uninhabitedness = move || {
179 self.ty(tcx, substs).uninhabited_from(tcx)
181 // FIXME(canndrew): Currently enum fields are (incorrectly) stored with
182 // Visibility::Invisible so we need to override self.vis if we're
183 // dealing with an enum.
185 data_uninhabitedness()
188 Visibility::Invisible => DefIdForest::empty(),
189 Visibility::Restricted(from) => {
190 let forest = DefIdForest::from_id(from);
191 let iter = Some(forest).into_iter().chain(Some(data_uninhabitedness()));
192 DefIdForest::intersection(tcx, iter)
194 Visibility::Public => data_uninhabitedness(),
200 impl<'a, 'gcx, 'tcx> TyS<'tcx> {
201 /// Calculate the forest of DefIds from which this type is visibly uninhabited.
202 fn uninhabited_from(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> DefIdForest
205 Adt(def, substs) => def.uninhabited_from(tcx, substs),
207 Never => DefIdForest::full(tcx),
210 DefIdForest::union(tcx, tys.iter().map(|ty| {
211 ty.uninhabited_from(tcx)
215 Array(ty, len) => match len.assert_usize(tcx) {
216 // If the array is definitely non-empty, it's uninhabited if
217 // the type of its elements is uninhabited.
218 Some(n) if n != 0 => ty.uninhabited_from(tcx),
219 _ => DefIdForest::empty()
222 // References to uninitialised memory is 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(),