Skip to content

Commit 623d432

Browse files
committed
coverage: Simplify code for adding prev to pending dups
If we only check for duplicate spans when `prev` is unmodified, we reduce the number of situations that `update_pending_dups` needs to handle. This could potentially change the coverage spans we produce in some unknown corner cases, but none of our current coverage tests indicate any change.
1 parent 779874f commit 623d432

File tree

1 file changed

+12
-40
lines changed
  • compiler/rustc_mir_transform/src/coverage

1 file changed

+12
-40
lines changed

compiler/rustc_mir_transform/src/coverage/spans.rs

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ impl PrevCovspan {
143143
}
144144

145145
fn into_dup(self) -> DuplicateCovspan {
146-
let Self { original_span: _, span, bcb, merged_spans: _, is_closure } = self;
146+
let Self { original_span, span, bcb, merged_spans: _, is_closure } = self;
147+
// Only unmodified spans end up in `pending_dups`.
148+
debug_assert_eq!(original_span, span);
147149
DuplicateCovspan { span, bcb, is_closure }
148150
}
149151

@@ -287,9 +289,9 @@ impl<'a> SpansRefiner<'a> {
287289
self.take_curr(); // Discards curr.
288290
} else if curr.is_closure {
289291
self.carve_out_span_for_closure();
290-
} else if prev.original_span == curr.span {
291-
// `prev` and `curr` have the same span, or would have had the
292-
// same span before `prev` was modified by other spans.
292+
} else if prev.original_span == prev.span && prev.span == curr.span {
293+
// Prev and curr have the same span, and prev's span hasn't
294+
// been modified by other spans.
293295
self.update_pending_dups();
294296
} else {
295297
self.cutoff_prev_at_overlapping_curr();
@@ -478,6 +480,12 @@ impl<'a> SpansRefiner<'a> {
478480
// impossible for `curr` to dominate any previous coverage span.
479481
debug_assert!(!self.basic_coverage_blocks.dominates(curr_bcb, prev_bcb));
480482

483+
// `prev` is a duplicate of `curr`, so add it to the list of pending dups.
484+
// If it dominates `curr`, it will be removed by the subsequent discard step.
485+
let prev = self.take_prev().into_dup();
486+
debug!(?prev, "adding prev to pending dups");
487+
self.pending_dups.push(prev);
488+
481489
let initial_pending_count = self.pending_dups.len();
482490
if initial_pending_count > 0 {
483491
self.pending_dups
@@ -490,42 +498,6 @@ impl<'a> SpansRefiner<'a> {
490498
);
491499
}
492500
}
493-
494-
if self.basic_coverage_blocks.dominates(prev_bcb, curr_bcb) {
495-
debug!(
496-
" different bcbs but SAME spans, and prev dominates curr. Discard prev={:?}",
497-
self.prev()
498-
);
499-
self.cutoff_prev_at_overlapping_curr();
500-
// If one span dominates the other, associate the span with the code from the dominated
501-
// block only (`curr`), and discard the overlapping portion of the `prev` span. (Note
502-
// that if `prev.span` is wider than `prev.original_span`, a coverage span will still
503-
// be created for `prev`s block, for the non-overlapping portion, left of `curr.span`.)
504-
//
505-
// For example:
506-
// match somenum {
507-
// x if x < 1 => { ... }
508-
// }...
509-
//
510-
// The span for the first `x` is referenced by both the pattern block (every time it is
511-
// evaluated) and the arm code (only when matched). The counter will be applied only to
512-
// the dominated block. This allows coverage to track and highlight things like the
513-
// assignment of `x` above, if the branch is matched, making `x` available to the arm
514-
// code; and to track and highlight the question mark `?` "try" operator at the end of
515-
// a function call returning a `Result`, so the `?` is covered when the function returns
516-
// an `Err`, and not counted as covered if the function always returns `Ok`.
517-
} else {
518-
// Save `prev` in `pending_dups`. (`curr` will become `prev` in the next iteration.)
519-
// If the `curr` span is later discarded, `pending_dups` can be discarded as
520-
// well; but if `curr` is added to refined_spans, the `pending_dups` will also be added.
521-
debug!(
522-
" different bcbs but SAME spans, and neither dominates, so keep curr for \
523-
next iter, and, pending upcoming spans (unless overlapping) add prev={:?}",
524-
self.prev()
525-
);
526-
let prev = self.take_prev().into_dup();
527-
self.pending_dups.push(prev);
528-
}
529501
}
530502

531503
/// `curr` overlaps `prev`. If `prev`s span extends left of `curr`s span, keep _only_

0 commit comments

Comments
 (0)