Skip to content

Commit c20b65c

Browse files
authored
Rollup merge of rust-lang#67499 - Centril:mir-match-clean, r=matthewjasper
Misc MIR building cleanups r? @matthewjasper
2 parents 8f17dcb + f5a8d1a commit c20b65c

File tree

8 files changed

+49
-103
lines changed

8 files changed

+49
-103
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

+26-51
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ mod simplify;
2626
mod test;
2727
mod util;
2828

29+
use itertools::Itertools;
2930
use std::convert::TryFrom;
3031

3132
impl<'a, 'tcx> Builder<'a, 'tcx> {
@@ -258,11 +259,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
258259
scrutinee_span,
259260
match_scope,
260261
);
261-
this.cfg.terminate(
262-
binding_end,
263-
source_info,
264-
TerminatorKind::Goto { target: arm_block },
265-
);
262+
this.cfg.goto(binding_end, source_info, arm_block);
266263
}
267264
}
268265

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

280277
for arm_block in arm_end_blocks {
281-
self.cfg.terminate(
282-
unpack!(arm_block),
283-
outer_source_info,
284-
TerminatorKind::Goto { target: end_block },
285-
);
278+
self.cfg.goto(unpack!(arm_block), outer_source_info, end_block);
286279
}
287280

288281
self.source_scope = outer_source_info.scope;
@@ -822,45 +815,34 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
822815
);
823816
let (matched_candidates, unmatched_candidates) = candidates.split_at_mut(fully_matched);
824817

825-
let block: BasicBlock;
826-
827-
if !matched_candidates.is_empty() {
818+
let block: BasicBlock = if !matched_candidates.is_empty() {
828819
let otherwise_block = self.select_matched_candidates(
829820
matched_candidates,
830821
start_block,
831822
fake_borrows,
832823
);
833824

834825
if let Some(last_otherwise_block) = otherwise_block {
835-
block = last_otherwise_block
826+
last_otherwise_block
836827
} else {
837828
// Any remaining candidates are unreachable.
838829
if unmatched_candidates.is_empty() {
839830
return;
840831
}
841-
block = self.cfg.start_new_block();
842-
};
832+
self.cfg.start_new_block()
833+
}
843834
} else {
844-
block = *start_block.get_or_insert_with(|| self.cfg.start_new_block());
845-
}
835+
*start_block.get_or_insert_with(|| self.cfg.start_new_block())
836+
};
846837

847838
// If there are no candidates that still need testing, we're
848839
// done. Since all matches are exhaustive, execution should
849840
// never reach this point.
850841
if unmatched_candidates.is_empty() {
851842
let source_info = self.source_info(span);
852-
if let Some(otherwise) = otherwise_block {
853-
self.cfg.terminate(
854-
block,
855-
source_info,
856-
TerminatorKind::Goto { target: otherwise },
857-
);
858-
} else {
859-
self.cfg.terminate(
860-
block,
861-
source_info,
862-
TerminatorKind::Unreachable,
863-
)
843+
match otherwise_block {
844+
Some(otherwise) => self.cfg.goto(block, source_info, otherwise),
845+
None => self.cfg.terminate(block, source_info, TerminatorKind::Unreachable),
864846
}
865847
return;
866848
}
@@ -885,7 +867,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
885867
/// ...
886868
///
887869
/// We generate real edges from:
888-
/// * `block` to the prebinding_block of the first pattern,
870+
/// * `start_block` to the `prebinding_block` of the first pattern,
889871
/// * the otherwise block of the first pattern to the second pattern,
890872
/// * the otherwise block of the third pattern to the a block with an
891873
/// Unreachable terminator.
@@ -948,32 +930,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
948930
let first_candidate = &reachable_candidates[0];
949931
let first_prebinding_block = first_candidate.pre_binding_block;
950932

933+
// `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
}
961940

962-
for window in reachable_candidates.windows(2) {
963-
if let [first_candidate, second_candidate] = window {
964-
let source_info = self.source_info(first_candidate.span);
965-
if let Some(otherwise_block) = first_candidate.otherwise_block {
966-
self.false_edges(
967-
otherwise_block,
968-
second_candidate.pre_binding_block,
969-
first_candidate.next_candidate_pre_binding_block,
970-
source_info,
971-
);
972-
} else {
973-
bug!("candidate other than the last has no guard");
974-
}
941+
for (first_candidate, second_candidate) in reachable_candidates.iter().tuple_windows() {
942+
let source_info = self.source_info(first_candidate.span);
943+
if let Some(otherwise_block) = first_candidate.otherwise_block {
944+
self.false_edges(
945+
otherwise_block,
946+
second_candidate.pre_binding_block,
947+
first_candidate.next_candidate_pre_binding_block,
948+
source_info,
949+
);
975950
} else {
976-
bug!("<[_]>::windows returned incorrectly sized window");
951+
bug!("candidate other than the last has no guard");
977952
}
978953
}
979954

@@ -992,8 +967,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
992967
}
993968
}
994969

995-
let last_candidate = reachable_candidates.last().unwrap();
996970

971+
let last_candidate = reachable_candidates.last().unwrap();
997972
if let Some(otherwise) = last_candidate.otherwise_block {
998973
let source_info = self.source_info(last_candidate.span);
999974
let block = self.cfg.start_new_block();

src/librustc_mir/build/matches/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
3030
Test {
3131
span: match_pair.pattern.span,
3232
kind: TestKind::Switch {
33-
adt_def: adt_def.clone(),
33+
adt_def,
3434
variants: BitSet::new_empty(adt_def.variants.len()),
3535
},
3636
}

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)