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.
This commit is contained in:
Ralf Jung 2020-05-24 12:17:09 +02:00 committed by GitHub
commit 94d96b1d75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 43 additions and 18 deletions

View file

@ -688,7 +688,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
) -> Span {
span.fresh_expansion(ExpnData {
allow_internal_unstable,
..ExpnData::default(ExpnKind::Desugaring(reason), span, self.sess.edition())
..ExpnData::default(ExpnKind::Desugaring(reason), span, self.sess.edition(), None)
})
}

View file

@ -13,6 +13,7 @@ use rustc_data_structures::sync::{self, Lrc};
use rustc_errors::{DiagnosticBuilder, ErrorReported};
use rustc_parse::{self, parser, MACRO_ARGUMENTS};
use rustc_session::parse::ParseSess;
use rustc_span::def_id::DefId;
use rustc_span::edition::Edition;
use rustc_span::hygiene::{AstPass, ExpnData, ExpnId, ExpnKind};
use rustc_span::source_map::SourceMap;
@ -857,7 +858,13 @@ impl SyntaxExtension {
SyntaxExtension::default(SyntaxExtensionKind::NonMacroAttr { mark_used }, edition)
}
pub fn expn_data(&self, parent: ExpnId, call_site: Span, descr: Symbol) -> ExpnData {
pub fn expn_data(
&self,
parent: ExpnId,
call_site: Span,
descr: Symbol,
macro_def_id: Option<DefId>,
) -> ExpnData {
ExpnData {
kind: ExpnKind::Macro(self.macro_kind(), descr),
parent,
@ -867,6 +874,7 @@ impl SyntaxExtension {
allow_internal_unsafe: self.allow_internal_unsafe,
local_inner_macros: self.local_inner_macros,
edition: self.edition,
macro_def_id,
}
}
}

View file

@ -988,6 +988,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
ExpnKind::Macro(MacroKind::Attr, sym::derive),
item.span(),
self.cx.parse_sess.edition,
None,
)
}),
_ => None,

View file

