Handle divide by zero in constant evaluator

This commit is contained in:
Seo Sanghyeon 2013-01-23 15:07:00 +09:00
parent 93e969e356
commit 3ed39ce26f
2 changed files with 10 additions and 0 deletions

View File

@ -295,7 +295,9 @@ fn eval_const_expr_partial(tcx: middle::ty::ctxt, e: @expr)
add => Ok(const_int(a + b)),
subtract => Ok(const_int(a - b)),
mul => Ok(const_int(a * b)),
div if b == 0 => Err(~"divide by zero"),
div => Ok(const_int(a / b)),
rem if b == 0 => Err(~"modulo zero"),
rem => Ok(const_int(a % b)),
and | bitand => Ok(const_int(a & b)),
or | bitor => Ok(const_int(a | b)),
@ -315,7 +317,9 @@ fn eval_const_expr_partial(tcx: middle::ty::ctxt, e: @expr)
add => Ok(const_uint(a + b)),
subtract => Ok(const_uint(a - b)),
mul => Ok(const_uint(a * b)),
div if b == 0 => Err(~"divide by zero"),
div => Ok(const_uint(a / b)),
rem if b == 0 => Err(~"modulo zero"),
rem => Ok(const_uint(a % b)),
and | bitand => Ok(const_uint(a & b)),
or | bitor => Ok(const_uint(a | b)),

View File

@ -0,0 +1,6 @@
enum test {
div_zero = 1/0, //~ERROR expected constant: divide by zero
rem_zero = 1%0 //~ERROR expected constant: modulo zero
}
fn main() {}