]> git.lizzy.rs Git - rust.git/commitdiff
Fix translation of semi-constant if-statements
authorAriel Ben-Yehuda <ariel.byd@gmail.com>
Tue, 19 May 2015 14:38:55 +0000 (17:38 +0300)
committerAriel Ben-Yehuda <ariel.byd@gmail.com>
Tue, 19 May 2015 14:42:15 +0000 (17:42 +0300)
Thanks @dotdash

src/librustc_trans/trans/base.rs
src/librustc_trans/trans/common.rs
src/librustc_trans/trans/controlflow.rs

index 4879975dde695c265bd7505b3e1127a0bccebf5c..0684150112409100826b81636d369fa6ff9ed1fa 100644 (file)
@@ -869,8 +869,7 @@ pub fn with_cond<'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
 {
     let _icx = push_ctxt("with_cond");
 
-    if bcx.unreachable.get() ||
-            (common::is_const(val) && common::const_to_uint(val) == 0) {
+    if bcx.unreachable.get() || common::const_to_opt_uint(val) == Some(0) {
         return bcx;
     }
 
index 03dda57e5689fab9d4112575892d0f4dd662a040..758702f54c049f7e5283ef3645e496cbed298a23 100644 (file)
@@ -919,12 +919,6 @@ pub fn const_get_elt(cx: &CrateContext, v: ValueRef, us: &[c_uint])
     }
 }
 
-pub fn is_const(v: ValueRef) -> bool {
-    unsafe {
-        llvm::LLVMIsConstant(v) == True
-    }
-}
-
 pub fn const_to_int(v: ValueRef) -> i64 {
     unsafe {
         llvm::LLVMConstIntGetSExtValue(v)
index 8cecc39ec3900093cdc329552d5d268d0e70cad7..ab8cfa0ce3b7eaa72508ab75824b002b2ceb3530 100644 (file)
@@ -166,31 +166,24 @@ pub fn trans_if<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
     let cond_val = unpack_result!(bcx, expr::trans(bcx, cond).to_llbool());
 
     // Drop branches that are known to be impossible
-    if is_const(cond_val) && !is_undef(cond_val) {
-        if const_to_uint(cond_val) == 1 {
-            match els {
-                Some(elexpr) => {
-                    let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx };
-                    trans.visit_expr(&*elexpr);
-                }
-                None => {}
-            }
+    if let Some(cv) = const_to_opt_uint(cond_val) {
+        if cv == 1 {
             // if true { .. } [else { .. }]
             bcx = trans_block(bcx, &*thn, dest);
             trans::debuginfo::clear_source_location(bcx.fcx);
+
+            if let Some(elexpr) = els {
+                let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx };
+                trans.visit_expr(&*elexpr);
+            }
         } else {
-            let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx } ;
+            // if false { .. } [else { .. }]
+            let mut trans = TransItemVisitor { ccx: bcx.fcx.ccx };
             trans.visit_block(&*thn);
 
-            match els {
-                // if false { .. } else { .. }
-                Some(elexpr) => {
-                    bcx = expr::trans_into(bcx, &*elexpr, dest);
-                    trans::debuginfo::clear_source_location(bcx.fcx);
-                }
-
-                // if false { .. }
-                None => { }
+            if let Some(elexpr) = els {
+                bcx = expr::trans_into(bcx, &*elexpr, dest);
+                trans::debuginfo::clear_source_location(bcx.fcx);
             }
         }