rustc_mir_transform/coverage/
spans.rs

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