From e1c08d6d81606a91f91c5f4b767013fcf8ecab51 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 7 Jan 2024 17:26:29 +0300 Subject: [PATCH] macro_rules: Add an expansion-local cache to span marker --- compiler/rustc_expand/src/mbe/transcribe.rs | 13 +++++++++---- compiler/rustc_span/src/hygiene.rs | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_expand/src/mbe/transcribe.rs b/compiler/rustc_expand/src/mbe/transcribe.rs index c969ca7ef89b3..a7f2f56e04991 100644 --- a/compiler/rustc_expand/src/mbe/transcribe.rs +++ b/compiler/rustc_expand/src/mbe/transcribe.rs @@ -13,19 +13,24 @@ use rustc_errors::DiagnosticBuilder; use rustc_errors::{pluralize, PResult}; use rustc_span::hygiene::{LocalExpnId, Transparency}; use rustc_span::symbol::{sym, Ident, MacroRulesNormalizedIdent}; -use rustc_span::Span; +use rustc_span::{Span, SyntaxContext}; use smallvec::{smallvec, SmallVec}; use std::mem; // A Marker adds the given mark to the syntax context. -struct Marker(LocalExpnId, Transparency); +struct Marker(LocalExpnId, Transparency, FxHashMap); impl MutVisitor for Marker { const VISIT_TOKENS: bool = true; fn visit_span(&mut self, span: &mut Span) { - *span = span.apply_mark(self.0.to_expn_id(), self.1) + let Marker(expn_id, transparency, ref mut cache) = *self; + let data = span.data(); + let marked_ctxt = *cache + .entry(data.ctxt) + .or_insert_with(|| data.ctxt.apply_mark(expn_id.to_expn_id(), transparency)); + *span = data.with_ctxt(marked_ctxt); } } @@ -123,7 +128,7 @@ pub(super) fn transcribe<'a>( // again, and we are done transcribing. let mut result: Vec = Vec::new(); let mut result_stack = Vec::new(); - let mut marker = Marker(cx.current_expansion.id, transparency); + let mut marker = Marker(cx.current_expansion.id, transparency, Default::default()); loop { // Look at the last frame on the stack. diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index 08fb1d1345df4..6a15961ee20fb 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -658,7 +658,7 @@ impl SyntaxContext { } /// Extend a syntax context with a given expansion and transparency. - pub(crate) fn apply_mark(self, expn_id: ExpnId, transparency: Transparency) -> SyntaxContext { + pub fn apply_mark(self, expn_id: ExpnId, transparency: Transparency) -> SyntaxContext { HygieneData::with(|data| data.apply_mark(self, expn_id, transparency)) }