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

View File

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

View File

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