Skip to content

Optimize the codegen for Span::from_expansion #140485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions compiler/rustc_span/src/span_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading