diff options
| author | Eduard Burtescu <edy.burt@gmail.com> | 2016-08-04 19:57:57 +0300 |
|---|---|---|
| committer | Eduard Burtescu <edy.burt@gmail.com> | 2016-08-04 19:57:57 +0300 |
| commit | fbabe616413d34c1ab68668de34de4322d963b93 (patch) | |
| tree | 405df1f63b219cdb7015409e4ce26bab3e764404 | |
| parent | e804a3cf256106c097d44f6e0212cd183122da07 (diff) | |
| download | rust-fbabe616413d34c1ab68668de34de4322d963b93.tar.gz rust-fbabe616413d34c1ab68668de34de4322d963b93.zip | |
rustc_trans: don't Assert(Overflow(Neg)) when overflow checks are off.
| -rw-r--r-- | src/librustc_trans/mir/block.rs | 18 | ||||
| -rw-r--r-- | src/test/run-pass/mir_overflow_off.rs | 26 |
2 files changed, 43 insertions, 1 deletions
diff --git a/src/librustc_trans/mir/block.rs b/src/librustc_trans/mir/block.rs index 2d1769b8637..9bfdb511c62 100644 --- a/src/librustc_trans/mir/block.rs +++ b/src/librustc_trans/mir/block.rs @@ -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) { diff --git a/src/test/run-pass/mir_overflow_off.rs b/src/test/run-pass/mir_overflow_off.rs new file mode 100644 index 00000000000..04ac606a8a9 --- /dev/null +++ b/src/test/run-pass/mir_overflow_off.rs @@ -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); +} |
