Merge pull request #4587 from sanxiyn/divide-by-zero

Handle divide by zero in constant evaluator
This commit is contained in:
Tim Chevalier 2013-01-23 14:56:07 -08:00
commit ab13beb05a
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() {}