Skip to content

Commit 6732cf1

Browse files
committed
coverage: Avoid getting extra unexpansion info when we don't need it
These particular callers don't actually use the returned macro information, so they can use a simpler span-unexpansion function that doesn't return it.
1 parent 617de8c commit 6732cf1

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

compiler/rustc_mir_transform/src/coverage/mappings.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_span::Span;
1010

1111
use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph, START_BCB};
1212
use crate::coverage::spans::extract_refined_covspans;
13-
use crate::coverage::unexpand::unexpand_into_body_span_with_visible_macro;
13+
use crate::coverage::unexpand::unexpand_into_body_span;
1414
use crate::coverage::ExtractedHirInfo;
1515

1616
/// Associates an ordinary executable code span with its corresponding BCB.
@@ -201,8 +201,7 @@ pub(super) fn extract_branch_pairs(
201201
if !raw_span.ctxt().outer_expn_data().is_root() {
202202
return None;
203203
}
204-
let (span, _) =
205-
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)?;
206205

207206
let bcb_from_marker =
208207
|marker: BlockMarkerId| basic_coverage_blocks.bcb_from_bb(block_markers[marker]?);
@@ -237,7 +236,7 @@ pub(super) fn extract_mcdc_mappings(
237236
if !raw_span.ctxt().outer_expn_data().is_root() {
238237
return None;
239238
}
240-
let (span, _) = unexpand_into_body_span_with_visible_macro(raw_span, body_span)?;
239+
let span = unexpand_into_body_span(raw_span, body_span)?;
241240

242241
let true_bcb = bcb_from_marker(true_marker)?;
243242
let false_bcb = bcb_from_marker(false_marker)?;
@@ -260,7 +259,7 @@ pub(super) fn extract_mcdc_mappings(
260259

261260
mcdc_decisions.extend(branch_info.mcdc_decision_spans.iter().filter_map(
262261
|decision: &mir::coverage::MCDCDecisionSpan| {
263-
let (span, _) = unexpand_into_body_span_with_visible_macro(decision.span, body_span)?;
262+
let span = unexpand_into_body_span(decision.span, body_span)?;
264263

265264
let end_bcbs = decision
266265
.end_markers

compiler/rustc_mir_transform/src/coverage/unexpand.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
use rustc_span::{ExpnKind, MacroKind, Span, Symbol};
22

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.
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`.
713
///
8-
/// [^1]Expansions result from Rust syntax including macros, syntactic sugar,
9-
/// etc.).
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`).
1016
pub(crate) fn unexpand_into_body_span_with_visible_macro(
1117
original_span: Span,
1218
body_span: Span,
@@ -24,15 +30,15 @@ pub(crate) fn unexpand_into_body_span_with_visible_macro(
2430
}
2531

2632
/// 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`.
33+
/// is contained in `body_span` and has the same [syntax context] as `body_span`.
2834
/// The ancestor that was traversed just before the matching span (if any) is
2935
/// also returned.
3036
///
31-
/// For example, a return value of `Some((ancestor, Some(prev))` means that:
37+
/// For example, a return value of `Some((ancestor, Some(prev)))` means that:
3238
/// - `ancestor == original_span.find_ancestor_inside_same_ctxt(body_span)`
33-
/// - `ancestor == prev.parent_callsite()`
39+
/// - `prev.parent_callsite() == ancestor`
3440
///
35-
/// [`SyntaxContext`]: rustc_span::SyntaxContext
41+
/// [syntax context]: rustc_span::SyntaxContext
3642
fn unexpand_into_body_span_with_prev(
3743
original_span: Span,
3844
body_span: Span,
@@ -45,7 +51,7 @@ fn unexpand_into_body_span_with_prev(
4551
curr = curr.parent_callsite()?;
4652
}
4753

48-
debug_assert_eq!(Some(curr), original_span.find_ancestor_in_same_ctxt(body_span));
54+
debug_assert_eq!(Some(curr), original_span.find_ancestor_inside_same_ctxt(body_span));
4955
if let Some(prev) = prev {
5056
debug_assert_eq!(Some(curr), prev.parent_callsite());
5157
}

0 commit comments

Comments
 (0)