Only store derive<->derive-helper mapping in DefMap

This commit is contained in:
Lukas Wirth 2022-03-09 00:01:19 +01:00
parent eba90936c1
commit dbada38b61
3 changed files with 21 additions and 30 deletions

View file

@ -78,8 +78,6 @@ use crate::{
AstId, BlockId, BlockLoc, LocalModuleId, ModuleDefId, ModuleId,
};
use self::proc_macro::ProcMacroDef;
/// Contains the results of (early) name resolution.
///
/// A `DefMap` stores the module tree and the definitions that are in scope in every module after
@ -102,11 +100,8 @@ pub struct DefMap {
prelude: Option<ModuleId>,
extern_prelude: FxHashMap<Name, ModuleDefId>,
/// Side table with additional proc. macro info, for use by name resolution in downstream
/// crates.
///
/// (the primary purpose is to resolve derive helpers)
exported_proc_macros: FxHashMap<MacroDefId, ProcMacroDef>,
/// Side table for resolving derive helpers.
exported_derives: FxHashMap<MacroDefId, Box<[Name]>>,
/// Custom attributes registered with `#![register_attr]`.
registered_attrs: Vec<SmolStr>,
@ -275,7 +270,7 @@ impl DefMap {
edition,
recursion_limit: None,
extern_prelude: FxHashMap::default(),
exported_proc_macros: FxHashMap::default(),
exported_derives: FxHashMap::default(),
prelude: None,
root,
modules,
@ -452,7 +447,7 @@ impl DefMap {
// Exhaustive match to require handling new fields.
let Self {
_c: _,
exported_proc_macros,
exported_derives: exported_proc_macros,
extern_prelude,
diagnostics,
modules,

View file

@ -554,9 +554,9 @@ impl DefCollector<'_> {
id: ItemTreeId<item_tree::Function>,
module_id: ModuleId,
) {
let kind = def.kind.to_basedb_kind();
self.exports_proc_macros = true;
let kind = def.kind.to_basedb_kind();
let (expander, kind) = match self.proc_macros.iter().find(|(n, _)| n == &def.name) {
Some(&(_, expander)) => (expander, kind),
None => (ProcMacroExpander::dummy(self.def_map.krate), kind),
@ -565,9 +565,11 @@ impl DefCollector<'_> {
let proc_macro_id =
ProcMacroLoc { container: module_id, id, expander, kind }.intern(self.db);
self.define_proc_macro(def.name.clone(), proc_macro_id.into());
self.def_map
.exported_proc_macros
.insert(macro_id_to_def_id(self.db, proc_macro_id.into()), def);
if let ProcMacroKind::CustomDerive { helpers } = def.kind {
self.def_map
.exported_derives
.insert(macro_id_to_def_id(self.db, proc_macro_id.into()), helpers);
}
}
/// Define a macro with `macro_rules`.
@ -1301,13 +1303,11 @@ impl DefCollector<'_> {
if let MacroCallKind::Derive { ast_id, .. } = &loc.kind {
if loc.def.krate != self.def_map.krate {
let def_map = self.db.crate_def_map(loc.def.krate);
if let Some(def) = def_map.exported_proc_macros.get(&loc.def) {
if let ProcMacroKind::CustomDerive { helpers } = &def.kind {
self.derive_helpers_in_scope
.entry(ast_id.map(|it| it.upcast()))
.or_default()
.extend(helpers.iter().cloned());
}
if let Some(helpers) = def_map.exported_derives.get(&loc.def) {
self.derive_helpers_in_scope
.entry(ast_id.map(|it| it.upcast()))
.or_default()
.extend(helpers.iter().cloned());
}
}
}

View file

@ -1,7 +1,5 @@
use super::*;
use crate::nameres::proc_macro::{ProcMacroDef, ProcMacroKind};
#[test]
fn macro_rules_are_globally_visible() {
check(
@ -978,14 +976,12 @@ fn collects_derive_helpers() {
",
);
assert_eq!(def_map.exported_proc_macros.len(), 1);
match def_map.exported_proc_macros.values().next() {
Some(ProcMacroDef { kind: ProcMacroKind::CustomDerive { helpers }, .. }) => {
match &**helpers {
[attr] => assert_eq!(attr.to_string(), "helper_attr"),
_ => unreachable!(),
}
}
assert_eq!(def_map.exported_derives.len(), 1);
match def_map.exported_derives.values().next() {
Some(helpers) => match &**helpers {
[attr] => assert_eq!(attr.to_string(), "helper_attr"),
_ => unreachable!(),
},
_ => unreachable!(),
}
}