Skip to content

Commit 4f71715

Browse files
committed
Auto merge of #961 - rust-lang:exact_div_reuse, r=RalfJung
Use the upstream `exact_div` implementation introduced in rust-lang/rust#63810
2 parents 22d0546 + fcf0f88 commit 4f71715

File tree

4 files changed

+9
-20
lines changed

4 files changed

+9
-20
lines changed

rust-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
aa69777ea2902208b24b3fd77767d577ceaf6386
1+
6c1b220fd747bf244f04b380e4d4ae005068f706

src/shims/intrinsics.rs

+6-17
Original file line numberDiff line numberDiff line change
@@ -313,23 +313,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
313313
this.write_scalar(Scalar::from_f64(res), dest)?;
314314
}
315315

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

334323
"forget" => {}
335324

tests/compile-fail/exact_div2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![feature(core_intrinsics)]
22
fn main() {
33
// divison with a remainder
4-
unsafe { std::intrinsics::exact_div(2u16, 3); } //~ ERROR Scalar(0x0002) cannot be divided by Scalar(0x0003) without remainder
4+
unsafe { std::intrinsics::exact_div(2u16, 3); } //~ ERROR 2 cannot be divided by 3 without remainder
55
}

tests/compile-fail/exact_div3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![feature(core_intrinsics)]
22
fn main() {
33
// signed divison with a remainder
4-
unsafe { std::intrinsics::exact_div(-19i8, 2); } //~ ERROR Scalar(0xed) cannot be divided by Scalar(0x02) without remainder
4+
unsafe { std::intrinsics::exact_div(-19i8, 2); } //~ ERROR 237 cannot be divided by 2 without remainder
55
}

0 commit comments

Comments
 (0)