From 4a3f9b8962fb3946f155a39c745297e8e3126b05 Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Fri, 3 Jun 2016 13:24:46 -0400 Subject: [PATCH] hir-trans: Don't generate code for unreachable operands in short-circuiting logical operations. --- src/librustc_trans/expr.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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);