Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the upstream exact_div implementation #961

Merged
merged 4 commits into from
Nov 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oli-obk I am not sure if this is the desired printing here... we are printing a negative number as a positive one.

}