Skip to content

Commit feec99d

Browse files
committed
resolve: Remove MacroData
All the necessary data can be taken from `SyntaxExtension` instead.
1 parent e95e732 commit feec99d

8 files changed

Lines changed: 66 additions & 67 deletions

File tree

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ pub struct MacroRulesMacroExpander {
170170
transparency: Transparency,
171171
kinds: MacroKinds,
172172
rules: Vec<MacroRule>,
173+
macro_rules: bool,
173174
}
174175

175176
impl MacroRulesMacroExpander {
@@ -189,6 +190,14 @@ impl MacroRulesMacroExpander {
189190
self.kinds
190191
}
191192

193+
pub fn nrules(&self) -> usize {
194+
self.rules.len()
195+
}
196+
197+
pub fn is_macro_rules(&self) -> bool {
198+
self.macro_rules
199+
}
200+
192201
pub fn expand_derive(
193202
&self,
194203
cx: &mut ExtCtxt<'_>,
@@ -714,13 +723,12 @@ pub fn compile_declarative_macro(
714723
span: Span,
715724
node_id: NodeId,
716725
edition: Edition,
717-
) -> (SyntaxExtension, usize) {
726+
) -> SyntaxExtension {
718727
let mk_syn_ext = |kind| {
719728
let is_local = is_defined_in_current_crate(node_id);
720729
SyntaxExtension::new(sess, kind, span, Vec::new(), edition, ident.name, attrs, is_local)
721730
};
722-
let dummy_syn_ext =
723-
|guar| (mk_syn_ext(SyntaxExtensionKind::Bang(Arc::new(DummyBang(guar)))), 0);
731+
let dummy_syn_ext = |guar| mk_syn_ext(SyntaxExtensionKind::Bang(Arc::new(DummyBang(guar))));
724732

725733
let macro_rules = macro_def.macro_rules;
726734
let exp_sep = if macro_rules { exp!(Semi) } else { exp!(Comma) };
@@ -857,9 +865,6 @@ pub fn compile_declarative_macro(
857865
return dummy_syn_ext(guar);
858866
}
859867

860-
// Return the number of rules for unused rule linting, if this is a local macro.
861-
let nrules = if is_defined_in_current_crate(node_id) { rules.len() } else { 0 };
862-
863868
let on_unmatch_args = find_attr!(
864869
attrs,
865870
OnUnmatchArgs { directive, .. } => directive.clone()
@@ -875,8 +880,9 @@ pub fn compile_declarative_macro(
875880
on_unmatch_args,
876881
transparency,
877882
rules,
883+
macro_rules,
878884
};
879-
(mk_syn_ext(SyntaxExtensionKind::MacroRules(Arc::new(exp))), nrules)
885+
mk_syn_ext(SyntaxExtensionKind::MacroRules(Arc::new(exp)))
880886
}
881887

882888
fn check_no_eof(sess: &Session, p: &Parser<'_>, msg: &'static str) -> Option<ErrorGuaranteed> {

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_ast::{
1414
TyAlias,
1515
};
1616
use rustc_attr_parsing::AttributeParser;
17-
use rustc_expand::base::ResolverExpand;
17+
use rustc_expand::base::{ResolverExpand, SyntaxExtension, SyntaxExtensionKind};
1818
use rustc_hir::Attribute;
1919
use rustc_hir::attrs::{AttributeKind, MacroUseArgs};
2020
use rustc_hir::def::{self, *};
@@ -37,9 +37,8 @@ use crate::macros::{MacroRulesDecl, MacroRulesScope, MacroRulesScopeRef};
3737
use crate::ref_mut::CmCell;
3838
use crate::{
3939
BindingKey, Decl, DeclData, DeclKind, DelayedVisResolutionError, ExternModule,
40-
ExternPreludeEntry, Finalize, IdentKey, LocalModule, MacroData, Module, ModuleKind,
41-
ModuleOrUniformRoot, ParentScope, PathResult, Res, Resolver, Segment, Used, VisResolutionError,
42-
errors,
40+
ExternPreludeEntry, Finalize, IdentKey, LocalModule, Module, ModuleKind, ModuleOrUniformRoot,
41+
ParentScope, PathResult, Res, Resolver, Segment, Used, VisResolutionError, errors,
4342
};
4443

4544
impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
@@ -207,28 +206,28 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
207206
}
208207
}
209208

210-
pub(crate) fn get_macro(&self, res: Res) -> Option<&'ra MacroData> {
209+
pub(crate) fn get_macro(&self, res: Res) -> Option<&'ra Arc<SyntaxExtension>> {
211210
match res {
212211
Res::Def(DefKind::Macro(..), def_id) => Some(self.get_macro_by_def_id(def_id)),
213212
Res::NonMacroAttr(_) => Some(self.non_macro_attr),
214213
_ => None,
215214
}
216215
}
217216

218-
pub(crate) fn get_macro_by_def_id(&self, def_id: DefId) -> &'ra MacroData {
217+
pub(crate) fn get_macro_by_def_id(&self, def_id: DefId) -> &'ra Arc<SyntaxExtension> {
219218
// Local macros are always compiled.
220219
match def_id.as_local() {
221220
Some(local_def_id) => self.local_macro_map[&local_def_id],
222-
None => *self.extern_macro_map.borrow_mut().entry(def_id).or_insert_with(|| {
221+
None => self.extern_macro_map.borrow_mut().entry(def_id).or_insert_with(|| {
223222
let loaded_macro = self.cstore().load_macro_untracked(self.tcx, def_id);
224-
let macro_data = match loaded_macro {
223+
let ext = match loaded_macro {
225224
LoadedMacro::MacroDef { def, ident, attrs, span, edition } => {
226225
self.compile_macro(&def, ident, &attrs, span, ast::DUMMY_NODE_ID, edition)
227226
}
228-
LoadedMacro::ProcMacro(ext) => MacroData::new(Arc::new(ext)),
227+
LoadedMacro::ProcMacro(ext) => ext,
229228
};
230229

231-
self.arenas.alloc_macro(macro_data)
230+
self.arenas.alloc_macro(Arc::new(ext))
232231
}),
233232
}
234233
}
@@ -1269,8 +1268,9 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
12691268
fn insert_unused_macro(&mut self, ident: Ident, def_id: LocalDefId, node_id: NodeId) {
12701269
if !ident.as_str().starts_with('_') {
12711270
self.r.unused_macros.insert(def_id, (node_id, ident));
1272-
let nrules = self.r.local_macro_map[&def_id].nrules;
1273-
self.r.unused_macro_rules.insert(node_id, DenseBitSet::new_filled(nrules));
1271+
if let SyntaxExtensionKind::MacroRules(mr) = &self.r.local_macro_map[&def_id].kind {
1272+
self.r.unused_macro_rules.insert(node_id, DenseBitSet::new_filled(mr.nrules()));
1273+
}
12741274
}
12751275
}
12761276

@@ -1291,8 +1291,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
12911291
Some((macro_kind, ident, span)) => {
12921292
let macro_kinds = macro_kind.into();
12931293
let res = Res::Def(DefKind::Macro(macro_kinds), def_id.to_def_id());
1294-
let macro_data = MacroData::new(self.r.dummy_ext(macro_kind));
1295-
self.r.new_local_macro(def_id, macro_data);
1294+
self.r.new_local_macro(def_id, self.r.dummy_ext(macro_kind));
12961295
self.r.proc_macro_stubs.insert(def_id);
12971296
(res, ident, span, false)
12981297
}

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::mem;
2+
use std::sync::Arc;
23

34
use rustc_ast::visit::FnKind;
45
use rustc_ast::*;
@@ -117,7 +118,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
117118
fn visit_item(&mut self, i: &'a Item) {
118119
// Pick the def data. This need not be unique, but the more
119120
// information we encapsulate into, the better
120-
let mut opt_macro_data = None;
121+
let mut opt_syn_ext = None;
121122
let def_kind = match &i.kind {
122123
ItemKind::Impl(i) => DefKind::Impl { of_trait: i.of_trait.is_some() },
123124
ItemKind::ForeignMod(..) => DefKind::ForeignMod,
@@ -165,9 +166,9 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
165166
},
166167
);
167168

168-
let macro_data = self.r.compile_macro(def, *ident, &attrs, i.span, i.id, edition);
169-
let macro_kinds = macro_data.ext.macro_kinds();
170-
opt_macro_data = Some(macro_data);
169+
let ext = self.r.compile_macro(def, *ident, &attrs, i.span, i.id, edition);
170+
let macro_kinds = ext.macro_kinds();
171+
opt_syn_ext = Some(ext);
171172
DefKind::Macro(macro_kinds)
172173
}
173174
ItemKind::GlobalAsm(..) => DefKind::GlobalAsm,
@@ -185,8 +186,8 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
185186
};
186187
let feed = self.create_def(i.id, i.kind.ident().map(|ident| ident.name), def_kind, i.span);
187188

188-
if let Some(macro_data) = opt_macro_data {
189-
self.r.new_local_macro(feed.def_id(), macro_data);
189+
if let Some(ext) = opt_syn_ext {
190+
self.r.new_local_macro(feed.def_id(), Arc::new(ext));
190191
}
191192

192193
self.with_parent(feed.def_id(), |this| {

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,7 +1742,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17421742
if let Some((def_id, unused_ident)) = unused_macro {
17431743
let scope = self.local_macro_def_scopes[&def_id];
17441744
let parent_nearest = parent_scope.module.nearest_parent_mod();
1745-
let unused_macro_kinds = self.local_macro_map[def_id].ext.macro_kinds();
1745+
let unused_macro_kinds = self.local_macro_map[def_id].macro_kinds();
17461746
if !unused_macro_kinds.contains(macro_kind.into()) {
17471747
match macro_kind {
17481748
MacroKind::Bang => {
@@ -1860,13 +1860,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18601860
let mut all_attrs: UnordMap<Symbol, Vec<_>> = UnordMap::default();
18611861
// We're collecting these in a hashmap, and handle ordering the output further down.
18621862
#[allow(rustc::potential_query_instability)]
1863-
for (def_id, data) in self
1863+
for (def_id, ext) in self
18641864
.local_macro_map
18651865
.iter()
1866-
.map(|(local_id, data)| (local_id.to_def_id(), data))
1866+
.map(|(local_id, ext)| (local_id.to_def_id(), ext))
18671867
.chain(self.extern_macro_map.borrow().iter().map(|(id, d)| (*id, d)))
18681868
{
1869-
for helper_attr in &data.ext.helper_attrs {
1869+
for helper_attr in &ext.helper_attrs {
18701870
let item_name = self.tcx.item_name(def_id);
18711871
all_attrs.entry(*helper_attr).or_default().push(item_name);
18721872
if helper_attr == &ident.name {

compiler/rustc_resolve/src/ident.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
271271
// The macro is a proc macro derive
272272
&& let Some(def_id) = module.expansion.expn_data().macro_def_id
273273
{
274-
let ext = &self.get_macro_by_def_id(def_id).ext;
274+
let ext = self.get_macro_by_def_id(def_id);
275275
if ext.builtin_name.is_none()
276276
&& ext.macro_kinds() == MacroKinds::DERIVE
277277
&& parent.expansion.outer_expn_is_descendant_of(**ctxt)

compiler/rustc_resolve/src/imports.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_errors::codes::*;
1212
use rustc_errors::{
1313
Applicability, BufferedEarlyLint, Diagnostic, MultiSpan, pluralize, struct_span_code_err,
1414
};
15+
use rustc_expand::base::SyntaxExtensionKind;
1516
use rustc_hir::Attribute;
1617
use rustc_hir::attrs::AttributeKind;
1718
use rustc_hir::attrs::diagnostic::{CustomDiagnostic, Directive, FormatArgs};
@@ -1639,7 +1640,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16391640
match decl.kind {
16401641
// exclude decl_macro
16411642
DeclKind::Def(Res::Def(DefKind::Macro(_), def_id))
1642-
if self.get_macro_by_def_id(def_id).macro_rules =>
1643+
if let SyntaxExtensionKind::MacroRules(mr) =
1644+
&self.get_macro_by_def_id(def_id).kind
1645+
&& mr.is_macro_rules() =>
16431646
{
16441647
err.subdiagnostic(ConsiderAddingMacroExport { span: decl.span });
16451648
err.subdiagnostic(ConsiderMarkingAsPubCrate { vis_span: import.vis_span });

compiler/rustc_resolve/src/lib.rs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,18 +1265,6 @@ struct DeriveData {
12651265
has_derive_copy: bool,
12661266
}
12671267

1268-
struct MacroData {
1269-
ext: Arc<SyntaxExtension>,
1270-
nrules: usize,
1271-
macro_rules: bool,
1272-
}
1273-
1274-
impl MacroData {
1275-
fn new(ext: Arc<SyntaxExtension>) -> MacroData {
1276-
MacroData { ext, nrules: 0, macro_rules: false }
1277-
}
1278-
}
1279-
12801268
pub struct ResolverOutputs<'tcx> {
12811269
pub global_ctxt: ResolverGlobalCtxt,
12821270
pub ast_lowering: ResolverAstLowering<'tcx>,
@@ -1396,12 +1384,12 @@ pub struct Resolver<'ra, 'tcx> {
13961384
registered_tools: &'tcx RegisteredTools,
13971385
macro_use_prelude: FxIndexMap<Symbol, Decl<'ra>>,
13981386
/// Eagerly populated map of all local macro definitions.
1399-
local_macro_map: FxHashMap<LocalDefId, &'ra MacroData> = default::fx_hash_map(),
1387+
local_macro_map: FxHashMap<LocalDefId, &'ra Arc<SyntaxExtension>> = default::fx_hash_map(),
14001388
/// Lazily populated cache of macro definitions loaded from external crates.
1401-
extern_macro_map: CacheRefCell<FxHashMap<DefId, &'ra MacroData>>,
1389+
extern_macro_map: CacheRefCell<FxHashMap<DefId, &'ra Arc<SyntaxExtension>>>,
14021390
dummy_ext_bang: Arc<SyntaxExtension>,
14031391
dummy_ext_derive: Arc<SyntaxExtension>,
1404-
non_macro_attr: &'ra MacroData,
1392+
non_macro_attr: &'ra Arc<SyntaxExtension>,
14051393
local_macro_def_scopes: FxHashMap<LocalDefId, LocalModule<'ra>> = default::fx_hash_map(),
14061394
ast_transform_scopes: FxHashMap<LocalExpnId, LocalModule<'ra>> = default::fx_hash_map(),
14071395
unused_macros: FxIndexMap<LocalDefId, (NodeId, Ident)>,
@@ -1520,7 +1508,7 @@ pub struct ResolverArenas<'ra> {
15201508
imports: TypedArena<ImportData<'ra>>,
15211509
name_resolutions: TypedArena<CmRefCell<NameResolution<'ra>>>,
15221510
ast_paths: TypedArena<ast::Path>,
1523-
macros: TypedArena<MacroData>,
1511+
macros: TypedArena<Arc<SyntaxExtension>>,
15241512
dropless: DroplessArena,
15251513
}
15261514

@@ -1599,8 +1587,8 @@ impl<'ra> ResolverArenas<'ra> {
15991587
fn alloc_ast_paths(&'ra self, paths: &[ast::Path]) -> &'ra [ast::Path] {
16001588
self.ast_paths.alloc_from_iter(paths.iter().cloned())
16011589
}
1602-
fn alloc_macro(&'ra self, macro_data: MacroData) -> &'ra MacroData {
1603-
self.macros.alloc(macro_data)
1590+
fn alloc_macro(&'ra self, ext: Arc<SyntaxExtension>) -> &'ra Arc<SyntaxExtension> {
1591+
self.macros.alloc(ext)
16041592
}
16051593
fn alloc_pattern_spans(&'ra self, spans: impl Iterator<Item = Span>) -> &'ra [Span] {
16061594
self.dropless.alloc_from_iter(spans)
@@ -1821,8 +1809,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18211809
extern_macro_map: Default::default(),
18221810
dummy_ext_bang: Arc::new(SyntaxExtension::dummy_bang(edition)),
18231811
dummy_ext_derive: Arc::new(SyntaxExtension::dummy_derive(edition)),
1824-
non_macro_attr: arenas
1825-
.alloc_macro(MacroData::new(Arc::new(SyntaxExtension::non_macro_attr(edition)))),
1812+
non_macro_attr: arenas.alloc_macro(Arc::new(SyntaxExtension::non_macro_attr(edition))),
18261813
unused_macros: Default::default(),
18271814
unused_macro_rules: Default::default(),
18281815
single_segment_macro_resolutions: Default::default(),
@@ -1889,8 +1876,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18891876
module
18901877
}
18911878

1892-
fn new_local_macro(&mut self, def_id: LocalDefId, macro_data: MacroData) -> &'ra MacroData {
1893-
let mac = self.arenas.alloc_macro(macro_data);
1879+
fn new_local_macro(
1880+
&mut self,
1881+
def_id: LocalDefId,
1882+
ext: Arc<SyntaxExtension>,
1883+
) -> &'ra Arc<SyntaxExtension> {
1884+
let mac = self.arenas.alloc_macro(ext);
18941885
self.local_macro_map.insert(def_id, mac);
18951886
mac
18961887
}
@@ -1993,7 +1984,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19931984
match macro_kind {
19941985
MacroKind::Bang => Arc::clone(&self.dummy_ext_bang),
19951986
MacroKind::Derive => Arc::clone(&self.dummy_ext_derive),
1996-
MacroKind::Attr => Arc::clone(&self.non_macro_attr.ext),
1987+
MacroKind::Attr => Arc::clone(self.non_macro_attr),
19971988
}
19981989
}
19991990

@@ -2022,11 +2013,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20222013
}
20232014

20242015
fn is_builtin_macro(&self, res: Res) -> bool {
2025-
self.get_macro(res).is_some_and(|macro_data| macro_data.ext.builtin_name.is_some())
2016+
self.get_macro(res).is_some_and(|ext| ext.builtin_name.is_some())
20262017
}
20272018

20282019
fn is_specific_builtin_macro(&self, res: Res, symbol: Symbol) -> bool {
2029-
self.get_macro(res).is_some_and(|macro_data| macro_data.ext.builtin_name == Some(symbol))
2020+
self.get_macro(res).is_some_and(|ext| ext.builtin_name == Some(symbol))
20302021
}
20312022

20322023
fn macro_def(&self, mut ctxt: SyntaxContext) -> DefId {

compiler/rustc_resolve/src/macros.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use crate::hygiene::Macros20NormalizedSyntaxContext;
4343
use crate::imports::Import;
4444
use crate::{
4545
BindingKey, CacheCell, CmResolver, Decl, DeclKind, DeriveData, Determinacy, Finalize, IdentKey,
46-
InvocationParent, MacroData, ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, Res,
46+
InvocationParent, ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, Res,
4747
ResolutionError, Resolver, ScopeSet, Segment, Used,
4848
};
4949

@@ -361,8 +361,8 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
361361
continue;
362362
}
363363
let def_id = self.local_def_id(node_id);
364-
let m = &self.local_macro_map[&def_id];
365-
let SyntaxExtensionKind::MacroRules(ref m) = m.ext.kind else {
364+
let ext = self.local_macro_map[&def_id];
365+
let SyntaxExtensionKind::MacroRules(ref m) = ext.kind else {
366366
continue;
367367
};
368368
for arm_i in unused_arms.iter() {
@@ -882,7 +882,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
882882
}
883883
_ => None,
884884
},
885-
None => self.get_macro(res).map(|macro_data| Arc::clone(&macro_data.ext)),
885+
None => self.get_macro(res).map(|ext| Arc::clone(ext)),
886886
};
887887
Ok((ext, res))
888888
}
@@ -1212,7 +1212,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12121212
// Reserve some names that are not quite covered by the general check
12131213
// performed on `Resolver::builtin_attrs`.
12141214
if name == sym::cfg || name == sym::cfg_attr {
1215-
let macro_kinds = self.get_macro(res).map(|macro_data| macro_data.ext.macro_kinds());
1215+
let macro_kinds = self.get_macro(res).map(|ext| ext.macro_kinds());
12161216
if macro_kinds.is_some() && sub_namespace_match(macro_kinds, Some(MacroKind::Attr)) {
12171217
self.dcx().emit_err(errors::NameReservedInAttributeNamespace { span, ident: name });
12181218
}
@@ -1230,8 +1230,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12301230
span: Span,
12311231
node_id: NodeId,
12321232
edition: Edition,
1233-
) -> MacroData {
1234-
let (mut ext, mut nrules) = compile_declarative_macro(
1233+
) -> SyntaxExtension {
1234+
let mut ext = compile_declarative_macro(
12351235
self.tcx.sess,
12361236
self.tcx.features(),
12371237
macro_def,
@@ -1248,13 +1248,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12481248
// The macro is a built-in, replace its expander function
12491249
// while still taking everything else from the source code.
12501250
ext.kind = builtin_ext_kind.clone();
1251-
nrules = 0;
12521251
} else {
12531252
self.dcx().emit_err(errors::CannotFindBuiltinMacroWithName { span, ident });
12541253
}
12551254
}
12561255

1257-
MacroData { ext: Arc::new(ext), nrules, macro_rules: macro_def.macro_rules }
1256+
ext
12581257
}
12591258

12601259
fn path_accessible(

0 commit comments

Comments
 (0)