]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_middle/src/ty/inhabitedness/mod.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / compiler / rustc_middle / src / ty / inhabitedness / mod.rs
1 pub use self::def_id_forest::DefIdForest;
2
3 use crate::ty;
4 use crate::ty::context::TyCtxt;
5 use crate::ty::{AdtDef, FieldDef, Ty, VariantDef};
6 use crate::ty::{AdtKind, Visibility};
7 use crate::ty::{DefId, SubstsRef};
8
9 use rustc_type_ir::sty::TyKind::*;
10
11 mod def_id_forest;
12
13 // The methods in this module calculate `DefIdForest`s of modules in which an
14 // `AdtDef`/`VariantDef`/`FieldDef` is visibly uninhabited.
15 //
16 // # Example
17 // ```rust
18 // enum Void {}
19 // mod a {
20 // pub mod b {
21 // pub struct SecretlyUninhabited {
22 // _priv: !,
23 // }
24 // }
25 // }
26 //
27 // mod c {
28 // pub struct AlsoSecretlyUninhabited {
29 // _priv: Void,
30 // }
31 // mod d {
32 // }
33 // }
34 //
35 // struct Foo {
36 // x: a::b::SecretlyUninhabited,
37 // y: c::AlsoSecretlyUninhabited,
38 // }
39 // ```
40 // In this code, the type `Foo` will only be visibly uninhabited inside the
41 // modules `b`, `c` and `d`. Calling `uninhabited_from` on `Foo` or its `AdtDef` will
42 // return the forest of modules {`b`, `c`->`d`} (represented in a `DefIdForest` by the
43 // set {`b`, `c`}).
44 //
45 // We need this information for pattern-matching on `Foo` or types that contain
46 // `Foo`.
47 //
48 // # Example
49 // ```rust
50 // let foo_result: Result<T, Foo> = ... ;
51 // let Ok(t) = foo_result;
52 // ```
53 // This code should only compile in modules where the uninhabitedness of `Foo` is
54 // visible.
55
56 impl<'tcx> TyCtxt<'tcx> {
57 /// Checks whether a type is visibly uninhabited from a particular module.
58 ///
59 /// # Example
60 /// ```
61 /// #![feature(never_type)]
62 /// # fn main() {}
63 /// enum Void {}
64 /// mod a {
65 /// pub mod b {
66 /// pub struct SecretlyUninhabited {
67 /// _priv: !,
68 /// }
69 /// }
70 /// }
71 ///
72 /// mod c {
73 /// use super::Void;
74 /// pub struct AlsoSecretlyUninhabited {
75 /// _priv: Void,
76 /// }
77 /// mod d {
78 /// }
79 /// }
80 ///
81 /// struct Foo {
82 /// x: a::b::SecretlyUninhabited,
83 /// y: c::AlsoSecretlyUninhabited,
84 /// }
85 /// ```
86 /// In this code, the type `Foo` will only be visibly uninhabited inside the
87 /// modules b, c and d. This effects pattern-matching on `Foo` or types that
88 /// contain `Foo`.
89 ///
90 /// # Example
91 /// ```ignore (illustrative)
92 /// let foo_result: Result<T, Foo> = ... ;
93 /// let Ok(t) = foo_result;
94 /// ```
95 /// This code should only compile in modules where the uninhabitedness of Foo is
96 /// visible.
97 pub fn is_ty_uninhabited_from(
98 self,
99 module: DefId,
100 ty: Ty<'tcx>,
101 param_env: ty::ParamEnv<'tcx>,
102 ) -> bool {
103 // To check whether this type is uninhabited at all (not just from the
104 // given node), you could check whether the forest is empty.
105 // ```
106 // forest.is_empty()
107 // ```
108 ty.uninhabited_from(self, param_env).contains(self, module)
109 }
110 }
111
112 impl<'tcx> AdtDef<'tcx> {
113 /// Calculates the forest of `DefId`s from which this ADT is visibly uninhabited.
114 fn uninhabited_from(
115 self,
116 tcx: TyCtxt<'tcx>,
117 substs: SubstsRef<'tcx>,
118 param_env: ty::ParamEnv<'tcx>,
119 ) -> DefIdForest<'tcx> {
120 // Non-exhaustive ADTs from other crates are always considered inhabited.
121 if self.is_variant_list_non_exhaustive() && !self.did().is_local() {
122 DefIdForest::empty()
123 } else {
124 DefIdForest::intersection(
125 tcx,
126 self.variants()
127 .iter()
128 .map(|v| v.uninhabited_from(tcx, substs, self.adt_kind(), param_env)),
129 )
130 }
131 }
132 }
133
134 impl<'tcx> VariantDef {
135 /// Calculates the forest of `DefId`s from which this variant is visibly uninhabited.
136 pub fn uninhabited_from(
137 &self,
138 tcx: TyCtxt<'tcx>,
139 substs: SubstsRef<'tcx>,
140 adt_kind: AdtKind,
141 param_env: ty::ParamEnv<'tcx>,
142 ) -> DefIdForest<'tcx> {
143 let is_enum = match adt_kind {
144 // For now, `union`s are never considered uninhabited.
145 // The precise semantics of inhabitedness with respect to unions is currently undecided.
146 AdtKind::Union => return DefIdForest::empty(),
147 AdtKind::Enum => true,
148 AdtKind::Struct => false,
149 };
150 // Non-exhaustive variants from other crates are always considered inhabited.
151 if self.is_field_list_non_exhaustive() && !self.def_id.is_local() {
152 DefIdForest::empty()
153 } else {
154 DefIdForest::union(
155 tcx,
156 self.fields.iter().map(|f| f.uninhabited_from(tcx, substs, is_enum, param_env)),
157 )
158 }
159 }
160 }
161
162 impl<'tcx> FieldDef {
163 /// Calculates the forest of `DefId`s from which this field is visibly uninhabited.
164 fn uninhabited_from(
165 &self,
166 tcx: TyCtxt<'tcx>,
167 substs: SubstsRef<'tcx>,
168 is_enum: bool,
169 param_env: ty::ParamEnv<'tcx>,
170 ) -> DefIdForest<'tcx> {
171 let data_uninhabitedness = move || self.ty(tcx, substs).uninhabited_from(tcx, param_env);
172 // FIXME(canndrew): Currently enum fields are (incorrectly) stored with
173 // `Visibility::Invisible` so we need to override `self.vis` if we're
174 // dealing with an enum.
175 if is_enum {
176 data_uninhabitedness()
177 } else {
178 match self.vis {
179 Visibility::Invisible => DefIdForest::empty(),
180 Visibility::Restricted(from) => {
181 let forest = DefIdForest::from_id(from);
182 let iter = Some(forest).into_iter().chain(Some(data_uninhabitedness()));
183 DefIdForest::intersection(tcx, iter)
184 }
185 Visibility::Public => data_uninhabitedness(),
186 }
187 }
188 }
189 }
190
191 impl<'tcx> Ty<'tcx> {
192 /// Calculates the forest of `DefId`s from which this type is visibly uninhabited.
193 fn uninhabited_from(
194 self,
195 tcx: TyCtxt<'tcx>,
196 param_env: ty::ParamEnv<'tcx>,
197 ) -> DefIdForest<'tcx> {
198 tcx.type_uninhabited_from(param_env.and(self))
199 }
200 }
201
202 // Query provider for `type_uninhabited_from`.
203 pub(crate) fn type_uninhabited_from<'tcx>(
204 tcx: TyCtxt<'tcx>,
205 key: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
206 ) -> DefIdForest<'tcx> {
207 let ty = key.value;
208 let param_env = key.param_env;
209 match *ty.kind() {
210 Adt(def, substs) => def.uninhabited_from(tcx, substs, param_env),
211
212 Never => DefIdForest::full(),
213
214 Tuple(ref tys) => {
215 DefIdForest::union(tcx, tys.iter().map(|ty| ty.uninhabited_from(tcx, param_env)))
216 }
217
218 Array(ty, len) => match len.try_eval_usize(tcx, param_env) {
219 Some(0) | None => DefIdForest::empty(),
220 // If the array is definitely non-empty, it's uninhabited if
221 // the type of its elements is uninhabited.
222 Some(1..) => ty.uninhabited_from(tcx, param_env),
223 },
224
225 // References to uninitialised memory are valid for any type, including
226 // uninhabited types, in unsafe code, so we treat all references as
227 // inhabited.
228 // The precise semantics of inhabitedness with respect to references is currently
229 // undecided.
230 Ref(..) => DefIdForest::empty(),
231
232 _ => DefIdForest::empty(),
233 }
234 }