Skip to content

Commit

Permalink
Auto merge of #86694 - cjgillot:pmmd, r=petrochenkov
Browse files Browse the repository at this point in the history
Store macro parent module in ExpnData.

As a consequence, its value is hashed as part of the ExpnId's stable hash.

Closes #85999
  • Loading branch information
bors committed Jul 6, 2021
2 parents 9a27044 + 3162c37 commit d7901f3
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 40 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ impl SyntaxExtension {
call_site: Span,
descr: Symbol,
macro_def_id: Option<DefId>,
parent_module: Option<DefId>,
) -> ExpnData {
use SyntaxExtensionKind::*;
let proc_macro = match self.kind {
Expand All @@ -828,6 +829,7 @@ impl SyntaxExtension {
self.local_inner_macros,
self.edition,
macro_def_id,
parent_module,
)
}
}
Expand Down
16 changes: 1 addition & 15 deletions compiler/rustc_hir/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
//! expressions) that are mostly just leftovers.

pub use crate::def_id::DefPathHash;
use crate::def_id::{
CrateNum, DefId, DefIndex, LocalDefId, StableCrateId, CRATE_DEF_INDEX, LOCAL_CRATE,
};
use crate::def_id::{CrateNum, DefIndex, LocalDefId, StableCrateId, CRATE_DEF_INDEX, LOCAL_CRATE};
use crate::hir;

use rustc_data_structures::fx::FxHashMap;
Expand Down Expand Up @@ -108,9 +106,6 @@ pub struct Definitions {
/// The reverse mapping of `def_id_to_hir_id`.
pub(super) hir_id_to_def_id: FxHashMap<hir::HirId, LocalDefId>,

/// If `ExpnId` is an ID of some macro expansion,
/// then `DefId` is the normal module (`mod`) in which the expanded macro was defined.
parent_modules_of_macro_defs: FxHashMap<ExpnId, DefId>,
/// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
expansions_that_defined: FxHashMap<LocalDefId, ExpnId>,
}
Expand Down Expand Up @@ -353,7 +348,6 @@ impl Definitions {
def_id_to_hir_id: Default::default(),
hir_id_to_def_id: Default::default(),
expansions_that_defined: Default::default(),
parent_modules_of_macro_defs: Default::default(),
}
}

Expand Down Expand Up @@ -420,14 +414,6 @@ impl Definitions {
self.expansions_that_defined.get(&id).copied().unwrap_or_else(ExpnId::root)
}

pub fn parent_module_of_macro_def(&self, expn_id: ExpnId) -> DefId {
self.parent_modules_of_macro_defs[&expn_id]
}

pub fn add_parent_module_of_macro_def(&mut self, expn_id: ExpnId, module: DefId) {
self.parent_modules_of_macro_defs.insert(expn_id, module);
}

pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
self.def_id_to_hir_id.iter_enumerated().map(|(k, _)| k)
}
Expand Down
12 changes: 5 additions & 7 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1902,13 +1902,11 @@ impl<'tcx> TyCtxt<'tcx> {
scope: DefId,
block: hir::HirId,
) -> (Ident, DefId) {
let scope =
match ident.span.normalize_to_macros_2_0_and_adjust(self.expn_that_defined(scope)) {
Some(actual_expansion) => {
self.hir().definitions().parent_module_of_macro_def(actual_expansion)
}
None => self.parent_module(block).to_def_id(),
};
let scope = ident
.span
.normalize_to_macros_2_0_and_adjust(self.expn_that_defined(scope))
.and_then(|actual_expansion| actual_expansion.expn_data().parent_module)
.unwrap_or_else(|| self.parent_module(block).to_def_id());
(ident, scope)
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir/src/transform/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> {

fn visit_span(&mut self, span: &mut Span) {
let mut expn_data =
ExpnData::default(ExpnKind::Inlined, *span, self.tcx.sess.edition(), None);
ExpnData::default(ExpnKind::Inlined, *span, self.tcx.sess.edition(), None, None);
expn_data.def_site = self.body_span;
// Make sure that all spans track the fact that they were inlined.
*span = self.callsite_span.fresh_expansion(expn_data);
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ impl<'a> Resolver<'a> {
Some(def_id) => def_id,
None => return self.ast_transform_scopes.get(&expn_id).unwrap_or(&self.graph_root),
};
self.macro_def_scope_from_def_id(def_id)
}

crate fn macro_def_scope_from_def_id(&mut self, def_id: DefId) -> Module<'a> {
if let Some(id) = def_id.as_local() {
self.local_macro_def_scopes[&id]
} else {
Expand Down
24 changes: 9 additions & 15 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use rustc_expand::compile_declarative_macro;
use rustc_expand::expand::{AstFragment, Invocation, InvocationKind, SupportsMacroExpansion};
use rustc_feature::is_builtin_attr_name;
use rustc_hir::def::{self, DefKind, NonMacroAttrKind};
use rustc_hir::def_id::{self, CrateNum};
use rustc_hir::def_id::{CrateNum, LocalDefId};
use rustc_hir::PrimTy;
use rustc_middle::middle::stability;
use rustc_middle::ty;
Expand Down Expand Up @@ -217,26 +217,20 @@ impl<'a> ResolverExpand for Resolver<'a> {
features: &[Symbol],
parent_module_id: Option<NodeId>,
) -> ExpnId {
let parent_module = parent_module_id.map(|module_id| self.local_def_id(module_id));
let expn_id = ExpnId::fresh(Some(ExpnData::allow_unstable(
ExpnKind::AstPass(pass),
call_site,
self.session.edition(),
features.into(),
None,
parent_module.map(LocalDefId::to_def_id),
)));

let parent_scope = if let Some(module_id) = parent_module_id {
let parent_def_id = self.local_def_id(module_id);
self.definitions.add_parent_module_of_macro_def(expn_id, parent_def_id.to_def_id());
self.module_map[&parent_def_id]
} else {
self.definitions.add_parent_module_of_macro_def(
expn_id,
def_id::DefId::local(def_id::CRATE_DEF_INDEX),
);
self.empty_module
};
let parent_scope = parent_module
.map_or(self.empty_module, |parent_def_id| self.module_map[&parent_def_id]);
self.ast_transform_scopes.insert(expn_id, parent_scope);

expn_id
}

Expand Down Expand Up @@ -298,12 +292,12 @@ impl<'a> ResolverExpand for Resolver<'a> {
span,
fast_print_path(path),
res.opt_def_id(),
res.opt_def_id().map(|macro_def_id| {
self.macro_def_scope_from_def_id(macro_def_id).nearest_parent_mod
}),
));

if let Res::Def(_, _) = res {
let normal_module_def_id = self.macro_def_scope(invoc_id).nearest_parent_mod;
self.definitions.add_parent_module_of_macro_def(invoc_id, normal_module_def_id);

// Gate macro attributes in `#[derive]` output.
if !self.session.features_untracked().macro_attributes_in_derive_output
&& kind == MacroKind::Attr
Expand Down
12 changes: 10 additions & 2 deletions compiler/rustc_span/src/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ impl HygieneData {
DUMMY_SP,
edition,
Some(DefId::local(CRATE_DEF_INDEX)),
None,
);
root_data.orig_id = Some(0);

Expand Down Expand Up @@ -687,7 +688,7 @@ impl Span {
) -> Span {
self.fresh_expansion(ExpnData {
allow_internal_unstable,
..ExpnData::default(ExpnKind::Desugaring(reason), self, edition, None)
..ExpnData::default(ExpnKind::Desugaring(reason), self, edition, None, None)
})
}
}
Expand Down Expand Up @@ -734,6 +735,8 @@ pub struct ExpnData {
/// The `DefId` of the macro being invoked,
/// if this `ExpnData` corresponds to a macro invocation
pub macro_def_id: Option<DefId>,
/// The normal module (`mod`) in which the expanded macro was defined.
pub parent_module: Option<DefId>,
/// The crate that originally created this `ExpnData`. During
/// metadata serialization, we only encode `ExpnData`s that were
/// created locally - when our serialized metadata is decoded,
Expand Down Expand Up @@ -777,6 +780,7 @@ impl ExpnData {
local_inner_macros: bool,
edition: Edition,
macro_def_id: Option<DefId>,
parent_module: Option<DefId>,
) -> ExpnData {
ExpnData {
kind,
Expand All @@ -788,6 +792,7 @@ impl ExpnData {
local_inner_macros,
edition,
macro_def_id,
parent_module,
krate: LOCAL_CRATE,
orig_id: None,
disambiguator: 0,
Expand All @@ -800,6 +805,7 @@ impl ExpnData {
call_site: Span,
edition: Edition,
macro_def_id: Option<DefId>,
parent_module: Option<DefId>,
) -> ExpnData {
ExpnData {
kind,
Expand All @@ -811,6 +817,7 @@ impl ExpnData {
local_inner_macros: false,
edition,
macro_def_id,
parent_module,
krate: LOCAL_CRATE,
orig_id: None,
disambiguator: 0,
Expand All @@ -823,10 +830,11 @@ impl ExpnData {
edition: Edition,
allow_internal_unstable: Lrc<[Symbol]>,
macro_def_id: Option<DefId>,
parent_module: Option<DefId>,
) -> ExpnData {
ExpnData {
allow_internal_unstable: Some(allow_internal_unstable),
..ExpnData::default(kind, call_site, edition, macro_def_id)
..ExpnData::default(kind, call_site, edition, macro_def_id, parent_module)
}
}

Expand Down

0 comments on commit d7901f3

Please sign in to comment.