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