Skip to content

Commit 60a0813

Browse files
committed
Change with_cond to build_cond_br
This is simpler to work with than `with_cond`.
1 parent 6d3f7a9 commit 60a0813

File tree

3 files changed

+25
-39
lines changed

3 files changed

+25
-39
lines changed

src/librustc_mir/build/block.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ impl<'a,'tcx> Builder<'a,'tcx> {
8181
})
8282
}
8383

84-
// Helper method for generating MIR inside a conditional block.
85-
pub fn with_cond<F>(&mut self, block: BasicBlock, span: Span,
86-
cond: Operand<'tcx>, f: F) -> BasicBlock
87-
where F: FnOnce(&mut Builder<'a, 'tcx>, BasicBlock) -> BasicBlock {
84+
// Helper method for generating a conditional branch
85+
// Returns (TrueBlock, FalseBlock)
86+
pub fn build_cond_br(&mut self, block: BasicBlock, span: Span,
87+
cond: Operand<'tcx>) -> (BasicBlock, BasicBlock) {
8888
let scope_id = self.innermost_scope_id();
8989

9090
let then_block = self.cfg.start_new_block();
@@ -96,15 +96,6 @@ impl<'a,'tcx> Builder<'a,'tcx> {
9696
targets: (then_block, else_block)
9797
});
9898

99-
let after = f(self, then_block);
100-
101-
// If the returned block isn't terminated, add a branch to the "else"
102-
// block
103-
if !self.cfg.terminated(after) {
104-
self.cfg.terminate(after, scope_id, span,
105-
TerminatorKind::Goto { target: else_block });
106-
}
107-
108-
else_block
99+
(then_block, else_block)
109100
}
110101
}

src/librustc_mir/build/cfg.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,12 @@ impl<'tcx> CFG<'tcx> {
8686
scope: ScopeId,
8787
span: Span,
8888
kind: TerminatorKind<'tcx>) {
89-
debug_assert!(!self.terminated(block),
89+
debug_assert!(self.block_data(block).terminator.is_none(),
9090
"terminate: block {:?} already has a terminator set", block);
9191
self.block_data_mut(block).terminator = Some(Terminator {
9292
span: span,
9393
scope: scope,
9494
kind: kind,
9595
});
9696
}
97-
98-
/// Returns whether or not the given block has been terminated or not
99-
pub fn terminated(&self, block: BasicBlock) -> bool {
100-
self.block_data(block).terminator.is_some()
101-
}
10297
}

src/librustc_mir/build/expr/as_rvalue.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,10 @@ impl<'a,'tcx> Builder<'a,'tcx> {
8888
this.cfg.push_assign(block, scope_id, expr_span, &is_min,
8989
Rvalue::BinaryOp(BinOp::Eq, arg.clone(), minval));
9090

91-
block = this.with_cond(
92-
block, expr_span, Operand::Consume(is_min), |this, block| {
93-
this.panic(block, "attempted to negate with overflow", expr_span);
94-
block
95-
});
91+
let (of_block, ok_block) = this.build_cond_br(block, expr_span,
92+
Operand::Consume(is_min));
93+
this.panic(of_block, "attempted to negate with overflow", expr_span);
94+
block = ok_block;
9695
}
9796
block.and(Rvalue::UnaryOp(op, arg))
9897
}
@@ -246,7 +245,8 @@ impl<'a,'tcx> Builder<'a,'tcx> {
246245
}
247246
}
248247

249-
pub fn build_binary_op(&mut self, mut block: BasicBlock, op: BinOp, span: Span, ty: ty::Ty<'tcx>,
248+
pub fn build_binary_op(&mut self, mut block: BasicBlock,
249+
op: BinOp, span: Span, ty: ty::Ty<'tcx>,
250250
lhs: Operand<'tcx>, rhs: Operand<'tcx>) -> BlockAnd<Rvalue<'tcx>> {
251251
let scope_id = self.innermost_scope_id();
252252
let bool_ty = self.hir.bool_ty();
@@ -270,12 +270,10 @@ impl<'a,'tcx> Builder<'a,'tcx> {
270270
"arithmetic operation overflowed"
271271
};
272272

273-
block = self.with_cond(block, span, Operand::Consume(of), |this, block| {
274-
this.panic(block, msg, span);
275-
block
276-
});
273+
let (of_block, ok_block) = self.build_cond_br(block, span, Operand::Consume(of));
274+
self.panic(of_block, msg, span);
277275

278-
block.and(Rvalue::Use(Operand::Consume(val)))
276+
ok_block.and(Rvalue::Use(Operand::Consume(val)))
279277
} else {
280278
if ty.is_integral() && (op == BinOp::Div || op == BinOp::Rem) {
281279
// Checking division and remainder is more complex, since we 1. always check
@@ -295,10 +293,11 @@ impl<'a,'tcx> Builder<'a,'tcx> {
295293
self.cfg.push_assign(block, scope_id, span, &is_zero,
296294
Rvalue::BinaryOp(BinOp::Eq, rhs.clone(), zero));
297295

298-
block = self.with_cond(block, span, Operand::Consume(is_zero), |this, block| {
299-
this.panic(block, zero_msg, span);
300-
block
301-
});
296+
let (zero_block, ok_block) = self.build_cond_br(block, span,
297+
Operand::Consume(is_zero));
298+
self.panic(zero_block, zero_msg, span);
299+
300+
block = ok_block;
302301

303302
// We only need to check for the overflow in one case:
304303
// MIN / -1, and only for signed values.
@@ -322,10 +321,11 @@ impl<'a,'tcx> Builder<'a,'tcx> {
322321
self.cfg.push_assign(block, scope_id, span, &of,
323322
Rvalue::BinaryOp(BinOp::BitAnd, is_neg_1, is_min));
324323

325-
block = self.with_cond(block, span, Operand::Consume(of), |this, block| {
326-
this.panic(block, overflow_msg, span);
327-
block
328-
});
324+
let (of_block, ok_block) = self.build_cond_br(block, span,
325+
Operand::Consume(of));
326+
self.panic(of_block, overflow_msg, span);
327+
328+
block = ok_block;
329329
}
330330
}
331331

0 commit comments

Comments
 (0)