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