Skip to content

Commit 6f38e9d

Browse files
authored
Rollup merge of rust-lang#124223 - Zalathar:conditional-let, r=compiler-errors
coverage: Branch coverage support for let-else and if-let This PR adds branch coverage instrumentation for let-else and if-let, including let-chains. This lifts two of the limitations listed at rust-lang#124118.
2 parents b923ea4 + 7c87ad0 commit 6f38e9d

File tree

6 files changed

+64
-13
lines changed

6 files changed

+64
-13
lines changed

compiler/rustc_mir_build/src/build/coverageinfo.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
mod mcdc;
21
use std::assert_matches::assert_matches;
32
use std::collections::hash_map::Entry;
43

54
use rustc_data_structures::fx::FxHashMap;
65
use rustc_middle::mir::coverage::{BlockMarkerId, BranchSpan, CoverageKind};
76
use rustc_middle::mir::{self, BasicBlock, SourceInfo, UnOp};
8-
use rustc_middle::thir::{ExprId, ExprKind, Thir};
7+
use rustc_middle::thir::{ExprId, ExprKind, Pat, Thir};
98
use rustc_middle::ty::TyCtxt;
109
use rustc_span::def_id::LocalDefId;
1110

1211
use crate::build::coverageinfo::mcdc::MCDCInfoBuilder;
1312
use crate::build::{Builder, CFG};
1413

14+
mod mcdc;
15+
1516
pub(crate) struct BranchInfoBuilder {
1617
/// Maps condition expressions to their enclosing `!`, for better instrumentation.
1718
nots: FxHashMap<ExprId, NotInfo>,
@@ -155,7 +156,7 @@ impl BranchInfoBuilder {
155156
}
156157
}
157158

158-
impl Builder<'_, '_> {
159+
impl<'tcx> Builder<'_, 'tcx> {
159160
/// If branch coverage is enabled, inject marker statements into `then_block`
160161
/// and `else_block`, and record their IDs in the table of branch spans.
161162
pub(crate) fn visit_coverage_branch_condition(
@@ -195,4 +196,23 @@ impl Builder<'_, '_> {
195196

196197
branch_info.add_two_way_branch(&mut self.cfg, source_info, then_block, else_block);
197198
}
199+
200+
/// If branch coverage is enabled, inject marker statements into `true_block`
201+
/// and `false_block`, and record their IDs in the table of branches.
202+
///
203+
/// Used to instrument let-else and if-let (including let-chains) for branch coverage.
204+
pub(crate) fn visit_coverage_conditional_let(
205+
&mut self,
206+
pattern: &Pat<'tcx>, // Pattern that has been matched when the true path is taken
207+
true_block: BasicBlock,
208+
false_block: BasicBlock,
209+
) {
210+
// Bail out if branch coverage is not enabled for this function.
211+
let Some(branch_info) = self.coverage_branch_info.as_mut() else { return };
212+
213+
// FIXME(#124144) This may need special handling when MC/DC is enabled.
214+
215+
let source_info = SourceInfo { span: pattern.span, scope: self.source_scope };
216+
branch_info.add_two_way_branch(&mut self.cfg, source_info, true_block, false_block);
217+
}
198218
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -2000,6 +2000,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
20002000
false,
20012001
);
20022002

2003+
// If branch coverage is enabled, record this branch.
2004+
self.visit_coverage_conditional_let(pat, post_guard_block, otherwise_post_guard_block);
2005+
20032006
post_guard_block.unit()
20042007
}
20052008

@@ -2492,6 +2495,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
24922495
None,
24932496
true,
24942497
);
2498+
2499+
// If branch coverage is enabled, record this branch.
2500+
this.visit_coverage_conditional_let(pattern, matching, failure);
2501+
24952502
this.break_for_else(failure, this.source_info(initializer_span));
24962503
matching.unit()
24972504
});

tests/coverage/branch/if-let.cov-map

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
Function name: if_let::if_let
2-
Raw bytes (38): 0x[01, 01, 02, 05, 09, 09, 02, 06, 01, 0c, 01, 01, 10, 02, 03, 11, 00, 12, 05, 00, 16, 00, 1b, 02, 00, 1c, 02, 06, 09, 02, 0c, 02, 06, 07, 03, 05, 01, 02]
2+
Raw bytes (45): 0x[01, 01, 02, 05, 09, 09, 02, 07, 01, 0c, 01, 01, 10, 20, 02, 09, 03, 0c, 00, 13, 02, 00, 11, 00, 12, 05, 00, 16, 00, 1b, 02, 00, 1c, 02, 06, 09, 02, 0c, 02, 06, 07, 03, 05, 01, 02]
33
Number of files: 1
44
- file 0 => global file 1
55
Number of expressions: 2
66
- expression 0 operands: lhs = Counter(1), rhs = Counter(2)
77
- expression 1 operands: lhs = Counter(2), rhs = Expression(0, Sub)
8-
Number of file 0 mappings: 6
8+
Number of file 0 mappings: 7
99
- Code(Counter(0)) at (prev + 12, 1) to (start + 1, 16)
10-
- Code(Expression(0, Sub)) at (prev + 3, 17) to (start + 0, 18)
10+
- Branch { true: Expression(0, Sub), false: Counter(2) } at (prev + 3, 12) to (start + 0, 19)
11+
true = (c1 - c2)
12+
false = c2
13+
- Code(Expression(0, Sub)) at (prev + 0, 17) to (start + 0, 18)
1114
= (c1 - c2)
1215
- Code(Counter(1)) at (prev + 0, 22) to (start + 0, 27)
1316
- Code(Expression(0, Sub)) at (prev + 0, 28) to (start + 2, 6)
@@ -17,20 +20,26 @@ Number of file 0 mappings: 6
1720
= (c2 + (c1 - c2))
1821

