Skip to content

Commit d6a25eb

Browse files
committed
Added smarter match guard branch coverage handling
1 parent 78a7fe1 commit d6a25eb

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

compiler/rustc_mir_build/src/build/coverageinfo.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ struct NotInfo {
4444
}
4545

4646
pub(crate) struct MatchArm {
47-
pub(crate) source_info: SourceInfo,
4847
pub(crate) sub_branches: Vec<MatchArmSubBranch>,
4948
}
5049

5150
#[derive(Debug)]
5251
pub(crate) struct MatchArmSubBranch {
5352
pub(crate) source_info: SourceInfo,
54-
pub(crate) block: BasicBlock,
53+
pub(crate) pre_binding_block: BasicBlock,
54+
pub(crate) branch_taken_block: BasicBlock,
5555
}
5656

5757
#[derive(Default)]
@@ -168,7 +168,6 @@ impl CoverageInfoBuilder {
168168
// Bail out if branch coverage is not enabled.
169169
let Some(branch_info) = self.branch_info.as_mut() else { return };
170170

171-
172171
// Avoid duplicates coverage markers.
173172
// When lowering match sub-branches (like or-patterns), `if` guards will
174173
// be added multiple times for each sub-branch
@@ -206,16 +205,20 @@ impl CoverageInfoBuilder {
206205

207206
let branch_arms = arms
208207
.iter()
209-
.flat_map(|MatchArm { source_info, sub_branches }| {
208+
.flat_map(|MatchArm { sub_branches }| {
210209
sub_branches
211210
.iter()
212211
.map(|sub_branch| {
213-
let block = sub_branch.block;
214-
215-
let pre_guard_marker =
216-
self.markers.inject_block_marker(cfg, sub_branch.source_info, block);
217-
let arm_taken_marker =
218-
self.markers.inject_block_marker(cfg, *source_info, block);
212+
let pre_guard_marker = self.markers.inject_block_marker(
213+
cfg,
214+
sub_branch.source_info,
215+
sub_branch.pre_binding_block,
216+
);
217+
let arm_taken_marker = self.markers.inject_block_marker(
218+
cfg,
219+
sub_branch.source_info,
220+
sub_branch.branch_taken_block,
221+
);
219222

220223
BranchArm {
221224
span: sub_branch.source_info.span,

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
453453
opt_scrutinee_place,
454454
);
455455

456-
let sub_branches: Vec<_> = branch
456+
let mut sub_branches: Vec<_> = branch
457457
.sub_branches
458458
.iter()
459459
.map(|b| coverageinfo::MatchArmSubBranch {
460460
source_info: this.source_info(b.span),
461-
block: b.success_block,
461+
pre_binding_block: b.success_block,
462+
branch_taken_block: b.success_block,
462463
})
463464
.collect();
464465

@@ -471,11 +472,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
471472
EmitStorageLive::Yes,
472473
);
473474

475+
// If the match arm has a guard, change the branch_taken_block for all of the
476+
// sub-branches to be the guard block.
477+
if arm.guard.is_some() {
478+
for sub_branch in sub_branches.iter_mut() {
479+
sub_branch.branch_taken_block = arm_block;
480+
}
481+
}
482+
474483
if let Some(coverage_match_arms) = coverage_match_arms.as_mut() {
475-
coverage_match_arms.push(coverageinfo::MatchArm {
476-
source_info: this.source_info(arm.pattern.span),
477-
sub_branches,
478-
})
484+
coverage_match_arms.push(coverageinfo::MatchArm { sub_branches });
479485
}
480486

481487
this.fixed_temps_scope = old_dedup_scope;

0 commit comments

Comments
 (0)