Skip to content

Commit 40f0f41

Browse files
committed
Tighten the loop a little bit
1 parent c64d27e commit 40f0f41

File tree

2 files changed

+13
-26
lines changed

2 files changed

+13
-26
lines changed

compiler/rustc_mir_build/src/build/matches/mod.rs

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,8 +1065,6 @@ pub(crate) struct ArmHasGuard(pub(crate) bool);
10651065

10661066
/// A single step in the match algorithm.
10671067
pub(crate) struct MatchAutomatonStep<'a, 'c, 'pat, 'tcx> {
1068-
/// FIXME: probably the span of the match.
1069-
span: Span,
10701068
/// Perform this test...
10711069
test: Test<'tcx>,
10721070
/// ... on this place...
@@ -1112,7 +1110,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
11121110
/// exhaustive in Rust. But during processing we sometimes divide
11131111
/// up the list of candidates and recurse with a non-exhaustive
11141112
/// list. This is important to keep the size of the generated code
1115-
/// under control. See [`Builder::test_candidates`] for more details.
1113+
/// under control. See [`Builder::build_test_step`] for more details.
11161114
///
11171115
/// If `fake_borrows` is `Some`, then places which need fake borrows
11181116
/// will be added to it.
@@ -1348,7 +1346,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
13481346
}
13491347

13501348
/// Tests a candidate where there are only or-patterns left to test, or
1351-
/// forwards to [Builder::test_candidates].
1349+
/// forwards to [Builder::build_test_step].
13521350
///
13531351
/// Given a pattern `(P | Q, R | S)` we (in principle) generate a CFG like
13541352
/// so:
@@ -1416,14 +1414,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
14161414
match first_candidate.match_pairs[0].pattern.kind {
14171415
PatKind::Or { .. } => (),
14181416
_ => {
1419-
self.test_candidates(
1420-
span,
1421-
scrutinee_span,
1422-
candidates,
1423-
block,
1424-
otherwise_block,
1425-
fake_borrows,
1426-
);
1417+
let step = self.build_test_step(candidates, otherwise_block, fake_borrows);
1418+
self.perform_test(span, scrutinee_span, block, step);
14271419
return;
14281420
}
14291421
}
@@ -1653,15 +1645,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
16531645
/// In addition to avoiding exponential-time blowups, this algorithm
16541646
/// also has the nice property that each guard and arm is only generated
16551647
/// once.
1656-
fn test_candidates<'pat, 'b, 'c>(
1648+
fn build_test_step<'pat, 'b, 'c>(
16571649
&mut self,
1658-
span: Span,
1659-
scrutinee_span: Span,
16601650
mut candidates: &'b mut [&'c mut Candidate<'pat, 'tcx>],
1661-
block: BasicBlock,
1662-
otherwise_block: &mut Option<BasicBlock>,
1663-
fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
1664-
) {
1651+
otherwise_block: &'b mut Option<BasicBlock>,
1652+
fake_borrows: &'b mut Option<FxIndexSet<Place<'tcx>>>,
1653+
) -> MatchAutomatonStep<'b, 'c, 'pat, 'tcx> {
16651654
// extract the match-pair from the highest priority candidate
16661655
let match_pair = &candidates.first().unwrap().match_pairs[0];
16671656
let mut test = self.test(match_pair);
@@ -1700,7 +1689,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
17001689
// those N possible outcomes, create a (initially empty)
17011690
// vector of candidates. Those are the candidates that still
17021691
// apply if the test has that particular outcome.
1703-
debug!("test_candidates: test={:?} match_pair={:?}", test, match_pair);
1692+
debug!("build_test_step: test={:?} match_pair={:?}", test, match_pair);
17041693
let mut target_candidates: Vec<Vec<&mut Candidate<'pat, 'tcx>>> = vec![];
17051694
target_candidates.resize_with(test.targets(), Default::default);
17061695

@@ -1726,16 +1715,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
17261715
debug!("tested_candidates: {}", total_candidate_count - candidates.len());
17271716
debug!("untested_candidates: {}", candidates.len());
17281717

1729-
let step = MatchAutomatonStep {
1730-
span,
1718+
MatchAutomatonStep {
17311719
test,
17321720
place: match_place,
17331721
remaining_candidates: candidates,
17341722
target_candidates,
17351723
otherwise_block,
17361724
fake_borrows,
1737-
};
1738-
self.perform_test(span, scrutinee_span, block, step);
1725+
}
17391726
}
17401727

17411728
/// Determine the fake borrows that are needed from a set of places that

compiler/rustc_mir_build/src/build/matches/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
186186
if !candidates.is_empty() {
187187
let candidate_start = this.cfg.start_new_block();
188188
this.match_candidates(
189-
step.span,
189+
match_start_span,
190190
scrutinee_span,
191191
candidate_start,
192192
remainder_start,
@@ -203,7 +203,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
203203
if !step.remaining_candidates.is_empty() {
204204
let remainder_start = remainder_start.unwrap_or_else(|| this.cfg.start_new_block());
205205
this.match_candidates(
206-
step.span,
206+
match_start_span,
207207
scrutinee_span,
208208
remainder_start,
209209
step.otherwise_block,

0 commit comments

Comments
 (0)