Skip to content

Commit 94d96b1

Browse files
authored
Rollup merge of #72284 - Aaron1011:feature/inline-macro-did, r=petrochenkov
Remove `macro_defs` map We now store the `DefId` directly in `ExpnKind::Macro`. This will allow us to serialize `ExpnData` in PR #72121 without needing to manage a side table.
2 parents 52b605c + d277904 commit 94d96b1

File tree

7 files changed

+44
-19
lines changed

7 files changed

+44
-19
lines changed

src/librustc_ast_lowering/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
688688
) -> Span {
689689
span.fresh_expansion(ExpnData {
690690
allow_internal_unstable,
691-
..ExpnData::default(ExpnKind::Desugaring(reason), span, self.sess.edition())
691+
..ExpnData::default(ExpnKind::Desugaring(reason), span, self.sess.edition(), None)
692692
})
693693
}
694694

src/librustc_expand/base.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_data_structures::sync::{self, Lrc};
1313
use rustc_errors::{DiagnosticBuilder, ErrorReported};
1414
use rustc_parse::{self, parser, MACRO_ARGUMENTS};
1515
use rustc_session::parse::ParseSess;
16+
use rustc_span::def_id::DefId;
1617
use rustc_span::edition::Edition;
1718
use rustc_span::hygiene::{AstPass, ExpnData, ExpnId, ExpnKind};
1819
use rustc_span::source_map::SourceMap;
@@ -857,7 +858,13 @@ impl SyntaxExtension {
857858
SyntaxExtension::default(SyntaxExtensionKind::NonMacroAttr { mark_used }, edition)
858859
}
859860

860-
pub fn expn_data(&self, parent: ExpnId, call_site: Span, descr: Symbol) -> ExpnData {
861+
pub fn expn_data(
862+
&self,
863+
parent: ExpnId,
864+
call_site: Span,
865+
descr: Symbol,
866+
macro_def_id: Option<DefId>,
867+
) -> ExpnData {
861868
ExpnData {
862869
kind: ExpnKind::Macro(self.macro_kind(), descr),
863870
parent,
@@ -867,6 +874,7 @@ impl SyntaxExtension {
867874
allow_internal_unsafe: self.allow_internal_unsafe,
868875
local_inner_macros: self.local_inner_macros,
869876
edition: self.edition,
877+
macro_def_id,
870878
}
871879
}
872880
}

src/librustc_expand/expand.rs

+1
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
988988
ExpnKind::Macro(MacroKind::Attr, sym::derive),
989989
item.span(),
990990
self.cx.parse_sess.edition,
991+
None,
991992
)
992993
}),
993994
_ => None,

src/librustc_resolve/build_reduced_graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ impl<'a> Resolver<'a> {
126126
}
127127

