Skip to content

Commit 79bb336

Browse files
authoredJun 5, 2024
Rollup merge of #125921 - Zalathar:buckets, r=oli-obk
coverage: Carve out hole spans in a separate early pass When extracting spans from MIR for use in coverage instrumentation, we sometimes need to identify *hole spans* (currently just closures), and carve up the other spans so that they don't overlap with holes. This PR simplifies the main coverage-span-refiner by extracting the hole-carving process into a separate early pass. That pass produces a series of independent buckets, and we run the span-refiner on each bucket separately. There is almost no difference in the resulting mappings, other than in some edge cases involving macros.
2 parents ebc66fd + c57a1d1 commit 79bb336

File tree

6 files changed

+243
-147
lines changed

6 files changed

+243
-147
lines changed
 

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

Lines changed: 26 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,31 @@ pub(super) fn extract_refined_covspans(
2020
basic_coverage_blocks: &CoverageGraph,
2121
code_mappings: &mut impl Extend<mappings::CodeMapping>,
2222
) {
23-
let sorted_spans =
23+
let sorted_span_buckets =
2424
from_mir::mir_to_initial_sorted_coverage_spans(mir_body, hir_info, basic_coverage_blocks);
25-
let coverage_spans = SpansRefiner::refine_sorted_spans(sorted_spans);
26-
code_mappings.extend(coverage_spans.into_iter().map(|RefinedCovspan { bcb, span, .. }| {
27-
// Each span produced by the generator represents an ordinary code region.
28-
mappings::CodeMapping { span, bcb }
29-
}));
25+
for bucket in sorted_span_buckets {
26+
let refined_spans = SpansRefiner::refine_sorted_spans(bucket);
27+
code_mappings.extend(refined_spans.into_iter().map(|RefinedCovspan { span, bcb }| {
28+
// Each span produced by the refiner represents an ordinary code region.
29+
mappings::CodeMapping { span, bcb }
30+
}));
31+
}
3032
}
3133

3234
#[derive(Debug)]
3335
struct CurrCovspan {
3436
span: Span,
3537
bcb: BasicCoverageBlock,
36-
is_hole: bool,
3738
}
3839

