Rename MacroDef to Macro

This commit is contained in:
Lukas Wirth 2022-03-08 23:51:19 +01:00
parent 42d46325a1
commit ad54ee2939
6 changed files with 57 additions and 31 deletions

View file

@ -5,7 +5,7 @@ use std::{fmt, hash::Hash, ops, sync::Arc};
use base_db::CrateId;
use cfg::{CfgExpr, CfgOptions};
use either::Either;
use hir_expand::{hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile};
use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile};
use itertools::Itertools;
use la_arena::ArenaMap;
use mbe::{syntax_node_to_token_tree, DelimiterKind, Punct};
@ -24,7 +24,7 @@ use crate::{
path::{ModPath, PathKind},
src::{HasChildSource, HasSource},
AdtId, AttrDefId, EnumId, GenericParamId, HasModule, LocalEnumVariantId, LocalFieldId, Lookup,
VariantId,
MacroId, VariantId,
};
/// Holds documentation
@ -358,9 +358,11 @@ impl AttrsWithOwner {
AdtId::UnionId(it) => attrs_from_item_tree(it.lookup(db).id, db),
},
AttrDefId::TraitId(it) => attrs_from_item_tree(it.lookup(db).id, db),
AttrDefId::MacroDefId(it) => it
.ast_id()
.either(|ast_id| attrs_from_ast(ast_id, db), |ast_id| attrs_from_ast(ast_id, db)),
AttrDefId::MacroId(it) => match it {
MacroId::Macro2Id(it) => attrs_from_item_tree(it.lookup(db).id, db),
MacroId::MacroRulesId(it) => attrs_from_item_tree(it.lookup(db).id, db),
MacroId::ProcMacroId(it) => attrs_from_item_tree(it.lookup(db).id, db),
},
AttrDefId::ImplId(it) => attrs_from_item_tree(it.lookup(db).id, db),
AttrDefId::ConstId(it) => attrs_from_item_tree(it.lookup(db).id, db),
AttrDefId::StaticId(it) => attrs_from_item_tree(it.lookup(db).id, db),
@ -461,10 +463,11 @@ impl AttrsWithOwner {
AttrDefId::ConstId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
AttrDefId::TraitId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
AttrDefId::TypeAliasId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
AttrDefId::MacroDefId(id) => id.ast_id().either(
|it| it.with_value(ast::AnyHasAttrs::new(it.to_node(db.upcast()))),
|it| it.with_value(ast::AnyHasAttrs::new(it.to_node(db.upcast()))),
),
AttrDefId::MacroId(id) => match id {
MacroId::Macro2Id(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
MacroId::MacroRulesId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
MacroId::ProcMacroId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
},
AttrDefId::ImplId(id) => id.lookup(db).source(db).map(ast::AnyHasAttrs::new),
AttrDefId::GenericParamId(id) => match id {
GenericParamId::ConstParamId(id) => {
@ -845,14 +848,6 @@ impl<'attr> AttrQuery<'attr> {
}
}
fn attrs_from_ast<N>(src: AstId<N>, db: &dyn DefDatabase) -> RawAttrs
where
N: ast::HasAttrs,
{
let src = InFile::new(src.file_id, src.to_node(db.upcast()));
RawAttrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn ast::HasAttrs))
}
fn attrs_from_item_tree<N: ItemTreeNode>(id: ItemTreeId<N>, db: &dyn DefDatabase) -> RawAttrs {
let tree = id.item_tree(db);
let mod_item = N::id_to_mod_item(id.value);

View file

@ -102,6 +102,12 @@ impl ChildBySource for ItemScope {
res[keys::ATTR_MACRO_CALL].insert(ast_id.to_node(db.upcast()), call_id);
},
);
self.legacy_macros().for_each(|(_, id)| {
let loc = id.lookup(db);
if loc.id.file_id() == file_id {
res[keys::MACRO_RULES].insert(loc.source(db).value, id);
}
});
self.derive_macro_invocs().filter(|(id, _)| id.file_id == file_id).for_each(
|(ast_id, calls)| {
let adt = ast_id.to_node(db.upcast());
@ -145,7 +151,9 @@ impl ChildBySource for ItemScope {
MacroId::MacroRulesId(id) => insert!(map[keys::MACRO_RULES].insert(id)),
MacroId::ProcMacroId(id) => insert!(map[keys::PROC_MACRO].insert(id)),
},
_ => (),
ModuleDefId::ModuleId(_)
| ModuleDefId::EnumVariantId(_)
| ModuleDefId::BuiltinType(_) => (),
}
}
fn add_impl(db: &dyn DefDatabase, map: &mut DynMap, file_id: HirFileId, imp: ImplId) {

View file

@ -1069,9 +1069,9 @@ mod tests {
Query::new("".to_string()).limit(2),
expect![[r#"
dep::fmt (t)
dep::Fmt (m)
dep::Fmt (t)
dep::Fmt (v)
dep::Fmt (m)
"#]],
);
}

View file

@ -538,7 +538,7 @@ pub enum AttrDefId {
ConstId(ConstId),
TraitId(TraitId),
TypeAliasId(TypeAliasId),
MacroDefId(MacroDefId),
MacroId(MacroId),
ImplId(ImplId),
GenericParamId(GenericParamId),
ExternBlockId(ExternBlockId),
@ -554,7 +554,7 @@ impl_from!(
FunctionId,
TraitId,
TypeAliasId,
MacroDefId,
MacroId,
ImplId,
GenericParamId
for AttrDefId
@ -757,9 +757,7 @@ impl AttrDefId {
.module(db)
.krate
}
// FIXME: `MacroDefId` should store the defining module, then this can implement
// `HasModule`
AttrDefId::MacroDefId(it) => it.krate,
AttrDefId::MacroId(it) => it.module(db).krate,
}
}
}
@ -856,7 +854,7 @@ fn macro_call_as_call_id(
Ok(res)
}
fn macro_id_to_def_id(db: &dyn db::DefDatabase, id: MacroId) -> MacroDefId {
pub fn macro_id_to_def_id(db: &dyn db::DefDatabase, id: MacroId) -> MacroDefId {
match id {
MacroId::Macro2Id(it) => {
let loc = it.lookup(db);

View file

@ -105,7 +105,7 @@ pub struct DefMap {
/// Side table with additional proc. macro info, for use by name resolution in downstream
/// crates.
///
/// (the primary purpose is to resolve derive helpers and fetch a proc-macros name)
/// (the primary purpose is to resolve derive helpers)
exported_proc_macros: FxHashMap<MacroDefId, ProcMacroDef>,
/// Custom attributes registered with `#![register_attr]`.
@ -295,9 +295,6 @@ impl DefMap {
pub fn modules(&self) -> impl Iterator<Item = (LocalModuleId, &ModuleData)> + '_ {
self.modules.iter()
}
pub fn exported_proc_macros(&self) -> impl Iterator<Item = (MacroDefId, Name)> + '_ {
self.exported_proc_macros.iter().map(|(id, def)| (*id, def.name.clone()))
}
pub fn registered_tools(&self) -> &[SmolStr] {
&self.registered_tools
}

View file

@ -21,8 +21,8 @@ use crate::{
visibility::{RawVisibility, Visibility},
AdtId, AssocItemId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, ExternBlockId,
FunctionId, GenericDefId, GenericParamId, HasModule, ImplId, ItemContainerId, LifetimeParamId,
LocalModuleId, Lookup, MacroId, ModuleDefId, ModuleId, StaticId, StructId, TraitId,
TypeAliasId, TypeOrConstParamId, TypeParamId, VariantId,
LocalModuleId, Lookup, Macro2Id, MacroId, MacroRulesId, ModuleDefId, ModuleId, ProcMacroId,
StaticId, StructId, TraitId, TypeAliasId, TypeOrConstParamId, TypeParamId, VariantId,
};
#[derive(Debug, Clone, Default)]
@ -864,3 +864,31 @@ impl HasResolver for VariantId {
}
}
}
impl HasResolver for MacroId {
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
match self {
MacroId::Macro2Id(it) => it.resolver(db),
MacroId::MacroRulesId(it) => it.resolver(db),
MacroId::ProcMacroId(it) => it.resolver(db),
}
}
}
impl HasResolver for Macro2Id {
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
self.lookup(db).container.resolver(db)
}
}
impl HasResolver for ProcMacroId {
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
self.lookup(db).container.resolver(db)
}
}
impl HasResolver for MacroRulesId {
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
self.lookup(db).container.resolver(db)
}
}