From c834e9fcbdadc829333f17037acaf3d75c725374 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 6 May 2026 13:38:10 +0300 Subject: [PATCH 1/2] resolve: Remove `MacroData` All the necessary data can be taken from `SyntaxExtension` instead. --- compiler/rustc_expand/src/mbe/macro_rules.rs | 20 ++++++++----- .../rustc_resolve/src/build_reduced_graph.rs | 28 ++++++++--------- compiler/rustc_resolve/src/def_collector.rs | 13 ++++---- compiler/rustc_resolve/src/diagnostics.rs | 8 ++--- compiler/rustc_resolve/src/ident.rs | 2 +- compiler/rustc_resolve/src/imports.rs | 5 +++- compiler/rustc_resolve/src/lib.rs | 30 +++++++------------ compiler/rustc_resolve/src/macros.rs | 13 ++++---- 8 files changed, 60 insertions(+), 59 deletions(-) diff --git a/compiler/rustc_expand/src/mbe/macro_rules.rs b/compiler/rustc_expand/src/mbe/macro_rules.rs index cb22ad136645b..5bf50804ab1e8 100644 --- a/compiler/rustc_expand/src/mbe/macro_rules.rs +++ b/compiler/rustc_expand/src/mbe/macro_rules.rs @@ -170,6 +170,7 @@ pub struct MacroRulesMacroExpander { transparency: Transparency, kinds: MacroKinds, rules: Vec, + macro_rules: bool, } impl MacroRulesMacroExpander { @@ -189,6 +190,14 @@ impl MacroRulesMacroExpander { self.kinds } + pub fn nrules(&self) -> usize { + self.rules.len() + } + + pub fn is_macro_rules(&self) -> bool { + self.macro_rules + } + pub fn expand_derive( &self, cx: &mut ExtCtxt<'_>, @@ -714,13 +723,12 @@ pub fn compile_declarative_macro( span: Span, node_id: NodeId, edition: Edition, -) -> (SyntaxExtension, usize) { +) -> SyntaxExtension { let mk_syn_ext = |kind| { let is_local = is_defined_in_current_crate(node_id); SyntaxExtension::new(sess, kind, span, Vec::new(), edition, ident.name, attrs, is_local) }; - let dummy_syn_ext = - |guar| (mk_syn_ext(SyntaxExtensionKind::Bang(Arc::new(DummyBang(guar)))), 0); + let dummy_syn_ext = |guar| mk_syn_ext(SyntaxExtensionKind::Bang(Arc::new(DummyBang(guar)))); let macro_rules = macro_def.macro_rules; let exp_sep = if macro_rules { exp!(Semi) } else { exp!(Comma) }; @@ -857,9 +865,6 @@ pub fn compile_declarative_macro( return dummy_syn_ext(guar); } - // Return the number of rules for unused rule linting, if this is a local macro. - let nrules = if is_defined_in_current_crate(node_id) { rules.len() } else { 0 }; - let on_unmatch_args = find_attr!( attrs, OnUnmatchArgs { directive, .. } => directive.clone() @@ -875,8 +880,9 @@ pub fn compile_declarative_macro( on_unmatch_args, transparency, rules, + macro_rules, }; - (mk_syn_ext(SyntaxExtensionKind::MacroRules(Arc::new(exp))), nrules) + mk_syn_ext(SyntaxExtensionKind::MacroRules(Arc::new(exp))) } fn check_no_eof(sess: &Session, p: &Parser<'_>, msg: &'static str) -> Option { diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index c37838706301a..f96fcb454a174 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -14,7 +14,7 @@ use rustc_ast::{ TyAlias, }; use rustc_attr_parsing::AttributeParser; -use rustc_expand::base::ResolverExpand; +use rustc_expand::base::{ResolverExpand, SyntaxExtension, SyntaxExtensionKind}; use rustc_hir::Attribute; use rustc_hir::attrs::{AttributeKind, MacroUseArgs}; use rustc_hir::def::{self, *}; @@ -37,9 +37,8 @@ use crate::macros::{MacroRulesDecl, MacroRulesScope, MacroRulesScopeRef}; use crate::ref_mut::CmCell; use crate::{ BindingKey, Decl, DeclData, DeclKind, DelayedVisResolutionError, ExternModule, - ExternPreludeEntry, Finalize, IdentKey, LocalModule, MacroData, Module, ModuleKind, - ModuleOrUniformRoot, ParentScope, PathResult, Res, Resolver, Segment, SyntaxExtension, Used, - VisResolutionError, errors, + ExternPreludeEntry, Finalize, IdentKey, LocalModule, Module, ModuleKind, ModuleOrUniformRoot, + ParentScope, PathResult, Res, Resolver, Segment, Used, VisResolutionError, errors, }; impl<'ra, 'tcx> Resolver<'ra, 'tcx> { @@ -210,26 +209,26 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { /// Gets the `SyntaxExtension` corresponding to `res`. pub(crate) fn get_macro(&self, res: Res) -> Option<&Arc> { match res { - Res::Def(DefKind::Macro(..), def_id) => Some(&self.get_macro_by_def_id(def_id).ext), + Res::Def(DefKind::Macro(..), def_id) => Some(self.get_macro_by_def_id(def_id)), Res::NonMacroAttr(_) => Some(&self.non_macro_attr), _ => None, } } - pub(crate) fn get_macro_by_def_id(&self, def_id: DefId) -> &'ra MacroData { + pub(crate) fn get_macro_by_def_id(&self, def_id: DefId) -> &'ra Arc { // Local macros are always compiled. match def_id.as_local() { Some(local_def_id) => self.local_macro_map[&local_def_id], - None => *self.extern_macro_map.borrow_mut().entry(def_id).or_insert_with(|| { + None => self.extern_macro_map.borrow_mut().entry(def_id).or_insert_with(|| { let loaded_macro = self.cstore().load_macro_untracked(self.tcx, def_id); - let macro_data = match loaded_macro { + let ext = match loaded_macro { LoadedMacro::MacroDef { def, ident, attrs, span, edition } => { self.compile_macro(&def, ident, &attrs, span, ast::DUMMY_NODE_ID, edition) } - LoadedMacro::ProcMacro(ext) => MacroData::new(Arc::new(ext)), + LoadedMacro::ProcMacro(ext) => ext, }; - self.arenas.alloc_macro(macro_data) + self.arenas.alloc_macro(Arc::new(ext)) }), } } @@ -1277,8 +1276,10 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> { fn insert_unused_macro(&mut self, ident: Ident, def_id: LocalDefId, node_id: NodeId) { if !ident.as_str().starts_with('_') { self.r.unused_macros.insert(def_id, (node_id, ident)); - let nrules = self.r.local_macro_map[&def_id].nrules; - self.r.unused_macro_rules.insert(node_id, (def_id, DenseBitSet::new_filled(nrules))); + if let SyntaxExtensionKind::MacroRules(mr) = &self.r.local_macro_map[&def_id].kind { + let value = (def_id, DenseBitSet::new_filled(mr.nrules())); + self.r.unused_macro_rules.insert(node_id, value); + } } } @@ -1299,8 +1300,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> { Some((macro_kind, ident, span)) => { let macro_kinds = macro_kind.into(); let res = Res::Def(DefKind::Macro(macro_kinds), def_id.to_def_id()); - let macro_data = MacroData::new(self.r.dummy_ext(macro_kind)); - self.r.new_local_macro(def_id, macro_data); + self.r.new_local_macro(def_id, self.r.dummy_ext(macro_kind)); self.r.proc_macro_stubs.insert(def_id); (res, ident, span, false) } diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index 0040ddbf5e24a..b18aa1a233e8e 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -1,4 +1,5 @@ use std::mem; +use std::sync::Arc; use rustc_ast::visit::FnKind; use rustc_ast::*; @@ -117,7 +118,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> { fn visit_item(&mut self, i: &'a Item) { // Pick the def data. This need not be unique, but the more // information we encapsulate into, the better - let mut opt_macro_data = None; + let mut opt_syn_ext = None; let def_kind = match &i.kind { ItemKind::Impl(i) => DefKind::Impl { of_trait: i.of_trait.is_some() }, ItemKind::ForeignMod(..) => DefKind::ForeignMod, @@ -165,9 +166,9 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> { }, ); - let macro_data = self.r.compile_macro(def, *ident, &attrs, i.span, i.id, edition); - let macro_kinds = macro_data.ext.macro_kinds(); - opt_macro_data = Some(macro_data); + let ext = self.r.compile_macro(def, *ident, &attrs, i.span, i.id, edition); + let macro_kinds = ext.macro_kinds(); + opt_syn_ext = Some(ext); DefKind::Macro(macro_kinds) } ItemKind::GlobalAsm(..) => DefKind::GlobalAsm, @@ -185,8 +186,8 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> { }; let feed = self.create_def(i.id, i.kind.ident().map(|ident| ident.name), def_kind, i.span); - if let Some(macro_data) = opt_macro_data { - self.r.new_local_macro(feed.def_id(), macro_data); + if let Some(ext) = opt_syn_ext { + self.r.new_local_macro(feed.def_id(), Arc::new(ext)); } self.with_parent(feed.def_id(), |this| { diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index c082455380ce7..9cdf79ca57d87 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1742,7 +1742,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { if let Some((def_id, unused_ident)) = unused_macro { let scope = self.local_macro_def_scopes[&def_id]; let parent_nearest = parent_scope.module.nearest_parent_mod(); - let unused_macro_kinds = self.local_macro_map[def_id].ext.macro_kinds(); + let unused_macro_kinds = self.local_macro_map[def_id].macro_kinds(); if !unused_macro_kinds.contains(macro_kind.into()) { match macro_kind { MacroKind::Bang => { @@ -1860,13 +1860,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { let mut all_attrs: UnordMap> = UnordMap::default(); // We're collecting these in a hashmap, and handle ordering the output further down. #[allow(rustc::potential_query_instability)] - for (def_id, data) in self + for (def_id, ext) in self .local_macro_map .iter() - .map(|(local_id, data)| (local_id.to_def_id(), data)) + .map(|(local_id, ext)| (local_id.to_def_id(), ext)) .chain(self.extern_macro_map.borrow().iter().map(|(id, d)| (*id, d))) { - for helper_attr in &data.ext.helper_attrs { + for helper_attr in &ext.helper_attrs { let item_name = self.tcx.item_name(def_id); all_attrs.entry(*helper_attr).or_default().push(item_name); if helper_attr == &ident.name { diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs index 1c43ca431d8bf..b33b77d43999d 100644 --- a/compiler/rustc_resolve/src/ident.rs +++ b/compiler/rustc_resolve/src/ident.rs @@ -271,7 +271,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { // The macro is a proc macro derive && let Some(def_id) = module.expansion.expn_data().macro_def_id { - let ext = &self.get_macro_by_def_id(def_id).ext; + let ext = self.get_macro_by_def_id(def_id); if ext.builtin_name.is_none() && ext.macro_kinds() == MacroKinds::DERIVE && parent.expansion.outer_expn_is_descendant_of(**ctxt) diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 1b9e5d2a2daa6..b16347af7ba0c 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -12,6 +12,7 @@ use rustc_errors::codes::*; use rustc_errors::{ Applicability, BufferedEarlyLint, Diagnostic, MultiSpan, pluralize, struct_span_code_err, }; +use rustc_expand::base::SyntaxExtensionKind; use rustc_hir::Attribute; use rustc_hir::attrs::AttributeKind; use rustc_hir::attrs::diagnostic::{CustomDiagnostic, Directive, FormatArgs}; @@ -1656,7 +1657,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { match decl.kind { // exclude decl_macro DeclKind::Def(Res::Def(DefKind::Macro(_), def_id)) - if self.get_macro_by_def_id(def_id).macro_rules => + if let SyntaxExtensionKind::MacroRules(mr) = + &self.get_macro_by_def_id(def_id).kind + && mr.is_macro_rules() => { err.subdiagnostic(ConsiderAddingMacroExport { span: decl.span }); err.subdiagnostic(ConsiderMarkingAsPubCrate { vis_span: import.vis_span }); diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 4352e10462901..be08dd445c680 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1265,18 +1265,6 @@ struct DeriveData { has_derive_copy: bool, } -struct MacroData { - ext: Arc, - nrules: usize, - macro_rules: bool, -} - -impl MacroData { - fn new(ext: Arc) -> MacroData { - MacroData { ext, nrules: 0, macro_rules: false } - } -} - pub struct ResolverOutputs<'tcx> { pub global_ctxt: ResolverGlobalCtxt, pub ast_lowering: ResolverAstLowering<'tcx>, @@ -1396,9 +1384,9 @@ pub struct Resolver<'ra, 'tcx> { registered_tools: &'tcx RegisteredTools, macro_use_prelude: FxIndexMap>, /// Eagerly populated map of all local macro definitions. - local_macro_map: FxHashMap = default::fx_hash_map(), + local_macro_map: FxHashMap> = default::fx_hash_map(), /// Lazily populated cache of macro definitions loaded from external crates. - extern_macro_map: CacheRefCell>, + extern_macro_map: CacheRefCell>>, dummy_ext_bang: Arc, dummy_ext_derive: Arc, non_macro_attr: Arc, @@ -1520,7 +1508,7 @@ pub struct ResolverArenas<'ra> { imports: TypedArena>, name_resolutions: TypedArena>>, ast_paths: TypedArena, - macros: TypedArena, + macros: TypedArena>, dropless: DroplessArena, } @@ -1599,8 +1587,8 @@ impl<'ra> ResolverArenas<'ra> { fn alloc_ast_paths(&'ra self, paths: &[ast::Path]) -> &'ra [ast::Path] { self.ast_paths.alloc_from_iter(paths.iter().cloned()) } - fn alloc_macro(&'ra self, macro_data: MacroData) -> &'ra MacroData { - self.macros.alloc(macro_data) + fn alloc_macro(&'ra self, ext: Arc) -> &'ra Arc { + self.macros.alloc(ext) } fn alloc_pattern_spans(&'ra self, spans: impl Iterator) -> &'ra [Span] { self.dropless.alloc_from_iter(spans) @@ -1888,8 +1876,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { module } - fn new_local_macro(&mut self, def_id: LocalDefId, macro_data: MacroData) -> &'ra MacroData { - let mac = self.arenas.alloc_macro(macro_data); + fn new_local_macro( + &mut self, + def_id: LocalDefId, + ext: Arc, + ) -> &'ra Arc { + let mac = self.arenas.alloc_macro(ext); self.local_macro_map.insert(def_id, mac); mac } diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index d20907f53f66a..05874d76183e4 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -43,7 +43,7 @@ use crate::hygiene::Macros20NormalizedSyntaxContext; use crate::imports::Import; use crate::{ BindingKey, CacheCell, CmResolver, Decl, DeclKind, DeriveData, Determinacy, Finalize, IdentKey, - InvocationParent, MacroData, ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, Res, + InvocationParent, ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, Res, ResolutionError, Resolver, ScopeSet, Segment, Used, }; @@ -360,8 +360,8 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> { if unused_arms.is_empty() { continue; } - let m = &self.local_macro_map[&def_id]; - let SyntaxExtensionKind::MacroRules(ref m) = m.ext.kind else { + let ext = self.local_macro_map[&def_id]; + let SyntaxExtensionKind::MacroRules(ref m) = ext.kind else { continue; }; for arm_i in unused_arms.iter() { @@ -1229,8 +1229,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { span: Span, node_id: NodeId, edition: Edition, - ) -> MacroData { - let (mut ext, mut nrules) = compile_declarative_macro( + ) -> SyntaxExtension { + let mut ext = compile_declarative_macro( self.tcx.sess, self.tcx.features(), macro_def, @@ -1247,13 +1247,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { // The macro is a built-in, replace its expander function // while still taking everything else from the source code. ext.kind = builtin_ext_kind.clone(); - nrules = 0; } else { self.dcx().emit_err(errors::CannotFindBuiltinMacroWithName { span, ident }); } } - MacroData { ext: Arc::new(ext), nrules, macro_rules: macro_def.macro_rules } + ext } fn path_accessible( From 503960c9a57b95352a00e257bc2c8cebdecdb927 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Thu, 7 May 2026 15:29:52 +0300 Subject: [PATCH 2/2] resolve: Avoid some `Arc` cloning with syntax extensions Clone only on interface boundaries with `rustc_expand` --- .../rustc_resolve/src/build_reduced_graph.rs | 8 ++--- compiler/rustc_resolve/src/def_collector.rs | 3 +- compiler/rustc_resolve/src/lib.rs | 34 +++++++------------ compiler/rustc_resolve/src/macros.rs | 16 ++++----- 4 files changed, 25 insertions(+), 36 deletions(-) diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index f96fcb454a174..61da931df4d5d 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -207,10 +207,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { } /// Gets the `SyntaxExtension` corresponding to `res`. - pub(crate) fn get_macro(&self, res: Res) -> Option<&Arc> { + pub(crate) fn get_macro(&self, res: Res) -> Option<&'ra Arc> { match res { Res::Def(DefKind::Macro(..), def_id) => Some(self.get_macro_by_def_id(def_id)), - Res::NonMacroAttr(_) => Some(&self.non_macro_attr), + Res::NonMacroAttr(_) => Some(self.non_macro_attr), _ => None, } } @@ -228,7 +228,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { LoadedMacro::ProcMacro(ext) => ext, }; - self.arenas.alloc_macro(Arc::new(ext)) + self.arenas.alloc_macro(ext) }), } } @@ -1300,7 +1300,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> { Some((macro_kind, ident, span)) => { let macro_kinds = macro_kind.into(); let res = Res::Def(DefKind::Macro(macro_kinds), def_id.to_def_id()); - self.r.new_local_macro(def_id, self.r.dummy_ext(macro_kind)); + self.r.local_macro_map.insert(def_id, self.r.dummy_ext(macro_kind)); self.r.proc_macro_stubs.insert(def_id); (res, ident, span, false) } diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index b18aa1a233e8e..b5f46052f03e3 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -1,5 +1,4 @@ use std::mem; -use std::sync::Arc; use rustc_ast::visit::FnKind; use rustc_ast::*; @@ -187,7 +186,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> { let feed = self.create_def(i.id, i.kind.ident().map(|ident| ident.name), def_kind, i.span); if let Some(ext) = opt_syn_ext { - self.r.new_local_macro(feed.def_id(), Arc::new(ext)); + self.r.local_macro_map.insert(feed.def_id(), self.r.arenas.alloc_macro(ext)); } self.with_parent(feed.def_id(), |this| { diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index be08dd445c680..e5038f2d7f2b9 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1387,9 +1387,9 @@ pub struct Resolver<'ra, 'tcx> { local_macro_map: FxHashMap> = default::fx_hash_map(), /// Lazily populated cache of macro definitions loaded from external crates. extern_macro_map: CacheRefCell>>, - dummy_ext_bang: Arc, - dummy_ext_derive: Arc, - non_macro_attr: Arc, + dummy_ext_bang: &'ra Arc, + dummy_ext_derive: &'ra Arc, + non_macro_attr: &'ra Arc, local_macro_def_scopes: FxHashMap> = default::fx_hash_map(), ast_transform_scopes: FxHashMap> = default::fx_hash_map(), unused_macros: FxIndexMap, @@ -1587,8 +1587,8 @@ impl<'ra> ResolverArenas<'ra> { fn alloc_ast_paths(&'ra self, paths: &[ast::Path]) -> &'ra [ast::Path] { self.ast_paths.alloc_from_iter(paths.iter().cloned()) } - fn alloc_macro(&'ra self, ext: Arc) -> &'ra Arc { - self.macros.alloc(ext) + fn alloc_macro(&'ra self, ext: SyntaxExtension) -> &'ra Arc { + self.macros.alloc(Arc::new(ext)) } fn alloc_pattern_spans(&'ra self, spans: impl Iterator) -> &'ra [Span] { self.dropless.alloc_from_iter(spans) @@ -1807,9 +1807,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { registered_tools, macro_use_prelude: Default::default(), extern_macro_map: Default::default(), - dummy_ext_bang: Arc::new(SyntaxExtension::dummy_bang(edition)), - dummy_ext_derive: Arc::new(SyntaxExtension::dummy_derive(edition)), - non_macro_attr: Arc::new(SyntaxExtension::non_macro_attr(edition)), + dummy_ext_bang: arenas.alloc_macro(SyntaxExtension::dummy_bang(edition)), + dummy_ext_derive: arenas.alloc_macro(SyntaxExtension::dummy_derive(edition)), + non_macro_attr: arenas.alloc_macro(SyntaxExtension::non_macro_attr(edition)), unused_macros: Default::default(), unused_macro_rules: Default::default(), single_segment_macro_resolutions: Default::default(), @@ -1876,16 +1876,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { module } - fn new_local_macro( - &mut self, - def_id: LocalDefId, - ext: Arc, - ) -> &'ra Arc { - let mac = self.arenas.alloc_macro(ext); - self.local_macro_map.insert(def_id, mac); - mac - } - fn next_node_id(&mut self) -> NodeId { let start = self.next_node_id; let next = start.as_u32().checked_add(1).expect("input too large; ran out of NodeIds"); @@ -1980,11 +1970,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { CStore::from_tcx_mut(self.tcx) } - fn dummy_ext(&self, macro_kind: MacroKind) -> Arc { + fn dummy_ext(&self, macro_kind: MacroKind) -> &'ra Arc { match macro_kind { - MacroKind::Bang => Arc::clone(&self.dummy_ext_bang), - MacroKind::Derive => Arc::clone(&self.dummy_ext_derive), - MacroKind::Attr => Arc::clone(&self.non_macro_attr), + MacroKind::Bang => self.dummy_ext_bang, + MacroKind::Derive => self.dummy_ext_derive, + MacroKind::Attr => self.non_macro_attr, } } diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 05874d76183e4..f4464138ef1fc 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -335,7 +335,7 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> { ) }); - Ok(ext) + Ok(Arc::clone(ext)) } fn record_macro_rule_usage(&mut self, id: NodeId, rule_i: usize) { @@ -404,7 +404,7 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> { let parent_scope = self.invocation_parent_scopes[&expn_id]; for (i, resolution) in entry.resolutions.iter_mut().enumerate() { if resolution.exts.is_none() { - resolution.exts = Some( + resolution.exts = Some(Arc::clone( match self.cm().resolve_derive_macro_path( &resolution.path, &parent_scope, @@ -431,7 +431,7 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> { return Err(Indeterminate); } }, - ); + )); } } // Sort helpers in a stable way independent from the derive resolution order. @@ -573,7 +573,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { deleg_impl: Option<(LocalDefId, Span)>, invoc_in_mod_inert_attr: Option, suggestion_span: Option, - ) -> Result<(Arc, Res), Indeterminate> { + ) -> Result<(&'ra Arc, Res), Indeterminate> { let (ext, res) = match self.cm().resolve_macro_or_delegation_path( path, kind, @@ -765,7 +765,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { parent_scope: &ParentScope<'ra>, force: bool, ignore_import: Option>, - ) -> Result<(Option>, Res), Determinacy> { + ) -> Result<(Option<&'r Arc>, Res), Determinacy> { self.resolve_macro_or_delegation_path( path, MacroKind::Derive, @@ -788,7 +788,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { invoc_in_mod_inert_attr: Option<(LocalDefId, NodeId)>, ignore_import: Option>, suggestion_span: Option, - ) -> Result<(Option>, Res), Determinacy> { + ) -> Result<(Option<&'ra Arc>, Res), Determinacy> { let path_span = ast_path.span; let mut path = Segment::from_path(ast_path); @@ -872,7 +872,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { Some((impl_def_id, star_span)) => match res { Res::Def(DefKind::Trait, def_id) => { let edition = self.tcx.sess.edition(); - Some(Arc::new(SyntaxExtension::glob_delegation( + Some(self.arenas.alloc_macro(SyntaxExtension::glob_delegation( def_id, impl_def_id, star_span, @@ -881,7 +881,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { } _ => None, }, - None => self.get_macro(res).map(Arc::clone), + None => self.get_macro(res), }; Ok((ext, res)) }