Skip to content

Resolver: refact macro map into external and local maps #143657

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

Merged
merged 2 commits into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
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
36 changes: 19 additions & 17 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,28 +162,30 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
}

pub(crate) fn get_macro(&mut self, res: Res) -> Option<&MacroData> {
pub(crate) fn get_macro(&self, res: Res) -> Option<&'ra MacroData> {
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,
}
}

pub(crate) fn get_macro_by_def_id(&mut self, def_id: DefId) -> &MacroData {
if self.macro_map.contains_key(&def_id) {
return &self.macro_map[&def_id];
}

let loaded_macro = self.cstore().load_macro_untracked(def_id, self.tcx);
let macro_data = 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)),
};
pub(crate) fn get_macro_by_def_id(&self, def_id: DefId) -> &'ra MacroData {
// 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(|| {
let loaded_macro = self.cstore().load_macro_untracked(def_id, self.tcx);
let macro_data = 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)),
};

self.macro_map.entry(def_id).or_insert(macro_data)
self.arenas.alloc_macro(macro_data)
}),
}
}

pub(crate) fn build_reduced_graph(
Expand Down Expand Up @@ -1203,7 +1205,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'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.macro_map[&def_id.to_def_id()].nrules;
let nrules = self.r.local_macro_map[&def_id].nrules;
self.r.unused_macro_rules.insert(node_id, DenseBitSet::new_filled(nrules));
}
}
Expand All @@ -1222,7 +1224,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
Some((macro_kind, ident, span)) => {
let res = Res::Def(DefKind::Macro(macro_kind), def_id.to_def_id());
let macro_data = MacroData::new(self.r.dummy_ext(macro_kind));
self.r.macro_map.insert(def_id.to_def_id(), macro_data);
self.r.new_local_macro(def_id, macro_data);
self.r.proc_macro_stubs.insert(def_id);
(res, ident, span, false)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
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.resolver.macro_map.insert(def_id.to_def_id(), macro_data);
self.resolver.new_local_macro(def_id, macro_data);
}

