Skip to content

Commit 33d2970

Browse files
MIR: Refactor mir::Terminator to use tuples instead of a fixed-size arrays.
1 parent d62de4c commit 33d2970

File tree

8 files changed

+30
-25
lines changed

8 files changed

+30
-25
lines changed

src/librustc/mir/repr.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use middle::def_id::DefId;
1313
use middle::subst::Substs;
1414
use middle::ty::{AdtDef, ClosureSubsts, FnOutput, Region, Ty};
1515
use rustc_back::slice;
16+
use rustc_data_structures::tuple_slice::TupleSlice;
1617
use rustc_front::hir::InlineAsm;
1718
use syntax::ast::Name;
1819
use syntax::codemap::Span;
@@ -206,7 +207,7 @@ pub enum Terminator<'tcx> {
206207
/// jump to branch 0 if this lvalue evaluates to true
207208
If {
208209
cond: Operand<'tcx>,
209-
targets: [BasicBlock; 2],
210+
targets: (BasicBlock, BasicBlock),
210211
},
211212

212213
/// lvalue evaluates to some enum; jump depending on the branch
@@ -254,7 +255,7 @@ pub enum Terminator<'tcx> {
254255
/// unwinding.
255256
Call {
256257
data: CallData<'tcx>,
257-
targets: [BasicBlock; 2],
258+
targets: (BasicBlock, BasicBlock),
258259
},
259260
}
260261

@@ -264,12 +265,12 @@ impl<'tcx> Terminator<'tcx> {
264265
match *self {
265266
Goto { target: ref b } => slice::ref_slice(b),
266267
Panic { target: ref b } => slice::ref_slice(b),
267-
If { cond: _, targets: ref b } => b,
268+
If { cond: _, targets: ref b } => b.as_slice(),
268269
Switch { targets: ref b, .. } => b,
269270
SwitchInt { targets: ref b, .. } => b,
270271
Diverge => &[],
271272
Return => &[],
272-
Call { data: _, targets: ref b } => b,
273+
Call { data: _, targets: ref b } => b.as_slice(),
273274
}
274275
}
275276

@@ -278,12 +279,12 @@ impl<'tcx> Terminator<'tcx> {
278279
match *self {
279280
Goto { target: ref mut b } => slice::mut_ref_slice(b),
280281
Panic { target: ref mut b } => slice::mut_ref_slice(b),
281-
If { cond: _, targets: ref mut b } => b,
282+
If { cond: _, targets: ref mut b } => b.as_mut_slice(),
282283
Switch { targets: ref mut b, .. } => b,
283284
SwitchInt { targets: ref mut b, .. } => b,
284285
Diverge => &mut [],
285286
Return => &mut [],
286-
Call { data: _, targets: ref mut b } => b,
287+
Call { data: _, targets: ref mut b } => b.as_mut_slice(),
287288
}
288289
}
289290
}

src/librustc/mir/visit.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use middle::ty::Region;
1212
use mir::repr::*;
13+
use rustc_data_structures::tuple_slice::TupleSlice;
1314

1415
pub trait Visitor<'tcx> {
1516
// Override these, and call `self.super_xxx` to revert back to the
@@ -97,7 +98,7 @@ pub trait Visitor<'tcx> {
9798

9899
Terminator::If { ref cond, ref targets } => {
99100
self.visit_operand(cond);
100-
for &target in &targets[..] {
101+
for &target in targets.as_slice() {
101102
self.visit_branch(block, target);
102103
}
103104
}
@@ -126,7 +127,7 @@ pub trait Visitor<'tcx> {
126127
for arg in &data.args {
127128
self.visit_operand(arg);
128129
}
129-
for &target in &targets[..] {
130+
for &target in targets.as_slice() {
130131
self.visit_branch(block, target);
131132
}
132133
}

src/librustc_mir/build/expr/as_lvalue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
6969
this.cfg.terminate(block,
7070
Terminator::If {
7171
cond: Operand::Consume(lt),
72-
targets: [success, failure],
72+
targets: (success, failure),
7373
});
7474
this.panic(failure);
7575
success.and(slice.index(idx))

src/librustc_mir/build/expr/into.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
5353
let mut else_block = this.cfg.start_new_block();
5454
this.cfg.terminate(block, Terminator::If {
5555
cond: operand,
56-
targets: [then_block, else_block]
56+
targets: (then_block, else_block)
5757
});
5858

5959
unpack!(then_block = this.into(destination, then_block, then_expr));
@@ -84,15 +84,15 @@ impl<'a,'tcx> Builder<'a,'tcx> {
8484

8585
let lhs = unpack!(block = this.as_operand(block, lhs));
8686
let blocks = match op {
87-
LogicalOp::And => [else_block, false_block],
88-
LogicalOp::Or => [true_block, else_block],
87+
LogicalOp::And => (else_block, false_block),
88+
LogicalOp::Or => (true_block, else_block),
8989
};
9090
this.cfg.terminate(block, Terminator::If { cond: lhs, targets: blocks });
9191

9292
let rhs = unpack!(else_block = this.as_operand(else_block, rhs));
9393
this.cfg.terminate(else_block, Terminator::If {
9494
cond: rhs,
95-
targets: [true_block, false_block]
95+
targets: (true_block, false_block)
9696
});
9797

9898
this.cfg.push_assign_constant(
@@ -149,7 +149,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
149149
this.cfg.terminate(loop_block_end,
150150
Terminator::If {
151151
cond: cond,
152-
targets: [body_block, exit_block]
152+
targets: (body_block, exit_block)
153153
});
154154
} else {
155155
body_block = loop_block;
@@ -225,7 +225,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
225225
func: fun,
226226
args: args,
227227
},
228-
targets: [success, panic],
228+
targets: (success, panic),
229229
});
230230
success.unit()
231231
}

