Check for uninhabitedness instead of never

This commit is contained in:
varkor 2018-09-11 12:41:56 +01:00
parent 1137d29d5e
commit 62b359094f
7 changed files with 26 additions and 60 deletions

View File

@ -415,8 +415,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
args: I) -> CFGIndex {
let func_or_rcvr_exit = self.expr(func_or_rcvr, pred);
let ret = self.straightline(call_expr, func_or_rcvr_exit, args);
// FIXME(canndrew): This is_never should probably be an is_uninhabited.
if self.tables.expr_ty(call_expr).is_never() {
if self.tables.expr_ty(call_expr).conservative_is_uninhabited() {
self.add_unreachable_node()
} else {
ret

View File

@ -1197,8 +1197,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
}
hir::ExprKind::Call(ref f, ref args) => {
// FIXME(canndrew): This is_never should really be an is_uninhabited
let succ = if self.tables.expr_ty(expr).is_never() {
let succ = if self.tables.expr_ty(expr).conservative_is_uninhabited() {
self.s.exit_ln
} else {
succ
@ -1208,8 +1207,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
}
hir::ExprKind::MethodCall(.., ref args) => {
// FIXME(canndrew): This is_never should really be an is_uninhabited
let succ = if self.tables.expr_ty(expr).is_never() {
let succ = if self.tables.expr_ty(expr).conservative_is_uninhabited() {
self.s.exit_ln
} else {
succ

View File

@ -1543,6 +1543,17 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
}
}
pub fn conservative_is_uninhabited(&self) -> bool {
// Checks whether a type is definitely uninhabited. This is
// conservative: for some types that are uninhabited we return `false`,
// but we only return `true` for types that are definitely uninhabited.
match self.sty {
ty::Never => true,
ty::Adt(def, _) => def.variants.is_empty(),
_ => false
}
}
pub fn is_primitive(&self) -> bool {
match self.sty {
Bool | Char | Int(_) | Uint(_) | Float(_) => true,

View File

@ -1546,8 +1546,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
}
}
None => {
// FIXME(canndrew): This is_never should probably be an is_uninhabited
if !sig.output().is_never() {
if !sig.output().conservative_is_uninhabited() {
span_mirbug!(self, term, "call to converging function {:?} w/o dest", sig);
}
}

View File

@ -275,8 +275,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
exit_block.unit()
}
ExprKind::Call { ty, fun, args, from_hir_call } => {
// FIXME(canndrew): This is_never should probably be an is_uninhabited
let diverges = expr.ty.is_never();
let diverges = expr.ty.conservative_is_uninhabited();
let intrinsic = match ty.sty {
ty::FnDef(def_id, _) => {
let f = ty.fn_sig(this.hir.tcx());
@ -332,7 +331,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
func: fun,
args,
cleanup: Some(cleanup),
destination: if diverges {
destination: if expr.ty.conservative_is_uninhabited() {
None
} else {
Some((destination.clone(), success))

View File

@ -230,7 +230,7 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
let scrutinee_is_uninhabited = if self.tcx.features().exhaustive_patterns {
self.tcx.is_ty_uninhabited_from(module, pat_ty)
} else {
self.conservative_is_uninhabited(pat_ty)
pat_ty.conservative_is_uninhabited()
};
if !scrutinee_is_uninhabited {
// We know the type is inhabited, so this must be wrong
@ -258,15 +258,6 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
})
}
fn conservative_is_uninhabited(&self, scrutinee_ty: Ty<'tcx>) -> bool {
// "rustc-1.0-style" uncontentious uninhabitableness check
match scrutinee_ty.sty {
ty::Never => true,
ty::Adt(def, _) => def.variants.is_empty(),
_ => false
}
}
fn check_irrefutable(&self, pat: &'tcx Pat, origin: &str) {
let module = self.tcx.hir().get_module_parent(pat.id);
MatchCheckCtxt::create_and_enter(self.tcx, self.param_env, module, |ref mut cx| {

View File

@ -1,55 +1,24 @@
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// NOTE Instantiating an empty enum is UB. This test may break in the future.
// LLDB can't handle zero-sized values
// LLDB can't handle zero-sized values.
// ignore-lldb
// Require LLVM with DW_TAG_variant_part and a gdb that can read it.
// gdb 8.2.0 crashes on this test case, see
// https://sourceware.org/bugzilla/show_bug.cgi?id=23626
// This will be fixed in the next release, which will be >= 8.2.1.
// min-system-llvm-version: 7.0
// min-gdb-version: 8.2.1
// compile-flags:-g
// gdb-command:run
// gdb-command:print first
// gdbr-check:$1 = nil_enum::ANilEnum {<No data fields>}
// gdb-command:print second
// gdbr-check:$2 = nil_enum::AnotherNilEnum {<No data fields>}
// gdb-command:print *first
// gdbg-check:$1 = {<No data fields>}
// gdbr-check:$1 = <error reading variable>
#![allow(unused_variables)]
#![feature(omit_gdb_pretty_printer_section)]
#![feature(maybe_uninit)]
#![omit_gdb_pretty_printer_section]
use std::mem::MaybeUninit;
enum Void {}
enum ANilEnum {}
enum AnotherNilEnum {}
// This test relies on gdbg printing the string "{<No data fields>}" for empty
// structs (which may change some time)
// The error from gdbr is expected since nil enums are not supposed to exist.
fn main() {
unsafe {
let first: ANilEnum = MaybeUninit::uninitialized().into_inner();
let second: AnotherNilEnum = MaybeUninit::uninitialized().into_inner();
let first: *const Void = 1 as *const _;
zzz(); // #break
}
zzz(); // #break
}
fn zzz() {()}
fn zzz() {}