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    // 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().outer_expn()) 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_expn_id in &node.child_expn_ids {
42        if let Some(covspan) = single_covspan_for_child_expn(tcx, graph, &expn_tree, child_expn_id)
43        {
44            covspans.push(covspan);
45        }
46    }
47
48    if let Some(body_span) = node.body_span {
49        covspans.retain(|covspan: &Covspan| {
50            let covspan_span = covspan.span;
51            // Discard any spans not contained within the function body span.
52            // Also discard any spans that fill the entire body, because they tend
53            // to represent compiler-inserted code, e.g. implicitly returning `()`.
54            if !body_span.contains(covspan_span) || body_span.source_equal(covspan_span) {
55                return false;
56            }
57
58            // Each pushed covspan should have the same context as the body span.
59            // If it somehow doesn't, discard the covspan, or panic in debug builds.
60            if !body_span.eq_ctxt(covspan_span) {
61                debug_assert!(
62                    false,
63                    "span context mismatch: body_span={body_span:?}, covspan.span={covspan_span:?}"
64                );
65                return false;
66            }
67
68            true
69        });
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    if let Some(span) = node.fn_sig_span.or_else(|| try { node.body_span?.shrink_to_lo() }) {
82        covspans.push(Covspan { span, bcb: START_BCB });
83    }
84
85    let compare_covspans = |a: &Covspan, b: &Covspan| {
86        compare_spans(a.span, b.span)
87            // After deduplication, we want to keep only the most-dominated BCB.
88            .then_with(|| graph.cmp_in_dominator_order(a.bcb, b.bcb).reverse())
89    };
90    covspans.sort_by(compare_covspans);
91
92    // Among covspans with the same span, keep only one,
93    // preferring the one with the most-dominated BCB.
94    // (Ideally we should try to preserve _all_ non-dominating BCBs, but that
95    // requires a lot more complexity in the span refiner, for little benefit.)
96    covspans.dedup_by(|b, a| a.span.source_equal(b.span));
97
98    // Sort the holes, and merge overlapping/adjacent holes.
99    let mut holes = node.hole_spans.iter().copied().map(|span| Hole { span }).collect::<Vec<_>>();
100
101    holes.sort_by(|a, b| compare_spans(a.span, b.span));
102    holes.dedup_by(|b, a| a.merge_if_overlapping_or_adjacent(b));
103
104    // Discard any span that overlaps with a hole.
105    discard_spans_overlapping_holes(&mut covspans, &holes);
106
107    // Discard spans that overlap in unwanted ways.
108    let mut covspans = remove_unwanted_overlapping_spans(covspans);
109
110    // For all empty spans, either enlarge them to be non-empty, or discard them.
111    let source_map = tcx.sess.source_map();
112    covspans.retain_mut(|covspan| {
113        let Some(span) = ensure_non_empty_span(source_map, covspan.span) else { return false };
114        covspan.span = span;
115        true
116    });
117
118    // Merge covspans that can be merged.
119    covspans.dedup_by(|b, a| a.merge_if_eligible(b));
120
121    mappings.extend(covspans.into_iter().map(|Covspan { span, bcb }| {
122        // Each span produced by the refiner represents an ordinary code region.
123        Mapping { span, kind: MappingKind::Code { bcb } }
124    }));
125}
126
127/// For a single child expansion, try to distill it into a single span+BCB mapping.
128fn single_covspan_for_child_expn(
129    tcx: TyCtxt<'_>,
130    graph: &CoverageGraph,
131    expn_tree: &ExpnTree,
132    expn_id: ExpnId,
133) -> Option<Covspan> {
134    let node = expn_tree.get(expn_id)?;
135
136    let bcbs =
137        expn_tree.iter_node_and_descendants(expn_id).flat_map(|n| n.spans.iter().map(|s| s.bcb));
138
139    let bcb = match node.expn_kind {
140        // For bang-macros (e.g. `assert!`, `trace!`) and for `await`, taking
141        // the "first" BCB in dominator order seems to give good results.
142        ExpnKind::Macro(MacroKind::Bang, _) | ExpnKind::Desugaring(DesugaringKind::Await) => {
143            bcbs.min_by(|&a, &b| graph.cmp_in_dominator_order(a, b))?
144        }
145        // For other kinds of expansion, taking the "last" (most-dominated) BCB
146        // seems to give good results.
147        _ => bcbs.max_by(|&a, &b| graph.cmp_in_dominator_order(a, b))?,
148    };
149
150    // For bang-macro expansions, limit the call-site span to just the macro
151    // name plus `!`, excluding the macro arguments.
152    let mut span = node.call_site?;
153    if matches!(node.expn_kind, ExpnKind::Macro(MacroKind::Bang, _)) {
154        span = tcx.sess.source_map().span_through_char(span, '!');
155    }
156
157    Some(Covspan { span, bcb })
158}
159
160/// Discard all covspans that overlap a hole.
161///
162/// The lists of covspans and holes must be sorted, and any holes that overlap
163/// with each other must have already been merged.
164fn discard_spans_overlapping_holes(covspans: &mut Vec<Covspan>, holes: &[Hole]) {
165    debug_assert!(covspans.is_sorted_by(|a, b| compare_spans(a.span, b.span).is_le()));
166    debug_assert!(holes.is_sorted_by(|a, b| compare_spans(a.span, b.span).is_le()));
167    debug_assert!(holes.array_windows().all(|[a, b]| !a.span.overlaps_or_adjacent(b.span)));
168
169    let mut curr_hole = 0usize;
170    let mut overlaps_hole = |covspan: &Covspan| -> bool {
171        while let Some(hole) = holes.get(curr_hole) {
172            // Both lists are sorted, so we can permanently skip any holes that
173            // end before the start of the current span.
174            if hole.span.hi() <= covspan.span.lo() {
175                curr_hole += 1;
176                continue;
177            }
178
179            return hole.span.overlaps(covspan.span);
180        }
181
182        // No holes left, so this covspan doesn't overlap with any holes.
183        false
184    };
185
186    covspans.retain(|covspan| !overlaps_hole(covspan));
187}
188
189/// Takes a list 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    /// If `self` and `other` can be merged, mutates `self.span` to also
230    /// include `other.span` and returns true.
231    ///
232    /// Two covspans can be merged if they have the same BCB, and they are
233    /// overlapping or adjacent.
234    fn merge_if_eligible(&mut self, other: &Self) -> bool {
235        let eligible_for_merge =
236            |a: &Self, b: &Self| (a.bcb == b.bcb) && a.span.overlaps_or_adjacent(b.span);
237
238        if eligible_for_merge(self, other) {
239            self.span = self.span.to(other.span);
240            true
241        } else {
242            false
243        }
244    }
245}
246
247/// Compares two spans in (lo ascending, hi descending) order.
248fn compare_spans(a: Span, b: Span) -> std::cmp::Ordering {
249    // First sort by span start.
250    Ord::cmp(&a.lo(), &b.lo())
251        // If span starts are the same, sort by span end in reverse order.
252        // This ensures that if spans A and B are adjacent in the list,
253        // and they overlap but are not equal, then either:
254        // - Span A extends further left, or
255        // - Both have the same start and span A extends further right
256        .then_with(|| Ord::cmp(&a.hi(), &b.hi()).reverse())
257}
258
259fn ensure_non_empty_span(source_map: &SourceMap, span: Span) -> Option<Span> {
260    if !span.is_empty() {
261        return Some(span);
262    }
263
264    // The span is empty, so try to enlarge it to cover an adjacent '{' or '}'.
265    source_map
266        .span_to_source(span, |src, start, end| try {
267            // Adjusting span endpoints by `BytePos(1)` is normally a bug,
268            // but in this case we have specifically checked that the character
269            // we're skipping over is one of two specific ASCII characters, so
270            // adjusting by exactly 1 byte is correct.
271            if src.as_bytes().get(end).copied() == Some(b'{') {
272                Some(span.with_hi(span.hi() + BytePos(1)))
273            } else if start > 0 && src.as_bytes()[start - 1] == b'}' {
274                Some(span.with_lo(span.lo() - BytePos(1)))
275            } else {
276                None
277            }
278        })
279        .ok()?
280}
281
282#[derive(Debug)]
283struct Hole {
284    span: Span,
285}
286
287impl Hole {
288    fn merge_if_overlapping_or_adjacent(&mut self, other: &mut Self) -> bool {
289        if !self.span.overlaps_or_adjacent(other.span) {
290            return false;
291        }
292
293        self.span = self.span.to(other.span);
294        true
295    }
296}