Skip to content

Commit 10cd5d6

Browse files
committed
coverage: Eagerly deduplicate covspans with the same span
1 parent bcb3545 commit 10cd5d6

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

compiler/rustc_mir_transform/src/coverage/spans/from_mir.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,19 @@ pub(super) fn mir_to_initial_sorted_coverage_spans(
5252
// - Span A extends further left, or
5353
// - Both have the same start and span A extends further right
5454
.then_with(|| Ord::cmp(&a.span.hi(), &b.span.hi()).reverse())
55-
// If both spans are equal, sort the BCBs in dominator order,
56-
// so that dominating BCBs come before other BCBs they dominate.
57-
.then_with(|| basic_coverage_blocks.cmp_in_dominator_order(a.bcb, b.bcb))
58-
// If two spans are otherwise identical, put closure spans first,
59-
// as this seems to be what the refinement step expects.
55+
// If two spans have the same lo & hi, put closure spans first,
56+
// as they take precedence over non-closure spans.
6057
.then_with(|| Ord::cmp(&a.is_closure, &b.is_closure).reverse())
58+
// After deduplication, we want to keep only the most-dominated BCB.
59+
.then_with(|| basic_coverage_blocks.cmp_in_dominator_order(a.bcb, b.bcb).reverse())
6160
});
6261

62+
// Among covspans with the same span, keep only one. Closure spans take
63+
// precedence, otherwise keep the one with the most-dominated BCB.
64+
// (Ideally we should try to preserve _all_ non-dominating BCBs, but that
65+
// requires a lot more complexity in the span refiner, for little benefit.)
66+
initial_spans.dedup_by(|b, a| a.span.source_equal(b.span));
67+
6368
initial_spans
6469
}
6570

tests/coverage/closure_macro.cov-map

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@ Number of file 0 mappings: 1
77
- Code(Counter(0)) at (prev + 29, 1) to (start + 2, 2)
88

99
Function name: closure_macro::main
10-
Raw bytes (43): 0x[01, 01, 02, 01, 05, 05, 02, 07, 01, 21, 01, 01, 21, 02, 02, 09, 00, 0f, 05, 00, 12, 00, 13, 02, 00, 12, 00, 13, 05, 00, 54, 00, 55, 02, 02, 09, 02, 0b, 07, 03, 01, 00, 02]
10+
Raw bytes (38): 0x[01, 01, 02, 01, 05, 05, 02, 06, 01, 21, 01, 01, 21, 02, 02, 09, 00, 12, 02, 00, 0f, 00, 54, 05, 00, 54, 00, 55, 02, 02, 09, 02, 0b, 07, 03, 01, 00, 02]
1111
Number of files: 1
1212
- file 0 => global file 1
1313
Number of expressions: 2
1414
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
1515
- expression 1 operands: lhs = Counter(1), rhs = Expression(0, Sub)
16-
Number of file 0 mappings: 7
16+
Number of file 0 mappings: 6
1717
- Code(Counter(0)) at (prev + 33, 1) to (start + 1, 33)
18-
- Code(Expression(0, Sub)) at (prev + 2, 9) to (start + 0, 15)
18+
- Code(Expression(0, Sub)) at (prev + 2, 9) to (start + 0, 18)
1919
= (c0 - c1)
20-
- Code(Counter(1)) at (prev + 0, 18) to (start + 0, 19)
21-
- Code(Expression(0, Sub)) at (prev + 0, 18) to (start + 0, 19)
20+
- Code(Expression(0, Sub)) at (prev + 0, 15) to (start + 0, 84)
2221
= (c0 - c1)
2322
- Code(Counter(1)) at (prev + 0, 84) to (start + 0, 85)
2423
- Code(Expression(0, Sub)) at (prev + 2, 9) to (start + 2, 11)

tests/coverage/closure_macro_async.cov-map

+4-5
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@ Number of file 0 mappings: 1
1515
- Code(Counter(0)) at (prev + 35, 1) to (start + 0, 43)
1616

1717
Function name: closure_macro_async::test::{closure#0}
18-
Raw bytes (43): 0x[01, 01, 02, 01, 05, 05, 02, 07, 01, 23, 2b, 01, 21, 02, 02, 09, 00, 0f, 05, 00, 12, 00, 13, 02, 00, 12, 00, 13, 05, 00, 54, 00, 55, 02, 02, 09, 02, 0b, 07, 03, 01, 00, 02]
18+
Raw bytes (38): 0x[01, 01, 02, 01, 05, 05, 02, 06, 01, 23, 2b, 01, 21, 02, 02, 09, 00, 12, 02, 00, 0f, 00, 54, 05, 00, 54, 00, 55, 02, 02, 09, 02, 0b, 07, 03, 01, 00, 02]
1919
Number of files: 1
2020
- file 0 => global file 1
2121
Number of expressions: 2
2222
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
2323
- expression 1 operands: lhs = Counter(1), rhs = Expression(0, Sub)
24-
Number of file 0 mappings: 7
24+
Number of file 0 mappings: 6
2525
- Code(Counter(0)) at (prev + 35, 43) to (start + 1, 33)
26-
- Code(Expression(0, Sub)) at (prev + 2, 9) to (start + 0, 15)
26+
- Code(Expression(0, Sub)) at (prev + 2, 9) to (start + 0, 18)
2727
= (c0 - c1)
28-
- Code(Counter(1)) at (prev + 0, 18) to (start + 0, 19)
29-
- Code(Expression(0, Sub)) at (prev + 0, 18) to (start + 0, 19)
28+
- Code(Expression(0, Sub)) at (prev + 0, 15) to (start + 0, 84)
3029
= (c0 - c1)
3130
- Code(Counter(1)) at (prev + 0, 84) to (start + 0, 85)
3231
- Code(Expression(0, Sub)) at (prev + 2, 9) to (start + 2, 11)

0 commit comments

Comments
 (0)