Skip to content

Commit 2537ad0

Browse files
committed
Simplify
1 parent 054ab5f commit 2537ad0

File tree

4 files changed

+34
-41
lines changed

4 files changed

+34
-41
lines changed

crates/hir/src/lib.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ mod display;
3535
use std::{collections::HashMap, iter, ops::ControlFlow, sync::Arc};
3636

3737
use arrayvec::ArrayVec;
38-
use base_db::{CrateDisplayName, CrateId, CrateOrigin, Edition, FileId};
38+
use base_db::{CrateDisplayName, CrateId, CrateOrigin, Edition, FileId, ProcMacroKind};
3939
use either::Either;
4040
use hir_def::{
4141
adt::{ReprKind, VariantData},
@@ -49,8 +49,8 @@ use hir_def::{
4949
src::HasSource as _,
5050
AdtId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, DefWithBodyId, EnumId,
5151
FunctionId, GenericDefId, HasModule, ImplId, ItemContainerId, LifetimeParamId,
52-
LocalEnumVariantId, LocalFieldId, Lookup, MacroId, ModuleId, StaticId, StructId, TraitId,
53-
TypeAliasId, TypeOrConstParamId, TypeParamId, UnionId,
52+
LocalEnumVariantId, LocalFieldId, Lookup, MacroExpander, MacroId, ModuleId, StaticId, StructId,
53+
TraitId, TypeAliasId, TypeOrConstParamId, TypeParamId, UnionId,
5454
};
5555
use hir_expand::{name::name, MacroCallKind};
5656
use hir_ty::{
@@ -1395,7 +1395,7 @@ impl Function {
13951395
}
13961396
let loc = self.id.lookup(db.upcast());
13971397
let def_map = db.crate_def_map(loc.krate(db).into());
1398-
def_map.fn_as_proc_macro(loc.id).map(|id| Macro { id: id.into() })
1398+
def_map.fn_as_proc_macro(self.id).map(|id| Macro { id: id.into() })
13991399
}
14001400

14011401
/// A textual representation of the HIR of this function for debugging purposes.
@@ -1768,23 +1768,21 @@ impl Macro {
17681768
pub fn kind(&self, db: &dyn HirDatabase) -> MacroKind {
17691769
match self.id {
17701770
MacroId::Macro2Id(it) => match it.lookup(db.upcast()).expander {
1771-
hir_def::MacroExpander::Declarative => MacroKind::Declarative,
1772-
hir_def::MacroExpander::BuiltIn(_) => MacroKind::BuiltIn,
1773-
hir_def::MacroExpander::BuiltInAttr(_) => MacroKind::Attr,
1774-
hir_def::MacroExpander::BuiltInDerive(_) => MacroKind::Derive,
1775-
hir_def::MacroExpander::BuiltInEager(_) => MacroKind::BuiltIn,
1771+
MacroExpander::Declarative => MacroKind::Declarative,
1772+
MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => MacroKind::BuiltIn,
1773+
MacroExpander::BuiltInAttr(_) => MacroKind::Attr,
1774+
MacroExpander::BuiltInDerive(_) => MacroKind::Derive,
17761775
},
17771776
MacroId::MacroRulesId(it) => match it.lookup(db.upcast()).expander {
1778-
hir_def::MacroExpander::Declarative => MacroKind::Declarative,
1779-
hir_def::MacroExpander::BuiltIn(_) => MacroKind::BuiltIn,
1780-
hir_def::MacroExpander::BuiltInAttr(_) => MacroKind::Attr,
1781-
hir_def::MacroExpander::BuiltInDerive(_) => MacroKind::Derive,
1782-
hir_def::MacroExpander::BuiltInEager(_) => MacroKind::BuiltIn,
1777+
MacroExpander::Declarative => MacroKind::Declarative,
1778+
MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => MacroKind::BuiltIn,
1779+
MacroExpander::BuiltInAttr(_) => MacroKind::Attr,
1780+
MacroExpander::BuiltInDerive(_) => MacroKind::Derive,
17831781
},
17841782
MacroId::ProcMacroId(it) => match it.lookup(db.upcast()).kind {
1785-
base_db::ProcMacroKind::CustomDerive => MacroKind::Derive,
1786-
base_db::ProcMacroKind::FuncLike => MacroKind::ProcMacro,
1787-
base_db::ProcMacroKind::Attr => MacroKind::Attr,
1783+
ProcMacroKind::CustomDerive => MacroKind::Derive,
1784+
ProcMacroKind::FuncLike => MacroKind::ProcMacro,
1785+
ProcMacroKind::Attr => MacroKind::Attr,
17881786
},
17891787
}
17901788
}
@@ -1799,11 +1797,11 @@ impl Macro {
17991797
pub fn is_builtin_derive(&self, db: &dyn HirDatabase) -> bool {
18001798
match self.id {
18011799
MacroId::Macro2Id(it) => match it.lookup(db.upcast()).expander {
1802-
hir_def::MacroExpander::BuiltInDerive(_) => true,
1800+
MacroExpander::BuiltInDerive(_) => true,
18031801
_ => false,
18041802
},
18051803
MacroId::MacroRulesId(it) => match it.lookup(db.upcast()).expander {
1806-
hir_def::MacroExpander::BuiltInDerive(_) => true,
1804+
MacroExpander::BuiltInDerive(_) => true,
18071805
_ => false,
18081806
},
18091807
MacroId::ProcMacroId(_) => false,

crates/hir_def/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ impl_from!(
554554
FunctionId,
555555
TraitId,
556556
TypeAliasId,
557-
MacroId,
557+
MacroId(Macro2Id, MacroRulesId, ProcMacroId),
558558
ImplId,
559559
GenericParamId
560560
for AttrDefId

crates/hir_def/src/nameres.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ use syntax::{ast, SmolStr};
7070
use crate::{
7171
db::DefDatabase,
7272
item_scope::{BuiltinShadowMode, ItemScope},
73-
item_tree::{self, ItemTreeId, TreeId},
73+
item_tree::TreeId,
7474
nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode},
7575
path::ModPath,
7676
per_ns::PerNs,
7777
visibility::Visibility,
78-
AstId, BlockId, BlockLoc, LocalModuleId, ModuleDefId, ModuleId, ProcMacroId,
78+
AstId, BlockId, BlockLoc, FunctionId, LocalModuleId, ModuleDefId, ModuleId, ProcMacroId,
7979
};
8080

8181
/// Contains the results of (early) name resolution.
@@ -102,7 +102,7 @@ pub struct DefMap {
102102

103103
/// Side table for resolving derive helpers.
104104
exported_derives: FxHashMap<MacroDefId, Box<[Name]>>,
105-
fn_proc_macro_mapping: FxHashMap<ItemTreeId<item_tree::Function>, ProcMacroId>,
105+
fn_proc_macro_mapping: FxHashMap<FunctionId, ProcMacroId>,
106106

107107
/// Custom attributes registered with `#![register_attr]`.
108108
registered_attrs: Vec<SmolStr>,
@@ -302,8 +302,7 @@ impl DefMap {
302302
self.root
303303
}
304304

305-
// FIXME: This is an odd interface....
306-
pub fn fn_as_proc_macro(&self, id: ItemTreeId<item_tree::Function>) -> Option<ProcMacroId> {
305+
pub fn fn_as_proc_macro(&self, id: FunctionId) -> Option<ProcMacroId> {
307306
self.fn_proc_macro_mapping.get(&id).copied()
308307
}
309308

@@ -454,7 +453,7 @@ impl DefMap {
454453
// Exhaustive match to require handling new fields.
455454
let Self {
456455
_c: _,
457-
exported_derives: exported_proc_macros,
456+
exported_derives,
458457
extern_prelude,
459458
diagnostics,
460459
modules,
@@ -470,7 +469,7 @@ impl DefMap {
470469
} = self;
471470

472471
extern_prelude.shrink_to_fit();
473-
exported_proc_macros.shrink_to_fit();
472+
exported_derives.shrink_to_fit();
474473
diagnostics.shrink_to_fit();
475474
modules.shrink_to_fit();
476475
registered_attrs.shrink_to_fit();

crates/hir_def/src/nameres/collector.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ use crate::{
4646
path::{ImportAlias, ModPath, PathKind},
4747
per_ns::PerNs,
4848
visibility::{RawVisibility, Visibility},
49-
AdtId, AstId, AstIdWithPath, ConstLoc, EnumLoc, EnumVariantId, ExternBlockLoc, FunctionLoc,
50-
ImplLoc, Intern, ItemContainerId, LocalModuleId, Macro2Id, Macro2Loc, MacroExpander, MacroId,
51-
MacroRulesId, MacroRulesLoc, ModuleDefId, ModuleId, ProcMacroId, ProcMacroLoc, StaticLoc,
52-
StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, UnresolvedMacro,
49+
AdtId, AstId, AstIdWithPath, ConstLoc, EnumLoc, EnumVariantId, ExternBlockLoc, FunctionId,
50+
FunctionLoc, ImplLoc, Intern, ItemContainerId, LocalModuleId, Macro2Id, Macro2Loc,
51+
MacroExpander, MacroId, MacroRulesId, MacroRulesLoc, ModuleDefId, ModuleId, ProcMacroId,
52+
ProcMacroLoc, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, UnresolvedMacro,
5353
};
5454

5555
static GLOB_RECURSION_LIMIT: Limit = Limit::new(100);
@@ -552,6 +552,7 @@ impl DefCollector<'_> {
552552
&mut self,
553553
def: ProcMacroDef,
554554
id: ItemTreeId<item_tree::Function>,
555+
fn_id: FunctionId,
555556
module_id: ModuleId,
556557
) {
557558
self.exports_proc_macros = true;
@@ -570,7 +571,7 @@ impl DefCollector<'_> {
570571
.exported_derives
571572
.insert(macro_id_to_def_id(self.db, proc_macro_id.into()), helpers);
572573
}
573-
self.def_map.fn_proc_macro_mapping.insert(id, proc_macro_id);
574+
self.def_map.fn_proc_macro_mapping.insert(fn_id, proc_macro_id);
574575
}
575576

576577
/// Define a macro with `macro_rules`.
@@ -1551,6 +1552,8 @@ impl ModCollector<'_, '_> {
15511552
}
15521553
ModItem::Function(id) => {
15531554
let it = &self.item_tree[id];
1555+
let fn_id =
1556+
FunctionLoc { container, id: ItemTreeId::new(self.tree_id, id) }.intern(db);
15541557

15551558
let is_proc_macro = attrs.parse_proc_macro_decl(&it.name);
15561559
let vis = match is_proc_macro {
@@ -1561,21 +1564,14 @@ impl ModCollector<'_, '_> {
15611564
self.def_collector.export_proc_macro(
15621565
proc_macro,
15631566
ItemTreeId::new(self.tree_id, id),
1567+
fn_id,
15641568
module_id,
15651569
);
15661570
Visibility::Module(module_id)
15671571
}
15681572
None => resolve_vis(def_map, &self.item_tree[it.visibility]),
15691573
};
1570-
update_def(
1571-
self.def_collector,
1572-
FunctionLoc { container, id: ItemTreeId::new(self.tree_id, id) }
1573-
.intern(db)
1574-
.into(),
1575-
&it.name,
1576-
vis,
1577-
false,
1578-
);
1574+
update_def(self.def_collector, fn_id.into(), &it.name, vis, false);
15791575
}
15801576
ModItem::Struct(id) => {
15811577
let it = &self.item_tree[id];

0 commit comments

Comments
 (0)