Skip to content

Commit

Permalink
get rid of ad-hoc inhabitedness test
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Sep 22, 2017
1 parent 728e664 commit 1ad9709
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
6 changes: 1 addition & 5 deletions src/librustc_mir/interpret/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1818,7 +1818,7 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
let val = match val {
PrimVal::Bytes(0) => false,
PrimVal::Bytes(1) => true,
// TODO: This seems a little overeager, should reading at bool type already be UB?
// TODO: This seems a little overeager, should reading at bool type already be insta-UB?
_ => return err!(InvalidBool),
};
PrimVal::from_bool(val)
Expand Down Expand Up @@ -2237,10 +2237,6 @@ impl IntegerExt for layout::Integer {
}
}

pub fn is_inhabited<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
ty.uninhabited_from(&mut HashMap::default(), tcx).is_empty()
}

/// FIXME: expose trans::monomorphize::resolve_closure
pub fn resolve_closure<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
Expand Down
3 changes: 0 additions & 3 deletions src/librustc_mir/interpret/terminator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,6 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
_ => return err!(Unreachable),
};
let ty = sig.output();
if !eval_context::is_inhabited(self.tcx, ty) {
return err!(Unreachable);
}
let layout = self.type_layout(ty)?;
M::call_intrinsic(self, instance, args, ret, ty, layout, target)?;
self.dump_local(ret);
Expand Down
3 changes: 3 additions & 0 deletions tests/compile-fail/never_say_never.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// This should fail even without validation
// compile-flags: -Zmir-emit-validate=0

#![feature(never_type)]
#![allow(unreachable_code)]

Expand Down
3 changes: 3 additions & 0 deletions tests/compile-fail/never_transmute_humans.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// This should fail even without validation
// compile-flags: -Zmir-emit-validate=0

#![feature(never_type)]
#![allow(unreachable_code)]
#![allow(unused_variables)]
Expand Down
7 changes: 5 additions & 2 deletions tests/compile-fail/never_transmute_void.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// This should fail even without validation
// compile-flags: -Zmir-emit-validate=0

#![feature(never_type)]
#![allow(unreachable_code)]
#![allow(unused_variables)]

enum Void {}

fn f(v: Void) -> ! {
match v {}
match v {} //~ ERROR entered unreachable code
}

fn main() {
let v: Void = unsafe {
std::mem::transmute::<(), Void>(()) //~ ERROR entered unreachable code
std::mem::transmute::<(), Void>(())
};
f(v);
}

0 comments on commit 1ad9709

Please sign in to comment.