Skip to content

Commit 867950f

Browse files
committed
coverage: Move helper add_basic_coverage_block into a local closure
This also switches from `split_off(0)` to `std::mem::take` when emptying the accumulated list of blocks, because `split_off(0)` handles capacity in a way that is unintuitive when used in a loop.
1 parent 229d098 commit 867950f

File tree

1 file changed

+18
-22
lines changed
  • compiler/rustc_mir_transform/src/coverage

1 file changed

+18
-22
lines changed

compiler/rustc_mir_transform/src/coverage/graph.rs

+18-22
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_data_structures::captures::Captures;
22
use rustc_data_structures::graph::dominators::{self, Dominators};
33
use rustc_data_structures::graph::{self, GraphSuccessors, WithNumNodes, WithStartNode};
44
use rustc_index::bit_set::BitSet;
5-
use rustc_index::{IndexSlice, IndexVec};
5+
use rustc_index::IndexVec;
66
use rustc_middle::mir::{self, BasicBlock, Terminator, TerminatorKind};
77

88
use std::cmp::Ordering;
@@ -81,9 +81,23 @@ impl CoverageGraph {
8181
IndexVec<BasicBlock, Option<BasicCoverageBlock>>,
8282
) {
8383
let num_basic_blocks = mir_body.basic_blocks.len();
84-
let mut bcbs = IndexVec::with_capacity(num_basic_blocks);
84+
let mut bcbs = IndexVec::<BasicCoverageBlock, _>::with_capacity(num_basic_blocks);
8585
let mut bb_to_bcb = IndexVec::from_elem_n(None, num_basic_blocks);
8686

87+
let mut add_basic_coverage_block = |basic_blocks: &mut Vec<BasicBlock>| {
88+
// Take the accumulated list of blocks, leaving the vector empty
89+
// to be used by subsequent BCBs.
90+
let basic_blocks = std::mem::take(basic_blocks);
91+
92+
let bcb = bcbs.next_index();
93+
for &bb in basic_blocks.iter() {
94+
bb_to_bcb[bb] = Some(bcb);
95+
}
96+
let bcb_data = BasicCoverageBlockData::from(basic_blocks);
97+
debug!("adding bcb{}: {:?}", bcb.index(), bcb_data);
98+
bcbs.push(bcb_data);
99+
};
100+
87101
// Walk the MIR CFG using a Preorder traversal, which starts from `START_BLOCK` and follows
88102
// each block terminator's `successors()`. Coverage spans must map to actual source code,
89103
// so compiler generated blocks and paths can be ignored. To that end, the CFG traversal
@@ -113,38 +127,20 @@ impl CoverageGraph {
113127
predecessors = ?&mir_body.basic_blocks.predecessors()[bb],
114128
"can't chain from {prev:?} to {bb:?}"
115129
);
116-
Self::add_basic_coverage_block(
117-
&mut bcbs,
118-
&mut bb_to_bcb,
119-
basic_blocks.split_off(0),
120-
);
130+
add_basic_coverage_block(&mut basic_blocks);
121131
}
122132

123133
basic_blocks.push(bb);
124134
}
125135

126136
if !basic_blocks.is_empty() {
127137
debug!("flushing accumulated blocks into one last BCB");
128-
Self::add_basic_coverage_block(&mut bcbs, &mut bb_to_bcb, basic_blocks.split_off(0));
138+
add_basic_coverage_block(&mut basic_blocks);
129139
}
130140

131141
(bcbs, bb_to_bcb)
132142
}
133143

134-
fn add_basic_coverage_block(
135-
bcbs: &mut IndexVec<BasicCoverageBlock, BasicCoverageBlockData>,
136-
bb_to_bcb: &mut IndexSlice<BasicBlock, Option<BasicCoverageBlock>>,
137-
basic_blocks: Vec<BasicBlock>,
138-
) {
139-
let bcb = bcbs.next_index();
140-
for &bb in basic_blocks.iter() {
141-
bb_to_bcb[bb] = Some(bcb);
142-
}
143-
let bcb_data = BasicCoverageBlockData::from(basic_blocks);
144-
debug!("adding bcb{}: {:?}", bcb.index(), bcb_data);
145-
bcbs.push(bcb_data);
146-
}
147-
148144
#[inline(always)]
149145
pub fn iter_enumerated(
150146
&self,

0 commit comments

Comments
 (0)