Skip to content

Commit 779874f

Browse files
committed
coverage: Move prev_original_span into PrevCovspan
Now that `prev` has its own dedicated struct, we can store the original span in that struct, instead of in a separate field in the refiner.
1 parent 28108b8 commit 779874f

File tree

1 file changed

+11
-19
lines changed
  • compiler/rustc_mir_transform/src/coverage

1 file changed

+11
-19
lines changed

compiler/rustc_mir_transform/src/coverage/spans.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_data_structures::graph::WithNumNodes;
22
use rustc_index::bit_set::BitSet;
33
use rustc_middle::mir;
4-
use rustc_span::{BytePos, Span, DUMMY_SP};
4+
use rustc_span::{BytePos, Span};
55

66
use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph, START_BCB};
77
use crate::coverage::spans::from_mir::SpanFromMir;
@@ -102,7 +102,7 @@ impl CurrCovspan {
102102

103103
fn into_prev(self) -> PrevCovspan {
104104
let Self { span, bcb, is_closure } = self;
105-
PrevCovspan { span, bcb, merged_spans: vec![span], is_closure }
105+
PrevCovspan { original_span: span, span, bcb, merged_spans: vec![span], is_closure }
106106
}
107107

108108
fn into_refined(self) -> RefinedCovspan {
@@ -115,6 +115,7 @@ impl CurrCovspan {
115115

116116
#[derive(Debug)]
117117
struct PrevCovspan {
118+
original_span: Span,
118119
span: Span,
119120
bcb: BasicCoverageBlock,
120121
/// List of all the original spans from MIR that have been merged into this
@@ -142,12 +143,12 @@ impl PrevCovspan {
142143
}
143144

144145
fn into_dup(self) -> DuplicateCovspan {
145-
let Self { span, bcb, merged_spans: _, is_closure } = self;
146+
let Self { original_span: _, span, bcb, merged_spans: _, is_closure } = self;
146147
DuplicateCovspan { span, bcb, is_closure }
147148
}
148149

149150
fn refined_copy(&self) -> RefinedCovspan {
150-
let &Self { span, bcb, merged_spans: _, is_closure } = self;
151+
let &Self { original_span: _, span, bcb, merged_spans: _, is_closure } = self;
151152
RefinedCovspan { span, bcb, is_closure }
152153
}
153154

@@ -217,11 +218,6 @@ struct SpansRefiner<'a> {
217218
/// If that `curr` was discarded, `prev` retains its value from the previous iteration.
218219
some_prev: Option<PrevCovspan>,
219220

220-
/// Assigned from `curr.span` from the previous iteration. The `prev_original_span`
221-
/// **must not be mutated** (except when advancing to the next `prev`), even if `prev.span()`
222-
/// is mutated.
223-
prev_original_span: Span,
224-
225221
/// One or more coverage spans with the same `Span` but different `BasicCoverageBlock`s, and
226222
/// no `BasicCoverageBlock` in this list dominates another `BasicCoverageBlock` in the list.
227223
/// If a new `curr` span also fits this criteria (compared to an existing list of
@@ -250,7 +246,6 @@ impl<'a> SpansRefiner<'a> {
250246
sorted_spans_iter: sorted_spans.into_iter(),
251247
some_curr: None,
252248
some_prev: None,
253-
prev_original_span: DUMMY_SP,
254249
pending_dups: Vec::new(),
255250
refined_spans: Vec::with_capacity(basic_coverage_blocks.num_nodes() * 2),
256251
};
@@ -292,7 +287,7 @@ impl<'a> SpansRefiner<'a> {
292287
self.take_curr(); // Discards curr.
293288
} else if curr.is_closure {
294289
self.carve_out_span_for_closure();
295-
} else if self.prev_original_span == curr.span {
290+
} else if prev.original_span == curr.span {
296291
// `prev` and `curr` have the same span, or would have had the
297292
// same span before `prev` was modified by other spans.
298293
self.update_pending_dups();
@@ -392,7 +387,6 @@ impl<'a> SpansRefiner<'a> {
392387
/// Advance `prev` to `curr` (if any), and `curr` to the next coverage span in sorted order.
393388
fn next_coverage_span(&mut self) -> bool {
394389
if let Some(curr) = self.some_curr.take() {
395-
self.prev_original_span = curr.span;
396390
self.some_prev = Some(curr.into_prev());
397391
}
398392
while let Some(curr) = self.sorted_spans_iter.next() {
@@ -445,9 +439,7 @@ impl<'a> SpansRefiner<'a> {
445439
}
446440

447441
if has_post_closure_span {
448-
// Mutate `prev.span()` to start after the closure (and discard curr).
449-
// (**NEVER** update `prev_original_span` because it affects the assumptions
450-
// about how the coverage spans are ordered.)
442+
// Mutate `prev.span` to start after the closure (and discard curr).
451443
self.prev_mut().span = self.prev().span.with_lo(right_cutoff);
452444
debug!(" Mutated prev.span to start after the closure. prev={:?}", self.prev());
453445

@@ -464,12 +456,12 @@ impl<'a> SpansRefiner<'a> {
464456
}
465457
}
466458

467-
/// Called if `curr.span` equals `prev_original_span` (and potentially equal to all
459+
/// Called if `curr.span` equals `prev.original_span` (and potentially equal to all
468460
/// `pending_dups` spans, if any). Keep in mind, `prev.span()` may have been changed.
469461
/// If prev.span() was merged into other spans (with matching BCB, for instance),
470-
/// `prev.span.hi()` will be greater than (further right of) `prev_original_span.hi()`.
462+
/// `prev.span.hi()` will be greater than (further right of) `prev.original_span.hi()`.
471463
/// If prev.span() was split off to the right of a closure, prev.span().lo() will be
472-
/// greater than prev_original_span.lo(). The actual span of `prev_original_span` is
464+
/// greater than prev.original_span.lo(). The actual span of `prev.original_span` is
473465
/// not as important as knowing that `prev()` **used to have the same span** as `curr()`,
474466
/// which means their sort order is still meaningful for determining the dominator
475467
/// relationship.
@@ -507,7 +499,7 @@ impl<'a> SpansRefiner<'a> {
507499
self.cutoff_prev_at_overlapping_curr();
508500
// If one span dominates the other, associate the span with the code from the dominated
509501
// block only (`curr`), and discard the overlapping portion of the `prev` span. (Note
510-
// that if `prev.span` is wider than `prev_original_span`, a coverage span will still
502+
// that if `prev.span` is wider than `prev.original_span`, a coverage span will still
511503
// be created for `prev`s block, for the non-overlapping portion, left of `curr.span`.)
512504
//
513505
// For example:

0 commit comments

Comments
 (0)