about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJason Newcomb <jsnewcomb@pm.me>2025-04-29 15:39:33 -0400
committerJason Newcomb <jsnewcomb@pm.me>2025-04-30 11:38:09 -0400
commitd9c060b88be29569e615496e20bdcf8ecdf780f8 (patch)
tree5d5a6f6b134508a328cc106529e92490e16fd97d
parent25cdf1f67463c9365d8d83778c933ec7480e940b (diff)
downloadrust-d9c060b88be29569e615496e20bdcf8ecdf780f8.tar.gz
rust-d9c060b88be29569e615496e20bdcf8ecdf780f8.zip
Optimize the codegen for `Span::from_expansion`
-rw-r--r--compiler/rustc_span/src/span_encoding.rs17
1 files changed, 15 insertions, 2 deletions
diff --git a/compiler/rustc_span/src/span_encoding.rs b/compiler/rustc_span/src/span_encoding.rs
index 9d6c7d2a42a..a4a47dc99b0 100644
--- a/compiler/rustc_span/src/span_encoding.rs
+++ b/compiler/rustc_span/src/span_encoding.rs
@@ -306,8 +306,21 @@ impl Span {
     /// Returns `true` if this span comes from any kind of macro, desugaring or inlining.
     #[inline]
     pub fn from_expansion(self) -> bool {
-        // If the span is fully inferred then ctxt > MAX_CTXT
-        self.inline_ctxt().map_or(true, |ctxt| !ctxt.is_root())
+        let ctxt = match_span_kind! {
+            self,
+            // All branches here, except `InlineParent`, actually return `span.ctxt_or_parent_or_marker`.
+            // Since `Interned` is selected if the field contains `CTXT_INTERNED_MARKER` returning that value
+            // as the context allows the compiler to optimize out the branch that selects between either
+            // `Interned` and `PartiallyInterned`.
+            //
+            // Interned contexts can never be the root context and `CTXT_INTERNED_MARKER` has a different value
+            // than the root context so this works for checking is this is an expansion.
+            InlineCtxt(span) => SyntaxContext::from_u16(span.ctxt),
+            InlineParent(_span) => SyntaxContext::root(),
+            PartiallyInterned(span) => SyntaxContext::from_u16(span.ctxt),
+            Interned(_span) => SyntaxContext::from_u16(CTXT_INTERNED_MARKER),
+        };
+        !ctxt.is_root()
     }
 
     /// Returns `true` if this is a dummy span with any hygienic context.