Skip to content

Commit f214497

Browse files
committed
coverage: Replace ShortCircuitPreorder with a single function
Instead of defining a named struct, we can use `std::iter::from_fn` and store intermediate state in a closure.
1 parent fa6d1e7 commit f214497

File tree

1 file changed

+17
-55
lines changed
  • compiler/rustc_mir_transform/src/coverage

1 file changed

+17
-55
lines changed

compiler/rustc_mir_transform/src/coverage/graph.rs

Lines changed: 17 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use rustc_data_structures::captures::Captures;
12
use rustc_data_structures::graph::dominators::{self, Dominators};
23
use rustc_data_structures::graph::{self, GraphSuccessors, WithNumNodes, WithStartNode};
34
use rustc_index::bit_set::BitSet;
45
use rustc_index::{IndexSlice, IndexVec};
5-
use rustc_middle::mir::{self, BasicBlock, BasicBlockData, Terminator, TerminatorKind};
6+
use rustc_middle::mir::{self, BasicBlock, Terminator, TerminatorKind};
67

78
use std::cmp::Ordering;
89
use std::ops::{Index, IndexMut};
@@ -80,10 +81,9 @@ impl CoverageGraph {
8081
// intentionally omits unwind paths.
8182
// FIXME(#78544): MIR InstrumentCoverage: Improve coverage of `#[should_panic]` tests and
8283
// `catch_unwind()` handlers.
83-
let mir_cfg_without_unwind = ShortCircuitPreorder::new(&mir_body, bcb_filtered_successors);
8484

8585
let mut basic_blocks = Vec::new();
86-
for (bb, data) in mir_cfg_without_unwind {
86+
for bb in short_circuit_preorder(mir_body, bcb_filtered_successors) {
8787
if let Some(last) = basic_blocks.last() {
8888
let predecessors = &mir_body.basic_blocks.predecessors()[bb];
8989
if predecessors.len() > 1 || !predecessors.contains(last) {
@@ -109,7 +109,7 @@ impl CoverageGraph {
109109
}
110110
basic_blocks.push(bb);
111111

112-
let term = data.terminator();
112+
let term = mir_body[bb].terminator();
113113

114114
match term.kind {
115115
TerminatorKind::Return { .. }
@@ -553,66 +553,28 @@ pub(super) fn find_loop_backedges(
553553
backedges
554554
}
555555

556-
pub struct ShortCircuitPreorder<
557-
'a,
558-
'tcx,
559-
F: Fn(&'a mir::Body<'tcx>, &'a TerminatorKind<'tcx>) -> Box<dyn Iterator<Item = BasicBlock> + 'a>,
560-
> {
556+
fn short_circuit_preorder<'a, 'tcx, F, Iter>(
561557
body: &'a mir::Body<'tcx>,
562-
visited: BitSet<BasicBlock>,
563-
worklist: Vec<BasicBlock>,
564558
filtered_successors: F,
565-
}
566-
567-
impl<
568-
'a,
569-
'tcx,
570-
F: Fn(&'a mir::Body<'tcx>, &'a TerminatorKind<'tcx>) -> Box<dyn Iterator<Item = BasicBlock> + 'a>,
571-
> ShortCircuitPreorder<'a, 'tcx, F>
572-
{
573-
pub fn new(
574-
body: &'a mir::Body<'tcx>,
575-
filtered_successors: F,
576-
) -> ShortCircuitPreorder<'a, 'tcx, F> {
577-
let worklist = vec![mir::START_BLOCK];
578-
579-
ShortCircuitPreorder {
580-
body,
581-
visited: BitSet::new_empty(body.basic_blocks.len()),
582-
worklist,
583-
filtered_successors,
584-
}
585-
}
586-
}
587-
588-
impl<
589-
'a,
590-
'tcx,
591-
F: Fn(&'a mir::Body<'tcx>, &'a TerminatorKind<'tcx>) -> Box<dyn Iterator<Item = BasicBlock> + 'a>,
592-
> Iterator for ShortCircuitPreorder<'a, 'tcx, F>
559+
) -> impl Iterator<Item = BasicBlock> + Captures<'a> + Captures<'tcx>
560+
where
561+
F: Fn(&'a mir::Body<'tcx>, &'a TerminatorKind<'tcx>) -> Iter,
562+
Iter: Iterator<Item = BasicBlock>,
593563
{
594-
type Item = (BasicBlock, &'a BasicBlockData<'tcx>);
564+
let mut visited = BitSet::new_empty(body.basic_blocks.len());
565+
let mut worklist = vec![mir::START_BLOCK];
595566

596-
fn next(&mut self) -> Option<(BasicBlock, &'a BasicBlockData<'tcx>)> {
597-
while let Some(idx) = self.worklist.pop() {
598-
if !self.visited.insert(idx) {
567+
std::iter::from_fn(move || {
568+
while let Some(bb) = worklist.pop() {
569+
if !visited.insert(bb) {
599570
continue;
600571
}
601572

602-
let data = &self.body[idx];
603-
604-
if let Some(ref term) = data.terminator {
605-
self.worklist.extend((self.filtered_successors)(&self.body, &term.kind));
606-
}
573+
worklist.extend(filtered_successors(body, &body[bb].terminator().kind));
607574

608-
return Some((idx, data));
575+
return Some(bb);
609576
}
610577

611578
None
612-
}
613-
614-
fn size_hint(&self) -> (usize, Option<usize>) {
615-
let size = self.body.basic_blocks.len() - self.visited.count();
616-
(size, Some(size))
617-
}
579+
})
618580
}

0 commit comments

Comments
 (0)