about summary refs log tree commit diff
path: root/compiler/rustc_mir_transform/src/coverage/unexpand.rs
diff options
context:
space:
mode:
authorZalathar <Zalathar@users.noreply.github.com>2024-06-30 18:20:45 +1000
committerZalathar <Zalathar@users.noreply.github.com>2024-06-30 19:05:14 +1000
commit6c3314905574651fa2e004173187bd5d202f0df1 (patch)
tree84949cf655e98e5b0de14069cd206fb073f90553 /compiler/rustc_mir_transform/src/coverage/unexpand.rs
parent617de8cfb59252a501fc7e0ab7c2510dd8a8a7f4 (diff)
downloadrust-6c3314905574651fa2e004173187bd5d202f0df1.tar.gz
rust-6c3314905574651fa2e004173187bd5d202f0df1.zip
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.
Diffstat (limited to 'compiler/rustc_mir_transform/src/coverage/unexpand.rs')
-rw-r--r--compiler/rustc_mir_transform/src/coverage/unexpand.rs28
1 files changed, 17 insertions, 11 deletions
diff --git a/compiler/rustc_mir_transform/src/coverage/unexpand.rs b/compiler/rustc_mir_transform/src/coverage/unexpand.rs
index 18532b8ee45..8cde291b907 100644
--- a/compiler/rustc_mir_transform/src/coverage/unexpand.rs
+++ b/compiler/rustc_mir_transform/src/coverage/unexpand.rs
@@ -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,
+    // 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,
@@ -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,
@@ -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());
     }