Skip to content

Commit 4a3f9b8

Browse files
hir-trans: Don't generate code for unreachable operands in short-circuiting logical operations.
1 parent b33240e commit 4a3f9b8

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/librustc_trans/expr.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1695,11 +1695,13 @@ fn trans_scalar_binop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
16951695
}
16961696

16971697
// refinement types would obviate the need for this
1698+
#[derive(Clone, Copy)]
16981699
enum lazy_binop_ty {
16991700
lazy_and,
17001701
lazy_or,
17011702
}
17021703

1704+
17031705
fn trans_lazy_binop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
17041706
binop_expr: &hir::Expr,
17051707
op: lazy_binop_ty,
@@ -1717,6 +1719,17 @@ fn trans_lazy_binop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
17171719
return immediate_rvalue_bcx(past_lhs, lhs, binop_ty).to_expr_datumblock();
17181720
}
17191721

1722+
// If the rhs can never be reached, don't generate code for it.
1723+
if let Some(cond_val) = const_to_opt_uint(lhs) {
1724+
match (cond_val, op) {
1725+
(0, lazy_and) |
1726+
(1, lazy_or) => {
1727+
return immediate_rvalue_bcx(past_lhs, lhs, binop_ty).to_expr_datumblock();
1728+
}
1729+
_ => { /* continue */ }
1730+
}
1731+
}
1732+
17201733
let join = fcx.new_id_block("join", binop_expr.id);
17211734
let before_rhs = fcx.new_id_block("before_rhs", b.id);
17221735

0 commit comments

Comments
 (0)