Skip to content

Commit f5a8d1a

Browse files
committed
simplify MIR building with cfg.goto(...)
1 parent 4f0dc7b commit f5a8d1a

File tree

7 files changed

+29
-79
lines changed

7 files changed

+29
-79
lines changed

src/librustc_mir/build/block.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
3333
this.ast_block_stmts(destination, block, span, stmts, expr,
3434
safety_mode)
3535
});
36-
this.cfg.terminate(unpack!(block_exit), source_info,
37-
TerminatorKind::Goto { target: exit_block });
36+
this.cfg.goto(unpack!(block_exit), source_info, exit_block);
3837
exit_block.unit()
3938
} else {
4039
this.ast_block_stmts(destination, block, span, stmts, expr,

src/librustc_mir/build/cfg.rs

+5
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,9 @@ impl<'tcx> CFG<'tcx> {
8585
kind,
8686
});
8787
}
88+
89+
/// In the `origin` block, push a `goto -> target` terminator.
90+
pub fn goto(&mut self, origin: BasicBlock, source_info: SourceInfo, target: BasicBlock) {
91+
self.terminate(origin, source_info, TerminatorKind::Goto { target })
92+
}
8893
}

src/librustc_mir/build/expr/into.rs

+6-22
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
140140
},
141141
);
142142

143-
this.cfg.terminate(
144-
true_block,
145-
source_info,
146-
TerminatorKind::Goto { target: join_block },
147-
);
148-
this.cfg.terminate(
149-
false_block,
150-
source_info,
151-
TerminatorKind::Goto { target: join_block },
152-
);
153-
143+
// Link up both branches:
144+
this.cfg.goto(true_block, source_info, join_block);
145+
this.cfg.goto(false_block, source_info, join_block);
154146
join_block.unit()
155147
}
156148
ExprKind::Loop { body } => {
@@ -167,12 +159,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
167159
let loop_block = this.cfg.start_new_block();
168160
let exit_block = this.cfg.start_new_block();
169161

170-
// start the loop
171-
this.cfg.terminate(
172-
block,
173-
source_info,
174-
TerminatorKind::Goto { target: loop_block },
175-
);
162+
// Start the loop.
163+
this.cfg.goto(block, source_info, loop_block);
176164

177165
this.in_breakable_scope(
178166
Some(loop_block),
@@ -196,11 +184,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
196184
let tmp = this.get_unit_temp();
197185
// Execute the body, branching back to the test.
198186
let body_block_end = unpack!(this.into(&tmp, body_block, body));
199-
this.cfg.terminate(
200-
body_block_end,
201-
source_info,
202-
TerminatorKind::Goto { target: loop_block },
203-
);
187+
this.cfg.goto(body_block_end, source_info, loop_block);
204188
},
205189
);
206190
exit_block.unit()

src/librustc_mir/build/matches/mod.rs

+7-28
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
259259
scrutinee_span,
260260
match_scope,
261261
);
262-
this.cfg.terminate(
263-
binding_end,
264-
source_info,
265-
TerminatorKind::Goto { target: arm_block },
266-
);
262+
this.cfg.goto(binding_end, source_info, arm_block);
267263
}
268264
}
269265

@@ -279,11 +275,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
279275
let end_block = self.cfg.start_new_block();
280276

281277
for arm_block in arm_end_blocks {
282-
self.cfg.terminate(
283-
unpack!(arm_block),
284-
outer_source_info,
285-
TerminatorKind::Goto { target: end_block },
286-
);
278+
self.cfg.goto(unpack!(arm_block), outer_source_info, end_block);
287279
}
288280

289281
self.source_scope = outer_source_info.scope;
@@ -848,18 +840,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
848840
// never reach this point.
849841
if unmatched_candidates.is_empty() {
850842
let source_info = self.source_info(span);
851-
if let Some(otherwise) = otherwise_block {
852-
self.cfg.terminate(
853-
block,
854-
source_info,
855-
TerminatorKind::Goto { target: otherwise },
856-
);
857-
} else {
858-
self.cfg.terminate(
859-
block,
860-
source_info,
861-
TerminatorKind::Unreachable,
862-
)
843+
match otherwise_block {
844+
Some(otherwise) => self.cfg.goto(block, source_info, otherwise),
845+
None => self.cfg.terminate(block, source_info, TerminatorKind::Unreachable),
863846
}
864847
return;
865848
}
@@ -950,11 +933,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
950933
// `goto -> first_prebinding_block` from the `start_block` if there is one.
951934
if let Some(start_block) = *start_block {
952935
let source_info = self.source_info(first_candidate.span);
953-
self.cfg.terminate(
954-
start_block,
955-
source_info,
956-
TerminatorKind::Goto { target: first_prebinding_block },
957-
);
936+
self.cfg.goto(start_block, source_info, first_prebinding_block);
958937
} else {
959938
*start_block = Some(first_prebinding_block);
960939
}
@@ -988,8 +967,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
988967
}
989968
}
990969