@ -126,8 +126,8 @@ impl<'a> Resolver<'a> {
}
crate fn macro_def_scope(&mut self, expn_id: ExpnId) -> Module<'a> {
let def_id = match self.macro_defs.get(&expn_id) {
Some(def_id) => *def_id,
let def_id = match expn_id.expn_data().macro_def_id {
Some(def_id) => def_id,
None => return self.ast_transform_scopes.get(&expn_id).unwrap_or(&self.graph_root),
};
if let Some(id) = self.definitions.as_local_node_id(def_id) {

View file

@ -922,7 +922,6 @@ pub struct Resolver<'a> {
dummy_ext_bang: Lrc<SyntaxExtension>,
dummy_ext_derive: Lrc<SyntaxExtension>,
non_macro_attrs: [Lrc<SyntaxExtension>; 2],
macro_defs: FxHashMap<ExpnId, DefId>,
local_macro_def_scopes: FxHashMap<NodeId, Module<'a>>,
ast_transform_scopes: FxHashMap<ExpnId, Module<'a>>,
unused_macros: NodeMap<Span>,
@ -1152,9 +1151,6 @@ impl<'a> Resolver<'a> {
let mut invocation_parent_scopes = FxHashMap::default();
invocation_parent_scopes.insert(ExpnId::root(), ParentScope::module(graph_root));
let mut macro_defs = FxHashMap::default();
macro_defs.insert(ExpnId::root(), root_def_id);
let features = session.features_untracked();
let non_macro_attr =
|mark_used| Lrc::new(SyntaxExtension::non_macro_attr(mark_used, session.edition()));
@ -1229,7 +1225,6 @@ impl<'a> Resolver<'a> {
invocation_parent_scopes,
output_macro_rules_scopes: Default::default(),
helper_attrs: Default::default(),
macro_defs,
local_macro_def_scopes: FxHashMap::default(),
name_already_seen: FxHashMap::default(),
potentially_unused_imports: Vec::new(),
@ -1335,8 +1330,8 @@ impl<'a> Resolver<'a> {
fn macro_def(&self, mut ctxt: SyntaxContext) -> DefId {
loop {
match self.macro_defs.get(&ctxt.outer_expn()) {
Some(&def_id) => return def_id,
match ctxt.outer_expn().expn_data().macro_def_id {
Some(def_id) => return def_id,
None => ctxt.remove_mark(),
};
}
@ -1820,7 +1815,7 @@ impl<'a> Resolver<'a> {
&& module.expansion.is_descendant_of(parent.expansion)
{
// The macro is a proc macro derive
if let Some(&def_id) = self.macro_defs.get(&module.expansion) {
if let Some(def_id) = module.expansion.expn_data().macro_def_id {
if let Some(ext) = self.get_macro_by_def_id(def_id) {
if !ext.is_builtin && ext.macro_kind() == MacroKind::Derive {
if parent.expansion.outer_expn_is_descendant_of(span.ctxt()) {

View file

@ -186,6 +186,7 @@ impl<'a> base::Resolver for Resolver<'a> {
call_site,
self.session.edition(),
features.into(),
None,
)));
let parent_scope = if let Some(module_id) = parent_module_id {
@ -290,13 +291,17 @@ impl<'a> base::Resolver for Resolver<'a> {
let (ext, res) = self.smart_resolve_macro_path(path, kind, parent_scope, force)?;
let span = invoc.span();
invoc_id.set_expn_data(ext.expn_data(parent_scope.expansion, span, fast_print_path(path)));
invoc_id.set_expn_data(ext.expn_data(
parent_scope.expansion,
span,
fast_print_path(path),
res.opt_def_id(),
));
if let Res::Def(_, def_id) = res {
if let Res::Def(_, _) = res {
if after_derive {
self.session.span_err(span, "macro attributes must be placed before `#[derive]`");
}
self.macro_defs.insert(invoc_id, def_id);
let normal_module_def_id = self.macro_def_scope(invoc_id).normal_ancestor_id;
self.definitions.add_parent_module_of_macro_def(invoc_id, normal_module_def_id);
}

View file

@ -25,6 +25,7 @@
// because getting it wrong can lead to nested `HygieneData::with` calls that
// trigger runtime aborts. (Fortunately these are obvious and easy to fix.)
use crate::def_id::{DefId, CRATE_DEF_INDEX};
use crate::edition::Edition;
use crate::symbol::{kw, sym, Symbol};
use crate::GLOBALS;
@ -155,7 +156,12 @@ crate struct HygieneData {
impl HygieneData {
crate fn new(edition: Edition) -> Self {
HygieneData {
expn_data: vec![Some(ExpnData::default(ExpnKind::Root, DUMMY_SP, edition))],
expn_data: vec![Some(ExpnData::default(
ExpnKind::Root,
DUMMY_SP,
edition,
Some(DefId::local(CRATE_DEF_INDEX)),
))],
syntax_context_data: vec![SyntaxContextData {
outer_expn: ExpnId::root(),
outer_transparency: Transparency::Opaque,
@ -673,11 +679,19 @@ pub struct ExpnData {
pub local_inner_macros: bool,
/// Edition of the crate in which the macro is defined.
pub edition: Edition,
/// The `DefId` of the macro being invoked,
/// if this `ExpnData` corresponds to a macro invocation
pub macro_def_id: Option<DefId>,
}
impl ExpnData {
/// Constructs expansion data with default properties.
pub fn default(kind: ExpnKind, call_site: Span, edition: Edition) -> ExpnData {
pub fn default(
kind: ExpnKind,
call_site: Span,
edition: Edition,
macro_def_id: Option<DefId>,
) -> ExpnData {
ExpnData {
kind,
parent: ExpnId::root(),
@ -687,6 +701,7 @@ impl ExpnData {
allow_internal_unsafe: false,
local_inner_macros: false,
edition,
macro_def_id,
}
}
@ -695,10 +710,11 @@ impl ExpnData {
call_site: Span,
edition: Edition,
allow_internal_unstable: Lrc<[Symbol]>,
macro_def_id: Option<DefId>,
) -> ExpnData {
ExpnData {
allow_internal_unstable: Some(allow_internal_unstable),
..ExpnData::default(kind, call_site, edition)
..ExpnData::default(kind, call_site, edition, macro_def_id)
}
}