Skip to content

Commit c0fec7c

Browse files
committed
coverage: Explain why we temporarily steal pending_dups
1 parent 9ba1874 commit c0fec7c

File tree

1 file changed

+20
-6
lines changed
  • compiler/rustc_mir_transform/src/coverage

1 file changed

+20
-6
lines changed

compiler/rustc_mir_transform/src/coverage/spans.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,10 @@ impl<'a> CoverageSpansGenerator<'a> {
352352

353353
let prev = self.take_prev();
354354
debug!(" AT END, adding last prev={prev:?}");
355-
let pending_dups = self.pending_dups.split_off(0);
356-
for dup in pending_dups {
355+
356+
// Take `pending_dups` so that we can drain it while calling self methods.
357+
// It is never used as a field after this point.
358+
for dup in std::mem::take(&mut self.pending_dups) {
357359
debug!(" ...adding at least one pending dup={:?}", dup);
358360
self.push_refined_span(dup);
359361
}
@@ -466,11 +468,16 @@ impl<'a> CoverageSpansGenerator<'a> {
466468
previous iteration, or prev started a new disjoint span"
467469
);
468470
if dup.span.hi() <= self.curr().span.lo() {
469-
let pending_dups = self.pending_dups.split_off(0);
470-
for dup in pending_dups.into_iter() {
471+
// Temporarily steal `pending_dups` into a local, so that we can
472+
// drain it while calling other self methods.
473+
let mut pending_dups = std::mem::take(&mut self.pending_dups);
474+
for dup in pending_dups.drain(..) {
471475
debug!(" ...adding at least one pending={:?}", dup);
472476
self.push_refined_span(dup);
473477
}
478+
// The list of dups is now empty, but we can recycle its capacity.
479+
self.pending_dups = pending_dups;
480+
assert!(self.pending_dups.is_empty());
474481
} else {
475482
self.pending_dups.clear();
476483
}
@@ -519,7 +526,10 @@ impl<'a> CoverageSpansGenerator<'a> {
519526
let has_pre_closure_span = prev.span.lo() < right_cutoff;
520527
let has_post_closure_span = prev.span.hi() > right_cutoff;
521528

522-
let mut pending_dups = self.pending_dups.split_off(0);
529+
// Temporarily steal `pending_dups` into a local, so that we can
530+
// mutate and/or drain it while calling other self methods.
531+
let mut pending_dups = std::mem::take(&mut self.pending_dups);
532+
523533
if has_pre_closure_span {
524534
let mut pre_closure = self.prev().clone();
525535
pre_closure.span = pre_closure.span.with_hi(left_cutoff);
@@ -533,6 +543,7 @@ impl<'a> CoverageSpansGenerator<'a> {
533543
}
534544
self.push_refined_span(pre_closure);
535545
}
546+
536547
if has_post_closure_span {
537548
// Mutate `prev.span()` to start after the closure (and discard curr).
538549
// (**NEVER** update `prev_original_span` because it affects the assumptions
@@ -543,12 +554,15 @@ impl<'a> CoverageSpansGenerator<'a> {
543554
debug!(" ...and at least one overlapping dup={:?}", dup);
544555
dup.span = dup.span.with_lo(right_cutoff);
545556
}
546-
self.pending_dups.append(&mut pending_dups);
547557
let closure_covspan = self.take_curr(); // Prevent this curr from becoming prev.
548558
self.push_refined_span(closure_covspan); // since self.prev() was already updated
549559
} else {
550560
pending_dups.clear();
551561
}
562+
563+
// Restore the modified post-closure spans, or the empty vector's capacity.
564+
assert!(self.pending_dups.is_empty());
565+
self.pending_dups = pending_dups;
552566
}
553567

554568
/// Called if `curr.span` equals `prev_original_span` (and potentially equal to all

0 commit comments

Comments
 (0)