Skip to main content

rustc_mir_transform/coverage/
spans.rs

1use rustc_middle::mir::coverage::{Mapping, MappingKind, START_BCB};
2use rustc_middle::ty::TyCtxt;
3use rustc_span::source_map::SourceMap;
4use rustc_span::{BytePos, DesugaringKind, ExpnKind, MacroKind, Span, SyntaxContext};
5use tracing::instrument;
6
7use crate::coverage::expansion::{ExpnTree, SpanWithBcb};
8use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph};
9use crate::coverage::hir_info::ExtractedHirInfo;
10
11pub(super) fn extract_refined_covspans<'tcx>(
12    tcx: TyCtxt<'tcx>,
13    hir_info: &ExtractedHirInfo,
14    graph: &CoverageGraph,
15    expn_tree: &ExpnTree,
16    mappings: &mut Vec<Mapping>,
17) {
18    if hir_info.is_async_fn {
19        // An async function desugars into a function that returns a future,
20        // with the user code wrapped in a closure. Any spans in the desugared
21        // outer function will be unhelpful, so just keep the signature span
22        // and ignore all of the spans in the MIR body.
23        if let Some(span) = hir_info.fn_sig_span {
24            mappings.push(Mapping { span, kind: MappingKind::Code { bcb: START_BCB } })
25        }
26        return;
27    }
28
29    // If there somehow isn't an expansion tree node corresponding to the
30    // body span, return now and don't create any mappings.
31    let Some(node) = expn_tree.get(hir_info.body_span.ctxt()) else { return };
32
33    let mut covspans = vec![];
34
35    for &SpanWithBcb { span, bcb } in &node.spans {
36        covspans.push(Covspan { span, bcb });
37    }
38
39    // For each expansion with its call-site in the body span, try to
40    // distill a corresponding covspan.
41    for &child_context in &node.child_contexts {
42        if let Some(covspan) = single_covspan_for_child_context(tcx, &expn_tree, child_context) {
43            covspans.push(covspan);
44        }
45    }
46
47    if let Some(body_span) = node.body_span {
48        covspans.retain(|covspan: &Covspan| {
49            let covspan_span = covspan.span;
50            // Discard any spans not contained within the function body span.
51            // Also discard any spans that fill the entire body, because they tend
52            // to represent compiler-inserted code, e.g. implicitly returning `()`.
53            if !body_span.contains(covspan_span) || body_span.source_equal(covspan_span) {
54                return false;
55            }
56
57            // Each pushed covspan should have the same context as the body span.
58            // If it somehow doesn't, discard the covspan.
59            if !body_span.eq_ctxt(covspan_span) {
60                // There is currently no known way for this to happen, but if it
61                // does happen then dropping the offending span is better than
62                // having tricky macro expansions trigger an ICE.
63                return false;
64            }
65
66            true
67        });
68    }
69
70    // Only proceed if we found at least one usable span.
71    if covspans.is_empty() {
72        return;
73    }
74
75    // Also add the function signature span, if available.
76    // Otherwise, add a fake span at the start of the body, to avoid an ugly
77    // gap between the start of the body and the first real span.
78    // FIXME: Find a more principled way to solve this problem.
79    if let Some(span) = node.fn_sig_span.or_else(|| try { node.body_span?.shrink_to_lo() }) {
80        covspans.push(Covspan { span, bcb: START_BCB });
81    }
82
83    let compare_covspans = |a: &Covspan, b: &Covspan| {
84        compare_spans(a.span, b.span)
85            // After deduplication, we want to keep only the most-dominated BCB.
86            .then_with(|| graph.cmp_in_dominator_order(a.bcb, b.bcb).reverse())
87    };
88    covspans.sort_by(compare_covspans);
89
90    // Among covspans with the same span, keep only one,
91    // preferring the one with the most-dominated BCB.
92    // (Ideally we should try to preserve _all_ non-dominating BCBs, but that
93    // requires a lot more complexity in the span refiner, for little benefit.)
94    covspans.dedup_by(|b, a| a.span.source_equal(b.span));
95
96    // Sort the holes, and merge overlapping/adjacent holes.
97    let mut holes = node.hole_spans.iter().copied().map(|span| Hole { span }).collect::<Vec<_>>();
98
99    holes.sort_by(|a, b| compare_spans(a.span, b.span));
100    holes.dedup_by(|b, a| a.merge_if_overlapping_or_adjacent(b));
101
102    // Discard any span that overlaps with a hole.
103    discard_spans_overlapping_holes(&mut covspans, &holes);
104
105    // Discard spans that overlap in unwanted ways.
106    let mut covspans = remove_unwanted_overlapping_spans(covspans);
107
108    // For all empty spans, either enlarge them to be non-empty, or discard them.
109    let source_map = tcx.sess.source_map();
110    covspans.retain_mut(|covspan| {
111        let Some(span) = ensure_non_empty_span(source_map, covspan.span) else { return false };
112        covspan.span = span;
113        true
114    });
115
116    // Merge covspans that can be merged.
117    covspans.dedup_by(|b, a| a.merge_if_eligible(b));
118
119    mappings.extend(covspans.into_iter().map(|Covspan { span, bcb }| {
120        // Each span produced by the refiner represents an ordinary code region.
121        Mapping { span, kind: MappingKind::Code { bcb } }
122    }));
123}
124
125/// For a single child expansion, try to distill it into a single span+BCB mapping.
126fn single_covspan_for_child_context(
127    tcx: TyCtxt<'_>,
128    expn_tree: &ExpnTree,
129    child_context: SyntaxContext,
130) -> Option<Covspan> {
131    let node = expn_tree.get(child_context)?;
132    let minmax_bcbs = node.minmax_bcbs?;
133
134    let bcb = match node.expn_kind {
135        // For bang-macros (e.g. `assert!`, `trace!`) and for `await`, taking
136        // the "first" BCB in dominator order seems to give good results.
137        ExpnKind::Macro(MacroKind::Bang, _) | ExpnKind::Desugaring(DesugaringKind::Await) => {
138            minmax_bcbs.min
139        }
140        // For other kinds of expansion, taking the "last" (most-dominated) BCB
141        // seems to give good results.
142        _ => minmax_bcbs.max,
143    };
144
145    // For bang-macro expansions, limit the call-site span to just the macro
146    // name plus `!`, excluding the macro arguments.
147    let mut span = node.call_site?;
148    if matches!(node.expn_kind, ExpnKind::Macro(MacroKind::Bang, _)) {
149        span = tcx.sess.source_map().span_through_char(span, '!');
150    }
151
152    Some(Covspan { span, bcb })
153}
154
155/// Discard all covspans that overlap a hole.
156///
157/// The lists of covspans and holes must be sorted, and any holes that overlap
158/// with each other must have already been merged.
159fn discard_spans_overlapping_holes(covspans: &mut Vec<Covspan>, holes: &[Hole]) {
160    debug_assert!(covspans.is_sorted_by(|a, b| compare_spans(a.span, b.span).is_le()));
161    debug_assert!(holes.is_sorted_by(|a, b| compare_spans(a.span, b.span).is_le()));
162    debug_assert!(holes.array_windows().all(|[a, b]| !a.span.overlaps_or_adjacent(b.span)));
163
164    let mut curr_hole = 0usize;
165    let mut overlaps_hole = |covspan: &Covspan| -> bool {
166        while let Some(hole) = holes.get(curr_hole) {
167            // Both lists are sorted, so we can permanently skip any holes that
168            // end before the start of the current span.
169            if hole.span.hi() <= covspan.span.lo() {
170                curr_hole += 1;
171                continue;
172            }
173
174            return hole.span.overlaps(covspan.span);
175        }
176
177        // No holes left, so this covspan doesn't overlap with any holes.
178        false
179    };
180
181    covspans.retain(|covspan| !overlaps_hole(covspan));
182}
183
184/// Takes a list of sorted spans extracted from MIR, and "refines"
185/// those spans by removing spans that overlap in unwanted ways.
186#[instrument(level = "debug")]
187fn remove_unwanted_overlapping_spans(sorted_spans: Vec<Covspan>) -> Vec<Covspan> {
188    debug_assert!(sorted_spans.is_sorted_by(|a, b| compare_spans(a.span, b.span).is_le()));
189
190    // Holds spans that have been read from the input vector, but haven't yet
191    // been committed to the output vector.
192    let mut pending = vec![];
193    let mut refined = vec![];
194
195    for curr in sorted_spans {
196        pending.retain(|prev: &Covspan| {
197            if prev.span.hi() <= curr.span.lo() {
198                // There's no overlap between the previous/current covspans,
199                // so move the previous one into the refined list.
200                refined.push(prev.clone());
201                false
202            } else {
203                // Otherwise, retain the previous covspan only if it has the
204                // same BCB. This tends to discard long outer spans that enclose
205                // smaller inner spans with different control flow.
206                prev.bcb == curr.bcb
207            }
208        });
209        pending.push(curr);
210    }
211
212    // Drain the rest of the pending list into the refined list.
213    refined.extend(pending);
214    refined
215}
216
217#[derive(Clone, Debug)]
218struct Covspan {
219    span: Span,
220    bcb: BasicCoverageBlock,
221}
222
223impl Covspan {
224    /// If `self` and `other` can be merged, mutates `self.span` to also
225    /// include `other.span` and returns true.
226    ///
227    /// Two covspans can be merged if they have the same BCB, and they are
228    /// overlapping or adjacent.
229    fn merge_if_eligible(&mut self, other: &Self) -> bool {
230        let eligible_for_merge =
231            |a: &Self, b: &Self| (a.bcb == b.bcb) && a.span.overlaps_or_adjacent(b.span);
232
233        if eligible_for_merge(self, other) {
234            self.span = self.span.to(other.span);
235            true
236        } else {
237            false
238        }
239    }
240}
241
242/// Compares two spans in (lo ascending, hi descending) order.
243fn compare_spans(a: Span, b: Span) -> std::cmp::Ordering {
244    // First sort by span start.
245    Ord::cmp(&a.lo(), &b.lo())
246        // If span starts are the same, sort by span end in reverse order.
247        // This ensures that if spans A and B are adjacent in the list,
248        // and they overlap but are not equal, then either:
249        // - Span A extends further left, or
250        // - Both have the same start and span A extends further right
251        .then_with(|| Ord::cmp(&a.hi(), &b.hi()).reverse())
252}
253
254fn ensure_non_empty_span(source_map: &SourceMap, span: Span) -> Option<Span> {
255    if !span.is_empty() {
256        return Some(span);
257    }
258
259    // The span is empty, so try to enlarge it to cover an adjacent '{' or '}'.
260    source_map
261        .span_to_source(span, |src, start, end| try {
262            // Adjusting span endpoints by `BytePos(1)` is normally a bug,
263            // but in this case we have specifically checked that the character
264            // we're skipping over is one of two specific ASCII characters, so
265            // adjusting by exactly 1 byte is correct.
266            if src.as_bytes().get(end).copied() == Some(b'{') {
267                Some(span.with_hi(span.hi() + BytePos(1)))
268            } else if start > 0 && src.as_bytes()[start - 1] == b'}' {
269                Some(span.with_lo(span.lo() - BytePos(1)))
270            } else {
271                None
272            }
273        })
274        .ok()?
275}
276
277#[derive(Debug)]
278struct Hole {
279    span: Span,
280}
281
282impl Hole {
283    fn merge_if_overlapping_or_adjacent(&mut self, other: &mut Self) -> bool {
284        if !self.span.overlaps_or_adjacent(other.span) {
285            return false;
286        }
287
288        self.span = self.span.to(other.span);
289        true
290    }
291}