src/librustc_mir/build/matches/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
555555
let cond = unpack!(block = self.as_operand(block, guard));
556556
let otherwise = self.cfg.start_new_block();
557557
self.cfg.terminate(block, Terminator::If { cond: cond,
558-
targets: [arm_block, otherwise]});
558+
targets: (arm_block, otherwise)});
559559
Some(otherwise)
560560
} else {
561561
self.cfg.terminate(block, Terminator::Goto { target: arm_block });

src/librustc_mir/build/matches/test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
232232
self.cfg.start_new_block()];
233233
self.cfg.terminate(block, Terminator::If {
234234
cond: Operand::Consume(result),
235-
targets: [target_blocks[0], target_blocks[1]]
235+
targets: (target_blocks[0], target_blocks[1])
236236
});
237237

238238
target_blocks
@@ -252,7 +252,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
252252
let bool_ty = self.hir.bool_ty();
253253
let eq_result = self.temp(bool_ty);
254254
let func = self.item_ref_operand(span, item_ref);
255-
let call_blocks = [self.cfg.start_new_block(), self.diverge_cleanup()];
255+
let call_blocks = (self.cfg.start_new_block(), self.diverge_cleanup());
256256
self.cfg.terminate(block,
257257
Terminator::Call {
258258
data: CallData {
@@ -264,10 +264,10 @@ impl<'a,'tcx> Builder<'a,'tcx> {
264264
});
265265

266266
// check the result
267-
self.cfg.terminate(call_blocks[0],
267+
self.cfg.terminate(call_blocks.0,
268268
Terminator::If {
269269
cond: Operand::Consume(eq_result),
270-
targets: [target_blocks[0], target_blocks[1]],
270+
targets: (target_blocks[0], target_blocks[1]),
271271
});
272272

273273
target_blocks

src/librustc_mir/transform/simplify_cfg.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,21 @@ impl SimplifyCfg {
9696
mem::swap(&mut terminator, &mut mir.basic_block_data_mut(bb).terminator);
9797

9898
mir.basic_block_data_mut(bb).terminator = match terminator {
99-
Terminator::If { ref targets, .. } if targets[0] == targets[1] => {
99+
Terminator::If { ref targets, .. } if targets.0 == targets.1 => {
100100
changed = true;
101-
Terminator::Goto { target: targets[0] }
101+
Terminator::Goto { target: targets.0 }
102102
}
103103
Terminator::If { ref targets, cond: Operand::Constant(Constant {
104104
literal: Literal::Value {
105105
value: ConstVal::Bool(cond)
106106
}, ..
107107
}) } => {
108108
changed = true;
109-
let target_idx = if cond { 0 } else { 1 };
110-
Terminator::Goto { target: targets[target_idx] }
109+
if cond {
110+
Terminator::Goto { target: targets.0 }
111+
} else {
112+
Terminator::Goto { target: targets.1 }
113+
}
111114
}
112115
Terminator::SwitchInt { ref targets, .. } if targets.len() == 1 => {
113116
Terminator::Goto { target: targets[0] }

src/librustc_trans/trans/mir/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
3939
unimplemented!()
4040
}
4141

42-
mir::Terminator::If { ref cond, targets: [true_bb, false_bb] } => {
42+
mir::Terminator::If { ref cond, targets: (true_bb, false_bb) } => {
4343
let cond = self.trans_operand(bcx, cond);
4444
let lltrue = self.llblock(true_bb);
4545
let llfalse = self.llblock(false_bb);

0 commit comments

Comments
 (0)