Skip to content

Commit e9e31b1

Browse files
MacroData in ResolverArenas + split macro_map into extern_macro_map and local_macro_map.
1 parent e3fccdd commit e9e31b1

File tree

5 files changed

+50
-29
lines changed

5 files changed

+50
-29
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,28 +162,30 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
162162
}
163163
}
164164

165-
pub(crate) fn get_macro(&mut self, res: Res) -> Option<&MacroData> {
165+
pub(crate) fn get_macro(&self, res: Res) -> Option<&'ra MacroData> {
166166
match res {
167167
Res::Def(DefKind::Macro(..), def_id) => Some(self.get_macro_by_def_id(def_id)),
168-
Res::NonMacroAttr(_) => Some(&self.non_macro_attr),
168+
Res::NonMacroAttr(_) => Some(self.non_macro_attr),
169169
_ => None,
170170
}
171171
}
172172

173-
pub(crate) fn get_macro_by_def_id(&mut self, def_id: DefId) -> &MacroData {
174-
if self.macro_map.contains_key(&def_id) {
175-
return &self.macro_map[&def_id];
176-
}
177-
178-
let loaded_macro = self.cstore().load_macro_untracked(def_id, self.tcx);
179-
let macro_data = match loaded_macro {
180-
LoadedMacro::MacroDef { def, ident, attrs, span, edition } => {
181-
self.compile_macro(&def, ident, &attrs, span, ast::DUMMY_NODE_ID, edition)
182-
}
183-
LoadedMacro::ProcMacro(ext) => MacroData::new(Arc::new(ext)),
184-
};
173+
pub(crate) fn get_macro_by_def_id(&self, def_id: DefId) -> &'ra MacroData {
174+
// Local macros are always compiled.
175+
match def_id.as_local() {
176+
Some(local_def_id) => self.local_macro_map[&local_def_id],
177+
None => *self.extern_macro_map.borrow_mut().entry(def_id).or_insert_with(|| {
178+
let loaded_macro = self.cstore().load_macro_untracked(def_id, self.tcx);
179+
let macro_data = match loaded_macro {
180+
LoadedMacro::MacroDef { def, ident, attrs, span, edition } => {
181+
self.compile_macro(&def, ident, &attrs, span, ast::DUMMY_NODE_ID, edition)
182+
}
183+
LoadedMacro::ProcMacro(ext) => MacroData::new(Arc::new(ext)),
184+
};
185185

186-
self.macro_map.entry(def_id).or_insert(macro_data)
186+
self.arenas.alloc_macro(macro_data)
187+
}),
188+
}
187189
}
188190

