Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove macro_defs map #72284

Merged
merged 1 commit into from
May 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc_ast_lowering/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

Expand Down
10 changes: 9 additions & 1 deletion src/librustc_expand/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/librustc_expand/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 3 additions & 8 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
};
}
Expand Down Expand Up @@ -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()) {
Expand Down
13 changes: 9 additions & 4 deletions src/librustc_resolve/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)));
Aaron1011 marked this conversation as resolved.
Show resolved Hide resolved

if let Res::Def(_, def_id) = res {
invoc_id.set_expn_data(ext.expn_data(
parent_scope.expansion,
span,
fast_print_path(path),
res.opt_def_id(),
));

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);
}
Expand Down
22 changes: 19 additions & 3 deletions src/librustc_span/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand All @@ -687,6 +701,7 @@ impl ExpnData {
allow_internal_unsafe: false,
local_inner_macros: false,
edition,
macro_def_id,
}
}

Expand All @@ -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)
}
}

Expand Down