991-
let last_candidate = reachable_candidates.last().unwrap();
992970

971+
let last_candidate = reachable_candidates.last().unwrap();
993972
if let Some(otherwise) = last_candidate.otherwise_block {
994973
let source_info = self.source_info(last_candidate.span);
995974
let block = self.cfg.start_new_block();

src/librustc_mir/build/matches/util.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
109109
},
110110
);
111111
}
112-
_ => {
113-
self.cfg.terminate(
114-
from_block,
115-
source_info,
116-
TerminatorKind::Goto {
117-
target: real_target
118-
}
119-
);
120-
}
112+
_ => self.cfg.goto(from_block, source_info, real_target),
121113
}
122114
}
123115
}

src/librustc_mir/build/mod.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -606,14 +606,11 @@ where
606606
let fn_end = span.shrink_to_hi();
607607
let source_info = builder.source_info(fn_end);
608608
let return_block = builder.return_block();
609-
builder.cfg.terminate(block, source_info,
610-
TerminatorKind::Goto { target: return_block });
611-
builder.cfg.terminate(return_block, source_info,
612-
TerminatorKind::Return);
609+
builder.cfg.goto(block, source_info, return_block);
610+
builder.cfg.terminate(return_block, source_info, TerminatorKind::Return);
613611
// Attribute any unreachable codepaths to the function's closing brace
614612
if let Some(unreachable_block) = builder.cached_unreachable_block {
615-
builder.cfg.terminate(unreachable_block, source_info,
616-
TerminatorKind::Unreachable);
613+
builder.cfg.terminate(unreachable_block, source_info, TerminatorKind::Unreachable);
617614
}
618615
return_block.unit()
619616
}));

src/librustc_mir/build/scope.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -564,14 +564,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
564564
let source_info = scope.source_info(span);
565565
block = match scope.cached_exits.entry((target, region_scope)) {
566566
Entry::Occupied(e) => {
567-
self.cfg.terminate(block, source_info,
568-
TerminatorKind::Goto { target: *e.get() });
567+
self.cfg.goto(block, source_info, *e.get());
569568
return;
570569
}
571570
Entry::Vacant(v) => {
572571
let b = self.cfg.start_new_block();
573-
self.cfg.terminate(block, source_info,
574-
TerminatorKind::Goto { target: b });
572+
self.cfg.goto(block, source_info, b);
575573
v.insert(b);
576574
b
577575
}
@@ -596,8 +594,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
596594
scope = next_scope;
597595
}
598596

599-
let source_info = self.scopes.source_info(scope_count, span);
600-
self.cfg.terminate(block, source_info, TerminatorKind::Goto { target });
597+
self.cfg.goto(block, self.scopes.source_info(scope_count, span), target);
601598
}
602599

603600
/// Creates a path that performs all required cleanup for dropping a generator.
@@ -616,14 +613,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
616613

617614
while let Some(scope) = scopes.next() {
618615
block = if let Some(b) = scope.cached_generator_drop {
619-
self.cfg.terminate(block, src_info,
620-
TerminatorKind::Goto { target: b });
616+
self.cfg.goto(block, src_info, b);
621617
return Some(result);
622618
} else {
623619
let b = self.cfg.start_new_block();
624620
scope.cached_generator_drop = Some(b);
625-
self.cfg.terminate(block, src_info,
626-
TerminatorKind::Goto { target: b });
621+
self.cfg.goto(block, src_info, b);
627622
b
628623
};
629624

@@ -1243,8 +1238,7 @@ fn build_diverge_scope<'tcx>(cfg: &mut CFG<'tcx>,
12431238
// block for our StorageDead statements.
12441239
let block = cfg.start_new_cleanup_block();
12451240
let source_info = SourceInfo { span: DUMMY_SP, scope: source_scope };
1246-
cfg.terminate(block, source_info,
1247-
TerminatorKind::Goto { target: target });
1241+
cfg.goto(block, source_info, target);
12481242
target = block;
12491243
target_built_by_us = true;
12501244
}

0 commit comments

Comments
 (0)