]> git.lizzy.rs Git - rust.git/commitdiff
Change `with_cond` to `build_cond_br`
authorJames Miller <james@aatch.net>
Fri, 29 Apr 2016 02:10:56 +0000 (14:10 +1200)
committerEduard Burtescu <edy.burt@gmail.com>
Sun, 5 Jun 2016 11:02:24 +0000 (14:02 +0300)
This is simpler to work with than `with_cond`.

src/librustc_mir/build/block.rs
src/librustc_mir/build/cfg.rs
src/librustc_mir/build/expr/as_rvalue.rs

index 01930d0ddc09822538223e7787a3293b5fc1df23..0236a6c0c80428cc74bf2348ed24a4e12b7a31c7 100644 (file)
@@ -83,10 +83,10 @@ pub fn ast_block(&mut self,
         })
     }
 
-    // Helper method for generating MIR inside a conditional block.
-    pub fn with_cond<F>(&mut self, block: BasicBlock, span: Span,
-                        cond: Operand<'tcx>, f: F) -> BasicBlock
-    where F: FnOnce(&mut Builder<'a, 'gcx, 'tcx>, BasicBlock) -> BasicBlock {
+    // Helper method for generating a conditional branch
+    // Returns (TrueBlock, FalseBlock)
+    pub fn build_cond_br(&mut self, block: BasicBlock, span: Span,
+                         cond: Operand<'tcx>) -> (BasicBlock, BasicBlock) {
         let scope_id = self.innermost_scope_id();
 
         let then_block = self.cfg.start_new_block();
@@ -98,15 +98,6 @@ pub fn with_cond<F>(&mut self, block: BasicBlock, span: Span,
                                targets: (then_block, else_block)
                            });
 
-        let after = f(self, then_block);
-
-        // If the returned block isn't terminated, add a branch to the "else"
-        // block
-        if !self.cfg.terminated(after) {
-            self.cfg.terminate(after, scope_id, span,
-                               TerminatorKind::Goto { target: else_block });
-        }
-
-        else_block
+        (then_block, else_block)
     }
 }
index 7ffef989e2f00e0b0442a5c35442ebbc5e810509..4859257f291c9767f694b9d4171ddcfeaabb0805 100644 (file)
@@ -86,7 +86,7 @@ pub fn terminate(&mut self,
                      scope: ScopeId,
                      span: Span,
                      kind: TerminatorKind<'tcx>) {
-        debug_assert!(!self.terminated(block),
+        debug_assert!(self.block_data(block).terminator.is_none(),
                       "terminate: block {:?} already has a terminator set", block);
         self.block_data_mut(block).terminator = Some(Terminator {
             span: span,
@@ -94,9 +94,4 @@ pub fn terminate(&mut self,
             kind: kind,
         });
     }
-
-    /// Returns whether or not the given block has been terminated or not
-    pub fn terminated(&self, block: BasicBlock) -> bool {
-        self.block_data(block).terminator.is_some()
-    }
 }
index 04609e7f8dd3115a55dbf21f631f402b2b6e60e7..683efdc141fbf36af9c3fef7a850ce02acb6d890 100644 (file)
@@ -88,11 +88,10 @@ fn expr_as_rvalue(&mut self,
                     this.cfg.push_assign(block, scope_id, expr_span, &is_min,
                                          Rvalue::BinaryOp(BinOp::Eq, arg.clone(), minval));
 
-                    block = this.with_cond(
-                        block, expr_span, Operand::Consume(is_min), |this, block| {
-                            this.panic(block, "attempted to negate with overflow", expr_span);
-                            block
-                        });
+                    let (of_block, ok_block) = this.build_cond_br(block, expr_span,
+                                                                  Operand::Consume(is_min));
+                    this.panic(of_block, "attempted to negate with overflow", expr_span);
+                    block = ok_block;
                 }
                 block.and(Rvalue::UnaryOp(op, arg))
             }
@@ -243,7 +242,8 @@ fn expr_as_rvalue(&mut self,
         }
     }
 
-    pub fn build_binary_op(&mut self, mut block: BasicBlock, op: BinOp, span: Span, ty: ty::Ty<'tcx>,
+    pub fn build_binary_op(&mut self, mut block: BasicBlock,
+                           op: BinOp, span: Span, ty: ty::Ty<'tcx>,
                            lhs: Operand<'tcx>, rhs: Operand<'tcx>) -> BlockAnd<Rvalue<'tcx>> {
         let scope_id = self.innermost_scope_id();
         let bool_ty = self.hir.bool_ty();
@@ -267,12 +267,10 @@ pub fn build_binary_op(&mut self, mut block: BasicBlock, op: BinOp, span: Span,
                 "arithmetic operation overflowed"
             };
 
-            block = self.with_cond(block, span, Operand::Consume(of), |this, block| {
-                this.panic(block, msg, span);
-                block
-            });
+            let (of_block, ok_block) = self.build_cond_br(block, span, Operand::Consume(of));
+            self.panic(of_block, msg, span);
 
-            block.and(Rvalue::Use(Operand::Consume(val)))
+            ok_block.and(Rvalue::Use(Operand::Consume(val)))
         } else {
             if ty.is_integral() && (op == BinOp::Div || op == BinOp::Rem) {
                 // Checking division and remainder is more complex, since we 1. always check
@@ -292,10 +290,11 @@ pub fn build_binary_op(&mut self, mut block: BasicBlock, op: BinOp, span: Span,
                 self.cfg.push_assign(block, scope_id, span, &is_zero,
                                      Rvalue::BinaryOp(BinOp::Eq, rhs.clone(), zero));
 
-                block = self.with_cond(block, span, Operand::Consume(is_zero), |this, block| {
-                    this.panic(block, zero_msg, span);
-                    block
-                });
+                let (zero_block, ok_block) = self.build_cond_br(block, span,
+                                                                Operand::Consume(is_zero));
+                self.panic(zero_block, zero_msg, span);
+
+                block = ok_block;
 
                 // We only need to check for the overflow in one case:
                 // MIN / -1, and only for signed values.
@@ -319,10 +318,11 @@ pub fn build_binary_op(&mut self, mut block: BasicBlock, op: BinOp, span: Span,
                     self.cfg.push_assign(block, scope_id, span, &of,
                                          Rvalue::BinaryOp(BinOp::BitAnd, is_neg_1, is_min));
 
-                    block = self.with_cond(block, span, Operand::Consume(of), |this, block| {
-                        this.panic(block, overflow_msg, span);
-                        block
-                    });
+                    let (of_block, ok_block) = self.build_cond_br(block, span,
+                                                                  Operand::Consume(of));
+                    self.panic(of_block, overflow_msg, span);
+
+                    block = ok_block;
                 }
             }