Skip to content

Commit 617de8c

Browse files
committed
coverage: Move span unexpansion into its own submodule
1 parent 716752e commit 617de8c

File tree

5 files changed

+59
-62
lines changed

5 files changed

+59
-62
lines changed

compiler/rustc_mir_transform/src/coverage/mappings.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ use rustc_middle::ty::TyCtxt;
99
use rustc_span::Span;
1010

1111
use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph, START_BCB};
12-
use crate::coverage::spans::{
13-
extract_refined_covspans, unexpand_into_body_span_with_visible_macro,
14-
};
12+
use crate::coverage::spans::extract_refined_covspans;
13+
use crate::coverage::unexpand::unexpand_into_body_span_with_visible_macro;
1514
use crate::coverage::ExtractedHirInfo;
1615

1716
/// Associates an ordinary executable code span with its corresponding BCB.

compiler/rustc_mir_transform/src/coverage/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod mappings;
66
mod spans;
77
#[cfg(test)]
88
mod tests;
9+
mod unexpand;
910

1011
use rustc_middle::mir::coverage::{
1112
CodeRegion, CoverageKind, DecisionInfo, FunctionCoverageInfo, Mapping, MappingKind,

compiler/rustc_mir_transform/src/coverage/spans.rs

-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ use crate::coverage::ExtractedHirInfo;
1414

1515
mod from_mir;
1616

17-
// FIXME(#124545) It's awkward that we have to re-export this, because it's an
18-
// internal detail of `from_mir` that is also needed when handling branch and
19-
// MC/DC spans. Ideally we would find a more natural home for it.
20-
pub(super) use from_mir::unexpand_into_body_span_with_visible_macro;
21-
2217
pub(super) fn extract_refined_covspans(
2318
mir_body: &mir::Body<'_>,
2419
hir_info: &ExtractedHirInfo,

compiler/rustc_mir_transform/src/coverage/spans/from_mir.rs

+2-54
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ use rustc_middle::mir::{
44
self, AggregateKind, FakeReadCause, Rvalue, Statement, StatementKind, Terminator,
55
TerminatorKind,
66
};
7-
use rustc_span::{ExpnKind, MacroKind, Span, Symbol};
7+
use rustc_span::{Span, Symbol};
88

99
use crate::coverage::graph::{
1010
BasicCoverageBlock, BasicCoverageBlockData, CoverageGraph, START_BCB,
1111
};
1212
use crate::coverage::spans::Covspan;
13+
use crate::coverage::unexpand::unexpand_into_body_span_with_visible_macro;
1314
use crate::coverage::ExtractedHirInfo;
1415

1516
pub(crate) struct ExtractedCovspans {
@@ -215,59 +216,6 @@ fn filtered_terminator_span(terminator: &Terminator<'_>) -> Option<Span> {
215216
}
216217
}
217218

218-
/// Returns an extrapolated span (pre-expansion[^1]) corresponding to a range
219-
/// within the function's body source. This span is guaranteed to be contained
220-
/// within, or equal to, the `body_span`. If the extrapolated span is not
221-
/// contained within the `body_span`, `None` is returned.
222-
///
223-
/// [^1]Expansions result from Rust syntax including macros, syntactic sugar,
224-
/// etc.).
225-
pub(crate) fn unexpand_into_body_span_with_visible_macro(
226-
original_span: Span,
227-
body_span: Span,
228-
) -> Option<(Span, Option<Symbol>)> {
229-
let (span, prev) = unexpand_into_body_span_with_prev(original_span, body_span)?;
230-
231-
let visible_macro = prev
232-
.map(|prev| match prev.ctxt().outer_expn_data().kind {
233-
ExpnKind::Macro(MacroKind::Bang, name) => Some(name),
234-
_ => None,
235-
})
236-
.flatten();
237-
238-
Some((span, visible_macro))
239-
}
240-
241-
/// Walks through the expansion ancestors of `original_span` to find a span that
242-
/// is contained in `body_span` and has the same [`SyntaxContext`] as `body_span`.
243-
/// The ancestor that was traversed just before the matching span (if any) is
244-
/// also returned.
245-
///
246-
/// For example, a return value of `Some((ancestor, Some(prev))` means that:
247-
/// - `ancestor == original_span.find_ancestor_inside_same_ctxt(body_span)`
248-
/// - `ancestor == prev.parent_callsite()`
249-
///
250-
/// [`SyntaxContext`]: rustc_span::SyntaxContext
251-
fn unexpand_into_body_span_with_prev(
252-
original_span: Span,
253-
body_span: Span,
254-
) -> Option<(Span, Option<Span>)> {
255-
let mut prev = None;
256-
let mut curr = original_span;
257-
258-
while !body_span.contains(curr) || !curr.eq_ctxt(body_span) {
259-
prev = Some(curr);
260-
curr = curr.parent_callsite()?;
261-
}
262-
263-
debug_assert_eq!(Some(curr), original_span.find_ancestor_in_same_ctxt(body_span));
264-
if let Some(prev) = prev {
265-
debug_assert_eq!(Some(curr), prev.parent_callsite());
266-
}
267-
268-
Some((curr, prev))
269-
}
270-
271219
#[derive(Debug)]
272220
pub(crate) struct Hole {
273221
pub(crate) span: Span,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use rustc_span::{ExpnKind, MacroKind, Span, Symbol};
2+
3+
/// Returns an extrapolated span (pre-expansion[^1]) corresponding to a range
4+
/// within the function's body source. This span is guaranteed to be contained
5+
/// within, or equal to, the `body_span`. If the extrapolated span is not
6+
/// contained within the `body_span`, `None` is returned.
7+
///
8+
/// [^1]Expansions result from Rust syntax including macros, syntactic sugar,
9+
/// etc.).
10+
pub(crate) fn unexpand_into_body_span_with_visible_macro(
11+
original_span: Span,
12+
body_span: Span,
13+
) -> Option<(Span, Option<Symbol>)> {
14+
let (span, prev) = unexpand_into_body_span_with_prev(original_span, body_span)?;
15+
16+
let visible_macro = prev
17+
.map(|prev| match prev.ctxt().outer_expn_data().kind {
18+
ExpnKind::Macro(MacroKind::Bang, name) => Some(name),
19+
_ => None,
20+
})
21+
.flatten();
22+
23+
Some((span, visible_macro))
24+
}
25+
26+
/// Walks through the expansion ancestors of `original_span` to find a span that
27+
/// is contained in `body_span` and has the same [`SyntaxContext`] as `body_span`.
28+
/// The ancestor that was traversed just before the matching span (if any) is
29+
/// also returned.
30+
///
31+
/// For example, a return value of `Some((ancestor, Some(prev))` means that:
32+
/// - `ancestor == original_span.find_ancestor_inside_same_ctxt(body_span)`
33+
/// - `ancestor == prev.parent_callsite()`
34+
///
35+
/// [`SyntaxContext`]: rustc_span::SyntaxContext
36+
fn unexpand_into_body_span_with_prev(
37+
original_span: Span,
38+
body_span: Span,
39+
) -> Option<(Span, Option<Span>)> {
40+
let mut prev = None;
41+
let mut curr = original_span;
42+
43+
while !body_span.contains(curr) || !curr.eq_ctxt(body_span) {
44+
prev = Some(curr);
45+
curr = curr.parent_callsite()?;
46+
}
47+
48+
debug_assert_eq!(Some(curr), original_span.find_ancestor_in_same_ctxt(body_span));
49+
if let Some(prev) = prev {
50+
debug_assert_eq!(Some(curr), prev.parent_callsite());
51+
}
52+
53+
Some((curr, prev))
54+
}

0 commit comments

Comments
 (0)