call_site: Span,
descr: Symbol,
macro_def_id: Option<DefId>,
+ parent_module: Option<DefId>,
) -> ExpnData {
use SyntaxExtensionKind::*;
let proc_macro = match self.kind {
self.local_inner_macros,
self.edition,
macro_def_id,
+ parent_module,
)
}
}
//! 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;
/// 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>,
}
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(),
}
}
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)
}
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)
}
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);
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 {
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;
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
}
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
DUMMY_SP,
edition,
Some(DefId::local(CRATE_DEF_INDEX)),
+ None,
);
root_data.orig_id = Some(0);
) -> 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)
})
}
}
/// 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,
local_inner_macros: bool,
edition: Edition,
macro_def_id: Option<DefId>,
+ parent_module: Option<DefId>,
) -> ExpnData {
ExpnData {
kind,
local_inner_macros,
edition,
macro_def_id,
+ parent_module,
krate: LOCAL_CRATE,
orig_id: None,
disambiguator: 0,
call_site: Span,
edition: Edition,
macro_def_id: Option<DefId>,
+ parent_module: Option<DefId>,
) -> ExpnData {
ExpnData {
kind,
local_inner_macros: false,
edition,
macro_def_id,
+ parent_module,
krate: LOCAL_CRATE,
orig_id: None,
disambiguator: 0,
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)
}
}