mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-28 02:57:37 +00:00
Store macro parent module in ExpnData.
This commit is contained in:
parent
969a6c2481
commit
3162c37b59
@ -809,6 +809,7 @@ impl SyntaxExtension {
|
|||||||
call_site: Span,
|
call_site: Span,
|
||||||
descr: Symbol,
|
descr: Symbol,
|
||||||
macro_def_id: Option<DefId>,
|
macro_def_id: Option<DefId>,
|
||||||
|
parent_module: Option<DefId>,
|
||||||
) -> ExpnData {
|
) -> ExpnData {
|
||||||
use SyntaxExtensionKind::*;
|
use SyntaxExtensionKind::*;
|
||||||
let proc_macro = match self.kind {
|
let proc_macro = match self.kind {
|
||||||
@ -828,6 +829,7 @@ impl SyntaxExtension {
|
|||||||
self.local_inner_macros,
|
self.local_inner_macros,
|
||||||
self.edition,
|
self.edition,
|
||||||
macro_def_id,
|
macro_def_id,
|
||||||
|
parent_module,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,7 @@
|
|||||||
//! expressions) that are mostly just leftovers.
|
//! expressions) that are mostly just leftovers.
|
||||||
|
|
||||||
pub use crate::def_id::DefPathHash;
|
pub use crate::def_id::DefPathHash;
|
||||||
use crate::def_id::{
|
use crate::def_id::{CrateNum, DefIndex, LocalDefId, StableCrateId, CRATE_DEF_INDEX, LOCAL_CRATE};
|
||||||
CrateNum, DefId, DefIndex, LocalDefId, StableCrateId, CRATE_DEF_INDEX, LOCAL_CRATE,
|
|
||||||
};
|
|
||||||
use crate::hir;
|
use crate::hir;
|
||||||
|
|
||||||
use rustc_data_structures::fx::FxHashMap;
|
use rustc_data_structures::fx::FxHashMap;
|
||||||
@ -108,9 +106,6 @@ pub struct Definitions {
|
|||||||
/// The reverse mapping of `def_id_to_hir_id`.
|
/// The reverse mapping of `def_id_to_hir_id`.
|
||||||
pub(super) hir_id_to_def_id: FxHashMap<hir::HirId, LocalDefId>,
|
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`.
|
/// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
|
||||||
expansions_that_defined: FxHashMap<LocalDefId, ExpnId>,
|
expansions_that_defined: FxHashMap<LocalDefId, ExpnId>,
|
||||||
}
|
}
|
||||||
@ -353,7 +348,6 @@ impl Definitions {
|
|||||||
def_id_to_hir_id: Default::default(),
|
def_id_to_hir_id: Default::default(),
|
||||||
hir_id_to_def_id: Default::default(),
|
hir_id_to_def_id: Default::default(),
|
||||||
expansions_that_defined: Default::default(),
|
expansions_that_defined: Default::default(),
|
||||||
parent_modules_of_macro_defs: Default::default(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -420,14 +414,6 @@ impl Definitions {
|
|||||||
self.expansions_that_defined.get(&id).copied().unwrap_or_else(ExpnId::root)
|
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> + '_ {
|
pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
|
||||||
self.def_id_to_hir_id.iter_enumerated().map(|(k, _)| k)
|
self.def_id_to_hir_id.iter_enumerated().map(|(k, _)| k)
|
||||||
}
|
}
|
||||||
|
@ -1902,13 +1902,11 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||||||
scope: DefId,
|
scope: DefId,
|
||||||
block: hir::HirId,
|
block: hir::HirId,
|
||||||
) -> (Ident, DefId) {
|
) -> (Ident, DefId) {
|
||||||
let scope =
|
let scope = ident
|
||||||
match ident.span.normalize_to_macros_2_0_and_adjust(self.expn_that_defined(scope)) {
|
.span
|
||||||
Some(actual_expansion) => {
|
.normalize_to_macros_2_0_and_adjust(self.expn_that_defined(scope))
|
||||||
self.hir().definitions().parent_module_of_macro_def(actual_expansion)
|
.and_then(|actual_expansion| actual_expansion.expn_data().parent_module)
|
||||||
}
|
.unwrap_or_else(|| self.parent_module(block).to_def_id());
|
||||||
None => self.parent_module(block).to_def_id(),
|
|
||||||
};
|
|
||||||
(ident, scope)
|
(ident, scope)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -836,7 +836,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> {
|
|||||||
|
|
||||||
fn visit_span(&mut self, span: &mut Span) {
|
fn visit_span(&mut self, span: &mut Span) {
|
||||||
let mut expn_data =
|
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;
|
expn_data.def_site = self.body_span;
|
||||||
// Make sure that all spans track the fact that they were inlined.
|
// Make sure that all spans track the fact that they were inlined.
|
||||||
*span = self.callsite_span.fresh_expansion(expn_data);
|
*span = self.callsite_span.fresh_expansion(expn_data);
|
||||||
|
@ -159,6 +159,10 @@ impl<'a> Resolver<'a> {
|
|||||||
Some(def_id) => def_id,
|
Some(def_id) => def_id,
|
||||||
None => return self.ast_transform_scopes.get(&expn_id).unwrap_or(&self.graph_root),
|
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() {
|
if let Some(id) = def_id.as_local() {
|
||||||
self.local_macro_def_scopes[&id]
|
self.local_macro_def_scopes[&id]
|
||||||
} else {
|
} else {
|
||||||
|
@ -20,7 +20,7 @@ use rustc_expand::compile_declarative_macro;
|
|||||||
use rustc_expand::expand::{AstFragment, Invocation, InvocationKind, SupportsMacroExpansion};
|
use rustc_expand::expand::{AstFragment, Invocation, InvocationKind, SupportsMacroExpansion};
|
||||||
use rustc_feature::is_builtin_attr_name;
|
use rustc_feature::is_builtin_attr_name;
|
||||||
use rustc_hir::def::{self, DefKind, NonMacroAttrKind};
|
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_hir::PrimTy;
|
||||||
use rustc_middle::middle::stability;
|
use rustc_middle::middle::stability;
|
||||||
use rustc_middle::ty;
|
use rustc_middle::ty;
|
||||||
@ -217,26 +217,20 @@ impl<'a> ResolverExpand for Resolver<'a> {
|
|||||||
features: &[Symbol],
|
features: &[Symbol],
|
||||||
parent_module_id: Option<NodeId>,
|
parent_module_id: Option<NodeId>,
|
||||||
) -> ExpnId {
|
) -> 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(
|
let expn_id = ExpnId::fresh(Some(ExpnData::allow_unstable(
|
||||||
ExpnKind::AstPass(pass),
|
ExpnKind::AstPass(pass),
|
||||||
call_site,
|
call_site,
|
||||||
self.session.edition(),
|
self.session.edition(),
|
||||||
features.into(),
|
features.into(),
|
||||||
None,
|
None,
|
||||||
|
parent_module.map(LocalDefId::to_def_id),
|
||||||
)));
|
)));
|
||||||
|
|
||||||
let parent_scope = if let Some(module_id) = parent_module_id {
|
let parent_scope = parent_module
|
||||||
let parent_def_id = self.local_def_id(module_id);
|
.map_or(self.empty_module, |parent_def_id| self.module_map[&parent_def_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
|
|
||||||
};
|
|
||||||
self.ast_transform_scopes.insert(expn_id, parent_scope);
|
self.ast_transform_scopes.insert(expn_id, parent_scope);
|
||||||
|
|
||||||
expn_id
|
expn_id
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -298,12 +292,12 @@ impl<'a> ResolverExpand for Resolver<'a> {
|
|||||||
span,
|
span,
|
||||||
fast_print_path(path),
|
fast_print_path(path),
|
||||||
res.opt_def_id(),
|
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 {
|
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.
|
// Gate macro attributes in `#[derive]` output.
|
||||||
if !self.session.features_untracked().macro_attributes_in_derive_output
|
if !self.session.features_untracked().macro_attributes_in_derive_output
|
||||||
&& kind == MacroKind::Attr
|
&& kind == MacroKind::Attr
|
||||||
|
@ -181,6 +181,7 @@ impl HygieneData {
|
|||||||
DUMMY_SP,
|
DUMMY_SP,
|
||||||
edition,
|
edition,
|
||||||
Some(DefId::local(CRATE_DEF_INDEX)),
|
Some(DefId::local(CRATE_DEF_INDEX)),
|
||||||
|
None,
|
||||||
);
|
);
|
||||||
root_data.orig_id = Some(0);
|
root_data.orig_id = Some(0);
|
||||||
|
|
||||||
@ -687,7 +688,7 @@ impl Span {
|
|||||||
) -> Span {
|
) -> Span {
|
||||||
self.fresh_expansion(ExpnData {
|
self.fresh_expansion(ExpnData {
|
||||||
allow_internal_unstable,
|
allow_internal_unstable,
|
||||||
..ExpnData::default(ExpnKind::Desugaring(reason), self, edition, None)
|
..ExpnData::default(ExpnKind::Desugaring(reason), self, edition, None, None)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -734,6 +735,8 @@ pub struct ExpnData {
|
|||||||
/// The `DefId` of the macro being invoked,
|
/// The `DefId` of the macro being invoked,
|
||||||
/// if this `ExpnData` corresponds to a macro invocation
|
/// if this `ExpnData` corresponds to a macro invocation
|
||||||
pub macro_def_id: Option<DefId>,
|
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
|
/// The crate that originally created this `ExpnData`. During
|
||||||
/// metadata serialization, we only encode `ExpnData`s that were
|
/// metadata serialization, we only encode `ExpnData`s that were
|
||||||
/// created locally - when our serialized metadata is decoded,
|
/// created locally - when our serialized metadata is decoded,
|
||||||
@ -777,6 +780,7 @@ impl ExpnData {
|
|||||||
local_inner_macros: bool,
|
local_inner_macros: bool,
|
||||||
edition: Edition,
|
edition: Edition,
|
||||||
macro_def_id: Option<DefId>,
|
macro_def_id: Option<DefId>,
|
||||||
|
parent_module: Option<DefId>,
|
||||||
) -> ExpnData {
|
) -> ExpnData {
|
||||||
ExpnData {
|
ExpnData {
|
||||||
kind,
|
kind,
|
||||||
@ -788,6 +792,7 @@ impl ExpnData {
|
|||||||
local_inner_macros,
|
local_inner_macros,
|
||||||
edition,
|
edition,
|
||||||
macro_def_id,
|
macro_def_id,
|
||||||
|
parent_module,
|
||||||
krate: LOCAL_CRATE,
|
krate: LOCAL_CRATE,
|
||||||
orig_id: None,
|
orig_id: None,
|
||||||
disambiguator: 0,
|
disambiguator: 0,
|
||||||
@ -800,6 +805,7 @@ impl ExpnData {
|
|||||||
call_site: Span,
|
call_site: Span,
|
||||||
edition: Edition,
|
edition: Edition,
|
||||||
macro_def_id: Option<DefId>,
|
macro_def_id: Option<DefId>,
|
||||||
|
parent_module: Option<DefId>,
|
||||||
) -> ExpnData {
|
) -> ExpnData {
|
||||||
ExpnData {
|
ExpnData {
|
||||||
kind,
|
kind,
|
||||||
@ -811,6 +817,7 @@ impl ExpnData {
|
|||||||
local_inner_macros: false,
|
local_inner_macros: false,
|
||||||
edition,
|
edition,
|
||||||
macro_def_id,
|
macro_def_id,
|
||||||
|
parent_module,
|
||||||
krate: LOCAL_CRATE,
|
krate: LOCAL_CRATE,
|
||||||
orig_id: None,
|
orig_id: None,
|
||||||
disambiguator: 0,
|
disambiguator: 0,
|
||||||
@ -823,10 +830,11 @@ impl ExpnData {
|
|||||||
edition: Edition,
|
edition: Edition,
|
||||||
allow_internal_unstable: Lrc<[Symbol]>,
|
allow_internal_unstable: Lrc<[Symbol]>,
|
||||||
macro_def_id: Option<DefId>,
|
macro_def_id: Option<DefId>,
|
||||||
|
parent_module: Option<DefId>,
|
||||||
) -> ExpnData {
|
) -> ExpnData {
|
||||||
ExpnData {
|
ExpnData {
|
||||||
allow_internal_unstable: Some(allow_internal_unstable),
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user