1922
Function name: if_let::if_let_chain
20-
Raw bytes (52): 0x[01, 01, 04, 01, 05, 05, 09, 0f, 0d, 05, 09, 08, 01, 17, 01, 00, 33, 02, 01, 11, 00, 12, 01, 00, 16, 00, 17, 0d, 01, 15, 00, 16, 02, 00, 1a, 00, 1b, 0d, 01, 05, 03, 06, 0f, 03, 0c, 02, 06, 0b, 03, 05, 01, 02]
23+
Raw bytes (66): 0x[01, 01, 04, 01, 05, 05, 09, 0f, 0d, 05, 09, 0a, 01, 17, 01, 00, 33, 20, 02, 05, 01, 0c, 00, 13, 02, 00, 11, 00, 12, 01, 00, 16, 00, 17, 20, 0d, 09, 01, 10, 00, 17, 0d, 00, 15, 00, 16, 02, 00, 1a, 00, 1b, 0d, 01, 05, 03, 06, 0f, 03, 0c, 02, 06, 0b, 03, 05, 01, 02]
2124
Number of files: 1
2225
- file 0 => global file 1
2326
Number of expressions: 4
2427
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
2528
- expression 1 operands: lhs = Counter(1), rhs = Counter(2)
2629
- expression 2 operands: lhs = Expression(3, Add), rhs = Counter(3)
2730
- expression 3 operands: lhs = Counter(1), rhs = Counter(2)
28-
Number of file 0 mappings: 8
31+
Number of file 0 mappings: 10
2932
- Code(Counter(0)) at (prev + 23, 1) to (start + 0, 51)
30-
- Code(Expression(0, Sub)) at (prev + 1, 17) to (start + 0, 18)
33+
- Branch { true: Expression(0, Sub), false: Counter(1) } at (prev + 1, 12) to (start + 0, 19)
34+
true = (c0 - c1)
35+
false = c1
36+
- Code(Expression(0, Sub)) at (prev + 0, 17) to (start + 0, 18)
3137
= (c0 - c1)
3238
- Code(Counter(0)) at (prev + 0, 22) to (start + 0, 23)
33-
- Code(Counter(3)) at (prev + 1, 21) to (start + 0, 22)
39+
- Branch { true: Counter(3), false: Counter(2) } at (prev + 1, 16) to (start + 0, 23)
40+
true = c3
41+
false = c2
42+
- Code(Counter(3)) at (prev + 0, 21) to (start + 0, 22)
3443
- Code(Expression(0, Sub)) at (prev + 0, 26) to (start + 0, 27)
3544
= (c0 - c1)
3645
- Code(Counter(3)) at (prev + 1, 5) to (start + 3, 6)

tests/coverage/branch/if-let.coverage

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
LL| |
1515
LL| 3| if let Some(x) = input {
1616
^2
17+
------------------
18+
| Branch (LL:12): [True: 2, False: 1]
19+
------------------
1720
LL| 2| say(x);
1821
LL| 2| } else {
1922
LL| 1| say("none");
@@ -24,8 +27,14 @@
2427
LL| 15|fn if_let_chain(a: Option<&str>, b: Option<&str>) {
2528
LL| 15| if let Some(x) = a
2629
^12
30+
------------------
31+
| Branch (LL:12): [True: 12, False: 3]
32+
------------------
2733
LL| 12| && let Some(y) = b
2834
^8
35+
------------------
36+
| Branch (LL:16): [True: 8, False: 4]
37+
------------------
2938
LL| 8| {
3039
LL| 8| say(x);
3140
LL| 8| say(y);

tests/coverage/branch/let-else.cov-map

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
Function name: let_else::let_else
2-
Raw bytes (38): 0x[01, 01, 02, 05, 09, 09, 02, 06, 01, 0c, 01, 01, 10, 02, 03, 0e, 00, 0f, 05, 00, 13, 00, 18, 09, 01, 09, 01, 0f, 02, 04, 05, 00, 0b, 07, 01, 01, 00, 02]
2+
Raw bytes (45): 0x[01, 01, 02, 05, 09, 09, 02, 07, 01, 0c, 01, 01, 10, 20, 02, 09, 03, 09, 00, 10, 02, 00, 0e, 00, 0f, 05, 00, 13, 00, 18, 09, 01, 09, 01, 0f, 02, 04, 05, 00, 0b, 07, 01, 01, 00, 02]
33
Number of files: 1
44
- file 0 => global file 1
55
Number of expressions: 2
66
- expression 0 operands: lhs = Counter(1), rhs = Counter(2)
77
- expression 1 operands: lhs = Counter(2), rhs = Expression(0, Sub)
8-
Number of file 0 mappings: 6
8+
Number of file 0 mappings: 7
99
- Code(Counter(0)) at (prev + 12, 1) to (start + 1, 16)
10-
- Code(Expression(0, Sub)) at (prev + 3, 14) to (start + 0, 15)
10+
- Branch { true: Expression(0, Sub), false: Counter(2) } at (prev + 3, 9) to (start + 0, 16)
11+
true = (c1 - c2)
12+
false = c2
13+
- Code(Expression(0, Sub)) at (prev + 0, 14) to (start + 0, 15)
1114
= (c1 - c2)
1215
- Code(Counter(1)) at (prev + 0, 19) to (start + 0, 24)
1316
- Code(Counter(2)) at (prev + 1, 9) to (start + 1, 15)

tests/coverage/branch/let-else.coverage

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
LL| |
1515
LL| 3| let Some(x) = value else {
1616
^2
17+
------------------
18+
| Branch (LL:9): [True: 2, False: 1]
19+
------------------
1720
LL| 1| say("none");
1821
LL| 1| return;
1922
LL| | };

0 commit comments

Comments
 (0)