Skip to content

Commit f66367d

Browse files
Oliver Schneideroli-obk
Oliver Schneider
authored andcommitted
Improve div by zero const eval errors
1 parent 1f3cb92 commit f66367d

File tree

7 files changed

+12
-9
lines changed

7 files changed

+12
-9
lines changed

src/librustc/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ impl<'tcx> TerminatorKind<'tcx> {
11341134
write!(fmt, "!")?;
11351135
}
11361136
write!(fmt, "{:?}, ", cond)?;
1137-
write!(fmt, "{:?}", msg)?;
1137+
write!(fmt, "\"{:?}\"", msg)?;
11381138

11391139
write!(fmt, ")")
11401140
},

src/librustc_mir/interpret/operator.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
140140
return Ok((PrimVal::from_bool(op(&l, &r)), false));
141141
}
142142
let op: Option<fn(i128, i128) -> (i128, bool)> = match bin_op {
143-
Rem | Div if r == 0 => return Ok((PrimVal::Bytes(l), true)),
143+
Div if r == 0 => return err!(DivisionByZero),
144+
Rem if r == 0 => return err!(RemainderByZero),
144145
Div => Some(i128::overflowing_div),
145146
Rem => Some(i128::overflowing_rem),
146147
Add => Some(i128::overflowing_add),
@@ -221,7 +222,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
221222
Add => u128::overflowing_add,
222223
Sub => u128::overflowing_sub,
223224
Mul => u128::overflowing_mul,
224-
Rem | Div if r == 0 => return Ok((PrimVal::Bytes(l), true)),
225+
Div if r == 0 => return err!(DivisionByZero),
226+
Rem if r == 0 => return err!(RemainderByZero),
225227
Div => u128::overflowing_div,
226228
Rem => u128::overflowing_rem,
227229
_ => bug!(),

src/librustc_mir/interpret/terminator/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
161161
}
162162
Overflow(op) => Err(Overflow(op).into()),
163163
OverflowNeg => Err(OverflowNeg.into()),
164+
DivisionByZero => Err(DivisionByZero.into()),
165+
RemainderByZero => Err(RemainderByZero.into()),
164166
GeneratorResumedAfterReturn |
165167
GeneratorResumedAfterPanic => unimplemented!(),
166168
_ => bug!(),

src/test/ui/const-eval/index_out_of_bound.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: constant evaluation error
22
--> $DIR/index_out_of_bound.rs:11:19
33
|
44
LL | static FOO: i32 = [][0];
5-
| ^^^^^ index out of bounds: the len is 0 but the index is 0 at $DIR/index_out_of_bound.rs:11:19: 11:24
5+
| ^^^^^ index out of bounds: the len is 0 but the index is 0
66

77
error: aborting due to previous error
88

src/test/ui/const-eval/promoted_errors.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ warning: constant evaluation error
2828
--> $DIR/promoted_errors.rs:19:20
2929
|
3030
LL | println!("{}", 1/(1-1));
31-
| ^^^^^^^ attempt to divide with overflow
31+
| ^^^^^^^ attempt to divide by zero
3232

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

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

src/test/ui/error-codes/E0080.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ enum Enum {
1515
//~| const_err
1616
//~| const_err
1717
//~| const_err
18-
//~| divide by zero
1918
}
2019

2120
fn main() {

src/test/ui/error-codes/E0080.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ warning: constant evaluation error
2424
--> $DIR/E0080.rs:14:9
2525
|
2626
LL | Y = (1 / 0) //~ ERROR E0080
27-
| ^^^^^^^ attempt to divide with overflow
27+
| ^^^^^^^ attempt to divide by zero
2828

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

0 commit comments

Comments
 (0)