Skip to content

Commit d2ac11c

Browse files
committed
Cleanups
1 parent 1897657 commit d2ac11c

File tree

3 files changed

+23
-28
lines changed

3 files changed

+23
-28
lines changed

src/librustc_mir/build/matches/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
100100
.collect();
101101

102102
// create binding start block for link them by false edges
103-
let candidate_count = arms.iter().fold(0, |ac, c| ac + c.patterns.len());
103+
let candidate_count = arms.iter().map(|c| c.patterns.len()).sum::<usize>();
104104
let pre_binding_blocks: Vec<_> = (0..=candidate_count)
105105
.map(|_| self.cfg.start_new_block())
106106
.collect();
@@ -337,7 +337,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
337337

338338
pub fn place_into_pattern(
339339
&mut self,
340-
mut block: BasicBlock,
340+
block: BasicBlock,
341341
irrefutable_pat: Pattern<'tcx>,
342342
initializer: &Place<'tcx>,
343343
set_match_place: bool,
@@ -359,7 +359,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
359359

360360
// Simplify the candidate. Since the pattern is irrefutable, this should
361361
// always convert all match-pairs into bindings.
362-
unpack!(block = self.simplify_candidate(block, &mut candidate));
362+
self.simplify_candidate(&mut candidate);
363363

364364
if !candidate.match_pairs.is_empty() {
365365
span_bug!(
@@ -745,7 +745,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
745745
// complete, all the match pairs which remain require some
746746
// form of test, whether it be a switch or pattern comparison.
747747
for candidate in &mut candidates {
748-
unpack!(block = self.simplify_candidate(block, candidate));
748+
self.simplify_candidate(candidate);
749749
}
750750

751751
// The candidates are sorted by priority. Check to see
@@ -1035,7 +1035,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
10351035
test, match_pair
10361036
);
10371037
let target_blocks = self.perform_test(block, &match_pair.place, &test);
1038-
let mut target_candidates: Vec<_> = (0..target_blocks.len()).map(|_| vec![]).collect();
1038+
let mut target_candidates = vec![vec![]; target_blocks.len()];
10391039

10401040
// Sort the candidates into the appropriate vector in
10411041
// `target_candidates`. Note that at some point we may

src/librustc_mir/build/matches/simplify.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222
//! sort of test: for example, testing which variant an enum is, or
2323
//! testing a value against a constant.
2424
25-
use build::{BlockAnd, BlockAndExtension, Builder};
25+
use build::Builder;
2626
use build::matches::{Ascription, Binding, MatchPair, Candidate};
2727
use hair::*;
28-
use rustc::mir::*;
2928
use rustc::ty;
3029
use rustc::ty::layout::{Integer, IntegerExt, Size};
3130
use syntax::attr::{SignedInt, UnsignedInt};
@@ -35,24 +34,23 @@ use std::mem;
3534

3635
impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
3736
pub fn simplify_candidate<'pat>(&mut self,
38-
block: BasicBlock,
39-
candidate: &mut Candidate<'pat, 'tcx>)
40-
-> BlockAnd<()> {
37+
candidate: &mut Candidate<'pat, 'tcx>) {
4138
// repeatedly simplify match pairs until fixed point is reached
4239
loop {
4340
let match_pairs = mem::replace(&mut candidate.match_pairs, vec![]);
44-
let mut progress = match_pairs.len(); // count how many were simplified
41+
let mut changed = false;
4542
for match_pair in match_pairs {
4643
match self.simplify_match_pair(match_pair, candidate) {
47-
Ok(()) => {}
44+
Ok(()) => {
45+
changed = true;
46+
}
4847
Err(match_pair) => {
4948
candidate.match_pairs.push(match_pair);
50-
progress -= 1; // this one was not simplified
5149
}
5250
}
5351
}
54-
if progress == 0 {
55-
return block.unit(); // if we were not able to simplify any, done.
52+
if !changed {
53+
return; // if we were not able to simplify any, done.
5654
}
5755
}
5856
}

src/librustc_mir/build/matches/test.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,18 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
200200
for (idx, discr) in adt_def.discriminants(tcx) {
201201
target_blocks.push(if variants.contains(idx) {
202202
values.push(discr.val);
203-
targets.push(self.cfg.start_new_block());
204-
*targets.last().unwrap()
203+
let block = self.cfg.start_new_block();
204+
targets.push(block);
205+
block
205206
} else {
206-
if otherwise_block.is_none() {
207-
otherwise_block = Some(self.cfg.start_new_block());
208-
}
209-
otherwise_block.unwrap()
207+
*otherwise_block
208+
.get_or_insert_with(|| self.cfg.start_new_block())
210209
});
211210
}
212-
if let Some(otherwise_block) = otherwise_block {
213-
targets.push(otherwise_block);
214-
} else {
215-
targets.push(self.unreachable_block());
216-
}
211+
targets.push(
212+
otherwise_block
213+
.unwrap_or_else(|| self.unreachable_block()),
214+
);
217215
debug!("num_enum_variants: {}, tested variants: {:?}, variants: {:?}",
218216
num_enum_variants, values, variants);
219217
let discr_ty = adt_def.repr.discr_type().to_ty(tcx);
@@ -490,8 +488,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
490488
// away.)
491489
let tested_match_pair = candidate.match_pairs.iter()
492490
.enumerate()
493-
.filter(|&(_, mp)| mp.place == *test_place)
494-
.next();
491+
.find(|&(_, mp)| mp.place == *test_place);
495492
let (match_pair_index, match_pair) = match tested_match_pair {
496493
Some(pair) => pair,
497494
None => {

0 commit comments

Comments
 (0)