Skip to content

Commit

Permalink
Auto merge of rust-lang#35300 - eddyb:mir-neg-overflow, r=arielb1
Browse files Browse the repository at this point in the history
rustc_trans: don't Assert(Overflow(Neg)) when overflow checks are off.

Generic functions using `Neg` on primitive types would panic even in release mode, with MIR trans.
The solution is a bit hacky, as I'm checking the message, since there's no dedicated `CheckedUnOp`.

Blocks Servo rustup ([failure #1](http://build.servo.org/builders/linux-rel/builds/2477/steps/test_3/logs/stdio), [failure #2](http://build.servo.org/builders/mac-rel-css/builds/2364/steps/test/logs/stdio)) - this should be the last hurdle, it affects only one test.
  • Loading branch information
bors authored Aug 4, 2016
2 parents e804a3c + fbabe61 commit 545a3a9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/librustc_trans/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,23 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {

mir::TerminatorKind::Assert { ref cond, expected, ref msg, target, cleanup } => {
let cond = self.trans_operand(&bcx, cond).immediate();
let const_cond = common::const_to_opt_uint(cond).map(|c| c == 1);
let mut const_cond = common::const_to_opt_uint(cond).map(|c| c == 1);

// This case can currently arise only from functions marked
// with #[rustc_inherit_overflow_checks] and inlined from
// another crate (mostly core::num generic/#[inline] fns),
// while the current crate doesn't use overflow checks.
// NOTE: Unlike binops, negation doesn't have its own
// checked operation, just a comparison with the minimum
// value, so we have to check for the assert message.
if !bcx.ccx().check_overflow() {
use rustc_const_math::ConstMathErr::Overflow;
use rustc_const_math::Op::Neg;

if let mir::AssertMessage::Math(Overflow(Neg)) = *msg {
const_cond = Some(expected);
}
}

// Don't translate the panic block if success if known.
if const_cond == Some(expected) {
Expand Down
26 changes: 26 additions & 0 deletions src/test/run-pass/mir_overflow_off.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: -Z force-overflow-checks=off -Z orbit

// Test that with MIR trans, overflow checks can be
// turned off, even when they're from core::ops::*.

use std::ops::*;

fn main() {
assert_eq!(i8::neg(-0x80), -0x80);

assert_eq!(u8::add(0xff, 1), 0_u8);
assert_eq!(u8::sub(0, 1), 0xff_u8);
assert_eq!(u8::mul(0xff, 2), 0xfe_u8);
assert_eq!(u8::shl(1, 9), 2_u8);
assert_eq!(u8::shr(2, 9), 1_u8);
}

0 comments on commit 545a3a9

Please sign in to comment.