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

View File

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

View File

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

View File

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