]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/inhabitedness/mod.rs
Rollup merge of #60443 - RalfJung:as_ptr, r=SimonSapin
[rust.git] / src / librustc / ty / inhabitedness / mod.rs
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::*;
6
7 pub use self::def_id_forest::DefIdForest;
8
9 mod def_id_forest;
10
11 // The methods in this module calculate DefIdForests of modules in which a
12 // AdtDef/VariantDef/FieldDef is visibly uninhabited.
13 //
14 // # Example
15 // ```rust
16 // enum Void {}
17 // mod a {
18 //     pub mod b {
19 //         pub struct SecretlyUninhabited {
20 //             _priv: !,
21 //         }
22 //     }
23 // }
24 //
25 // mod c {
26 //     pub struct AlsoSecretlyUninhabited {
27 //         _priv: Void,
28 //     }
29 //     mod d {
30 //     }
31 // }
32 //
33 // struct Foo {
34 //     x: a::b::SecretlyUninhabited,
35 //     y: c::AlsoSecretlyUninhabited,
36 // }
37 // ```
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
41 // set {b, c})
42 //
43 // We need this information for pattern-matching on Foo or types that contain
44 // Foo.
45 //
46 // # Example
47 // ```rust
48 // let foo_result: Result<T, Foo> = ... ;
49 // let Ok(t) = foo_result;
50 // ```
51 // This code should only compile in modules where the uninhabitedness of Foo is
52 // visible.
53
54 impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
55     /// Checks whether a type is visibly uninhabited from a particular module.
56     /// # Example
57     /// ```rust
58     /// enum Void {}
59     /// mod a {
60     ///     pub mod b {
61     ///         pub struct SecretlyUninhabited {
62     ///             _priv: !,
63     ///         }
64     ///     }
65     /// }
66     ///
67     /// mod c {
68     ///     pub struct AlsoSecretlyUninhabited {
69     ///         _priv: Void,
70     ///     }
71     ///     mod d {
72     ///     }
73     /// }
74     ///
75     /// struct Foo {
76     ///     x: a::b::SecretlyUninhabited,
77     ///     y: c::AlsoSecretlyUninhabited,
78     /// }
79     /// ```
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
82     /// contain `Foo`.
83     ///
84     /// # Example
85     /// ```rust
86     /// let foo_result: Result<T, Foo> = ... ;
87     /// let Ok(t) = foo_result;
88     /// ```
89     /// This code should only compile in modules where the uninhabitedness of Foo is
90     /// visible.
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.
94         // ```
95         // forest.is_empty()
96         // ```
97         self.ty_inhabitedness_forest(ty).contains(self, module)
98     }
99
100     pub fn is_ty_uninhabited_from_all_modules(self, ty: Ty<'tcx>) -> bool {
101         !self.ty_inhabitedness_forest(ty).is_empty()
102     }
103
104     fn ty_inhabitedness_forest(self, ty: Ty<'tcx>) -> DefIdForest {
105         ty.uninhabited_from(self)
106     }
107 }
108
109 impl<'a, 'gcx, 'tcx> AdtDef {
110     /// Calculate the forest of DefIds from which this adt is visibly uninhabited.
111     fn uninhabited_from(
112         &self,
113         tcx: TyCtxt<'a, 'gcx, 'tcx>,
114         substs: SubstsRef<'tcx>) -> DefIdForest
115     {
116         // Non-exhaustive ADTs from other crates are always considered inhabited.
117         if self.is_variant_list_non_exhaustive() && !self.did.is_local() {
118             DefIdForest::empty()
119         } else {
120             DefIdForest::intersection(tcx, self.variants.iter().map(|v| {
121                 v.uninhabited_from(tcx, substs, self.adt_kind())
122             }))
123         }
124     }
125 }
126
127 impl<'a, 'gcx, 'tcx> VariantDef {
128     /// Calculate the forest of DefIds from which this variant is visibly uninhabited.
129     pub fn uninhabited_from(
130         &self,
131         tcx: TyCtxt<'a, 'gcx, 'tcx>,
132         substs: SubstsRef<'tcx>,
133         adt_kind: AdtKind) -> DefIdForest
134     {
135         let is_enum = match adt_kind {
136             // For now, `union`s are never considered uninhabited.
137             // The precise semantics of inhabitedness with respect to unions is currently undecided.
138             AdtKind::Union => return DefIdForest::empty(),
139             AdtKind::Enum => true,
140             AdtKind::Struct => false,
141         };
142         // Non-exhaustive variants from other crates are always considered inhabited.
143         if self.is_field_list_non_exhaustive() && !self.def_id.is_local() {
144             DefIdForest::empty()
145         } else {
146             DefIdForest::union(tcx, self.fields.iter().map(|f| {
147                 f.uninhabited_from(tcx, substs, is_enum)
148             }))
149         }
150     }
151 }
152
153 impl<'a, 'gcx, 'tcx> FieldDef {
154     /// Calculate the forest of DefIds from which this field is visibly uninhabited.
155     fn uninhabited_from(
156         &self,
157         tcx: TyCtxt<'a, 'gcx, 'tcx>,
158         substs: SubstsRef<'tcx>,
159         is_enum: bool,
160     ) -> DefIdForest {
161         let data_uninhabitedness = move || {
162             self.ty(tcx, substs).uninhabited_from(tcx)
163         };
164         // FIXME(canndrew): Currently enum fields are (incorrectly) stored with
165         // Visibility::Invisible so we need to override self.vis if we're
166         // dealing with an enum.
167         if is_enum {
168             data_uninhabitedness()
169         } else {
170             match self.vis {
171                 Visibility::Invisible => DefIdForest::empty(),
172                 Visibility::Restricted(from) => {
173                     let forest = DefIdForest::from_id(from);
174                     let iter = Some(forest).into_iter().chain(Some(data_uninhabitedness()));
175                     DefIdForest::intersection(tcx, iter)
176                 },
177                 Visibility::Public => data_uninhabitedness(),
178             }
179         }
180     }
181 }
182
183 impl<'a, 'gcx, 'tcx> TyS<'tcx> {
184     /// Calculate the forest of DefIds from which this type is visibly uninhabited.
185     fn uninhabited_from(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> DefIdForest
186     {
187         match self.sty {
188             Adt(def, substs) => def.uninhabited_from(tcx, substs),
189
190             Never => DefIdForest::full(tcx),
191
192             Tuple(ref tys) => {
193                 DefIdForest::union(tcx, tys.iter().map(|ty| {
194                     ty.expect_ty().uninhabited_from(tcx)
195                 }))
196             }
197
198             Array(ty, len) => match len.assert_usize(tcx) {
199                 // If the array is definitely non-empty, it's uninhabited if
200                 // the type of its elements is uninhabited.
201                 Some(n) if n != 0 => ty.uninhabited_from(tcx),
202                 _ => DefIdForest::empty()
203             },
204
205             // References to uninitialised memory is valid for any type, including
206             // uninhabited types, in unsafe code, so we treat all references as
207             // inhabited.
208             // The precise semantics of inhabitedness with respect to references is currently
209             // undecided.
210             Ref(..) => DefIdForest::empty(),
211
212             _ => DefIdForest::empty(),
213         }
214     }
215 }