rustc_mir_transform/coverage/
spans.rs

1use std::collections::VecDeque;
2
3use rustc_data_structures::captures::Captures;
4use rustc_data_structures::fx::FxHashSet;
5use rustc_middle::mir;
6use rustc_span::{DesugaringKind, ExpnKind, MacroKind, Span};
7use tracing::{debug, debug_span, instrument};
8
9use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph};
10use crate::coverage::spans::from_mir::{
11    ExtractedCovspans, Hole, SpanFromMir, extract_covspans_from_mir,
12};
13use crate::coverage::{ExtractedHirInfo, mappings};
14
15mod from_mir;
16
17pub(super) fn extract_refined_covspans(
18    mir_body: &mir::Body<'_>,
19    hir_info: &ExtractedHirInfo,
20    graph: &CoverageGraph,
21    code_mappings: &mut impl Extend<mappings::CodeMapping>,
22) {
23    let ExtractedCovspans { mut covspans } = extract_covspans_from_mir(mir_body, hir_info, graph);
24
25    // First, perform the passes that need macro information.
26    covspans.sort_by(|a, b| graph.cmp_in_dominator_order(a.bcb, b.bcb));
27    remove_unwanted_expansion_spans(&mut covspans);
28    split_visible_macro_spans(&mut covspans);
29
30    // We no longer need the extra information in `SpanFromMir`, so convert to `Covspan`.
31    let mut covspans = covspans.into_iter().map(SpanFromMir::into_covspan).collect::<Vec<_>>();
32
33    let compare_covspans = |a: &Covspan, b: &Covspan| {
34        compare_spans(a.span, b.span)
35            // After deduplication, we want to keep only the most-dominated BCB.
36            .then_with(|| graph.cmp_in_dominator_order(a.bcb, b.bcb).reverse())
37    };
38    covspans.sort_by(compare_covspans);
39
40    // Among covspans with the same span, keep only one,
41    // preferring the one with the most-dominated BCB.
42    // (Ideally we should try to preserve _all_ non-dominating BCBs, but that
43    // requires a lot more complexity in the span refiner, for little benefit.)
44    covspans.dedup_by(|b, a| a.span.source_equal(b.span));
45
46    // Sort the holes, and merge overlapping/adjacent holes.
47    let mut holes = hir_info.hole_spans.iter().map(|&span| Hole { span }).collect::<Vec<_>>();
48    holes.sort_by(|a, b| compare_spans(a.span, b.span));
49    holes.dedup_by(|b, a| a.merge_if_overlapping_or_adjacent(b));
50
51    // Split the covspans into separate buckets that don't overlap any holes.
52    let buckets = divide_spans_into_buckets(covspans, &holes);
53
54    for mut covspans in buckets {
55        // Make sure each individual bucket is internally sorted.
56        covspans.sort_by(compare_covspans);
57        let _span = debug_span!("processing bucket", ?covspans).entered();
58
59        let mut covspans = remove_unwanted_overlapping_spans(covspans);
60        debug!(?covspans, "after removing overlaps");
61
62        // Do one last merge pass, to simplify the output.
63        covspans.dedup_by(|b, a| a.merge_if_eligible(b));
64        debug!(?covspans, "after merge");
65
66        code_mappings.extend(covspans.into_iter().map(|Covspan { span, bcb }| {
67            // Each span produced by the refiner represents an ordinary code region.
68            mappings::CodeMapping { span, bcb }
69        }));
70    }
71}
72
73/// Macros that expand into branches (e.g. `assert!`, `trace!`) tend to generate
74/// multiple condition/consequent blocks that have the span of the whole macro
75/// invocation, which is unhelpful. Keeping only the first such span seems to
76/// give better mappings, so remove the others.
77///
78/// Similarly, `await` expands to a branch on the discriminant of `Poll`, which
79/// leads to incorrect coverage if the `Future` is immediately ready (#98712).
80///
81/// (The input spans should be sorted in BCB dominator order, so that the
82/// retained "first" span is likely to dominate the others.)
83fn remove_unwanted_expansion_spans(covspans: &mut Vec<SpanFromMir>) {
84    let mut deduplicated_spans = FxHashSet::default();
85
86    covspans.retain(|covspan| {
87        match covspan.expn_kind {
88            // Retain only the first await-related or macro-expanded covspan with this span.
89            Some(ExpnKind::Desugaring(DesugaringKind::Await)) => {
90                deduplicated_spans.insert(covspan.span)
91            }
92            Some(ExpnKind::Macro(MacroKind::Bang, _)) => deduplicated_spans.insert(covspan.span),
93            // Ignore (retain) other spans.
94            _ => true,
95        }
96    });
97}
98
99/// When a span corresponds to a macro invocation that is visible from the
100/// function body, split it into two parts. The first part covers just the
101/// macro name plus `!`, and the second part covers the rest of the macro
102/// invocation. This seems to give better results for code that uses macros.
103fn split_visible_macro_spans(covspans: &mut Vec<SpanFromMir>) {
104    let mut extra_spans = vec![];
105
106    covspans.retain(|covspan| {
107        let Some(ExpnKind::Macro(MacroKind::Bang, visible_macro)) = covspan.expn_kind else {
108            return true;
109        };
110
111        let split_len = visible_macro.as_str().len() as u32 + 1;
112        let (before, after) = covspan.span.split_at(split_len);
113        if !covspan.span.contains(before) || !covspan.span.contains(after) {
114            // Something is unexpectedly wrong with the split point.
115            // The debug assertion in `split_at` will have already caught this,
116            // but in release builds it's safer to do nothing and maybe get a
117            // bug report for unexpected coverage, rather than risk an ICE.
118            return true;
119        }
120
121        extra_spans.push(SpanFromMir::new(before, covspan.expn_kind.clone(), covspan.bcb));
122        extra_spans.push(SpanFromMir::new(after, covspan.expn_kind.clone(), covspan.bcb));
123        false // Discard the original covspan that we just split.
124    });
125
126    // The newly-split spans are added at the end, so any previous sorting
127    // is not preserved.
128    covspans.extend(extra_spans);
129}
130
131/// Uses the holes to divide the given covspans into buckets, such that:
132/// - No span in any hole overlaps a bucket (truncating the spans if necessary).
133/// - The spans in each bucket are strictly after all spans in previous buckets,
134///   and strictly before all spans in subsequent buckets.
135///
136/// The resulting buckets are sorted relative to each other, but might not be
137/// internally sorted.
138#[instrument(level = "debug")]
139fn divide_spans_into_buckets(input_covspans: Vec<Covspan>, holes: &[Hole]) -> Vec<Vec<Covspan>> {
140    debug_assert!(input_covspans.is_sorted_by(|a, b| compare_spans(a.span, b.span).is_le()));
141    debug_assert!(holes.is_sorted_by(|a, b| compare_spans(a.span, b.span).is_le()));
142
143    // Now we're ready to start carving holes out of the initial coverage spans,
144    // and grouping them in buckets separated by the holes.
145
146    let mut input_covspans = VecDeque::from(input_covspans);
147    let mut fragments = vec![];
148
149    // For each hole:
150    // - Identify the spans that are entirely or partly before the hole.
151    // - Put those spans in a corresponding bucket, truncated to the start of the hole.
152    // - If one of those spans also extends after the hole, put the rest of it
153    //   in a "fragments" vector that is processed by the next hole.
154    let mut buckets = (0..holes.len()).map(|_| vec![]).collect::<Vec<_>>();
155    for (hole, bucket) in holes.iter().zip(&mut buckets) {
156        let fragments_from_prev = std::mem::take(&mut fragments);
157
158        // Only inspect spans that precede or overlap this hole,
159        // leaving the rest to be inspected by later holes.
160        // (This relies on the spans and holes both being sorted.)
161        let relevant_input_covspans =
162            drain_front_while(&mut input_covspans, |c| c.span.lo() < hole.span.hi());
163
164        for covspan in fragments_from_prev.into_iter().chain(relevant_input_covspans) {
165            let (before, after) = covspan.split_around_hole_span(hole.span);
166            bucket.extend(before);
167            fragments.extend(after);
168        }
169    }
170
171    // After finding the spans before each hole, any remaining fragments/spans
172    // form their own final bucket, after the final hole.
173    // (If there were no holes, this will just be all of the initial spans.)
174    fragments.extend(input_covspans);
175    buckets.push(fragments);
176
177    buckets
178}
179
180/// Similar to `.drain(..)`, but stops just before it would remove an item not
181/// satisfying the predicate.
182fn drain_front_while<'a, T>(
183    queue: &'a mut VecDeque<T>,
184    mut pred_fn: impl FnMut(&T) -> bool,
185) -> impl Iterator<Item = T> + Captures<'a> {
186    std::iter::from_fn(move || if pred_fn(queue.front()?) { queue.pop_front() } else { None })
187}
188
189/// Takes one of the buckets of (sorted) spans extracted from MIR, and "refines"
190/// those spans by removing spans that overlap in unwanted ways.
191#[instrument(level = "debug")]
192fn remove_unwanted_overlapping_spans(sorted_spans: Vec<Covspan>) -> Vec<Covspan> {
193    debug_assert!(sorted_spans.is_sorted_by(|a, b| compare_spans(a.span, b.span).is_le()));
194
195    // Holds spans that have been read from the input vector, but haven't yet
196    // been committed to the output vector.
197    let mut pending = vec![];
198    let mut refined = vec![];
199
200    for curr in sorted_spans {
201        pending.retain(|prev: &Covspan| {
202            if prev.span.hi() <= curr.span.lo() {
203                // There's no overlap between the previous/current covspans,
204                // so move the previous one into the refined list.
205                refined.push(prev.clone());
206                false
207            } else {
208                // Otherwise, retain the previous covspan only if it has the
209                // same BCB. This tends to discard long outer spans that enclose
210                // smaller inner spans with different control flow.
211                prev.bcb == curr.bcb
212            }
213        });
214        pending.push(curr);
215    }
216
217    // Drain the rest of the pending list into the refined list.
218    refined.extend(pending);
219    refined
220}
221
222#[derive(Clone, Debug)]
223struct Covspan {
224    span: Span,
225    bcb: BasicCoverageBlock,
226}
227
228impl Covspan {
229    /// Splits this covspan into 0-2 parts:
230    /// - The part that is strictly before the hole span, if any.
231    /// - The part that is strictly after the hole span, if any.
232    fn split_around_hole_span(&self, hole_span: Span) -> (Option<Self>, Option<Self>) {
233        let before = try {
234            let span = self.span.trim_end(hole_span)?;
235            Self { span, ..*self }
236        };
237        let after = try {
238            let span = self.span.trim_start(hole_span)?;
239            Self { span, ..*self }
240        };
241
242        (before, after)
243    }
244
245    /// If `self` and `other` can be merged (i.e. they have the same BCB),
246    /// mutates `self.span` to also include `other.span` and returns true.
247    ///
248    /// Note that compatible covspans can be merged even if their underlying
249    /// spans are not overlapping/adjacent; any space between them will also be
250    /// part of the merged covspan.
251    fn merge_if_eligible(&mut self, other: &Self) -> bool {
252        if self.bcb != other.bcb {
253            return false;
254        }
255
256        self.span = self.span.to(other.span);
257        true
258    }
259}
260
261/// Compares two spans in (lo ascending, hi descending) order.
262fn compare_spans(a: Span, b: Span) -> std::cmp::Ordering {
263    // First sort by span start.
264    Ord::cmp(&a.lo(), &b.lo())
265        // If span starts are the same, sort by span end in reverse order.
266        // This ensures that if spans A and B are adjacent in the list,
267        // and they overlap but are not equal, then either:
268        // - Span A extends further left, or
269        // - Both have the same start and span A extends further right
270        .then_with(|| Ord::cmp(&a.hi(), &b.hi()).reverse())
271}