Skip to content

Commit

Permalink
trans: use the same messages for both MIR and old arithmetic checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Jun 5, 2016
1 parent 1447fbf commit b8c5053
Show file tree
Hide file tree
Showing 17 changed files with 38 additions and 27 deletions.
15 changes: 8 additions & 7 deletions src/librustc_trans/base.rs
Expand Up @@ -769,12 +769,12 @@ pub fn fail_if_zero_or_overflows<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
rhs: ValueRef,
rhs_t: Ty<'tcx>)
-> Block<'blk, 'tcx> {
let (zero_text, overflow_text) = if divrem.node == hir::BiDiv {
("attempted to divide by zero",
"attempted to divide with overflow")
use rustc_const_math::{ConstMathErr, Op};

let (zero_err, overflow_err) = if divrem.node == hir::BiDiv {
(ConstMathErr::DivisionByZero, ConstMathErr::Overflow(Op::Div))
} else {
("attempted remainder with a divisor of zero",
"attempted remainder with overflow")
(ConstMathErr::RemainderByZero, ConstMathErr::Overflow(Op::Rem))
};
let debug_loc = call_info.debug_loc();

Expand Down Expand Up @@ -802,7 +802,7 @@ pub fn fail_if_zero_or_overflows<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
}
};
let bcx = with_cond(cx, is_zero, |bcx| {
controlflow::trans_fail(bcx, call_info, InternedString::new(zero_text))
controlflow::trans_fail(bcx, call_info, InternedString::new(zero_err.description()))
});

