This commit is contained in:
Lukas Wirth 2022-03-09 11:26:06 +01:00
parent 054ab5fd9c
commit 2537ad0d9e
4 changed files with 34 additions and 41 deletions

View File

@ -35,7 +35,7 @@
use std::{collections::HashMap, iter, ops::ControlFlow, sync::Arc};
use arrayvec::ArrayVec;
use base_db::{CrateDisplayName, CrateId, CrateOrigin, Edition, FileId};
use base_db::{CrateDisplayName, CrateId, CrateOrigin, Edition, FileId, ProcMacroKind};
use either::Either;
use hir_def::{
adt::{ReprKind, VariantData},
@ -49,8 +49,8 @@
src::HasSource as _,
AdtId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, DefWithBodyId, EnumId,
FunctionId, GenericDefId, HasModule, ImplId, ItemContainerId, LifetimeParamId,
LocalEnumVariantId, LocalFieldId, Lookup, MacroId, ModuleId, StaticId, StructId, TraitId,
TypeAliasId, TypeOrConstParamId, TypeParamId, UnionId,
LocalEnumVariantId, LocalFieldId, Lookup, MacroExpander, MacroId, ModuleId, StaticId, StructId,
TraitId, TypeAliasId, TypeOrConstParamId, TypeParamId, UnionId,
};
use hir_expand::{name::name, MacroCallKind};
use hir_ty::{
@ -1395,7 +1395,7 @@ pub fn as_proc_macro(self, db: &dyn HirDatabase) -> Option<Macro> {
}
let loc = self.id.lookup(db.upcast());
let def_map = db.crate_def_map(loc.krate(db).into());
def_map.fn_as_proc_macro(loc.id).map(|id| Macro { id: id.into() })
def_map.fn_as_proc_macro(self.id).map(|id| Macro { id: id.into() })
}
/// A textual representation of the HIR of this function for debugging purposes.
@ -1768,23 +1768,21 @@ pub fn name(self, db: &dyn HirDatabase) -> Name {
pub fn kind(&self, db: &dyn HirDatabase) -> MacroKind {
match self.id {
MacroId::Macro2Id(it) => match it.lookup(db.upcast()).expander {
hir_def::MacroExpander::Declarative => MacroKind::Declarative,
hir_def::MacroExpander::BuiltIn(_) => MacroKind::BuiltIn,
hir_def::MacroExpander::BuiltInAttr(_) => MacroKind::Attr,
hir_def::MacroExpander::BuiltInDerive(_) => MacroKind::Derive,
hir_def::MacroExpander::BuiltInEager(_) => MacroKind::BuiltIn,
MacroExpander::Declarative => MacroKind::Declarative,
MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => MacroKind::BuiltIn,
MacroExpander::BuiltInAttr(_) => MacroKind::Attr,
MacroExpander::BuiltInDerive(_) => MacroKind::Derive,
},
MacroId::MacroRulesId(it) => match it.lookup(db.upcast()).expander {
hir_def::MacroExpander::Declarative => MacroKind::Declarative,
hir_def::MacroExpander::BuiltIn(_) => MacroKind::BuiltIn,
hir_def::MacroExpander::BuiltInAttr(_) => MacroKind::Attr,
hir_def::MacroExpander::BuiltInDerive(_) => MacroKind::Derive,
hir_def::MacroExpander::BuiltInEager(_) => MacroKind::BuiltIn,
MacroExpander::Declarative => MacroKind::Declarative,
MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => MacroKind::BuiltIn,
MacroExpander::BuiltInAttr(_) => MacroKind::Attr,
MacroExpander::BuiltInDerive(_) => MacroKind::Derive,
},
MacroId::ProcMacroId(it) => match it.lookup(db.upcast()).kind {
base_db::ProcMacroKind::CustomDerive => MacroKind::Derive,
base_db::ProcMacroKind::FuncLike => MacroKind::ProcMacro,
base_db::ProcMacroKind::Attr => MacroKind::Attr,
ProcMacroKind::CustomDerive => MacroKind::Derive,
ProcMacroKind::FuncLike => MacroKind::ProcMacro,
ProcMacroKind::Attr => MacroKind::Attr,
},
}
}
@ -1799,11 +1797,11 @@ pub fn is_fn_like(&self, db: &dyn HirDatabase) -> bool {
pub fn is_builtin_derive(&self, db: &dyn HirDatabase) -> bool {
match self.id {
MacroId::Macro2Id(it) => match it.lookup(db.upcast()).expander {
hir_def::MacroExpander::BuiltInDerive(_) => true,
MacroExpander::BuiltInDerive(_) => true,
_ => false,
},
MacroId::MacroRulesId(it) => match it.lookup(db.upcast()).expander {
hir_def::MacroExpander::BuiltInDerive(_) => true,
MacroExpander::BuiltInDerive(_) => true,
_ => false,
},
MacroId::ProcMacroId(_) => false,

View File

@ -554,7 +554,7 @@ pub enum AttrDefId {
FunctionId,
TraitId,
TypeAliasId,
MacroId,
MacroId(Macro2Id, MacroRulesId, ProcMacroId),
ImplId,
GenericParamId
for AttrDefId

View File

@ -70,12 +70,12 @@
use crate::{
db::DefDatabase,
item_scope::{BuiltinShadowMode, ItemScope},
item_tree::{self, ItemTreeId, TreeId},
item_tree::TreeId,
nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode},
path::ModPath,
per_ns::PerNs,
visibility::Visibility,
AstId, BlockId, BlockLoc, LocalModuleId, ModuleDefId, ModuleId, ProcMacroId,
AstId, BlockId, BlockLoc, FunctionId, LocalModuleId, ModuleDefId, ModuleId, ProcMacroId,
};
/// Contains the results of (early) name resolution.
@ -102,7 +102,7 @@ pub struct DefMap {
/// Side table for resolving derive helpers.
exported_derives: FxHashMap<MacroDefId, Box<[Name]>>,
fn_proc_macro_mapping: FxHashMap<ItemTreeId<item_tree::Function>, ProcMacroId>,
fn_proc_macro_mapping: FxHashMap<FunctionId, ProcMacroId>,
/// Custom attributes registered with `#![register_attr]`.
registered_attrs: Vec<SmolStr>,
@ -302,8 +302,7 @@ pub fn root(&self) -> LocalModuleId {
self.root
}
// FIXME: This is an odd interface....
pub fn fn_as_proc_macro(&self, id: ItemTreeId<item_tree::Function>) -> Option<ProcMacroId> {
pub fn fn_as_proc_macro(&self, id: FunctionId) -> Option<ProcMacroId> {
self.fn_proc_macro_mapping.get(&id).copied()
}
@ -454,7 +453,7 @@ fn shrink_to_fit(&mut self) {
// Exhaustive match to require handling new fields.
let Self {
_c: _,
exported_derives: exported_proc_macros,
exported_derives,
extern_prelude,
diagnostics,
modules,
@ -470,7 +469,7 @@ fn shrink_to_fit(&mut self) {
} = self;
extern_prelude.shrink_to_fit();
exported_proc_macros.shrink_to_fit();
exported_derives.shrink_to_fit();
diagnostics.shrink_to_fit();
modules.shrink_to_fit();
registered_attrs.shrink_to_fit();

View File

@ -46,10 +46,10 @@
path::{ImportAlias, ModPath, PathKind},
per_ns::PerNs,
visibility::{RawVisibility, Visibility},
AdtId, AstId, AstIdWithPath, ConstLoc, EnumLoc, EnumVariantId, ExternBlockLoc, FunctionLoc,
ImplLoc, Intern, ItemContainerId, LocalModuleId, Macro2Id, Macro2Loc, MacroExpander, MacroId,
MacroRulesId, MacroRulesLoc, ModuleDefId, ModuleId, ProcMacroId, ProcMacroLoc, StaticLoc,
StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, UnresolvedMacro,
AdtId, AstId, AstIdWithPath, ConstLoc, EnumLoc, EnumVariantId, ExternBlockLoc, FunctionId,
FunctionLoc, ImplLoc, Intern, ItemContainerId, LocalModuleId, Macro2Id, Macro2Loc,
MacroExpander, MacroId, MacroRulesId, MacroRulesLoc, ModuleDefId, ModuleId, ProcMacroId,
ProcMacroLoc, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, UnresolvedMacro,
};
static GLOB_RECURSION_LIMIT: Limit = Limit::new(100);
@ -552,6 +552,7 @@ fn export_proc_macro(
&mut self,
def: ProcMacroDef,
id: ItemTreeId<item_tree::Function>,
fn_id: FunctionId,
module_id: ModuleId,
) {
self.exports_proc_macros = true;
@ -570,7 +571,7 @@ fn export_proc_macro(
.exported_derives
.insert(macro_id_to_def_id(self.db, proc_macro_id.into()), helpers);
}
self.def_map.fn_proc_macro_mapping.insert(id, proc_macro_id);
self.def_map.fn_proc_macro_mapping.insert(fn_id, proc_macro_id);
}
/// Define a macro with `macro_rules`.
@ -1551,6 +1552,8 @@ fn collect(&mut self, items: &[ModItem], container: ItemContainerId) {
}
ModItem::Function(id) => {
let it = &self.item_tree[id];
let fn_id =
FunctionLoc { container, id: ItemTreeId::new(self.tree_id, id) }.intern(db);
let is_proc_macro = attrs.parse_proc_macro_decl(&it.name);
let vis = match is_proc_macro {
@ -1561,21 +1564,14 @@ fn collect(&mut self, items: &[ModItem], container: ItemContainerId) {
self.def_collector.export_proc_macro(
proc_macro,
ItemTreeId::new(self.tree_id, id),
fn_id,
module_id,
);
Visibility::Module(module_id)
}
None => resolve_vis(def_map, &self.item_tree[it.visibility]),
};
update_def(
self.def_collector,
FunctionLoc { container, id: ItemTreeId::new(self.tree_id, id) }
.intern(db)
.into(),
&it.name,
vis,
false,
);
update_def(self.def_collector, fn_id.into(), &it.name, vis, false);
}
ModItem::Struct(id) => {
let it = &self.item_tree[id];