Skip to content

Commit 393fb1f

Browse files
committed
coverage: Flatten BcbMappingKind into mappings::CodeMapping
Now that branch and MC/DC mappings have been split out into separate types and vectors, this enum is no longer needed, since it only represents ordinary "code" regions. (We can revisit this decision if we ever add support for other region kinds, such as skipped regions or expansion regions. But at that point, we might just add new structs/vectors for those kinds as well.)
1 parent 1faabe5 commit 393fb1f

File tree

3 files changed

+20
-32
lines changed

3 files changed

+20
-32
lines changed

compiler/rustc_mir_transform/src/coverage/mappings.rs

+12-22
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,14 @@ use crate::coverage::spans::{
1313
};
1414
use crate::coverage::ExtractedHirInfo;
1515

16-
#[derive(Clone, Copy, Debug)]
17-
pub(super) enum BcbMappingKind {
18-
/// Associates an ordinary executable code span with its corresponding BCB.
19-
Code(BasicCoverageBlock),
20-
//
21-
// Branch and MC/DC mappings are more complex, so they are represented
22-
// separately.
23-
}
24-
16+
/// Associates an ordinary executable code span with its corresponding BCB.
2517
#[derive(Debug)]
26-
pub(super) struct BcbMapping {
27-
pub(super) kind: BcbMappingKind,
18+
pub(super) struct CodeMapping {
2819
pub(super) span: Span,
20+
pub(super) bcb: BasicCoverageBlock,
2921
}
3022

31-
/// This is separate from [`BcbMappingKind`] to help prepare for larger changes
23+
/// This is separate from [`MCDCBranch`] to help prepare for larger changes
3224
/// that will be needed for improved branch coverage in the future.
3325
/// (See <https://github.com/rust-lang/rust/pull/124217>.)
3426
#[derive(Debug)]
@@ -62,7 +54,7 @@ pub(super) struct MCDCDecision {
6254

6355
pub(super) struct CoverageSpans {
6456
bcb_has_mappings: BitSet<BasicCoverageBlock>,
65-
pub(super) mappings: Vec<BcbMapping>,
57+
pub(super) code_mappings: Vec<CodeMapping>,
6658
pub(super) branch_pairs: Vec<BcbBranchPair>,
6759
test_vector_bitmap_bytes: u32,
6860
pub(super) mcdc_branches: Vec<MCDCBranch>,
@@ -88,7 +80,7 @@ pub(super) fn generate_coverage_spans(
8880
hir_info: &ExtractedHirInfo,
8981
basic_coverage_blocks: &CoverageGraph,
9082
) -> Option<CoverageSpans> {
91-
let mut mappings = vec![];
83+
let mut code_mappings = vec![];
9284
let mut branch_pairs = vec![];
9385
let mut mcdc_branches = vec![];
9486
let mut mcdc_decisions = vec![];
@@ -99,10 +91,10 @@ pub(super) fn generate_coverage_spans(
9991
// outer function will be unhelpful, so just keep the signature span
10092
// and ignore all of the spans in the MIR body.
10193
if let Some(span) = hir_info.fn_sig_span_extended {
102-
mappings.push(BcbMapping { kind: BcbMappingKind::Code(START_BCB), span });
94+
code_mappings.push(CodeMapping { span, bcb: START_BCB });
10395
}
10496
} else {
105-
extract_refined_covspans(mir_body, hir_info, basic_coverage_blocks, &mut mappings);
97+
extract_refined_covspans(mir_body, hir_info, basic_coverage_blocks, &mut code_mappings);
10698

10799
branch_pairs.extend(extract_branch_pairs(mir_body, hir_info, basic_coverage_blocks));
108100

@@ -115,7 +107,7 @@ pub(super) fn generate_coverage_spans(
115107
);
116108
}
117109

118-
if mappings.is_empty()
110+
if code_mappings.is_empty()
119111
&& branch_pairs.is_empty()
120112
&& mcdc_branches.is_empty()
121113
&& mcdc_decisions.is_empty()
@@ -129,10 +121,8 @@ pub(super) fn generate_coverage_spans(
129121
bcb_has_mappings.insert(bcb);
130122
};
131123

132-
for &BcbMapping { kind, span: _ } in &mappings {
133-
match kind {
134-
BcbMappingKind::Code(bcb) => insert(bcb),
135-
}
124+
for &CodeMapping { span: _, bcb } in &code_mappings {
125+
insert(bcb);
136126
}
137127
for &BcbBranchPair { true_bcb, false_bcb, .. } in &branch_pairs {
138128
insert(true_bcb);
@@ -154,7 +144,7 @@ pub(super) fn generate_coverage_spans(
154144

155145
Some(CoverageSpans {
156146
bcb_has_mappings,
157-
mappings,
147+
code_mappings,
158148
branch_pairs,
159149
test_vector_bitmap_bytes,
160150
mcdc_branches,

compiler/rustc_mir_transform/src/coverage/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod tests;
99

1010
use self::counters::{CounterIncrementSite, CoverageCounters};
1111
use self::graph::{BasicCoverageBlock, CoverageGraph};
12-
use self::mappings::{BcbBranchPair, BcbMapping, BcbMappingKind, CoverageSpans};
12+
use self::mappings::{BcbBranchPair, CoverageSpans};
1313

1414
use crate::MirPass;
1515

@@ -150,12 +150,10 @@ fn create_mappings<'tcx>(
150150

151151
let mut mappings = Vec::new();
152152

153-
mappings.extend(coverage_spans.mappings.iter().filter_map(
154-
|&BcbMapping { kind: bcb_mapping_kind, span }| {
155-
let kind = match bcb_mapping_kind {
156-
BcbMappingKind::Code(bcb) => MappingKind::Code(term_for_bcb(bcb)),
157-
};
153+
mappings.extend(coverage_spans.code_mappings.iter().filter_map(
154+
|&mappings::CodeMapping { span, bcb }| {
158155
let code_region = region_for_span(span)?;
156+
let kind = MappingKind::Code(term_for_bcb(bcb));
159157
Some(Mapping { kind, code_region })
160158
},
161159
));

compiler/rustc_mir_transform/src/coverage/spans.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_middle::mir;
22
use rustc_span::{BytePos, Span};
33

44
use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph};
5-
use crate::coverage::mappings::{BcbMapping, BcbMappingKind};
5+
use crate::coverage::mappings;
66
use crate::coverage::spans::from_mir::SpanFromMir;
77
use crate::coverage::ExtractedHirInfo;
88

@@ -17,14 +17,14 @@ pub(super) fn extract_refined_covspans(
1717
mir_body: &mir::Body<'_>,
1818
hir_info: &ExtractedHirInfo,
1919
basic_coverage_blocks: &CoverageGraph,
20-
mappings: &mut impl Extend<BcbMapping>,
20+
code_mappings: &mut impl Extend<mappings::CodeMapping>,
2121
) {
2222
let sorted_spans =
2323
from_mir::mir_to_initial_sorted_coverage_spans(mir_body, hir_info, basic_coverage_blocks);
2424
let coverage_spans = SpansRefiner::refine_sorted_spans(sorted_spans);
25-
mappings.extend(coverage_spans.into_iter().map(|RefinedCovspan { bcb, span, .. }| {
25+
code_mappings.extend(coverage_spans.into_iter().map(|RefinedCovspan { bcb, span, .. }| {
2626
// Each span produced by the generator represents an ordinary code region.
27-
BcbMapping { kind: BcbMappingKind::Code(bcb), span }
27+
mappings::CodeMapping { span, bcb }
2828
}));
2929
}
3030

0 commit comments

Comments
 (0)