1
Fork 0

Rollup merge of #112168 - scottmcm:lower-div-rem-unchecked-to-mir, r=oli-obk

Lower `unchecked_div`/`_rem` to MIR's `BinOp::Div`/`Rem`

As described in <https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/enum.BinOp.html#variant.Div>, the ordinary `BinOp`s for these are already UB for division by zero ([or overflow](https://llvm.org/docs/LangRef.html#sdiv-instruction), [demo](https://rust.godbolt.org/z/71e7P7Exh)), as MIR building is responsible for inserting code to panic for those cases regardless of whether the overflow checks are enabled.

So we can lower these in the same arm that lowers `wrapping_add` to MIR `BinOp::Add` and such, as all these cases turn into ordinary `Rvalue::BinaryOp`s.
This commit is contained in:
Michael Goulet 2023-06-02 16:02:06 -07:00 committed by GitHub
commit 5460f92a0f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 134 additions and 81 deletions

View file

@ -1273,13 +1273,18 @@ pub enum BinOp {
Mul,
/// The `/` operator (division)
///
/// Division by zero is UB, because the compiler should have inserted checks
/// prior to this.
/// For integer types, division by zero is UB, as is `MIN / -1` for signed.
/// The compiler should have inserted checks prior to this.
///
/// Floating-point division by zero is safe, and does not need guards.
Div,
/// The `%` operator (modulus)
///
/// Using zero as the modulus (second operand) is UB, because the compiler
/// should have inserted checks prior to this.
/// For integer types, using zero as the modulus (second operand) is UB,
/// as is `MIN % -1` for signed.
/// The compiler should have inserted checks prior to this.
///
/// Floating-point remainder by zero is safe, and does not need guards.
Rem,
/// The `^` operator (bitwise xor)
BitXor,