Skip to content

Commit

Permalink
coverage: Avoid getting extra unexpansion info when we don't need it
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Zalathar committed Jun 30, 2024
1 parent 617de8c commit 6732cf1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
9 changes: 4 additions & 5 deletions compiler/rustc_mir_transform/src/coverage/mappings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_span::Span;

use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph, START_BCB};
use crate::coverage::spans::extract_refined_covspans;
use crate::coverage::unexpand::unexpand_into_body_span_with_visible_macro;
use crate::coverage::unexpand::unexpand_into_body_span;
use crate::coverage::ExtractedHirInfo;

/// Associates an ordinary executable code span with its corresponding BCB.
Expand Down Expand Up @@ -201,8 +201,7 @@ pub(super) fn extract_branch_pairs(
if !raw_span.ctxt().outer_expn_data().is_root() {
return None;
}
let (span, _) =
unexpand_into_body_span_with_visible_macro(raw_span, hir_info.body_span)?;
let span = unexpand_into_body_span(raw_span, hir_info.body_span)?;

let bcb_from_marker =
|marker: BlockMarkerId| basic_coverage_blocks.bcb_from_bb(block_markers[marker]?);
Expand Down Expand Up @@ -237,7 +236,7 @@ pub(super) fn extract_mcdc_mappings(
if !raw_span.ctxt().outer_expn_data().is_root() {
return None;
}
let (span, _) = unexpand_into_body_span_with_visible_macro(raw_span, body_span)?;
let span = unexpand_into_body_span(raw_span, body_span)?;

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

mcdc_decisions.extend(branch_info.mcdc_decision_spans.iter().filter_map(
|decision: &mir::coverage::MCDCDecisionSpan| {
let (span, _) = unexpand_into_body_span_with_visible_macro(decision.span, body_span)?;
let span = unexpand_into_body_span(decision.span, body_span)?;

let end_bcbs = decision
.end_markers
Expand Down
28 changes: 17 additions & 11 deletions compiler/rustc_mir_transform/src/coverage/unexpand.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use rustc_span::{ExpnKind, MacroKind, Span, Symbol};

/// Returns an extrapolated span (pre-expansion[^1]) corresponding to a range
/// within the function's body source. This span is guaranteed to be contained
/// within, or equal to, the `body_span`. If the extrapolated span is not
/// contained within the `body_span`, `None` is returned.
/// Walks through the expansion ancestors of `original_span` to find a span that
/// is contained in `body_span` and has the same [syntax context] as `body_span`.
pub(crate) fn unexpand_into_body_span(original_span: Span, body_span: Span) -> Option<Span> {
// Because we don't need to return any extra ancestor information,

Check failure on line 6 in compiler/rustc_mir_transform/src/coverage/unexpand.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

2-line comment block with odd number of backticks
// we can just delegate directly to `find_ancestor_inside_same_ctxt``
original_span.find_ancestor_inside_same_ctxt(body_span)
}

/// Walks through the expansion ancestors of `original_span` to find a span that
/// is contained in `body_span` and has the same [syntax context] as `body_span`.
///
/// [^1]Expansions result from Rust syntax including macros, syntactic sugar,
/// etc.).
/// If the returned span represents a bang-macro invocation (e.g. `foo!(..)`),
/// the returned symbol will be the name of that macro (e.g. `foo`).
pub(crate) fn unexpand_into_body_span_with_visible_macro(
original_span: Span,
body_span: Span,
Expand All @@ -24,15 +30,15 @@ pub(crate) fn unexpand_into_body_span_with_visible_macro(
}

/// Walks through the expansion ancestors of `original_span` to find a span that
/// is contained in `body_span` and has the same [`SyntaxContext`] as `body_span`.
/// is contained in `body_span` and has the same [syntax context] as `body_span`.
/// The ancestor that was traversed just before the matching span (if any) is
/// also returned.
///
/// For example, a return value of `Some((ancestor, Some(prev))` means that:
/// For example, a return value of `Some((ancestor, Some(prev)))` means that:
/// - `ancestor == original_span.find_ancestor_inside_same_ctxt(body_span)`
/// - `ancestor == prev.parent_callsite()`
/// - `prev.parent_callsite() == ancestor`
///
/// [`SyntaxContext`]: rustc_span::SyntaxContext
/// [syntax context]: rustc_span::SyntaxContext
fn unexpand_into_body_span_with_prev(
original_span: Span,
body_span: Span,
Expand All @@ -45,7 +51,7 @@ fn unexpand_into_body_span_with_prev(
curr = curr.parent_callsite()?;
}

debug_assert_eq!(Some(curr), original_span.find_ancestor_in_same_ctxt(body_span));
debug_assert_eq!(Some(curr), original_span.find_ancestor_inside_same_ctxt(body_span));
if let Some(prev) = prev {
debug_assert_eq!(Some(curr), prev.parent_callsite());
}
Expand Down

0 comments on commit 6732cf1

Please sign in to comment.