3940
impl CurrCovspan {
40-
fn new(span: Span, bcb: BasicCoverageBlock, is_hole: bool) -> Self {
41-
Self { span, bcb, is_hole }
41+
fn new(span: Span, bcb: BasicCoverageBlock) -> Self {
42+
Self { span, bcb }
4243
}
4344

4445
fn into_prev(self) -> PrevCovspan {
45-
let Self { span, bcb, is_hole } = self;
46-
PrevCovspan { span, bcb, merged_spans: vec![span], is_hole }
47-
}
48-
49-
fn into_refined(self) -> RefinedCovspan {
50-
// This is only called in cases where `curr` is a hole span that has
51-
// been carved out of `prev`.
52-
debug_assert!(self.is_hole);
53-
self.into_prev().into_refined()
46+
let Self { span, bcb } = self;
47+
PrevCovspan { span, bcb, merged_spans: vec![span] }
5448
}
5549
}
5650

@@ -61,12 +55,11 @@ struct PrevCovspan {
6155
/// List of all the original spans from MIR that have been merged into this
6256
/// span. Mainly used to precisely skip over gaps when truncating a span.
6357
merged_spans: Vec<Span>,
64-
is_hole: bool,
6558
}
6659

6760
impl PrevCovspan {
6861
fn is_mergeable(&self, other: &CurrCovspan) -> bool {
69-
self.bcb == other.bcb && !self.is_hole && !other.is_hole
62+
self.bcb == other.bcb
7063
}
7164

7265
fn merge_from(&mut self, other: &CurrCovspan) {
@@ -84,27 +77,21 @@ impl PrevCovspan {
8477
if self.merged_spans.is_empty() { None } else { Some(self.into_refined()) }
8578
}
8679

87-
fn refined_copy(&self) -> RefinedCovspan {
88-
let &Self { span, bcb, merged_spans: _, is_hole } = self;
89-
RefinedCovspan { span, bcb, is_hole }
90-
}
91-
9280
fn into_refined(self) -> RefinedCovspan {
93-
// Even though we consume self, we can just reuse the copying impl.
94-
self.refined_copy()
81+
let Self { span, bcb, merged_spans: _ } = self;
82+
RefinedCovspan { span, bcb }
9583
}
9684
}
9785

9886
#[derive(Debug)]
9987
struct RefinedCovspan {
10088
span: Span,
10189
bcb: BasicCoverageBlock,
102-
is_hole: bool,
10390
}
10491

10592
impl RefinedCovspan {
10693
fn is_mergeable(&self, other: &Self) -> bool {
107-
self.bcb == other.bcb && !self.is_hole && !other.is_hole
94+
self.bcb == other.bcb
10895
}
10996

11097
fn merge_from(&mut self, other: &Self) {
@@ -119,8 +106,6 @@ impl RefinedCovspan {
119106
/// * Remove duplicate source code coverage regions
120107
/// * Merge spans that represent continuous (both in source code and control flow), non-branching
121108
/// execution
122-
/// * Carve out (leave uncovered) any "hole" spans that need to be left blank
123-
/// (e.g. closures that will be counted by their own MIR body)
124109
struct SpansRefiner {
125110
/// The initial set of coverage spans, sorted by `Span` (`lo` and `hi`) and by relative
126111
/// dominance between the `BasicCoverageBlock`s of equal `Span`s.
@@ -181,13 +166,6 @@ impl SpansRefiner {
181166
);
182167
let prev = self.take_prev().into_refined();
183168
self.refined_spans.push(prev);
184-
} else if prev.is_hole {
185-
// drop any equal or overlapping span (`curr`) and keep `prev` to test again in the
186-
// next iter
187-
debug!(?prev, "prev (a hole) overlaps curr, so discarding curr");
188-
self.take_curr(); // Discards curr.
189-
} else if curr.is_hole {
190-
self.carve_out_span_for_hole();
191169
} else {
192170
self.cutoff_prev_at_overlapping_curr();
193171
}
@@ -211,9 +189,6 @@ impl SpansRefiner {
211189
}
212190
});
213191

214-
// Discard hole spans, since their purpose was to carve out chunks from
215-
// other spans, but we don't want the holes themselves in the final mappings.
216-
self.refined_spans.retain(|covspan| !covspan.is_hole);
217192
self.refined_spans
218193
}
219194

@@ -249,50 +224,17 @@ impl SpansRefiner {
249224
if let Some(curr) = self.some_curr.take() {
250225
self.some_prev = Some(curr.into_prev());
251226
}
252-
while let Some(curr) = self.sorted_spans_iter.next() {
253-
debug!("FOR curr={:?}", curr);
254-
if let Some(prev) = &self.some_prev
255-
&& prev.span.lo() > curr.span.lo()
256-
{
257-
// Skip curr because prev has already advanced beyond the end of curr.
258-
// This can only happen if a prior iteration updated `prev` to skip past
259-
// a region of code, such as skipping past a hole.
260-
debug!(?prev, "prev.span starts after curr.span, so curr will be dropped");
261-
} else {
262-
self.some_curr = Some(CurrCovspan::new(curr.span, curr.bcb, curr.is_hole));
263-
return true;
227+
if let Some(SpanFromMir { span, bcb, .. }) = self.sorted_spans_iter.next() {
228+
// This code only sees sorted spans after hole-carving, so there should
229+
// be no way for `curr` to start before `prev`.
230+
if let Some(prev) = &self.some_prev {
231+
debug_assert!(prev.span.lo() <= span.lo());
264232
}
265-
}
266-
false
267-
}
268-
269-
/// If `prev`s span extends left of the hole (`curr`), carve out the hole's span from
270-
/// `prev`'s span. Add the portion of the span to the left of the hole; and if the span
271-
/// extends to the right of the hole, update `prev` to that portion of the span.
272-
fn carve_out_span_for_hole(&mut self) {
273-
let prev = self.prev();
274-
let curr = self.curr();
275-
276-
let left_cutoff = curr.span.lo();
277-
let right_cutoff = curr.span.hi();
278-
let has_pre_hole_span = prev.span.lo() < right_cutoff;
279-
let has_post_hole_span = prev.span.hi() > right_cutoff;
280-
281-
if has_pre_hole_span {
282-
let mut pre_hole = prev.refined_copy();
283-
pre_hole.span = pre_hole.span.with_hi(left_cutoff);
284-
debug!(?pre_hole, "prev overlaps a hole; adding pre-hole span");
285-
self.refined_spans.push(pre_hole);
286-
}
287-
288-
if has_post_hole_span {
289-
// Mutate `prev.span` to start after the hole (and discard curr).
290-
self.prev_mut().span = self.prev().span.with_lo(right_cutoff);
291-
debug!(prev=?self.prev(), "mutated prev to start after the hole");
292-
293-
// Prevent this curr from becoming prev.
294-
let hole_covspan = self.take_curr().into_refined();
295-
self.refined_spans.push(hole_covspan); // since self.prev() was already updated
233+
self.some_curr = Some(CurrCovspan::new(span, bcb));
234+
debug!(?self.some_prev, ?self.some_curr, "next_coverage_span");
235+
true
236+
} else {
237+
false
296238
}
297239
}
298240

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

Lines changed: 147 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::VecDeque;
2+
13
use rustc_data_structures::captures::Captures;
24
use rustc_data_structures::fx::FxHashSet;
35
use rustc_middle::bug;
@@ -17,23 +19,34 @@ use crate::coverage::ExtractedHirInfo;
1719
/// spans, each associated with a node in the coverage graph (BCB) and possibly
1820
/// other metadata.
1921
///
20-
/// The returned spans are sorted in a specific order that is expected by the
21-
/// subsequent span-refinement step.
22+
/// The returned spans are divided into one or more buckets, such that:
23+
/// - The spans in each bucket are strictly after all spans in previous buckets,
24+
/// and strictly before all spans in subsequent buckets.
25+
/// - The contents of each bucket are also sorted, in a specific order that is
26+
/// expected by the subsequent span-refinement step.
2227
pub(super) fn mir_to_initial_sorted_coverage_spans(
2328
mir_body: &mir::Body<'_>,
2429
hir_info: &ExtractedHirInfo,
2530
basic_coverage_blocks: &CoverageGraph,
26-
) -> Vec<SpanFromMir> {
31+
) -> Vec<Vec<SpanFromMir>> {
2732
let &ExtractedHirInfo { body_span, .. } = hir_info;
2833

2934
let mut initial_spans = vec![];
35+
let mut holes = vec![];
3036

3137
for (bcb, bcb_data) in basic_coverage_blocks.iter_enumerated() {
32-
initial_spans.extend(bcb_to_initial_coverage_spans(mir_body, body_span, bcb, bcb_data));
38+
bcb_to_initial_coverage_spans(
39+
mir_body,
40+
body_span,
41+
bcb,
42+
bcb_data,
43+
&mut initial_spans,
44+
&mut holes,
45+
);
3346
}
3447

3548
// Only add the signature span if we found at least one span in the body.
36-
if !initial_spans.is_empty() {
49+
if !initial_spans.is_empty() || !holes.is_empty() {
3750
// If there is no usable signature span, add a fake one (before refinement)
3851
// to avoid an ugly gap between the body start and the first real span.
3952
// FIXME: Find a more principled way to solve this problem.
@@ -45,29 +58,82 @@ pub(super) fn mir_to_initial_sorted_coverage_spans(
4558
remove_unwanted_macro_spans(&mut initial_spans);
4659
split_visible_macro_spans(&mut initial_spans);
4760

48-
initial_spans.sort_by(|a, b| {
49-
// First sort by span start.
50-
Ord::cmp(&a.span.lo(), &b.span.lo())
51-
// If span starts are the same, sort by span end in reverse order.
52-
// This ensures that if spans A and B are adjacent in the list,
53-
// and they overlap but are not equal, then either:
54-
// - Span A extends further left, or
55-
// - Both have the same start and span A extends further right
56-
.then_with(|| Ord::cmp(&a.span.hi(), &b.span.hi()).reverse())
57-
// If two spans have the same lo & hi, put hole spans first,
58-
// as they take precedence over non-hole spans.
59-
.then_with(|| Ord::cmp(&a.is_hole, &b.is_hole).reverse())
61+
let compare_covspans = |a: &SpanFromMir, b: &SpanFromMir| {
62+
compare_spans(a.span, b.span)
6063
// After deduplication, we want to keep only the most-dominated BCB.
6164
.then_with(|| basic_coverage_blocks.cmp_in_dominator_order(a.bcb, b.bcb).reverse())
62-
});
65+
};
66+
initial_spans.sort_by(compare_covspans);
6367

64-
// Among covspans with the same span, keep only one. Hole spans take
65-
// precedence, otherwise keep the one with the most-dominated BCB.
68+
// Among covspans with the same span, keep only one,
69+
// preferring the one with the most-dominated BCB.
6670
// (Ideally we should try to preserve _all_ non-dominating BCBs, but that
6771
// requires a lot more complexity in the span refiner, for little benefit.)
6872
initial_spans.dedup_by(|b, a| a.span.source_equal(b.span));
6973

70-
initial_spans
74+
// Sort the holes, and merge overlapping/adjacent holes.
75+
holes.sort_by(|a, b| compare_spans(a.span, b.span));
76+
holes.dedup_by(|b, a| a.merge_if_overlapping_or_adjacent(b));
77+
78+
// Now we're ready to start carving holes out of the initial coverage spans,
79+
// and grouping them in buckets separated by the holes.
80+
81+
let mut initial_spans = VecDeque::from(initial_spans);
82+
let mut fragments: Vec<SpanFromMir> = vec![];
83+
84+
// For each hole:
85+
// - Identify the spans that are entirely or partly before the hole.
86+
// - Put those spans in a corresponding bucket, truncated to the start of the hole.
87+
// - If one of those spans also extends after the hole, put the rest of it
88+
// in a "fragments" vector that is processed by the next hole.
89+
let mut buckets = (0..holes.len()).map(|_| vec![]).collect::<Vec<_>>();
90+
for (hole, bucket) in holes.iter().zip(&mut buckets) {
91+
let fragments_from_prev = std::mem::take(&mut fragments);
92+
93+
// Only inspect spans that precede or overlap this hole,
94+
// leaving the rest to be inspected by later holes.
95+
// (This relies on the spans and holes both being sorted.)
96+
let relevant_initial_spans =
97+
drain_front_while(&mut initial_spans, |c| c.span.lo() < hole.span.hi());
98+
99+
for covspan in fragments_from_prev.into_iter().chain(relevant_initial_spans) {
100+
let (before, after) = covspan.split_around_hole_span(hole.span);
101+
bucket.extend(before);
102+
fragments.extend(after);
103+
}
104+
}
105+
106+
// After finding the spans before each hole, any remaining fragments/spans
107+
// form their own final bucket, after the final hole.
108+
// (If there were no holes, this will just be all of the initial spans.)
109+
fragments.extend(initial_spans);
110+
buckets.push(fragments);
111+
112+
// Make sure each individual bucket is still internally sorted.
113+
for bucket in &mut buckets {
114+
bucket.sort_by(compare_covspans);
115+
}
116+
buckets
117+
}
118+
119+
fn compare_spans(a: Span, b: Span) -> std::cmp::Ordering {
120+
// First sort by span start.
121+
Ord::cmp(&a.lo(), &b.lo())
122+
// If span starts are the same, sort by span end in reverse order.
123+
// This ensures that if spans A and B are adjacent in the list,
124+
// and they overlap but are not equal, then either:
125+
// - Span A extends further left, or
126+
// - Both have the same start and span A extends further right
127+
.then_with(|| Ord::cmp(&a.hi(), &b.hi()).reverse())
128+
}
129+
130+
/// Similar to `.drain(..)`, but stops just before it would remove an item not
131+
/// satisfying the predicate.
132+
fn drain_front_while<'a, T>(
133+
queue: &'a mut VecDeque<T>,
134+
mut pred_fn: impl FnMut(&T) -> bool,
135+
) -> impl Iterator<Item = T> + Captures<'a> {
136+
std::iter::from_fn(move || if pred_fn(queue.front()?) { queue.pop_front() } else { None })
71137
}
72138

73139
/// Macros that expand into branches (e.g. `assert!`, `trace!`) tend to generate
@@ -80,8 +146,8 @@ pub(super) fn mir_to_initial_sorted_coverage_spans(
80146
fn remove_unwanted_macro_spans(initial_spans: &mut Vec<SpanFromMir>) {
81147
let mut seen_macro_spans = FxHashSet::default();
82148
initial_spans.retain(|covspan| {
83-
// Ignore (retain) hole spans and non-macro-expansion spans.
84-
if covspan.is_hole || covspan.visible_macro.is_none() {
149+
// Ignore (retain) non-macro-expansion spans.
150+
if covspan.visible_macro.is_none() {
85151
return true;
86152
}
87153

@@ -98,10 +164,6 @@ fn split_visible_macro_spans(initial_spans: &mut Vec<SpanFromMir>) {
98164
let mut extra_spans = vec![];
99165

100166
initial_spans.retain(|covspan| {
101-
if covspan.is_hole {
102-
return true;
103-
}
104-
105167
let Some(visible_macro) = covspan.visible_macro else { return true };
106168

107169
let split_len = visible_macro.as_str().len() as u32 + 1;
@@ -114,9 +176,8 @@ fn split_visible_macro_spans(initial_spans: &mut Vec<SpanFromMir>) {
114176
return true;
115177
}
116178

117-
assert!(!covspan.is_hole);
118-
extra_spans.push(SpanFromMir::new(before, covspan.visible_macro, covspan.bcb, false));
119-
extra_spans.push(SpanFromMir::new(after, covspan.visible_macro, covspan.bcb, false));
179+
extra_spans.push(SpanFromMir::new(before, covspan.visible_macro, covspan.bcb));
180+
extra_spans.push(SpanFromMir::new(after, covspan.visible_macro, covspan.bcb));
120181
false // Discard the original covspan that we just split.
121182
});
122183

@@ -135,8 +196,10 @@ fn bcb_to_initial_coverage_spans<'a, 'tcx>(
135196
body_span: Span,
136197
bcb: BasicCoverageBlock,
137198
bcb_data: &'a BasicCoverageBlockData,
138-
) -> impl Iterator<Item = SpanFromMir> + Captures<'a> + Captures<'tcx> {
139-
bcb_data.basic_blocks.iter().flat_map(move |&bb| {
199+
initial_covspans: &mut Vec<SpanFromMir>,
200+
holes: &mut Vec<Hole>,
201+
) {
202+
for &bb in &bcb_data.basic_blocks {
140203
let data = &mir_body[bb];
141204

142205
let unexpand = move |expn_span| {
@@ -146,24 +209,32 @@ fn bcb_to_initial_coverage_spans<'a, 'tcx>(
146209
.filter(|(span, _)| !span.source_equal(body_span))
147210
};
148211

149-
let statement_spans = data.statements.iter().filter_map(move |statement| {
212+
let mut extract_statement_span = |statement| {
150213
let expn_span = filtered_statement_span(statement)?;
151214
let (span, visible_macro) = unexpand(expn_span)?;
152215

153216
// A statement that looks like the assignment of a closure expression
154217
// is treated as a "hole" span, to be carved out of other spans.
155-
Some(SpanFromMir::new(span, visible_macro, bcb, is_closure_like(statement)))
156-
});
218+
if is_closure_like(statement) {
219+
holes.push(Hole { span });
220+
} else {
221+
initial_covspans.push(SpanFromMir::new(span, visible_macro, bcb));
222+
}
223+
Some(())
224+
};
225+
for statement in data.statements.iter() {
226+
extract_statement_span(statement);
227+
}
157228

158-
let terminator_span = Some(data.terminator()).into_iter().filter_map(move |terminator| {
229+
let mut extract_terminator_span = |terminator| {
159230
let expn_span = filtered_terminator_span(terminator)?;
160231
let (span, visible_macro) = unexpand(expn_span)?;
161232

162-
Some(SpanFromMir::new(span, visible_macro, bcb, false))
163-
});
164-
165-
statement_spans.chain(terminator_span)
166-
})
233+
initial_covspans.push(SpanFromMir::new(span, visible_macro, bcb));
234+
Some(())
235+
};
236+
extract_terminator_span(data.terminator());
237+
}
167238
}
168239

169240
fn is_closure_like(statement: &Statement<'_>) -> bool {
@@ -330,6 +401,22 @@ fn unexpand_into_body_span_with_prev(
330401
Some((curr, prev))
331402
}
332403

404+
#[derive(Debug)]
405+
struct Hole {
406+
span: Span,
407+
}
408+
409+
impl Hole {
410+
fn merge_if_overlapping_or_adjacent(&mut self, other: &mut Self) -> bool {
411+
if !self.span.overlaps_or_adjacent(other.span) {
412+
return false;
413+
}
414+
415+
self.span = self.span.to(other.span);
416+
true
417+
}
418+
}
419+
333420
#[derive(Debug)]
334421
pub(super) struct SpanFromMir {
335422
/// A span that has been extracted from MIR and then "un-expanded" back to
@@ -342,23 +429,30 @@ pub(super) struct SpanFromMir {
342429
pub(super) span: Span,
343430
visible_macro: Option<Symbol>,
344431
pub(super) bcb: BasicCoverageBlock,
345-
/// If true, this covspan represents a "hole" that should be carved out
346-
/// from other spans, e.g. because it represents a closure expression that
347-
/// will be instrumented separately as its own function.
348-
pub(super) is_hole: bool,
349432
}
350433

351434
impl SpanFromMir {
352435
fn for_fn_sig(fn_sig_span: Span) -> Self {
353-
Self::new(fn_sig_span, None, START_BCB, false)
436+
Self::new(fn_sig_span, None, START_BCB)
437+
}
438+
439+
fn new(span: Span, visible_macro: Option<Symbol>, bcb: BasicCoverageBlock) -> Self {
440+
Self { span, visible_macro, bcb }
354441
}
355442

356-
fn new(
357-
span: Span,
358-
visible_macro: Option<Symbol>,
359-
bcb: BasicCoverageBlock,
360-
is_hole: bool,
361-
) -> Self {
362-
Self { span, visible_macro, bcb, is_hole }
443+
/// Splits this span into 0-2 parts:
444+
/// - The part that is strictly before the hole span, if any.
445+
/// - The part that is strictly after the hole span, if any.
446+
fn split_around_hole_span(&self, hole_span: Span) -> (Option<Self>, Option<Self>) {
447+
let before = try {
448+
let span = self.span.trim_end(hole_span)?;
449+
Self { span, ..*self }
450+
};
451+
let after = try {
452+
let span = self.span.trim_start(hole_span)?;
453+
Self { span, ..*self }
454+
};
455+
456+
(before, after)
363457
}
364458
}

‎compiler/rustc_span/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,13 @@ impl Span {
682682
if span.hi > other.hi { Some(span.with_lo(cmp::max(span.lo, other.hi))) } else { None }
683683
}
684684

685+
/// Returns `Some(span)`, where the end is trimmed by the start of `other`.
686+
pub fn trim_end(self, other: Span) -> Option<Span> {
687+
let span = self.data();
688+
let other = other.data();
689+
if span.lo < other.lo { Some(span.with_hi(cmp::min(span.hi, other.lo))) } else { None }
690+
}
691+
685692
/// Returns the source span -- this is either the supplied span, or the span for
686693
/// the macro callsite that expanded to it.
687694
pub fn source_callsite(self) -> Span {

‎compiler/rustc_span/src/tests.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,60 @@ fn test_normalize_newlines() {
4242
check("\r\r\n", "\r\n", &[2]);
4343
check("hello\rworld", "hello\rworld", &[]);
4444
}
45+
46+
#[test]
47+
fn test_trim() {
48+
let span = |lo: usize, hi: usize| {
49+
Span::new(BytePos::from_usize(lo), BytePos::from_usize(hi), SyntaxContext::root(), None)
50+
};
51+
52+
// Various positions, named for their relation to `start` and `end`.
53+
let well_before = 1;
54+
let before = 3;
55+
let start = 5;
56+
let mid = 7;
57+
let end = 9;
58+
let after = 11;
59+
let well_after = 13;
60+
61+
// The resulting span's context should be that of `self`, not `other`.
62+
let other = span(start, end).with_ctxt(SyntaxContext::from_u32(999));
63+
64+
// Test cases for `trim_end`.
65+
66+
assert_eq!(span(well_before, before).trim_end(other), Some(span(well_before, before)));
67+
assert_eq!(span(well_before, start).trim_end(other), Some(span(well_before, start)));
68+
assert_eq!(span(well_before, mid).trim_end(other), Some(span(well_before, start)));
69+
assert_eq!(span(well_before, end).trim_end(other), Some(span(well_before, start)));
70+
assert_eq!(span(well_before, after).trim_end(other), Some(span(well_before, start)));
71+
72+
assert_eq!(span(start, mid).trim_end(other), None);
73+
assert_eq!(span(start, end).trim_end(other), None);
74+
assert_eq!(span(start, after).trim_end(other), None);
75+
76+
assert_eq!(span(mid, end).trim_end(other), None);
77+
assert_eq!(span(mid, after).trim_end(other), None);
78+
79+
assert_eq!(span(end, after).trim_end(other), None);
80+
81+
assert_eq!(span(after, well_after).trim_end(other), None);
82+
83+
// Test cases for `trim_start`.
84+
85+
assert_eq!(span(after, well_after).trim_start(other), Some(span(after, well_after)));
86+
assert_eq!(span(end, well_after).trim_start(other), Some(span(end, well_after)));
87+
assert_eq!(span(mid, well_after).trim_start(other), Some(span(end, well_after)));
88+
assert_eq!(span(start, well_after).trim_start(other), Some(span(end, well_after)));
89+
assert_eq!(span(before, well_after).trim_start(other), Some(span(end, well_after)));
90+
91+
assert_eq!(span(mid, end).trim_start(other), None);
92+
assert_eq!(span(start, end).trim_start(other), None);
93+
assert_eq!(span(before, end).trim_start(other), None);
94+
95+
assert_eq!(span(start, mid).trim_start(other), None);
96+
assert_eq!(span(before, mid).trim_start(other), None);
97+
98+
assert_eq!(span(before, start).trim_start(other), None);
99+
100+
assert_eq!(span(well_before, before).trim_start(other), None);
101+
}

‎tests/coverage/closure_macro.cov-map

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@ 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 (36): 0x[01, 01, 01, 01, 05, 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, 01, 03, 01, 00, 02]
10+
Raw bytes (31): 0x[01, 01, 01, 01, 05, 05, 01, 21, 01, 01, 21, 02, 02, 09, 00, 0f, 05, 00, 54, 00, 55, 02, 02, 09, 02, 0b, 01, 03, 01, 00, 02]
1111
Number of files: 1
1212
- file 0 => global file 1
1313
Number of expressions: 1
1414
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
15-
Number of file 0 mappings: 6
15+
Number of file 0 mappings: 5
1616
- Code(Counter(0)) at (prev + 33, 1) to (start + 1, 33)
17-
- Code(Expression(0, Sub)) at (prev + 2, 9) to (start + 0, 18)
18-
= (c0 - c1)
19-
- Code(Expression(0, Sub)) at (prev + 0, 15) to (start + 0, 84)
17+
- Code(Expression(0, Sub)) at (prev + 2, 9) to (start + 0, 15)
2018
= (c0 - c1)
2119
- Code(Counter(1)) at (prev + 0, 84) to (start + 0, 85)
2220
- Code(Expression(0, Sub)) at (prev + 2, 9) to (start + 2, 11)

‎tests/coverage/closure_macro_async.cov-map

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@ 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 (36): 0x[01, 01, 01, 01, 05, 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, 01, 03, 01, 00, 02]
18+
Raw bytes (31): 0x[01, 01, 01, 01, 05, 05, 01, 23, 2b, 01, 21, 02, 02, 09, 00, 0f, 05, 00, 54, 00, 55, 02, 02, 09, 02, 0b, 01, 03, 01, 00, 02]
1919
Number of files: 1
2020
- file 0 => global file 1
2121
Number of expressions: 1
2222
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
23-
Number of file 0 mappings: 6
23+
Number of file 0 mappings: 5
2424
- Code(Counter(0)) at (prev + 35, 43) to (start + 1, 33)
25-
- Code(Expression(0, Sub)) at (prev + 2, 9) to (start + 0, 18)
26-
= (c0 - c1)
27-
- Code(Expression(0, Sub)) at (prev + 0, 15) to (start + 0, 84)
25+
- Code(Expression(0, Sub)) at (prev + 2, 9) to (start + 0, 15)
2826
= (c0 - c1)
2927
- Code(Counter(1)) at (prev + 0, 84) to (start + 0, 85)
3028
- Code(Expression(0, Sub)) at (prev + 2, 9) to (start + 2, 11)

0 commit comments

Comments
 (0)
Please sign in to comment.