Skip to content

Commit

Permalink
Auto merge of #961 - rust-lang:exact_div_reuse, r=RalfJung
Browse files Browse the repository at this point in the history
Use the upstream `exact_div` implementation

introduced in rust-lang/rust#63810
  • Loading branch information
bors committed Nov 3, 2019
2 parents 22d0546 + fcf0f88 commit 4f71715
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 20 deletions.
2 changes: 1 addition & 1 deletion rust-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
aa69777ea2902208b24b3fd77767d577ceaf6386
6c1b220fd747bf244f04b380e4d4ae005068f706
23 changes: 6 additions & 17 deletions src/shims/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,23 +313,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.write_scalar(Scalar::from_f64(res), dest)?;
}

"exact_div" => {
// Performs an exact division, resulting in undefined behavior where
// `x % y != 0` or `y == 0` or `x == T::min_value() && y == -1`
let a = this.read_immediate(args[0])?;
let b = this.read_immediate(args[1])?;
// check x % y != 0
if this.overflowing_binary_op(mir::BinOp::Rem, a, b)?.0.to_bits(dest.layout.size)? != 0 {
// Check if `b` is -1, which is the "min_value / -1" case.
let minus1 = Scalar::from_int(-1, dest.layout.size);
return Err(if b.to_scalar().unwrap() == minus1 {
err_ub_format!("exact_div: result of dividing MIN by -1 cannot be represented")
} else {
err_ub_format!("exact_div: {:?} cannot be divided by {:?} without remainder", *a, *b)
}.into());
}
this.binop_ignore_overflow(mir::BinOp::Div, a, b, dest)?;
},
"exact_div" =>
this.exact_div(
this.read_immediate(args[0])?,
this.read_immediate(args[1])?,
dest,
)?,

"forget" => {}

Expand Down
2 changes: 1 addition & 1 deletion tests/compile-fail/exact_div2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![feature(core_intrinsics)]
fn main() {
// divison with a remainder
unsafe { std::intrinsics::exact_div(2u16, 3); } //~ ERROR Scalar(0x0002) cannot be divided by Scalar(0x0003) without remainder
unsafe { std::intrinsics::exact_div(2u16, 3); } //~ ERROR 2 cannot be divided by 3 without remainder
}
2 changes: 1 addition & 1 deletion tests/compile-fail/exact_div3.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![feature(core_intrinsics)]
fn main() {
// signed divison with a remainder
unsafe { std::intrinsics::exact_div(-19i8, 2); } //~ ERROR Scalar(0xed) cannot be divided by Scalar(0x02) without remainder
unsafe { std::intrinsics::exact_div(-19i8, 2); } //~ ERROR 237 cannot be divided by 2 without remainder
}

0 comments on commit 4f71715

Please sign in to comment.