Skip to content

Commit 5580132

Browse files
authored
Unrolled build for rust-lang#127157
Rollup merge of rust-lang#127157 - Zalathar:unexpand, r=cjgillot coverage: Avoid getting extra unexpansion info when we don't need it Several callers of `unexpand_into_body_span_with_visible_macro` would immediately discard the additional macro-related information, which is wasteful. We can avoid this by having them instead call a simpler method that just returns the span they care about. This PR also moves the relevant functions out of `coverage::spans::from_mir` and into a new submodule `coverage::unexpand`, so that calling them from `coverage::mappings` is less awkward. There should be no actual changes to coverage-instrumentation output, as demonstrated by the absence of test updates.
2 parents ef3d6fd + 6c33149 commit 5580132

File tree

5 files changed

+68
-66
lines changed

5 files changed

+68
-66
lines changed

compiler/rustc_mir_transform/src/coverage/mappings.rs

+5-7
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;
1514
use crate::coverage::ExtractedHirInfo;
1615

1716
/// Associates an ordinary executable code span with its corresponding BCB.
@@ -202,8 +201,7 @@ pub(super) fn extract_branch_pairs(
202201
if !raw_span.ctxt().outer_expn_data().is_root() {
203202
return None;
204203
}
205-
let (span, _) =
206-
unexpand_into_body_span_with_visible_macro(raw_span, hir_info.body_span)?;
204+
let span = unexpand_into_body_span(raw_span, hir_info.body_span)?;
207205

208206
let bcb_from_marker =
209207
|marker: BlockMarkerId| basic_coverage_blocks.bcb_from_bb(block_markers[marker]?);
@@ -238,7 +236,7 @@ pub(super) fn extract_mcdc_mappings(
238236
if !raw_span.ctxt().outer_expn_data().is_root() {
239237
return None;
240238
}
241-
let (span, _) = unexpand_into_body_span_with_visible_macro(raw_span, body_span)?;
239+
let span = unexpand_into_body_span(raw_span, body_span)?;
242240

243241
let true_bcb = bcb_from_marker(true_marker)?;
244242
let false_bcb = bcb_from_marker(false_marker)?;
@@ -261,7 +259,7 @@ pub(super) fn extract_mcdc_mappings(
261259

262260
mcdc_decisions.extend(branch_info.mcdc_decision_spans.iter().filter_map(
263261
|decision: &mir::coverage::MCDCDecisionSpan| {
264-
let (span, _) = unexpand_into_body_span_with_visible_macro(decision.span, body_span)?;
262+
let span = unexpand_into_body_span(decision.span, body_span)?;
265263

266264
let end_bcbs = decision
267265
.end_markers

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,60 @@
1+
use rustc_span::{ExpnKind, MacroKind, Span, Symbol};
2+
3+
/// Walks through the expansion ancestors of `original_span` to find a span that
4+
/// is contained in `body_span` and has the same [syntax context] as `body_span`.
5+
pub(crate) fn unexpand_into_body_span(original_span: Span, body_span: Span) -> Option<Span> {
6+
// Because we don't need to return any extra ancestor information,
7+
// we can just delegate directly to `find_ancestor_inside_same_ctxt`.
8+
original_span.find_ancestor_inside_same_ctxt(body_span)
9+
}
10+
11+
/// Walks through the expansion ancestors of `original_span` to find a span that
12+
/// is contained in `body_span` and has the same [syntax context] as `body_span`.
13+
///
14+
/// If the returned span represents a bang-macro invocation (e.g. `foo!(..)`),
15+
/// the returned symbol will be the name of that macro (e.g. `foo`).
16+
pub(crate) fn unexpand_into_body_span_with_visible_macro(
17+
original_span: Span,
18+
body_span: Span,
19+
) -> Option<(Span, Option<Symbol>)> {
20+
let (span, prev) = unexpand_into_body_span_with_prev(original_span, body_span)?;
21+
22+
let visible_macro = prev
23+
.map(|prev| match prev.ctxt().outer_expn_data().kind {
24+
ExpnKind::Macro(MacroKind::Bang, name) => Some(name),
25+
_ => None,
26+
})
27+
.flatten();
28+
29+
Some((span, visible_macro))
30+
}
31+
32+
/// Walks through the expansion ancestors of `original_span` to find a span that
33+
/// is contained in `body_span` and has the same [syntax context] as `body_span`.
34+
/// The ancestor that was traversed just before the matching span (if any) is
35+
/// also returned.
36+
///
37+
/// For example, a return value of `Some((ancestor, Some(prev)))` means that:
38+
/// - `ancestor == original_span.find_ancestor_inside_same_ctxt(body_span)`
39+
/// - `prev.parent_callsite() == ancestor`
40+
///
41+
/// [syntax context]: rustc_span::SyntaxContext
42+
fn unexpand_into_body_span_with_prev(
43+
original_span: Span,
44+
body_span: Span,
45+
) -> Option<(Span, Option<Span>)> {
46+
let mut prev = None;
47+
let mut curr = original_span;
48+
49+
while !body_span.contains(curr) || !curr.eq_ctxt(body_span) {
50+
prev = Some(curr);
51+
curr = curr.parent_callsite()?;
52+
}
53+
54+
debug_assert_eq!(Some(curr), original_span.find_ancestor_inside_same_ctxt(body_span));
55+
if let Some(prev) = prev {
56+
debug_assert_eq!(Some(curr), prev.parent_callsite());
57+
}
58+
59+
Some((curr, prev))
60+
}

0 commit comments

Comments
 (0)