rustc_trans: don't Assert(Overflow(Neg)) when overflow checks are off.

This commit is contained in:
Eduard Burtescu 2016-08-04 19:57:57 +03:00
parent e804a3cf25
commit fbabe61641
2 changed files with 43 additions and 1 deletions
src
librustc_trans/mir
test/run-pass

@ -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) {

@ -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);
}