// To quote LLVM's documentation for the sdiv instruction:
Expand All @@ -828,7 +828,8 @@ pub fn fail_if_zero_or_overflows<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
C_integral(llty, min, true),
debug_loc);
with_cond(bcx, is_min, |bcx| {
controlflow::trans_fail(bcx, call_info, InternedString::new(overflow_text))
controlflow::trans_fail(bcx, call_info,
InternedString::new(overflow_err.description()))
})
})
} else {
Expand Down
20 changes: 15 additions & 5 deletions src/librustc_trans/expr.rs
Expand Up @@ -2220,6 +2220,8 @@ impl OverflowOpViaIntrinsic {
rhs: ValueRef,
binop_debug_loc: DebugLoc)
-> (Block<'blk, 'tcx>, ValueRef) {
use rustc_const_math::{ConstMathErr, Op};

let llfn = self.to_intrinsic(bcx, lhs_t);

let val = Call(bcx, llfn, &[lhs, rhs], binop_debug_loc);
Expand All @@ -2233,10 +2235,16 @@ impl OverflowOpViaIntrinsic {
let expected = Call(bcx, expect, &[cond, C_bool(bcx.ccx(), false)],
binop_debug_loc);

let op = match *self {
OverflowOpViaIntrinsic::Add => Op::Add,
OverflowOpViaIntrinsic::Sub => Op::Sub,
OverflowOpViaIntrinsic::Mul => Op::Mul
};

let bcx =
base::with_cond(bcx, expected, |bcx|
controlflow::trans_fail(bcx, info,
InternedString::new("arithmetic operation overflowed")));
InternedString::new(ConstMathErr::Overflow(op).description())));

(bcx, result)
}
Expand All @@ -2252,6 +2260,8 @@ impl OverflowOpViaInputCheck {
binop_debug_loc: DebugLoc)
-> (Block<'blk, 'tcx>, ValueRef)
{
use rustc_const_math::{ConstMathErr, Op};

let lhs_llty = val_ty(lhs);
let rhs_llty = val_ty(rhs);

Expand All @@ -2266,16 +2276,16 @@ impl OverflowOpViaInputCheck {

let outer_bits = And(bcx, rhs, invert_mask, binop_debug_loc);
let cond = build_nonzero_check(bcx, outer_bits, binop_debug_loc);
let result = match *self {
let (result, op) = match *self {
OverflowOpViaInputCheck::Shl =>
build_unchecked_lshift(bcx, lhs, rhs, binop_debug_loc),
(build_unchecked_lshift(bcx, lhs, rhs, binop_debug_loc), Op::Shl),
OverflowOpViaInputCheck::Shr =>
build_unchecked_rshift(bcx, lhs_t, lhs, rhs, binop_debug_loc),
(build_unchecked_rshift(bcx, lhs_t, lhs, rhs, binop_debug_loc), Op::Shr)
};
let bcx =
base::with_cond(bcx, cond, |bcx|
controlflow::trans_fail(bcx, info,
InternedString::new("shift operation overflowed")));
InternedString::new(ConstMathErr::Overflow(op).description())));

(bcx, result)
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/mod-zero.rs
Expand Up @@ -10,7 +10,7 @@

// ignore-pretty : (#23623) problems when ending with // comments

// error-pattern:attempted remainder with a divisor of zero
// error-pattern:attempted to calculate the remainder with a divisor of zero

fn main() {
let y = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/overflowing-add.rs
Expand Up @@ -10,7 +10,7 @@

// ignore-pretty : (#23623) problems when ending with // comments

// error-pattern:thread 'main' panicked at 'arithmetic operation overflowed'
// error-pattern:thread 'main' panicked at 'attempted to add with overflow'
// compile-flags: -C debug-assertions


Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/overflowing-lsh-1.rs
Expand Up @@ -10,7 +10,7 @@

// ignore-pretty : (#23623) problems when ending with // comments

// error-pattern:thread 'main' panicked at 'shift operation overflowed'
// error-pattern:thread 'main' panicked at 'attempted to shift left with overflow'
// compile-flags: -C debug-assertions

#![warn(exceeding_bitshifts)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/overflowing-lsh-2.rs
Expand Up @@ -10,7 +10,7 @@

// ignore-pretty : (#23623) problems when ending with // comments

// error-pattern:thread 'main' panicked at 'shift operation overflowed'
// error-pattern:thread 'main' panicked at 'attempted to shift left with overflow'
// compile-flags: -C debug-assertions

#![warn(exceeding_bitshifts)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/overflowing-lsh-3.rs
Expand Up @@ -10,7 +10,7 @@

// ignore-pretty : (#23623) problems when ending with // comments

// error-pattern:thread 'main' panicked at 'shift operation overflowed'
// error-pattern:thread 'main' panicked at 'attempted to shift left with overflow'
// compile-flags: -C debug-assertions

#![warn(exceeding_bitshifts)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/overflowing-lsh-4.rs
Expand Up @@ -10,7 +10,7 @@

// ignore-pretty : (#23623) problems when ending with // comments

// error-pattern:thread 'main' panicked at 'shift operation overflowed'
// error-pattern:thread 'main' panicked at 'attempted to shift left with overflow'
// compile-flags: -C debug-assertions

// This function is checking that our automatic truncation does not
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/overflowing-mul.rs
Expand Up @@ -10,7 +10,7 @@

// ignore-pretty : (#23623) problems when ending with // comments

// error-pattern:thread 'main' panicked at 'arithmetic operation overflowed'
// error-pattern:thread 'main' panicked at 'attempted to multiply with overflow'
// compile-flags: -C debug-assertions

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/overflowing-pow.rs
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// error-pattern:thread 'main' panicked at 'arithmetic operation overflowed'
// error-pattern:thread 'main' panicked at 'attempted to multiply with overflow'
// compile-flags: -C debug-assertions

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/overflowing-rsh-1.rs
Expand Up @@ -10,7 +10,7 @@

// ignore-pretty : (#23623) problems when ending with // comments

// error-pattern:thread 'main' panicked at 'shift operation overflowed'
// error-pattern:thread 'main' panicked at 'attempted to shift right with overflow'
// compile-flags: -C debug-assertions

#![warn(exceeding_bitshifts)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/overflowing-rsh-2.rs
Expand Up @@ -10,7 +10,7 @@

// ignore-pretty : (#23623) problems when ending with // comments

// error-pattern:thread 'main' panicked at 'shift operation overflowed'
// error-pattern:thread 'main' panicked at 'attempted to shift right with overflow'
// compile-flags: -C debug-assertions

#![warn(exceeding_bitshifts)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/overflowing-rsh-3.rs
Expand Up @@ -10,7 +10,7 @@

// ignore-pretty : (#23623) problems when ending with // comments

// error-pattern:thread 'main' panicked at 'shift operation overflowed'
// error-pattern:thread 'main' panicked at 'attempted to shift right with overflow'
// compile-flags: -C debug-assertions

#![warn(exceeding_bitshifts)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/overflowing-rsh-4.rs
Expand Up @@ -10,7 +10,7 @@

// ignore-pretty : (#23623) problems when ending with // comments

// error-pattern:thread 'main' panicked at 'shift operation overflowed'
// error-pattern:thread 'main' panicked at 'attempted to shift right with overflow'
// compile-flags: -C debug-assertions

// This function is checking that our (type-based) automatic
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/overflowing-rsh-5.rs
Expand Up @@ -10,7 +10,7 @@

// ignore-pretty : (#23623) problems when ending with // comments

// error-pattern:thread 'main' panicked at 'shift operation overflowed'
// error-pattern:thread 'main' panicked at 'attempted to shift right with overflow'
// compile-flags: -C debug-assertions

#![warn(exceeding_bitshifts)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/overflowing-rsh-6.rs
Expand Up @@ -10,7 +10,7 @@

// ignore-pretty : (#23623) problems when ending with // comments

// error-pattern:thread 'main' panicked at 'shift operation overflowed'
// error-pattern:thread 'main' panicked at 'attempted to shift right with overflow'
// compile-flags: -C debug-assertions

#![warn(exceeding_bitshifts)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-fail/overflowing-sub.rs
Expand Up @@ -10,7 +10,7 @@

// ignore-pretty : (#23623) problems when ending with // comments

// error-pattern:thread 'main' panicked at 'arithmetic operation overflowed'
// error-pattern:thread 'main' panicked at 'attempted to subtract with overflow'
// compile-flags: -C debug-assertions

fn main() {
Expand Down

0 comments on commit b8c5053

Please sign in to comment.