about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Woerister <michaelwoerister@posteo.net>2016-06-03 13:24:46 -0400
committerMichael Woerister <michaelwoerister@posteo.net>2016-07-08 10:42:48 -0400
commit4a3f9b8962fb3946f155a39c745297e8e3126b05 (patch)
treebc004fc32cf83325393efce1fcbb6e4ac9d58051
parentb33240e2cc08cc62511e6c996e68c87ad8bd4951 (diff)
downloadrust-4a3f9b8962fb3946f155a39c745297e8e3126b05.tar.gz
rust-4a3f9b8962fb3946f155a39c745297e8e3126b05.zip
hir-trans: Don't generate code for unreachable operands in short-circuiting logical operations.
-rw-r--r--src/librustc_trans/expr.rs13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/librustc_trans/expr.rs b/src/librustc_trans/expr.rs
index 71c6cba9cc2..b8dd7273a83 100644
--- a/src/librustc_trans/expr.rs
+++ b/src/librustc_trans/expr.rs
@@ -1695,11 +1695,13 @@ fn trans_scalar_binop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
 }
 
 // refinement types would obviate the need for this
+#[derive(Clone, Copy)]
 enum lazy_binop_ty {
     lazy_and,
     lazy_or,
 }
 
+
 fn trans_lazy_binop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
                                 binop_expr: &hir::Expr,
                                 op: lazy_binop_ty,
@@ -1717,6 +1719,17 @@ fn trans_lazy_binop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
         return immediate_rvalue_bcx(past_lhs, lhs, binop_ty).to_expr_datumblock();
     }
 
+    // If the rhs can never be reached, don't generate code for it.
+    if let Some(cond_val) = const_to_opt_uint(lhs) {
+        match (cond_val, op) {
+            (0, lazy_and) |
+            (1, lazy_or)  => {
+                return immediate_rvalue_bcx(past_lhs, lhs, binop_ty).to_expr_datumblock();
+            }
+            _ => { /* continue */ }
+        }
+    }
+
     let join = fcx.new_id_block("join", binop_expr.id);
     let before_rhs = fcx.new_id_block("before_rhs", b.id);