self.with_parent(def_id, |this| {
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1669,9 +1669,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let mut all_attrs: UnordMap<Symbol, Vec<_>> = 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.macro_map {
for (def_id, data) in self
.local_macro_map
.iter()
.map(|(local_id, data)| (local_id.to_def_id(), data))
.chain(self.extern_macro_map.borrow().iter().map(|(id, d)| (*id, d)))
{
for helper_attr in &data.ext.helper_attrs {
let item_name = self.tcx.item_name(*def_id);
let item_name = self.tcx.item_name(def_id);
all_attrs.entry(*helper_attr).or_default().push(item_name);
if helper_attr == &ident.name {
derives.push(item_name);
Expand Down
26 changes: 20 additions & 6 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,10 +1128,12 @@ pub struct Resolver<'ra, 'tcx> {
builtin_macros: FxHashMap<Symbol, SyntaxExtensionKind>,
registered_tools: &'tcx RegisteredTools,
macro_use_prelude: FxIndexMap<Symbol, NameBinding<'ra>>,
macro_map: FxHashMap<DefId, MacroData>,
local_macro_map: FxHashMap<LocalDefId, &'ra MacroData>,
/// Lazily populated cache of macros loaded from external crates.
extern_macro_map: RefCell<FxHashMap<DefId, &'ra MacroData>>,
dummy_ext_bang: Arc<SyntaxExtension>,
dummy_ext_derive: Arc<SyntaxExtension>,
non_macro_attr: MacroData,
non_macro_attr: &'ra MacroData,
local_macro_def_scopes: FxHashMap<LocalDefId, Module<'ra>>,
ast_transform_scopes: FxHashMap<LocalExpnId, Module<'ra>>,
unused_macros: FxIndexMap<LocalDefId, (NodeId, Ident)>,
Expand Down Expand Up @@ -1241,6 +1243,7 @@ pub struct ResolverArenas<'ra> {
imports: TypedArena<ImportData<'ra>>,
name_resolutions: TypedArena<RefCell<NameResolution<'ra>>>,
ast_paths: TypedArena<ast::Path>,
macros: TypedArena<MacroData>,
dropless: DroplessArena,
}

Expand Down Expand Up @@ -1287,7 +1290,7 @@ impl<'ra> ResolverArenas<'ra> {
self.name_resolutions.alloc(Default::default())
}
fn alloc_macro_rules_scope(&'ra self, scope: MacroRulesScope<'ra>) -> MacroRulesScopeRef<'ra> {
Interned::new_unchecked(self.dropless.alloc(Cell::new(scope)))
self.dropless.alloc(Cell::new(scope))
}
fn alloc_macro_rules_binding(
&'ra self,
Expand All @@ -1298,6 +1301,9 @@ 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_pattern_spans(&'ra self, spans: impl Iterator<Item = Span>) -> &'ra [Span] {
self.dropless.alloc_from_iter(spans)
}
Expand Down Expand Up @@ -1540,10 +1546,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
builtin_macros: Default::default(),
registered_tools,
macro_use_prelude: Default::default(),
macro_map: FxHashMap::default(),
local_macro_map: 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: MacroData::new(Arc::new(SyntaxExtension::non_macro_attr(edition))),
non_macro_attr: arenas
.alloc_macro(MacroData::new(Arc::new(SyntaxExtension::non_macro_attr(edition)))),
invocation_parent_scopes: Default::default(),
output_macro_rules_scopes: Default::default(),
macro_rules_scopes: Default::default(),
Expand Down Expand Up @@ -1616,6 +1624,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
)
}

fn new_local_macro(&mut self, def_id: LocalDefId, macro_data: MacroData) -> &'ra MacroData {
let mac = self.arenas.alloc_macro(macro_data);
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");
Expand Down Expand Up @@ -1734,7 +1748,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
f(self, MacroNS);
}

fn is_builtin_macro(&mut self, res: Res) -> bool {
fn is_builtin_macro(&self, res: Res) -> bool {
self.get_macro(res).is_some_and(|macro_data| macro_data.ext.builtin_name.is_some())
}

Expand Down
11 changes: 5 additions & 6 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use rustc_ast::expand::StrippedCfgItem;
use rustc_ast::{self as ast, Crate, NodeId, attr};
use rustc_ast_pretty::pprust;
use rustc_attr_data_structures::StabilityLevel;
use rustc_data_structures::intern::Interned;
use rustc_errors::{Applicability, DiagCtxtHandle, StashKey};
use rustc_expand::base::{
Annotatable, DeriveResolution, Indeterminate, ResolverExpand, SyntaxExtension,
Expand Down Expand Up @@ -80,7 +79,7 @@ pub(crate) enum MacroRulesScope<'ra> {
/// This helps to avoid uncontrollable growth of `macro_rules!` scope chains,
/// which usually grow linearly with the number of macro invocations
/// in a module (including derives) and hurt performance.
pub(crate) type MacroRulesScopeRef<'ra> = Interned<'ra, Cell<MacroRulesScope<'ra>>>;
pub(crate) type MacroRulesScopeRef<'ra> = &'ra Cell<MacroRulesScope<'ra>>;

/// Macro namespace is separated into two sub-namespaces, one for bang macros and
/// one for attribute-like macros (attributes, derives).
Expand Down Expand Up @@ -354,8 +353,8 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
if unused_arms.is_empty() {
continue;
}
let def_id = self.local_def_id(node_id).to_def_id();
let m = &self.macro_map[&def_id];
let def_id = self.local_def_id(node_id);
let m = &self.local_macro_map[&def_id];
let SyntaxExtensionKind::LegacyBang(ref ext) = m.ext.kind else {
continue;
};
Expand Down Expand Up @@ -1132,7 +1131,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
}

pub(crate) fn check_reserved_macro_name(&mut self, ident: Ident, res: Res) {
pub(crate) fn check_reserved_macro_name(&self, ident: Ident, res: Res) {
// Reserve some names that are not quite covered by the general check
// performed on `Resolver::builtin_attrs`.
if ident.name == sym::cfg || ident.name == sym::cfg_attr {
Expand All @@ -1148,7 +1147,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
///
/// Possibly replace its expander to a pre-defined one for built-in macros.
pub(crate) fn compile_macro(
&mut self,
&self,
macro_def: &ast::MacroDef,
ident: Ident,
attrs: &[rustc_hir::Attribute],
Expand Down
Loading