128128
crate fn macro_def_scope(&mut self, expn_id: ExpnId) -> Module<'a> {
129-
let def_id = match self.macro_defs.get(&expn_id) {
130-
Some(def_id) => *def_id,
129+
let def_id = match expn_id.expn_data().macro_def_id {
130+
Some(def_id) => def_id,
131131
None => return self.ast_transform_scopes.get(&expn_id).unwrap_or(&self.graph_root),
132132
};
133133
if let Some(id) = self.definitions.as_local_node_id(def_id) {

src/librustc_resolve/lib.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,6 @@ pub struct Resolver<'a> {
922922
dummy_ext_bang: Lrc<SyntaxExtension>,
923923
dummy_ext_derive: Lrc<SyntaxExtension>,
924924
non_macro_attrs: [Lrc<SyntaxExtension>; 2],
925-
macro_defs: FxHashMap<ExpnId, DefId>,
926925
local_macro_def_scopes: FxHashMap<NodeId, Module<'a>>,
927926
ast_transform_scopes: FxHashMap<ExpnId, Module<'a>>,
928927
unused_macros: NodeMap<Span>,
@@ -1152,9 +1151,6 @@ impl<'a> Resolver<'a> {
11521151
let mut invocation_parent_scopes = FxHashMap::default();
11531152
invocation_parent_scopes.insert(ExpnId::root(), ParentScope::module(graph_root));
11541153

1155-
let mut macro_defs = FxHashMap::default();
1156-
macro_defs.insert(ExpnId::root(), root_def_id);
1157-
11581154
let features = session.features_untracked();
11591155
let non_macro_attr =
11601156
|mark_used| Lrc::new(SyntaxExtension::non_macro_attr(mark_used, session.edition()));
@@ -1229,7 +1225,6 @@ impl<'a> Resolver<'a> {
12291225
invocation_parent_scopes,
12301226
output_macro_rules_scopes: Default::default(),
12311227
helper_attrs: Default::default(),
1232-
macro_defs,
12331228
local_macro_def_scopes: FxHashMap::default(),
12341229
name_already_seen: FxHashMap::default(),
12351230
potentially_unused_imports: Vec::new(),
@@ -1335,8 +1330,8 @@ impl<'a> Resolver<'a> {
13351330

13361331
fn macro_def(&self, mut ctxt: SyntaxContext) -> DefId {
13371332
loop {
1338-
match self.macro_defs.get(&ctxt.outer_expn()) {
1339-
Some(&def_id) => return def_id,
1333+
match ctxt.outer_expn().expn_data().macro_def_id {
1334+
Some(def_id) => return def_id,
13401335
None => ctxt.remove_mark(),
13411336
};
13421337
}
@@ -1820,7 +1815,7 @@ impl<'a> Resolver<'a> {
18201815
&& module.expansion.is_descendant_of(parent.expansion)
18211816
{
18221817
// The macro is a proc macro derive
1823-
if let Some(&def_id) = self.macro_defs.get(&module.expansion) {
1818+
if let Some(def_id) = module.expansion.expn_data().macro_def_id {
18241819
if let Some(ext) = self.get_macro_by_def_id(def_id) {
18251820
if !ext.is_builtin && ext.macro_kind() == MacroKind::Derive {
18261821
if parent.expansion.outer_expn_is_descendant_of(span.ctxt()) {

src/librustc_resolve/macros.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ impl<'a> base::Resolver for Resolver<'a> {
186186
call_site,
187187
self.session.edition(),
188188
features.into(),
189+
None,
189190
)));
190191

191192
let parent_scope = if let Some(module_id) = parent_module_id {
@@ -290,13 +291,17 @@ impl<'a> base::Resolver for Resolver<'a> {
290291
let (ext, res) = self.smart_resolve_macro_path(path, kind, parent_scope, force)?;
291292

292293
let span = invoc.span();
293-
invoc_id.set_expn_data(ext.expn_data(parent_scope.expansion, span, fast_print_path(path)));
294-
295-
if let Res::Def(_, def_id) = res {
294+
invoc_id.set_expn_data(ext.expn_data(
295+
parent_scope.expansion,
296+
span,
297+
fast_print_path(path),
298+
res.opt_def_id(),
299+
));
300+
301+
if let Res::Def(_, _) = res {
296302
if after_derive {
297303
self.session.span_err(span, "macro attributes must be placed before `#[derive]`");
298304
}
299-
self.macro_defs.insert(invoc_id, def_id);
300305
let normal_module_def_id = self.macro_def_scope(invoc_id).normal_ancestor_id;
301306
self.definitions.add_parent_module_of_macro_def(invoc_id, normal_module_def_id);
302307
}

src/librustc_span/hygiene.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
// because getting it wrong can lead to nested `HygieneData::with` calls that
2626
// trigger runtime aborts. (Fortunately these are obvious and easy to fix.)
2727

28+
use crate::def_id::{DefId, CRATE_DEF_INDEX};
2829
use crate::edition::Edition;
2930
use crate::symbol::{kw, sym, Symbol};
3031
use crate::GLOBALS;
@@ -155,7 +156,12 @@ crate struct HygieneData {
155156
impl HygieneData {
156157
crate fn new(edition: Edition) -> Self {
157158
HygieneData {
158-
expn_data: vec![Some(ExpnData::default(ExpnKind::Root, DUMMY_SP, edition))],
159+
expn_data: vec![Some(ExpnData::default(
160+
ExpnKind::Root,
161+
DUMMY_SP,
162+
edition,
163+
Some(DefId::local(CRATE_DEF_INDEX)),
164+
))],
159165
syntax_context_data: vec![SyntaxContextData {
160166
outer_expn: ExpnId::root(),
161167
outer_transparency: Transparency::Opaque,
@@ -673,11 +679,19 @@ pub struct ExpnData {
673679
pub local_inner_macros: bool,
674680
/// Edition of the crate in which the macro is defined.
675681
pub edition: Edition,
682+
/// The `DefId` of the macro being invoked,
683+
/// if this `ExpnData` corresponds to a macro invocation
684+
pub macro_def_id: Option<DefId>,
676685
}
677686

678687
impl ExpnData {
679688
/// Constructs expansion data with default properties.
680-
pub fn default(kind: ExpnKind, call_site: Span, edition: Edition) -> ExpnData {
689+
pub fn default(
690+
kind: ExpnKind,
691+
call_site: Span,
692+
edition: Edition,
693+
macro_def_id: Option<DefId>,
694+
) -> ExpnData {
681695
ExpnData {
682696
kind,
683697
parent: ExpnId::root(),
@@ -687,6 +701,7 @@ impl ExpnData {
687701
allow_internal_unsafe: false,
688702
local_inner_macros: false,
689703
edition,
704+
macro_def_id,
690705
}
691706
}
692707

@@ -695,10 +710,11 @@ impl ExpnData {
695710
call_site: Span,
696711
edition: Edition,
697712
allow_internal_unstable: Lrc<[Symbol]>,
713+
macro_def_id: Option<DefId>,
698714
) -> ExpnData {
699715
ExpnData {
700716
allow_internal_unstable: Some(allow_internal_unstable),
701-
..ExpnData::default(kind, call_site, edition)
717+
..ExpnData::default(kind, call_site, edition, macro_def_id)
702718
}
703719
}
704720

0 commit comments

Comments
 (0)