Skip to content

Commit 682f0f8

Browse files
committed
Consider references and unions potentially inhabited during privacy-respecting inhabitedness checks
1 parent 3dde9e1 commit 682f0f8

File tree

1 file changed

+20
-24
lines changed
  • src/librustc/ty/inhabitedness

1 file changed

+20
-24
lines changed

src/librustc/ty/inhabitedness/mod.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,15 @@ impl<'a, 'gcx, 'tcx> VariantDef {
167167
substs: &'tcx Substs<'tcx>,
168168
adt_kind: AdtKind) -> DefIdForest
169169
{
170-
match adt_kind {
171-
AdtKind::Union => {
172-
DefIdForest::intersection(tcx, self.fields.iter().map(|f| {
173-
f.uninhabited_from(visited, tcx, substs, false)
174-
}))
175-
},
176-
AdtKind::Struct => {
177-
DefIdForest::union(tcx, self.fields.iter().map(|f| {
178-
f.uninhabited_from(visited, tcx, substs, false)
179-
}))
180-
},
181-
AdtKind::Enum => {
182-
DefIdForest::union(tcx, self.fields.iter().map(|f| {
183-
f.uninhabited_from(visited, tcx, substs, true)
184-
}))
185-
},
186-
}
170+
let is_enum = match adt_kind {
171+
// For now, `union`s are never considered uninhabited.
172+
AdtKind::Union => return DefIdForest::empty(),
173+
AdtKind::Enum => true,
174+
AdtKind::Struct => false,
175+
};
176+
DefIdForest::union(tcx, self.fields.iter().map(|f| {
177+
f.uninhabited_from(visited, tcx, substs, is_enum)
178+
}))
187179
}
188180
}
189181

@@ -194,8 +186,8 @@ impl<'a, 'gcx, 'tcx> FieldDef {
194186
visited: &mut FxHashMap<DefId, FxHashSet<&'tcx Substs<'tcx>>>,
195187
tcx: TyCtxt<'a, 'gcx, 'tcx>,
196188
substs: &'tcx Substs<'tcx>,
197-
is_enum: bool) -> DefIdForest
198-
{
189+
is_enum: bool,
190+
) -> DefIdForest {
199191
let mut data_uninhabitedness = move || {
200192
self.ty(tcx, substs).uninhabited_from(visited, tcx)
201193
};
@@ -253,14 +245,16 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
253245
let substs_set = visited.get_mut(&def.did).unwrap();
254246
substs_set.remove(substs);
255247
ret
256-
},
248+
}
257249

258250
Never => DefIdForest::full(tcx),
251+
259252
Tuple(ref tys) => {
260253
DefIdForest::union(tcx, tys.iter().map(|ty| {
261254
ty.uninhabited_from(visited, tcx)
262255
}))
263-
},
256+
}
257+
264258
Array(ty, len) => {
265259
match len.assert_usize(tcx) {
266260
// If the array is definitely non-empty, it's uninhabited if
@@ -269,9 +263,11 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
269263
_ => DefIdForest::empty()
270264
}
271265
}
272-
Ref(_, ty, _) => {
273-
ty.uninhabited_from(visited, tcx)
274-
}
266+
267+
// References to uninitialised memory is valid for any type, including
268+
// uninhabited types, in unsafe code, so we treat all references as
269+
// inhabited.
270+
Ref(..) => DefIdForest::empty(),
275271

276272
_ => DefIdForest::empty(),
277273
}

0 commit comments

Comments
 (0)