diff options
| author | Lukas Wirth <lukastw97@gmail.com> | 2022-03-09 11:26:06 +0100 |
|---|---|---|
| committer | Lukas Wirth <lukastw97@gmail.com> | 2022-03-09 11:26:06 +0100 |
| commit | 2537ad0d9e53933a9c5a54ec78ea89d5a025de8b (patch) | |
| tree | d94398a99dca51b507540b7a3954cde2ee68cb55 | |
| parent | 054ab5fd9c5c373f3c22677f9455ea845eb51dc5 (diff) | |
| download | rust-2537ad0d9e53933a9c5a54ec78ea89d5a025de8b.tar.gz rust-2537ad0d9e53933a9c5a54ec78ea89d5a025de8b.zip | |
Simplify
| -rw-r--r-- | crates/hir/src/lib.rs | 36 | ||||
| -rw-r--r-- | crates/hir_def/src/lib.rs | 2 | ||||
| -rw-r--r-- | crates/hir_def/src/nameres.rs | 13 | ||||
| -rw-r--r-- | crates/hir_def/src/nameres/collector.rs | 24 |
4 files changed, 34 insertions, 41 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 1c52ed57955..d38c91ac437 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -35,7 +35,7 @@ mod display; 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 @@ use hir_def::{ 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 @@ impl Function { } 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 @@ impl Macro { 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 @@ impl Macro { 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, diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index 065922841a2..d7292b00637 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs @@ -554,7 +554,7 @@ impl_from!( FunctionId, TraitId, TypeAliasId, - MacroId, + MacroId(Macro2Id, MacroRulesId, ProcMacroId), ImplId, GenericParamId for AttrDefId diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index 279784952d3..e9d3d976f9e 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs @@ -70,12 +70,12 @@ use syntax::{ast, SmolStr}; 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 @@ impl DefMap { 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 @@ impl DefMap { // 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 @@ impl DefMap { } = 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(); diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index a8928d07e79..e1a297a3fc4 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -46,10 +46,10 @@ use crate::{ 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 @@ impl DefCollector<'_> { &mut self, def: ProcMacroDef, id: ItemTreeId<item_tree::Function>, + fn_id: FunctionId, module_id: ModuleId, ) { self.exports_proc_macros = true; @@ -570,7 +571,7 @@ impl DefCollector<'_> { .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 @@ impl ModCollector<'_, '_> { } 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 @@ impl ModCollector<'_, '_> { 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]; |
