Skip to content

Commit e650081

Browse files
committed
Make uninhabitedness checking more intelligent
1 parent 7892257 commit e650081

File tree

7 files changed

+36
-10
lines changed

7 files changed

+36
-10
lines changed

src/librustc/cfg/construct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
415415
args: I) -> CFGIndex {
416416
let func_or_rcvr_exit = self.expr(func_or_rcvr, pred);
417417
let ret = self.straightline(call_expr, func_or_rcvr_exit, args);
418-
if self.tables.expr_ty(call_expr).conservative_is_uninhabited() {
418+
if self.tables.expr_ty(call_expr).conservative_is_uninhabited(self.tcx) {
419419
self.add_unreachable_node()
420420
} else {
421421
ret

src/librustc/middle/liveness.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
11971197
}
11981198

11991199
hir::ExprKind::Call(ref f, ref args) => {
1200-
let succ = if self.tables.expr_ty(expr).conservative_is_uninhabited() {
1200+
let succ = if self.tables.expr_ty(expr).conservative_is_uninhabited(self.ir.tcx) {
12011201
self.s.exit_ln
12021202
} else {
12031203
succ
@@ -1207,7 +1207,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
12071207
}
12081208

12091209
hir::ExprKind::MethodCall(.., ref args) => {
1210-
let succ = if self.tables.expr_ty(expr).conservative_is_uninhabited() {
1210+
let succ = if self.tables.expr_ty(expr).conservative_is_uninhabited(self.ir.tcx) {
12111211
self.s.exit_ln
12121212
} else {
12131213
succ

src/librustc/ty/sty.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,14 +1504,40 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
15041504
}
15051505
}
15061506

1507-
pub fn conservative_is_uninhabited(&self) -> bool {
1507+
pub fn conservative_is_uninhabited(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> bool {
15081508
// Checks whether a type is definitely uninhabited. This is
15091509
// conservative: for some types that are uninhabited we return `false`,
15101510
// but we only return `true` for types that are definitely uninhabited.
15111511
match self.sty {
15121512
ty::Never => true,
1513-
ty::Adt(def, _) => def.variants.is_empty(),
1514-
_ => false
1513+
ty::Adt(def, _) => {
1514+
// Any ADT is uninhabited if:
1515+
// (a) It has no variants (i.e. an empty `enum`);
1516+
// (b) Each of its variants (a single one in the case of a `struct`) has at least
1517+
// one uninhabited field.
1518+
def.variants.iter().all(|var| {
1519+
var.fields.iter().any(|field| {
1520+
tcx.type_of(field.did).conservative_is_uninhabited(tcx)
1521+
})
1522+
})
1523+
}
1524+
ty::Tuple(tys) => tys.iter().any(|ty| ty.conservative_is_uninhabited(tcx)),
1525+
ty::Array(ty, len) => {
1526+
match len.val.try_to_scalar() {
1527+
// If the array is definitely non-empty, it's uninhabited if
1528+
// the type of its elements is uninhabited.
1529+
Some(n) if !n.is_null() => ty.conservative_is_uninhabited(tcx),
1530+
_ => false
1531+
}
1532+
}
1533+
ty::Ref(..) => {
1534+
// Though references to uninhabited types are trivially uninhabited
1535+
// theoretically, null references are permitted in unsafe code (as
1536+
// long as the value is not dereferenced), so we treat all references
1537+
// as inhabited.
1538+
false
1539+
}
1540+
_ => false,
15151541
}
15161542
}
15171543

src/librustc_codegen_llvm/mir/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ impl FunctionCx<'a, 'll, 'tcx> {
463463
// we can do what we like. Here, we declare that transmuting
464464
// into an uninhabited type is impossible, so anything following
465465
// it must be unreachable.
466-
assert!(sig.output().conservative_is_uninhabited());
466+
assert!(sig.output().conservative_is_uninhabited(bx.tcx()));
467467
bx.unreachable();
468468
}
469469
return;

src/librustc_mir/borrow_check/nll/type_check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
14191419
}
14201420
}
14211421
None => {
1422-
if !sig.output().conservative_is_uninhabited() {
1422+
if !sig.output().conservative_is_uninhabited(self.tcx()) {
14231423
span_mirbug!(self, term, "call to converging function {:?} w/o dest", sig);
14241424
}
14251425
}

src/librustc_mir/build/expr/into.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
331331
func: fun,
332332
args,
333333
cleanup: Some(cleanup),
334-
destination: if expr.ty.conservative_is_uninhabited() {
334+
destination: if expr.ty.conservative_is_uninhabited(this.hir.tcx()) {
335335
None
336336
} else {
337337
Some((destination.clone(), success))

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
229229
let scrutinee_is_uninhabited = if self.tcx.features().exhaustive_patterns {
230230
self.tcx.is_ty_uninhabited_from(module, pat_ty)
231231
} else {
232-
pat_ty.conservative_is_uninhabited()
232+
pat_ty.conservative_is_uninhabited(self.tcx)
233233
};
234234
if !scrutinee_is_uninhabited {
235235
// We know the type is inhabited, so this must be wrong

0 commit comments

Comments
 (0)