Make legacy macros private

This commit is contained in:
Aleksey Kladov 2019-12-20 17:09:13 +01:00
parent af42cb5981
commit 7a862f0d47
4 changed files with 24 additions and 19 deletions

View File

@ -23,7 +23,7 @@ pub struct ItemScope {
/// Module scoped macros will be inserted into `items` instead of here.
// FIXME: Macro shadowing in one module is not properly handled. Non-item place macros will
// be all resolved to the last one defined if shadowing happens.
pub(crate) legacy_macros: FxHashMap<Name, MacroDefId>,
legacy_macros: FxHashMap<Name, MacroDefId>,
}
static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| {
@ -108,6 +108,10 @@ impl ItemScope {
self.impls.push(imp)
}
pub(crate) fn define_legacy_macro(&mut self, name: Name, mac: MacroDefId) {
self.legacy_macros.insert(name, mac);
}
pub(crate) fn push_res(
&mut self,
name: Name,
@ -146,6 +150,10 @@ impl ItemScope {
pub(crate) fn collect_resolutions(&self) -> Vec<(Name, Resolution)> {
self.items.iter().map(|(name, res)| (name.clone(), res.clone())).collect()
}
pub(crate) fn collect_legacy_macros(&self) -> FxHashMap<Name, MacroDefId> {
self.legacy_macros.clone()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]

View File

@ -231,9 +231,9 @@ where
/// the definition of current module.
/// And also, `macro_use` on a module will import all legacy macros visible inside to
/// current legacy scope, with possible shadowing.
fn define_legacy_macro(&mut self, module_id: LocalModuleId, name: Name, macro_: MacroDefId) {
fn define_legacy_macro(&mut self, module_id: LocalModuleId, name: Name, mac: MacroDefId) {
// Always shadowing
self.def_map.modules[module_id].scope.legacy_macros.insert(name, macro_);
self.def_map.modules[module_id].scope.define_legacy_macro(name, mac);
}
/// Import macros from `#[macro_use] extern crate`.
@ -711,7 +711,9 @@ where
let res = modules.alloc(ModuleData::default());
modules[res].parent = Some(self.module_id);
modules[res].origin = ModuleOrigin::not_sure_file(definition, declaration);
modules[res].scope.legacy_macros = modules[self.module_id].scope.legacy_macros.clone();
for (name, mac) in modules[self.module_id].scope.collect_legacy_macros() {
modules[res].scope.define_legacy_macro(name, mac)
}
modules[self.module_id].children.insert(name.clone(), res);
let resolution = Resolution {
def: PerNs::types(
@ -875,7 +877,7 @@ where
}
fn import_all_legacy_macros(&mut self, module_id: LocalModuleId) {
let macros = self.def_collector.def_map[module_id].scope.legacy_macros.clone();
let macros = self.def_collector.def_map[module_id].scope.collect_legacy_macros();
for (name, macro_) in macros {
self.def_collector.define_legacy_macro(self.module_id, name.clone(), macro_);
}

View File

@ -32,27 +32,22 @@ fn render_crate_def_map(map: &CrateDefMap) -> String {
*buf += path;
*buf += "\n";
let mut entries = map.modules[module]
.scope
.items
.iter()
.map(|(name, res)| (name, res.def))
.collect::<Vec<_>>();
entries.sort_by_key(|(name, _)| *name);
let mut entries = map.modules[module].scope.collect_resolutions();
entries.sort_by_key(|(name, _)| name.clone());
for (name, res) in entries {
*buf += &format!("{}:", name);
if res.types.is_some() {
if res.def.types.is_some() {
*buf += " t";
}
if res.values.is_some() {
if res.def.values.is_some() {
*buf += " v";
}
if res.macros.is_some() {
if res.def.macros.is_some() {
*buf += " m";
}
if res.is_none() {
if res.def.is_none() {
*buf += " _";
}
@ -587,6 +582,6 @@ mod b {
T: v
crate::a
T: t v
T: t v
"###);
}

View File

@ -116,7 +116,7 @@ fn typing_inside_a_macro_should_not_invalidate_def_map() {
let events = db.log_executed(|| {
let crate_def_map = db.crate_def_map(krate);
let (_, module_data) = crate_def_map.modules.iter().last().unwrap();
assert_eq!(module_data.scope.items.len(), 1);
assert_eq!(module_data.scope.collect_resolutions().len(), 1);
});
assert!(format!("{:?}", events).contains("crate_def_map"), "{:#?}", events)
}
@ -126,7 +126,7 @@ fn typing_inside_a_macro_should_not_invalidate_def_map() {
let events = db.log_executed(|| {
let crate_def_map = db.crate_def_map(krate);
let (_, module_data) = crate_def_map.modules.iter().last().unwrap();
assert_eq!(module_data.scope.items.len(), 1);
assert_eq!(module_data.scope.collect_resolutions().len(), 1);
});
assert!(!format!("{:?}", events).contains("crate_def_map"), "{:#?}", events)
}