error on failed assumptions

This commit is contained in:
Oliver Schneider 2016-09-13 13:08:57 +02:00
parent c57233abca
commit 23eb8a5cf2
No known key found for this signature in database
GPG key ID: 56D6EEA0FC67AC46
3 changed files with 18 additions and 2 deletions

View file

@ -43,6 +43,7 @@ pub enum EvalError<'tcx> {
CalledClosureAsFunction,
VtableForArgumentlessMethod,
ModifiedConstantMemory,
AssumptionNotHeld,
}
pub type EvalResult<'tcx, T> = Result<T, EvalError<'tcx>>;
@ -97,6 +98,8 @@ impl<'tcx> Error for EvalError<'tcx> {
"tried to call a vtable function without arguments",
EvalError::ModifiedConstantMemory =>
"tried to modify constant memory",
EvalError::AssumptionNotHeld =>
"`assume` argument was false"
}
}

View file

@ -284,8 +284,11 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
"sub_with_overflow" => self.intrinsic_with_overflow(mir::BinOp::Sub, &args[0], &args[1], dest, dest_layout)?,
"mul_with_overflow" => self.intrinsic_with_overflow(mir::BinOp::Mul, &args[0], &args[1], dest, dest_layout)?,
// FIXME: turn into an assertion to catch wrong `assume` that would cause UB in llvm
"assume" => {}
"assume" => {
if !self.memory.read_bool(args_ptrs[0])? {
return Err(EvalError::AssumptionNotHeld);
}
}
"copy_nonoverlapping" => {
let elem_ty = substs.type_at(0);

View file

@ -0,0 +1,10 @@
#![feature(core_intrinsics)]
fn main() {
let x = 5;
unsafe {
std::intrinsics::assume(x < 10);
std::intrinsics::assume(x > 1);
std::intrinsics::assume(x > 42); //~ ERROR: `assume` argument was false
}
}