mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-11 16:15:03 +00:00
Merge pull request #4587 from sanxiyn/divide-by-zero
Handle divide by zero in constant evaluator
This commit is contained in:
commit
ab13beb05a
@ -295,7 +295,9 @@ fn eval_const_expr_partial(tcx: middle::ty::ctxt, e: @expr)
|
|||||||
add => Ok(const_int(a + b)),
|
add => Ok(const_int(a + b)),
|
||||||
subtract => Ok(const_int(a - b)),
|
subtract => Ok(const_int(a - b)),
|
||||||
mul => 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)),
|
div => Ok(const_int(a / b)),
|
||||||
|
rem if b == 0 => Err(~"modulo zero"),
|
||||||
rem => Ok(const_int(a % b)),
|
rem => Ok(const_int(a % b)),
|
||||||
and | bitand => Ok(const_int(a & b)),
|
and | bitand => Ok(const_int(a & b)),
|
||||||
or | bitor => 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)),
|
add => Ok(const_uint(a + b)),
|
||||||
subtract => Ok(const_uint(a - b)),
|
subtract => Ok(const_uint(a - b)),
|
||||||
mul => 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)),
|
div => Ok(const_uint(a / b)),
|
||||||
|
rem if b == 0 => Err(~"modulo zero"),
|
||||||
rem => Ok(const_uint(a % b)),
|
rem => Ok(const_uint(a % b)),
|
||||||
and | bitand => Ok(const_uint(a & b)),
|
and | bitand => Ok(const_uint(a & b)),
|
||||||
or | bitor => Ok(const_uint(a | b)),
|
or | bitor => Ok(const_uint(a | b)),
|
||||||
|
6
src/test/compile-fail/eval-enum.rs
Normal file
6
src/test/compile-fail/eval-enum.rs
Normal 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() {}
|
Loading…
Reference in New Issue
Block a user