rustc_mir_transform/coverage/
spans.rs

1use rustc_data_structures::fx::FxHashSet;
2use rustc_middle::mir;
3use rustc_middle::ty::TyCtxt;
4use rustc_span::source_map::SourceMap;
5use rustc_span::{BytePos, DesugaringKind, ExpnKind, MacroKind, Span};
6use tracing::instrument;
7
8use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph};
9use crate::coverage::spans::from_mir::{Hole, RawSpanFromMir, SpanFromMir};
10use crate::coverage::{ExtractedHirInfo, mappings, unexpand};
11
12mod from_mir;
13
14pub(super) fn extract_refined_covspans<'tcx>(
15    tcx: TyCtxt<'tcx>,
16    mir_body: &mir::Body<'tcx>,
17    hir_info: &ExtractedHirInfo,
18    graph: &CoverageGraph,
19    code_mappings: &mut impl Extend<mappings::CodeMapping>,
20) {
21    let &ExtractedHirInfo { body_span, .. } = hir_info;
22
23    let raw_spans = from_mir::extract_raw_spans_from_mir(mir_body, graph);
24    let mut covspans = raw_spans
25        .into_iter()
26        .filter_map(|RawSpanFromMir { raw_span, bcb }| try {
27            let (span, expn_kind) =
28                unexpand::unexpand_into_body_span_with_expn_kind(raw_span, body_span)?;
29            // Discard any spans that fill the entire body, because they tend
30            // to represent compiler-inserted code, e.g. implicitly returning `()`.
31            if span.source_equal(body_span) {
32                return None;
33            };
34            SpanFromMir { span, expn_kind, bcb }
35        })
36        .collect::<Vec<_>>();
37
38    // Only proceed if we found at least one usable span.
39    if covspans.is_empty() {
40        return;
41    }
42
43    // Also add the function signature span, if available.
44    // Otherwise, add a fake span at the start of the body, to avoid an ugly
45    // gap between the start of the body and the first real span.
46    // FIXME: Find a more principled way to solve this problem.
47    covspans.push(SpanFromMir::for_fn_sig(
48        hir_info.fn_sig_span.unwrap_or_else(|| body_span.shrink_to_lo()),
49    ));
50
51    // First, perform the passes that need macro information.
52    covspans.sort_by(|a, b| graph.cmp_in_dominator_order(a.bcb, b.bcb));
53    remove_unwanted_expansion_spans(&mut covspans);
54    shrink_visible_macro_spans(tcx, &mut covspans);
55
56    // We no longer need the extra information in `SpanFromMir`, so convert to `Covspan`.
57    let mut covspans = covspans.into_iter().map(SpanFromMir::into_covspan).collect::<Vec<_>>();
58
59    let compare_covspans = |a: &Covspan, b: &Covspan| {
60        compare_spans(a.span, b.span)
61            // After deduplication, we want to keep only the most-dominated BCB.
62            .then_with(|| graph.cmp_in_dominator_order(a.bcb, b.bcb).reverse())
63    };
64    covspans.sort_by(compare_covspans);
65
66    // Among covspans with the same span, keep only one,
67    // preferring the one with the most-dominated BCB.
68    // (Ideally we should try to preserve _all_ non-dominating BCBs, but that
69    // requires a lot more complexity in the span refiner, for little benefit.)
70    covspans.dedup_by(|b, a| a.span.source_equal(b.span));
71
72    // Sort the holes, and merge overlapping/adjacent holes.
73    let mut holes = hir_info
74        .hole_spans
75        .iter()
76        .copied()
77        // Discard any holes that aren't directly visible within the body span.
78        .filter(|&hole_span| body_span.contains(hole_span) && body_span.eq_ctxt(hole_span))
79        .map(|span| Hole { span })
80        .collect::<Vec<_>>();
81    holes.sort_by(|a, b| compare_spans(a.span, b.span));
82    holes.dedup_by(|b, a| a.merge_if_overlapping_or_adjacent(b));
83
84    // Discard any span that overlaps with a hole.
85    discard_spans_overlapping_holes(&mut covspans, &holes);
86
87    // Discard spans that overlap in unwanted ways.
88    let mut covspans = remove_unwanted_overlapping_spans(covspans);
89
90    // For all empty spans, either enlarge them to be non-empty, or discard them.
91    let source_map = tcx.sess.source_map();
92    covspans.retain_mut(|covspan| {
93        let Some(span) = ensure_non_empty_span(source_map, covspan.span) else { return false };
94        covspan.span = span;
95        true
96    });
97
98    // Merge covspans that can be merged.
99    covspans.dedup_by(|b, a| a.merge_if_eligible(b));
100
101    code_mappings.extend(covspans.into_iter().map(|Covspan { span, bcb }| {
102        // Each span produced by the refiner represents an ordinary code region.
103        mappings::CodeMapping { span, bcb }
104    }));
105}
106
107/// Macros that expand into branches (e.g. `assert!`, `trace!`) tend to generate
108/// multiple condition/consequent blocks that have the span of the whole macro
109/// invocation, which is unhelpful. Keeping only the first such span seems to
110/// give better mappings, so remove the others.
111///
112/// Similarly, `await` expands to a branch on the discriminant of `Poll`, which
113/// leads to incorrect coverage if the `Future` is immediately ready (#98712).
114///
115/// (The input spans should be sorted in BCB dominator order, so that the
116/// retained "first" span is likely to dominate the others.)
117fn remove_unwanted_expansion_spans(covspans: &mut Vec<SpanFromMir>) {
118    let mut deduplicated_spans = FxHashSet::default();
119
120    covspans.retain(|covspan| {
121        match covspan.expn_kind {
122            // Retain only the first await-related or macro-expanded covspan with this span.
123            Some(ExpnKind::Desugaring(DesugaringKind::Await)) => {
124                deduplicated_spans.insert(covspan.span)
125            }
126            Some(ExpnKind::Macro(MacroKind::Bang, _)) => deduplicated_spans.insert(covspan.span),
127            // Ignore (retain) other spans.
128            _ => true,
129        }
130    });
131}
132
133/// When a span corresponds to a macro invocation that is visible from the
134/// function body, truncate it to just the macro name plus `!`.
135/// This seems to give better results for code that uses macros.
136fn shrink_visible_macro_spans(tcx: TyCtxt<'_>, covspans: &mut Vec<SpanFromMir>) {
137    let source_map = tcx.sess.source_map();
138
139    for covspan in covspans {
140        if matches!(covspan.expn_kind, Some(ExpnKind::Macro(MacroKind::Bang, _))) {
141            covspan.span = source_map.span_through_char(covspan.span, '!');
142        }
143    }
144}
145
146/// Discard all covspans that overlap a hole.
147///
148/// The lists of covspans and holes must be sorted, and any holes that overlap
149/// with each other must have already been merged.
150fn discard_spans_overlapping_holes(covspans: &mut Vec<Covspan>, holes: &[Hole]) {
151    debug_assert!(covspans.is_sorted_by(|a, b| compare_spans(a.span, b.span).is_le()));
152    debug_assert!(holes.is_sorted_by(|a, b| compare_spans(a.span, b.span).is_le()));
153    debug_assert!(holes.array_windows().all(|[a, b]| !a.span.overlaps_or_adjacent(b.span)));
154
155    let mut curr_hole = 0usize;
156    let mut overlaps_hole = |covspan: &Covspan| -> bool {
157        while let Some(hole) = holes.get(curr_hole) {
158            // Both lists are sorted, so we can permanently skip any holes that
159            // end before the start of the current span.
160            if hole.span.hi() <= covspan.span.lo() {
161                curr_hole += 1;
162                continue;
163            }
164
165            return hole.span.overlaps(covspan.span);
166        }
167
168        // No holes left, so this covspan doesn't overlap with any holes.
169        false
170    };
171
172    covspans.retain(|covspan| !overlaps_hole(covspan));
173}
174
175/// Takes a list of sorted spans extracted from MIR, and "refines"
176/// those spans by removing spans that overlap in unwanted ways.
177#[instrument(level = "debug")]
178fn remove_unwanted_overlapping_spans(sorted_spans: Vec<Covspan>) -> Vec<Covspan> {
179    debug_assert!(sorted_spans.is_sorted_by(|a, b| compare_spans(a.span, b.span).is_le()));
180
181    // Holds spans that have been read from the input vector, but haven't yet
182    // been committed to the output vector.
183    let mut pending = vec![];
184    let mut refined = vec![];
185
186    for curr in sorted_spans {
187        pending.retain(|prev: &Covspan| {
188            if prev.span.hi() <= curr.span.lo() {
189                // There's no overlap between the previous/current covspans,
190                // so move the previous one into the refined list.
191                refined.push(prev.clone());
192                false
193            } else {
194                // Otherwise, retain the previous covspan only if it has the
195                // same BCB. This tends to discard long outer spans that enclose
196                // smaller inner spans with different control flow.
197                prev.bcb == curr.bcb
198            }
199        });
200        pending.push(curr);
201    }
202
203    // Drain the rest of the pending list into the refined list.
204    refined.extend(pending);
205    refined
206}
207
208#[derive(Clone, Debug)]
209struct Covspan {
210    span: Span,
211    bcb: BasicCoverageBlock,
212}
213
214impl Covspan {
215    /// If `self` and `other` can be merged, mutates `self.span` to also
216    /// include `other.span` and returns true.
217    ///
218    /// Two covspans can be merged if they have the same BCB, and they are
219    /// overlapping or adjacent.
220    fn merge_if_eligible(&mut self, other: &Self) -> bool {
221        let eligible_for_merge =
222            |a: &Self, b: &Self| (a.bcb == b.bcb) && a.span.overlaps_or_adjacent(b.span);
223
224        if eligible_for_merge(self, other) {
225            self.span = self.span.to(other.span);
226            true
227        } else {
228            false
229        }
230    }
231}
232
233/// Compares two spans in (lo ascending, hi descending) order.
234fn compare_spans(a: Span, b: Span) -> std::cmp::Ordering {
235    // First sort by span start.
236    Ord::cmp(&a.lo(), &b.lo())
237        // If span starts are the same, sort by span end in reverse order.
238        // This ensures that if spans A and B are adjacent in the list,
239        // and they overlap but are not equal, then either:
240        // - Span A extends further left, or
241        // - Both have the same start and span A extends further right
242        .then_with(|| Ord::cmp(&a.hi(), &b.hi()).reverse())
243}
244
245fn ensure_non_empty_span(source_map: &SourceMap, span: Span) -> Option<Span> {
246    if !span.is_empty() {
247        return Some(span);
248    }
249
250    // The span is empty, so try to enlarge it to cover an adjacent '{' or '}'.
251    source_map
252        .span_to_source(span, |src, start, end| try {
253            // Adjusting span endpoints by `BytePos(1)` is normally a bug,
254            // but in this case we have specifically checked that the character
255            // we're skipping over is one of two specific ASCII characters, so
256            // adjusting by exactly 1 byte is correct.
257            if src.as_bytes().get(end).copied() == Some(b'{') {
258                Some(span.with_hi(span.hi() + BytePos(1)))
259            } else if start > 0 && src.as_bytes()[start - 1] == b'}' {
260                Some(span.with_lo(span.lo() - BytePos(1)))
261            } else {
262                None
263            }
264        })
265        .ok()?
266}