Skip to content

Commit 303c333

Browse files
committed
coverage: Rename is_closure to is_hole
When refining covspans, we don't specifically care which ones represent closures; we just want to know which ones represent "holes" that should be carved out of other spans and then discarded. (Closures are currently the only source of hole spans, but in the future we might want to also create hole spans for nested items and inactive `#[cfg(..)]` regions.)
1 parent f380bd7 commit 303c333

File tree

2 files changed

+49
-52
lines changed

2 files changed

+49
-52
lines changed

compiler/rustc_mir_transform/src/coverage/spans.rs

+35-41
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,17 @@ pub(super) fn generate_coverage_spans(
9090
struct CurrCovspan {
9191
span: Span,
9292
bcb: BasicCoverageBlock,
93-
is_closure: bool,
93+
is_hole: bool,
9494
}
9595

9696
impl CurrCovspan {
97-
fn new(span: Span, bcb: BasicCoverageBlock, is_closure: bool) -> Self {
98-
Self { span, bcb, is_closure }
97+
fn new(span: Span, bcb: BasicCoverageBlock, is_hole: bool) -> Self {
98+
Self { span, bcb, is_hole }
9999
}
100100

101101
fn into_prev(self) -> PrevCovspan {
102-
let Self { span, bcb, is_closure } = self;
103-
PrevCovspan { span, bcb, merged_spans: vec![span], is_closure }
102+
let Self { span, bcb, is_hole } = self;
103+
PrevCovspan { span, bcb, merged_spans: vec![span], is_hole }
104104
}
105105
}
106106

@@ -111,12 +111,12 @@ struct PrevCovspan {
111111
/// List of all the original spans from MIR that have been merged into this
112112
/// span. Mainly used to precisely skip over gaps when truncating a span.
113113
merged_spans: Vec<Span>,
114-
is_closure: bool,
114+
is_hole: bool,
115115
}
116116

117117
impl PrevCovspan {
118118
fn is_mergeable(&self, other: &CurrCovspan) -> bool {
119-
self.bcb == other.bcb && !self.is_closure && !other.is_closure
119+
self.bcb == other.bcb && !self.is_hole && !other.is_hole
120120
}
121121

122122
fn merge_from(&mut self, other: &CurrCovspan) {
@@ -135,8 +135,8 @@ impl PrevCovspan {
135135
}
136136

137137
fn refined_copy(&self) -> Option<RefinedCovspan> {
138-
let &Self { span, bcb, merged_spans: _, is_closure } = self;
139-
(!is_closure).then_some(RefinedCovspan { span, bcb })
138+
let &Self { span, bcb, merged_spans: _, is_hole } = self;
139+
(!is_hole).then_some(RefinedCovspan { span, bcb })
140140
}
141141

142142
fn into_refined(self) -> Option<RefinedCovspan> {
@@ -209,7 +209,7 @@ impl SpansRefiner {
209209
let curr = self.curr();
210210

211211
if prev.is_mergeable(curr) {
212-
debug!(" same bcb (and neither is a closure), merge with prev={prev:?}");
212+
debug!(?prev, "curr will be merged into prev");
213213
let curr = self.take_curr();
214214
self.prev_mut().merge_from(&curr);
215215
} else if prev.span.hi() <= curr.span.lo() {
@@ -218,15 +218,13 @@ impl SpansRefiner {
218218
);
219219
let prev = self.take_prev().into_refined();
220220
self.refined_spans.extend(prev);
221-
} else if prev.is_closure {
221+
} else if prev.is_hole {
222222
// drop any equal or overlapping span (`curr`) and keep `prev` to test again in the
223223
// next iter
224-
debug!(
225-
" curr overlaps a closure (prev). Drop curr and keep prev for next iter. prev={prev:?}",
226-
);
224+
debug!(?prev, "prev (a hole) overlaps curr, so discarding curr");
227225
self.take_curr(); // Discards curr.
228-
} else if curr.is_closure {
229-
self.carve_out_span_for_closure();
226+
} else if curr.is_hole {
227+
self.carve_out_span_for_hole();
230228
} else {
231229
self.cutoff_prev_at_overlapping_curr();
232230
}
@@ -281,48 +279,44 @@ impl SpansRefiner {
281279
{
282280
// Skip curr because prev has already advanced beyond the end of curr.
283281
// This can only happen if a prior iteration updated `prev` to skip past
284-
// a region of code, such as skipping past a closure.
285-
debug!(
286-
" prev.span starts after curr.span, so curr will be dropped (skipping past \
287-
closure?); prev={prev:?}",
288-
);
282+
// a region of code, such as skipping past a hole.
283+
debug!(?prev, "prev.span starts after curr.span, so curr will be dropped");
289284
} else {
290-
self.some_curr = Some(CurrCovspan::new(curr.span, curr.bcb, curr.is_closure));
285+
self.some_curr = Some(CurrCovspan::new(curr.span, curr.bcb, curr.is_hole));
291286
return true;
292287
}
293288
}
294289
false
295290
}
296291

297-
/// If `prev`s span extends left of the closure (`curr`), carve out the closure's span from
298-
/// `prev`'s span. (The closure's coverage counters will be injected when processing the
299-
/// closure's own MIR.) Add the portion of the span to the left of the closure; and if the span
300-
/// extends to the right of the closure, update `prev` to that portion of the span.
301-
fn carve_out_span_for_closure(&mut self) {
292+
/// If `prev`s span extends left of the hole (`curr`), carve out the hole's span from
293+
/// `prev`'s span. Add the portion of the span to the left of the hole; and if the span
294+
/// extends to the right of the hole, update `prev` to that portion of the span.
295+
fn carve_out_span_for_hole(&mut self) {
302296
let prev = self.prev();
303297
let curr = self.curr();
304-
assert!(!prev.is_closure && curr.is_closure);
298+
assert!(!prev.is_hole && curr.is_hole);
305299

306300
let left_cutoff = curr.span.lo();
307301
let right_cutoff = curr.span.hi();
308-
let has_pre_closure_span = prev.span.lo() < right_cutoff;
309-
let has_post_closure_span = prev.span.hi() > right_cutoff;
310-
311-
if has_pre_closure_span {
312-
let mut pre_closure = prev.refined_copy().expect("prev is not a closure span");
313-
pre_closure.span = pre_closure.span.with_hi(left_cutoff);
314-
debug!(" prev overlaps a closure. Adding span for pre_closure={:?}", pre_closure);
315-
self.refined_spans.push(pre_closure);
302+
let has_pre_hole_span = prev.span.lo() < right_cutoff;
303+
let has_post_hole_span = prev.span.hi() > right_cutoff;
304+
305+
if has_pre_hole_span {
306+
let mut pre_hole = prev.refined_copy().expect("prev is not a hole span");
307+
pre_hole.span = pre_hole.span.with_hi(left_cutoff);
308+
debug!(?pre_hole, "prev overlaps a hole; adding pre-hole span");
309+
self.refined_spans.push(pre_hole);
316310
}
317311

318-
if has_post_closure_span {
319-
// Mutate `prev.span` to start after the closure (and discard curr).
312+
if has_post_hole_span {
313+
// Mutate `prev.span` to start after the hole (and discard curr).
320314
self.prev_mut().span = self.prev().span.with_lo(right_cutoff);
321-
debug!(" Mutated prev.span to start after the closure. prev={:?}", self.prev());
315+
debug!(prev=?self.prev(), "mutated prev to start after the hole");
322316

323-
// Discard this curr, since it's a closure span.
317+
// Discard this curr, since it's a hole span.
324318
let curr = self.take_curr();
325-
assert!(curr.is_closure);
319+
assert!(curr.is_hole);
326320
}
327321
}
328322

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

+14-11
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ 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 two spans have the same lo & hi, put closure spans first,
56-
// as they take precedence over non-closure spans.
57-
.then_with(|| Ord::cmp(&a.is_closure, &b.is_closure).reverse())
55+
// If two spans have the same lo & hi, put hole spans first,
56+
// as they take precedence over non-hole spans.
57+
.then_with(|| Ord::cmp(&a.is_hole, &b.is_hole).reverse())
5858
// After deduplication, we want to keep only the most-dominated BCB.
5959
.then_with(|| basic_coverage_blocks.cmp_in_dominator_order(a.bcb, b.bcb).reverse())
6060
});
6161

62-
// Among covspans with the same span, keep only one. Closure spans take
62+
// Among covspans with the same span, keep only one. Hole spans take
6363
// precedence, otherwise keep the one with the most-dominated BCB.
6464
// (Ideally we should try to preserve _all_ non-dominating BCBs, but that
6565
// requires a lot more complexity in the span refiner, for little benefit.)
@@ -78,8 +78,8 @@ pub(super) fn mir_to_initial_sorted_coverage_spans(
7878
fn remove_unwanted_macro_spans(initial_spans: &mut Vec<SpanFromMir>) {
7979
let mut seen_macro_spans = FxHashSet::default();
8080
initial_spans.retain(|covspan| {
81-
// Ignore (retain) closure spans and non-macro-expansion spans.
82-
if covspan.is_closure || covspan.visible_macro.is_none() {
81+
// Ignore (retain) hole spans and non-macro-expansion spans.
82+
if covspan.is_hole || covspan.visible_macro.is_none() {
8383
return true;
8484
}
8585

@@ -96,7 +96,7 @@ fn split_visible_macro_spans(initial_spans: &mut Vec<SpanFromMir>) {
9696
let mut extra_spans = vec![];
9797

9898
initial_spans.retain(|covspan| {
99-
if covspan.is_closure {
99+
if covspan.is_hole {
100100
return true;
101101
}
102102

@@ -112,7 +112,7 @@ fn split_visible_macro_spans(initial_spans: &mut Vec<SpanFromMir>) {
112112
return true;
113113
}
114114

115-
assert!(!covspan.is_closure);
115+
assert!(!covspan.is_hole);
116116
extra_spans.push(SpanFromMir::new(before, covspan.visible_macro, covspan.bcb, false));
117117
extra_spans.push(SpanFromMir::new(after, covspan.visible_macro, covspan.bcb, false));
118118
false // Discard the original covspan that we just split.
@@ -336,7 +336,10 @@ pub(super) struct SpanFromMir {
336336
pub(super) span: Span,
337337
visible_macro: Option<Symbol>,
338338
pub(super) bcb: BasicCoverageBlock,
339-
pub(super) is_closure: bool,
339+
/// If true, this covspan represents a "hole" that should be carved out
340+
/// from other spans, e.g. because it represents a closure expression that
341+
/// will be instrumented separately as its own function.
342+
pub(super) is_hole: bool,
340343
}
341344

342345
impl SpanFromMir {
@@ -348,8 +351,8 @@ impl SpanFromMir {
348351
span: Span,
349352
visible_macro: Option<Symbol>,
350353
bcb: BasicCoverageBlock,
351-
is_closure: bool,
354+
is_hole: bool,
352355
) -> Self {
353-
Self { span, visible_macro, bcb, is_closure }
356+
Self { span, visible_macro, bcb, is_hole }
354357
}
355358
}

0 commit comments

Comments
 (0)