189191
pub(crate) fn build_reduced_graph(
@@ -1203,7 +1205,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12031205
fn insert_unused_macro(&mut self, ident: Ident, def_id: LocalDefId, node_id: NodeId) {
12041206
if !ident.as_str().starts_with('_') {
12051207
self.r.unused_macros.insert(def_id, (node_id, ident));
1206-
let nrules = self.r.macro_map[&def_id.to_def_id()].nrules;
1208+
let nrules = self.r.local_macro_map[&def_id].nrules;
12071209
self.r.unused_macro_rules.insert(node_id, DenseBitSet::new_filled(nrules));
12081210
}
12091211
}
@@ -1222,7 +1224,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12221224
Some((macro_kind, ident, span)) => {
12231225
let res = Res::Def(DefKind::Macro(macro_kind), def_id.to_def_id());
12241226
let macro_data = MacroData::new(self.r.dummy_ext(macro_kind));
1225-
self.r.macro_map.insert(def_id.to_def_id(), macro_data);
1227+
self.r.new_local_macro(def_id, macro_data);
12261228
self.r.proc_macro_stubs.insert(def_id);
12271229
(res, ident, span, false)
12281230
}

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
165165
self.create_def(i.id, i.kind.ident().map(|ident| ident.name), def_kind, i.span);
166166

167167
if let Some(macro_data) = opt_macro_data {
168-
self.resolver.macro_map.insert(def_id.to_def_id(), macro_data);
168+
self.resolver.new_local_macro(def_id, macro_data);
169169
}
170170

171171
self.with_parent(def_id, |this| {

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,9 +1669,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16691669
let mut all_attrs: UnordMap<Symbol, Vec<_>> = UnordMap::default();
16701670
// We're collecting these in a hashmap, and handle ordering the output further down.
16711671
#[allow(rustc::potential_query_instability)]
1672-
for (def_id, data) in &self.macro_map {
1672+
for (def_id, data) in self
1673+
.local_macro_map
1674+
.iter()
1675+
.map(|(local_id, data)| (local_id.to_def_id(), data))
1676+
.chain(self.extern_macro_map.borrow().iter().map(|(id, d)| (*id, d)))
1677+
{
16731678
for helper_attr in &data.ext.helper_attrs {
1674-
let item_name = self.tcx.item_name(*def_id);
1679+
let item_name = self.tcx.item_name(def_id);
16751680
all_attrs.entry(*helper_attr).or_default().push(item_name);
16761681
if helper_attr == &ident.name {
16771682
derives.push(item_name);

compiler/rustc_resolve/src/lib.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,10 +1128,12 @@ pub struct Resolver<'ra, 'tcx> {
11281128
builtin_macros: FxHashMap<Symbol, SyntaxExtensionKind>,
11291129
registered_tools: &'tcx RegisteredTools,
11301130
macro_use_prelude: FxIndexMap<Symbol, NameBinding<'ra>>,
1131-
macro_map: FxHashMap<DefId, MacroData>,
1131+
local_macro_map: FxHashMap<LocalDefId, &'ra MacroData>,
1132+
/// Lazily populated cache of macros loaded from external crates.
1133+
extern_macro_map: RefCell<FxHashMap<DefId, &'ra MacroData>>,
11321134
dummy_ext_bang: Arc<SyntaxExtension>,
11331135
dummy_ext_derive: Arc<SyntaxExtension>,
1134-
non_macro_attr: MacroData,
1136+
non_macro_attr: &'ra MacroData,
11351137
local_macro_def_scopes: FxHashMap<LocalDefId, Module<'ra>>,
11361138
ast_transform_scopes: FxHashMap<LocalExpnId, Module<'ra>>,
11371139
unused_macros: FxIndexMap<LocalDefId, (NodeId, Ident)>,
@@ -1241,6 +1243,7 @@ pub struct ResolverArenas<'ra> {
12411243
imports: TypedArena<ImportData<'ra>>,
12421244
name_resolutions: TypedArena<RefCell<NameResolution<'ra>>>,
12431245
ast_paths: TypedArena<ast::Path>,
1246+
macros: TypedArena<MacroData>,
12441247
dropless: DroplessArena,
12451248
}
12461249

@@ -1298,6 +1301,9 @@ impl<'ra> ResolverArenas<'ra> {
12981301
fn alloc_ast_paths(&'ra self, paths: &[ast::Path]) -> &'ra [ast::Path] {
12991302
self.ast_paths.alloc_from_iter(paths.iter().cloned())
13001303
}
1304+
fn alloc_macro(&'ra self, macro_data: MacroData) -> &'ra MacroData {
1305+
self.macros.alloc(macro_data)
1306+
}
13011307
fn alloc_pattern_spans(&'ra self, spans: impl Iterator<Item = Span>) -> &'ra [Span] {
13021308
self.dropless.alloc_from_iter(spans)
13031309
}
@@ -1540,10 +1546,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15401546
builtin_macros: Default::default(),
15411547
registered_tools,
15421548
macro_use_prelude: Default::default(),
1543-
macro_map: FxHashMap::default(),
1549+
local_macro_map: Default::default(),
1550+
extern_macro_map: Default::default(),
15441551
dummy_ext_bang: Arc::new(SyntaxExtension::dummy_bang(edition)),
15451552
dummy_ext_derive: Arc::new(SyntaxExtension::dummy_derive(edition)),
1546-
non_macro_attr: MacroData::new(Arc::new(SyntaxExtension::non_macro_attr(edition))),
1553+
non_macro_attr: arenas
1554+
.alloc_macro(MacroData::new(Arc::new(SyntaxExtension::non_macro_attr(edition)))),
15471555
invocation_parent_scopes: Default::default(),
15481556
output_macro_rules_scopes: Default::default(),
15491557
macro_rules_scopes: Default::default(),
@@ -1616,6 +1624,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16161624
)
16171625
}
16181626

1627+
fn new_local_macro(&mut self, def_id: LocalDefId, macro_data: MacroData) -> &'ra MacroData {
1628+
let mac = self.arenas.alloc_macro(macro_data);
1629+
self.local_macro_map.insert(def_id, mac);
1630+
mac
1631+
}
1632+
16191633
fn next_node_id(&mut self) -> NodeId {
16201634
let start = self.next_node_id;
16211635
let next = start.as_u32().checked_add(1).expect("input too large; ran out of NodeIds");
@@ -1734,7 +1748,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17341748
f(self, MacroNS);
17351749
}
17361750

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

compiler/rustc_resolve/src/macros.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
354354
if unused_arms.is_empty() {
355355
continue;
356356
}
357-
let def_id = self.local_def_id(node_id).to_def_id();
358-
let m = &self.macro_map[&def_id];
357+
let def_id = self.local_def_id(node_id);
358+
let m = &self.local_macro_map[&def_id];
359359
let SyntaxExtensionKind::LegacyBang(ref ext) = m.ext.kind else {
360360
continue;
361361
};
@@ -1132,7 +1132,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11321132
}
11331133
}
11341134

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

0 commit comments

Comments
 (0)