Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Aug 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}