Skip to content

Commit

Permalink
Improve div by zero const eval errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Schneider authored and oli-obk committed Apr 30, 2018
1 parent 1f3cb92 commit f66367d
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@ impl<'tcx> TerminatorKind<'tcx> {
write!(fmt, "!")?;
}
write!(fmt, "{:?}, ", cond)?;
write!(fmt, "{:?}", msg)?;
write!(fmt, "\"{:?}\"", msg)?;

write!(fmt, ")")
},
Expand Down
6 changes: 4 additions & 2 deletions src/librustc_mir/interpret/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
return Ok((PrimVal::from_bool(op(&l, &r)), false));
}
let op: Option<fn(i128, i128) -> (i128, bool)> = match bin_op {
Rem | Div if r == 0 => return Ok((PrimVal::Bytes(l), true)),
Div if r == 0 => return err!(DivisionByZero),
Rem if r == 0 => return err!(RemainderByZero),
Div => Some(i128::overflowing_div),
Rem => Some(i128::overflowing_rem),
Add => Some(i128::overflowing_add),
Expand Down Expand Up @@ -221,7 +222,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
Add => u128::overflowing_add,
Sub => u128::overflowing_sub,
Mul => u128::overflowing_mul,
Rem | Div if r == 0 => return Ok((PrimVal::Bytes(l), true)),
Div if r == 0 => return err!(DivisionByZero),
Rem if r == 0 => return err!(RemainderByZero),
Div => u128::overflowing_div,
Rem => u128::overflowing_rem,
_ => bug!(),
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_mir/interpret/terminator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
}
Overflow(op) => Err(Overflow(op).into()),
OverflowNeg => Err(OverflowNeg.into()),
DivisionByZero => Err(DivisionByZero.into()),
RemainderByZero => Err(RemainderByZero.into()),
GeneratorResumedAfterReturn |
GeneratorResumedAfterPanic => unimplemented!(),
_ => bug!(),
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/const-eval/index_out_of_bound.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0080]: constant evaluation error
--> $DIR/index_out_of_bound.rs:11:19
|
LL | static FOO: i32 = [][0];
| ^^^^^ index out of bounds: the len is 0 but the index is 0 at $DIR/index_out_of_bound.rs:11:19: 11:24
| ^^^^^ index out of bounds: the len is 0 but the index is 0

error: aborting due to previous error

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/const-eval/promoted_errors.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ warning: constant evaluation error
--> $DIR/promoted_errors.rs:19:20
|
LL | println!("{}", 1/(1-1));
| ^^^^^^^ attempt to divide with overflow
| ^^^^^^^ attempt to divide by zero

warning: attempt to divide by zero
--> $DIR/promoted_errors.rs:22:14
Expand All @@ -40,11 +40,11 @@ warning: constant evaluation error
--> $DIR/promoted_errors.rs:22:14
|
LL | let _x = 1/(1-1);
| ^^^^^^^ attempt to divide with overflow
| ^^^^^^^ attempt to divide by zero

warning: constant evaluation error
--> $DIR/promoted_errors.rs:25:20
|
LL | println!("{}", 1/(false as u32));
| ^^^^^^^^^^^^^^^^ attempt to divide with overflow
| ^^^^^^^^^^^^^^^^ attempt to divide by zero

1 change: 0 additions & 1 deletion src/test/ui/error-codes/E0080.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ enum Enum {
//~| const_err
//~| const_err
//~| const_err
//~| divide by zero
}

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0080.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ warning: constant evaluation error
--> $DIR/E0080.rs:14:9
|
LL | Y = (1 / 0) //~ ERROR E0080
| ^^^^^^^ attempt to divide with overflow
| ^^^^^^^ attempt to divide by zero

error[E0080]: constant evaluation error
--> $DIR/E0080.rs:14:9
Expand Down

0 comments on commit f66367d

Please sign in to comment.