Skip to content

Commit ab13beb

Browse files
committed
Merge pull request #4587 from sanxiyn/divide-by-zero
Handle divide by zero in constant evaluator
2 parents 154488d + 3ed39ce commit ab13beb

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/librustc/middle/const_eval.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,9 @@ fn eval_const_expr_partial(tcx: middle::ty::ctxt, e: @expr)
295295
add => Ok(const_int(a + b)),
296296
subtract => Ok(const_int(a - b)),
297297
mul => Ok(const_int(a * b)),
298+
div if b == 0 => Err(~"divide by zero"),
298299
div => Ok(const_int(a / b)),
300+
rem if b == 0 => Err(~"modulo zero"),
299301
rem => Ok(const_int(a % b)),
300302
and | bitand => Ok(const_int(a & b)),
301303
or | bitor => Ok(const_int(a | b)),
@@ -315,7 +317,9 @@ fn eval_const_expr_partial(tcx: middle::ty::ctxt, e: @expr)
315317
add => Ok(const_uint(a + b)),
316318
subtract => Ok(const_uint(a - b)),
317319
mul => Ok(const_uint(a * b)),
320+
div if b == 0 => Err(~"divide by zero"),
318321
div => Ok(const_uint(a / b)),
322+
rem if b == 0 => Err(~"modulo zero"),
319323
rem => Ok(const_uint(a % b)),
320324
and | bitand => Ok(const_uint(a & b)),
321325
or | bitor => Ok(const_uint(a | b)),

src/test/compile-fail/eval-enum.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
enum test {
2+
div_zero = 1/0, //~ERROR expected constant: divide by zero
3+
rem_zero = 1%0 //~ERROR expected constant: modulo zero
4+
}
5+
6+
fn main() {}

0 commit comments

Comments
 (0)