about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSeo Sanghyeon <sanxiyn@gmail.com>2013-01-23 15:07:00 +0900
committerSeo Sanghyeon <sanxiyn@gmail.com>2013-01-23 15:07:00 +0900
commit3ed39ce26ff6dbe3d3a8f4196b9226550cc6a979 (patch)
tree27a8849e0774987e29cb17adcecd8e4fba04a3b5
parent93e969e356dc8908a7eeef675269149f81a10eed (diff)
Handle divide by zero in constant evaluator
-rw-r--r--src/librustc/middle/const_eval.rs4
-rw-r--r--src/test/compile-fail/eval-enum.rs6
2 files changed, 10 insertions, 0 deletions
diff --git a/src/librustc/middle/const_eval.rs b/src/librustc/middle/const_eval.rs
index 530e63acf57..3b9b0348615 100644
--- a/src/librustc/middle/const_eval.rs
+++ b/src/librustc/middle/const_eval.rs
@@ -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)),
diff --git a/src/test/compile-fail/eval-enum.rs b/src/test/compile-fail/eval-enum.rs
new file mode 100644
index 00000000000..d8443f836f5
--- /dev/null
+++ b/src/test/compile-fail/eval-enum.rs
@@ -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() {}