Skip to content

Commit 3afd760

Browse files
committed
Fix translation of semi-constant if-statements
Thanks @dotdash
1 parent 27d2bd1 commit 3afd760

File tree

3 files changed

+13
-27
lines changed

3 files changed

+13
-27
lines changed

src/librustc_trans/trans/base.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,8 +869,7 @@ pub fn with_cond<'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
869869
{
870870
let _icx = push_ctxt("with_cond");
871871

872-
if bcx.unreachable.get() ||
873-
(common::is_const(val) && common::const_to_uint(val) == 0) {
872+
if bcx.unreachable.get() || common::const_to_opt_uint(val) == Some(0) {
874873
return bcx;
875874
}
876875

src/librustc_trans/trans/common.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -919,12 +919,6 @@ pub fn const_get_elt(cx: &CrateContext, v: ValueRef, us: &[c_uint])
919919
}
920920
}
921921

922-
pub fn is_const(v: ValueRef) -> bool {
923-
unsafe {
924-
llvm::LLVMIsConstant(v) == True
925-
}
926-
}
927-
928922
pub fn const_to_int(v: ValueRef) -> i64 {
929923
unsafe {
930924
llvm::LLVMConstIntGetSExtValue(v)

src/librustc_trans/trans/controlflow.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -166,31 +166,24 @@ pub fn trans_if<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
166166
let cond_val = unpack_result!(bcx, expr::trans(bcx, cond).to_llbool());
167167

168168
// Drop branches that are known to be impossible
169-
if is_const(cond_val) && !is_undef(cond_val) {
170-
if const_to_uint(cond_val) == 1 {
171-
match els {
172-
Some(elexpr) => {
173-
let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx };
174-
trans.visit_expr(&*elexpr);
175-
}
176-
None => {}
177-
}
169+
if let Some(cv) = const_to_opt_uint(cond_val) {
170+
if cv == 1 {
178171
// if true { .. } [else { .. }]
179172
bcx = trans_block(bcx, &*thn, dest);
180173
trans::debuginfo::clear_source_location(bcx.fcx);
174+
175+
if let Some(elexpr) = els {
176+
let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx };
177+
trans.visit_expr(&*elexpr);
178+
}
181179
} else {
182-
let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx } ;
180+
// if false { .. } [else { .. }]
181+
let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx };
183182
trans.visit_block(&*thn);
184183

185-
match els {
186-
// if false { .. } else { .. }
187-
Some(elexpr) => {
188-
bcx = expr::trans_into(bcx, &*elexpr, dest);
189-
trans::debuginfo::clear_source_location(bcx.fcx);
190-
}
191-
192-
// if false { .. }
193-
None => { }
184+
if let Some(elexpr) = els {
185+
bcx = expr::trans_into(bcx, &*elexpr, dest);
186+
trans::debuginfo::clear_source_location(bcx.fcx);
194187
}
195188
}
196189

0 commit comments

Comments
 (0)