Skip to content

Commit 8009764

Browse files
split macr_map into a external and local map
1 parent 2335d05 commit 8009764

File tree

5 files changed

+56
-28
lines changed

5 files changed

+56
-28
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::def_collector::collect_definitions;
3232
use crate::imports::{ImportData, ImportKind};
3333
use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
3434
use crate::{
35-
BindingKey, Determinacy, ExternPreludeEntry, Finalize, MacroData, Module, ModuleKind,
35+
BindingKey, Determinacy, ExternPreludeEntry, Finalize, Macro, MacroData, Module, ModuleKind,
3636
ModuleOrUniformRoot, NameBinding, NameBindingData, NameBindingKind, ParentScope, PathResult,
3737
ResolutionError, Resolver, ResolverArenas, Segment, ToNameBinding, Used, VisResolutionError,
3838
errors,
@@ -162,28 +162,35 @@ 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(&mut self, res: Res) -> Option<Macro<'ra>> {
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)
173+
pub(crate) fn get_macro_by_def_id(&self, def_id: DefId) -> Macro<'ra> {
174+
match def_id.as_local() {
175+
Some(local_def_id) => {
176+
// local macros are always compiled.
177+
self.local_macro_map
178+
.get(&local_def_id)
179+
.copied()
180+
.expect("Local Macros should be compiled and available.")
182181
}
183-
LoadedMacro::ProcMacro(ext) => MacroData::new(Arc::new(ext)),
184-
};
182+
None => *self.external_macro_map.borrow_mut().entry(def_id).or_insert_with(|| {
183+
let loaded_macro = self.cstore().load_macro_untracked(def_id, self.tcx);
184+
let macro_data = match loaded_macro {
185+
LoadedMacro::MacroDef { def, ident, attrs, span, edition } => {
186+
self.compile_macro(&def, ident, &attrs, span, ast::DUMMY_NODE_ID, edition)
187+
}
188+
LoadedMacro::ProcMacro(ext) => MacroData::new(Arc::new(ext)),
189+
};
185190

186-
self.macro_map.entry(def_id).or_insert(macro_data)
191+
self.arenas.alloc_macro(macro_data)
192+
}),
193+
}
187194
}
188195

189196
pub(crate) fn build_reduced_graph(
@@ -1203,7 +1210,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12031210
fn insert_unused_macro(&mut self, ident: Ident, def_id: LocalDefId, node_id: NodeId) {
12041211
if !ident.as_str().starts_with('_') {
12051212
self.r.unused_macros.insert(def_id, (node_id, ident));
1206-
let nrules = self.r.macro_map[&def_id.to_def_id()].nrules;
1213+
let nrules = self.r.local_macro_map[&def_id].nrules;
12071214
self.r.unused_macro_rules.insert(node_id, DenseBitSet::new_filled(nrules));
12081215
}
12091216
}
@@ -1222,7 +1229,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12221229
Some((macro_kind, ident, span)) => {
12231230
let res = Res::Def(DefKind::Macro(macro_kind), def_id.to_def_id());
12241231
let macro_data = MacroData::new(self.r.dummy_ext(macro_kind));
1225-
self.r.macro_map.insert(def_id.to_def_id(), macro_data);
1232+
self.r.new_local_macro(def_id, macro_data);
12261233
self.r.proc_macro_stubs.insert(def_id);
12271234
(res, ident, span, false)
12281235
}

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.external_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: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ struct DeriveData {
10131013
has_derive_copy: bool,
10141014
}
10151015

1016-
struct MacroData {
1016+
pub struct MacroData {
10171017
ext: Arc<SyntaxExtension>,
10181018
nrules: usize,
10191019
macro_rules: bool,
@@ -1143,10 +1143,11 @@ pub struct Resolver<'ra, 'tcx> {
11431143
builtin_macros: FxHashMap<Symbol, SyntaxExtensionKind>,
11441144
registered_tools: &'tcx RegisteredTools,
11451145
macro_use_prelude: FxIndexMap<Symbol, NameBinding<'ra>>,
1146-
macro_map: FxHashMap<DefId, MacroData>,
1146+
local_macro_map: FxHashMap<LocalDefId, Macro<'ra>>,
1147+
external_macro_map: RefCell<FxHashMap<DefId, Macro<'ra>>>,
11471148
dummy_ext_bang: Arc<SyntaxExtension>,
11481149
dummy_ext_derive: Arc<SyntaxExtension>,
1149-
non_macro_attr: MacroData,
1150+
non_macro_attr: Macro<'ra>,
11501151
local_macro_def_scopes: FxHashMap<LocalDefId, Module<'ra>>,
11511152
ast_transform_scopes: FxHashMap<LocalExpnId, Module<'ra>>,
11521153
unused_macros: FxIndexMap<LocalDefId, (NodeId, Ident)>,
@@ -1314,6 +1315,9 @@ impl<'ra> ResolverArenas<'ra> {
13141315
fn alloc_ast_paths(&'ra self, paths: &[ast::Path]) -> &'ra [ast::Path] {
13151316
self.ast_paths.alloc_from_iter(paths.iter().cloned())
13161317
}
1318+
fn alloc_macro(&'ra self, macro_data: MacroData) -> Macro<'ra> {
1319+
Interned::new_unchecked(self.macros.alloc(macro_data))
1320+
}
13171321
fn alloc_pattern_spans(&'ra self, spans: impl Iterator<Item = Span>) -> &'ra [Span] {
13181322
self.dropless.alloc_from_iter(spans)
13191323
}
@@ -1556,10 +1560,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15561560
builtin_macros: Default::default(),
15571561
registered_tools,
15581562
macro_use_prelude: Default::default(),
1559-
macro_map: FxHashMap::default(),
1563+
local_macro_map: FxHashMap::default(),
1564+
external_macro_map: RefCell::default(),
15601565
dummy_ext_bang: Arc::new(SyntaxExtension::dummy_bang(edition)),
15611566
dummy_ext_derive: Arc::new(SyntaxExtension::dummy_derive(edition)),
1562-
non_macro_attr: MacroData::new(Arc::new(SyntaxExtension::non_macro_attr(edition))),
1567+
non_macro_attr: arenas
1568+
.alloc_macro(MacroData::new(Arc::new(SyntaxExtension::non_macro_attr(edition)))),
15631569
invocation_parent_scopes: Default::default(),
15641570
output_macro_rules_scopes: Default::default(),
15651571
macro_rules_scopes: Default::default(),
@@ -1632,6 +1638,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16321638
)
16331639
}
16341640

1641+
pub fn new_local_macro(
1642+
&mut self,
1643+
local_def_id: LocalDefId,
1644+
macro_data: MacroData,
1645+
) -> Macro<'ra> {
1646+
let mac = self.arenas.alloc_macro(macro_data);
1647+
self.local_macro_map.insert(local_def_id, mac);
1648+
mac
1649+
}
1650+
16351651
fn next_node_id(&mut self) -> NodeId {
16361652
let start = self.next_node_id;
16371653
let next = start.as_u32().checked_add(1).expect("input too large; ran out of NodeIds");

compiler/rustc_resolve/src/macros.rs

Lines changed: 3 additions & 3 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 local_def_id = self.local_def_id(node_id);
358+
let m = &self.local_macro_map[&local_def_id];
359359
let SyntaxExtensionKind::LegacyBang(ref ext) = m.ext.kind else {
360360
continue;
361361
};
@@ -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)