Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 071fe4e

Browse files
committed
Move Intern and Lookup traits to hir-expand
1 parent f211a40 commit 071fe4e

File tree

6 files changed

+152
-117
lines changed

6 files changed

+152
-117
lines changed

crates/hir-def/src/attr.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -637,9 +637,12 @@ impl<'attr> AttrQuery<'attr> {
637637
}
638638
}
639639

640-
fn any_has_attrs(
641-
db: &dyn DefDatabase,
642-
id: impl Lookup<Data = impl HasSource<Value = impl ast::HasAttrs>>,
640+
fn any_has_attrs<'db>(
641+
db: &(dyn DefDatabase + 'db),
642+
id: impl Lookup<
643+
Database<'db> = dyn DefDatabase + 'db,
644+
Data = impl HasSource<Value = impl ast::HasAttrs>,
645+
>,
643646
) -> InFile<ast::AnyHasAttrs> {
644647
id.lookup(db).source(db).map(ast::AnyHasAttrs::new)
645648
}
@@ -650,17 +653,17 @@ fn attrs_from_item_tree<N: ItemTreeNode>(db: &dyn DefDatabase, id: ItemTreeId<N>
650653
tree.raw_attrs(mod_item.into()).clone()
651654
}
652655

653-
fn attrs_from_item_tree_loc<N: ItemTreeNode>(
654-
db: &dyn DefDatabase,
655-
lookup: impl Lookup<Data = ItemLoc<N>>,
656+
fn attrs_from_item_tree_loc<'db, N: ItemTreeNode>(
657+
db: &(dyn DefDatabase + 'db),
658+
lookup: impl Lookup<Database<'db> = dyn DefDatabase + 'db, Data = ItemLoc<N>>,
656659
) -> RawAttrs {
657660
let id = lookup.lookup(db).id;
658661
attrs_from_item_tree(db, id)
659662
}
660663

661-
fn attrs_from_item_tree_assoc<N: ItemTreeNode>(
662-
db: &dyn DefDatabase,
663-
lookup: impl Lookup<Data = AssocItemLoc<N>>,
664+
fn attrs_from_item_tree_assoc<'db, N: ItemTreeNode>(
665+
db: &(dyn DefDatabase + 'db),
666+
lookup: impl Lookup<Database<'db> = dyn DefDatabase + 'db, Data = AssocItemLoc<N>>,
664667
) -> RawAttrs {
665668
let id = lookup.lookup(db).id;
666669
attrs_from_item_tree(db, id)

crates/hir-def/src/lib.rs

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ use hir_expand::{
7272
builtin_fn_macro::{BuiltinFnLikeExpander, EagerExpander},
7373
db::ExpandDatabase,
7474
eager::expand_eager_macro_input,
75+
impl_intern_lookup,
7576
name::Name,
7677
proc_macro::{CustomProcMacroExpander, ProcMacroKind},
7778
AstId, ExpandError, ExpandResult, ExpandTo, HirFileId, InFile, MacroCallId, MacroCallKind,
@@ -84,11 +85,12 @@ use span::Span;
8485
use stdx::impl_from;
8586
use syntax::{ast, AstNode};
8687

87-
pub use hir_expand::tt;
88+
pub use hir_expand::{tt, Intern, Lookup};
8889

8990
use crate::{
9091
builtin_type::BuiltinType,
9192
data::adt::VariantData,
93+
db::DefDatabase,
9294
item_tree::{
9395
Const, Enum, ExternCrate, Function, Impl, ItemTreeId, ItemTreeNode, MacroDef, MacroRules,
9496
Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use,
@@ -102,7 +104,7 @@ pub struct CrateRootModuleId {
102104
}
103105

104106
impl CrateRootModuleId {
105-
pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
107+
pub fn def_map(&self, db: &dyn DefDatabase) -> Arc<DefMap> {
106108
db.crate_def_map(self.krate)
107109
}
108110

@@ -164,7 +166,7 @@ pub struct ModuleId {
164166
}
165167

166168
impl ModuleId {
167-
pub fn def_map(self, db: &dyn db::DefDatabase) -> Arc<DefMap> {
169+
pub fn def_map(self, db: &dyn DefDatabase) -> Arc<DefMap> {
168170
match self.block {
169171
Some(block) => db.block_def_map(block),
170172
None => db.crate_def_map(self.krate),
@@ -175,7 +177,7 @@ impl ModuleId {
175177
self.krate
176178
}
177179

178-
pub fn name(self, db: &dyn db::DefDatabase) -> Option<Name> {
180+
pub fn name(self, db: &dyn DefDatabase) -> Option<Name> {
179181
let def_map = self.def_map(db);
180182
let parent = def_map[self.local_id].parent?;
181183
def_map[parent].children.iter().find_map(|(name, module_id)| {
@@ -187,7 +189,7 @@ impl ModuleId {
187189
})
188190
}
189191

190-
pub fn containing_module(self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
192+
pub fn containing_module(self, db: &dyn DefDatabase) -> Option<ModuleId> {
191193
self.def_map(db).containing_module(self.local_id)
192194
}
193195

@@ -264,20 +266,7 @@ impl<N: ItemTreeNode> Hash for AssocItemLoc<N> {
264266
macro_rules! impl_intern {
265267
($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
266268
impl_intern_key!($id);
267-
268-
impl Intern for $loc {
269-
type ID = $id;
270-
fn intern(self, db: &dyn db::DefDatabase) -> $id {
271-
db.$intern(self)
272-
}
273-
}
274-
275-
impl Lookup for $id {
276-
type Data = $loc;
277-
fn lookup(&self, db: &dyn db::DefDatabase) -> $loc {
278-
db.$lookup(*self)
279-
}
280-
}
269+
impl_intern_lookup!(DefDatabase, $id, $loc, $intern, $lookup);
281270
};
282271
}
283272

@@ -511,7 +500,7 @@ pub enum MacroId {
511500
impl_from!(Macro2Id, MacroRulesId, ProcMacroId for MacroId);
512501

513502
impl MacroId {
514-
pub fn is_attribute(self, db: &dyn db::DefDatabase) -> bool {
503+
pub fn is_attribute(self, db: &dyn DefDatabase) -> bool {
515504
matches!(self, MacroId::ProcMacroId(it) if it.lookup(db).kind == ProcMacroKind::Attr)
516505
}
517506
}
@@ -723,7 +712,7 @@ impl PartialEq for InTypeConstLoc {
723712
}
724713

725714
impl InTypeConstId {
726-
pub fn source(&self, db: &dyn db::DefDatabase) -> ast::ConstArg {
715+
pub fn source(&self, db: &dyn DefDatabase) -> ast::ConstArg {
727716
let src = self.lookup(db).id;
728717
let file_id = src.file_id;
729718
let root = &db.parse_or_expand(file_id);
@@ -743,15 +732,15 @@ pub enum GeneralConstId {
743732
impl_from!(ConstId, ConstBlockId, InTypeConstId for GeneralConstId);
744733

745734
impl GeneralConstId {
746-
pub fn generic_def(self, db: &dyn db::DefDatabase) -> Option<GenericDefId> {
735+
pub fn generic_def(self, db: &dyn DefDatabase) -> Option<GenericDefId> {
747736
match self {
748737
GeneralConstId::ConstId(it) => Some(it.into()),
749738
GeneralConstId::ConstBlockId(it) => it.lookup(db).parent.as_generic_def_id(),
750739
GeneralConstId::InTypeConstId(it) => it.lookup(db).owner.as_generic_def_id(),
751740
}
752741
}
753742

754-
pub fn name(self, db: &dyn db::DefDatabase) -> String {
743+
pub fn name(self, db: &dyn DefDatabase) -> String {
755744
match self {
756745
GeneralConstId::ConstId(const_id) => db
757746
.const_data(const_id)
@@ -934,7 +923,7 @@ pub enum VariantId {
934923
impl_from!(EnumVariantId, StructId, UnionId for VariantId);
935924

936925
impl VariantId {
937-
pub fn variant_data(self, db: &dyn db::DefDatabase) -> Arc<VariantData> {
926+
pub fn variant_data(self, db: &dyn DefDatabase) -> Arc<VariantData> {
938927
match self {
939928
VariantId::StructId(it) => db.struct_data(it).variant_data.clone(),
940929
VariantId::UnionId(it) => db.union_data(it).variant_data.clone(),
@@ -944,7 +933,7 @@ impl VariantId {
944933
}
945934
}
946935

947-
pub fn file_id(self, db: &dyn db::DefDatabase) -> HirFileId {
936+
pub fn file_id(self, db: &dyn DefDatabase) -> HirFileId {
948937
match self {
949938
VariantId::EnumVariantId(it) => it.parent.lookup(db).id.file_id(),
950939
VariantId::StructId(it) => it.lookup(db).id.file_id(),
@@ -961,22 +950,12 @@ impl VariantId {
961950
}
962951
}
963952

964-
trait Intern {
965-
type ID;
966-
fn intern(self, db: &dyn db::DefDatabase) -> Self::ID;
967-
}
968-
969-
pub trait Lookup {
970-
type Data;
971-
fn lookup(&self, db: &dyn db::DefDatabase) -> Self::Data;
972-
}
973-
974953
pub trait HasModule {
975-
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId;
954+
fn module(&self, db: &dyn DefDatabase) -> ModuleId;
976955
}
977956

978957
impl HasModule for ItemContainerId {
979-
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
958+
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
980959
match *self {
981960
ItemContainerId::ModuleId(it) => it,
982961
ItemContainerId::ImplId(it) => it.lookup(db).container,
@@ -987,13 +966,13 @@ impl HasModule for ItemContainerId {
987966
}
988967

989968
impl<N: ItemTreeNode> HasModule for AssocItemLoc<N> {
990-
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
969+
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
991970
self.container.module(db)
992971
}
993972
}
994973

995974
impl HasModule for AdtId {
996-
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
975+
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
997976
match self {
998977
AdtId::StructId(it) => it.lookup(db).container,
999978
AdtId::UnionId(it) => it.lookup(db).container,
@@ -1003,13 +982,13 @@ impl HasModule for AdtId {
1003982
}
1004983

1005984
impl HasModule for ExternCrateId {
1006-
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
985+
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
1007986
self.lookup(db).container
1008987
}
1009988
}
1010989

1011990
impl HasModule for VariantId {
1012-
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
991+
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
1013992
match self {
1014993
VariantId::EnumVariantId(it) => it.parent.lookup(db).container,
1015994
VariantId::StructId(it) => it.lookup(db).container,
@@ -1019,7 +998,7 @@ impl HasModule for VariantId {
1019998
}
1020999

10211000
impl HasModule for MacroId {
1022-
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
1001+
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
10231002
match self {
10241003
MacroId::MacroRulesId(it) => it.lookup(db).container,
10251004
MacroId::Macro2Id(it) => it.lookup(db).container,
@@ -1029,7 +1008,7 @@ impl HasModule for MacroId {
10291008
}
10301009

10311010
impl HasModule for TypeOwnerId {
1032-
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
1011+
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
10331012
match self {
10341013
TypeOwnerId::FunctionId(it) => it.lookup(db).module(db),
10351014
TypeOwnerId::StaticId(it) => it.lookup(db).module(db),
@@ -1046,7 +1025,7 @@ impl HasModule for TypeOwnerId {
10461025
}
10471026

10481027
impl HasModule for DefWithBodyId {
1049-
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
1028+
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
10501029
match self {
10511030
DefWithBodyId::FunctionId(it) => it.lookup(db).module(db),
10521031
DefWithBodyId::StaticId(it) => it.lookup(db).module(db),
@@ -1058,7 +1037,7 @@ impl HasModule for DefWithBodyId {
10581037
}
10591038

10601039
impl HasModule for GenericDefId {
1061-
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
1040+
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
10621041
match self {
10631042
GenericDefId::FunctionId(it) => it.lookup(db).module(db),
10641043
GenericDefId::AdtId(it) => it.module(db),
@@ -1073,13 +1052,13 @@ impl HasModule for GenericDefId {
10731052
}
10741053

10751054
impl HasModule for TypeAliasId {
1076-
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
1055+
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
10771056
self.lookup(db).module(db)
10781057
}
10791058
}
10801059

10811060
impl HasModule for TraitId {
1082-
fn module(&self, db: &dyn db::DefDatabase) -> ModuleId {
1061+
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
10831062
self.lookup(db).container
10841063
}
10851064
}
@@ -1088,7 +1067,7 @@ impl ModuleDefId {
10881067
/// Returns the module containing `self` (or `self`, if `self` is itself a module).
10891068
///
10901069
/// Returns `None` if `self` refers to a primitive type.
1091-
pub fn module(&self, db: &dyn db::DefDatabase) -> Option<ModuleId> {
1070+
pub fn module(&self, db: &dyn DefDatabase) -> Option<ModuleId> {
10921071
Some(match self {
10931072
ModuleDefId::ModuleId(id) => *id,
10941073
ModuleDefId::FunctionId(id) => id.lookup(db).module(db),
@@ -1106,7 +1085,7 @@ impl ModuleDefId {
11061085
}
11071086

11081087
impl AttrDefId {
1109-
pub fn krate(&self, db: &dyn db::DefDatabase) -> CrateId {
1088+
pub fn krate(&self, db: &dyn DefDatabase) -> CrateId {
11101089
match self {
11111090
AttrDefId::ModuleId(it) => it.krate,
11121091
AttrDefId::FieldId(it) => it.parent.module(db).krate,
@@ -1244,7 +1223,7 @@ fn macro_call_as_call_id_with_eager(
12441223
Ok(res)
12451224
}
12461225

1247-
pub fn macro_id_to_def_id(db: &dyn db::DefDatabase, id: MacroId) -> MacroDefId {
1226+
pub fn macro_id_to_def_id(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
12481227
match id {
12491228
MacroId::Macro2Id(it) => {
12501229
let loc = it.lookup(db);
@@ -1316,7 +1295,7 @@ pub fn macro_id_to_def_id(db: &dyn db::DefDatabase, id: MacroId) -> MacroDefId {
13161295
}
13171296

13181297
fn derive_macro_as_call_id(
1319-
db: &dyn db::DefDatabase,
1298+
db: &dyn DefDatabase,
13201299
item_attr: &AstIdWithPath<ast::Adt>,
13211300
derive_attr_index: AttrId,
13221301
derive_pos: u32,
@@ -1341,7 +1320,7 @@ fn derive_macro_as_call_id(
13411320
}
13421321

13431322
fn attr_macro_as_call_id(
1344-
db: &dyn db::DefDatabase,
1323+
db: &dyn DefDatabase,
13451324
item_attr: &AstIdWithPath<ast::Item>,
13461325
macro_attr: &Attr,
13471326
krate: CrateId,

crates/hir-expand/src/eager.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use crate::{
2828
db::ExpandDatabase,
2929
mod_path::ModPath,
3030
span_map::SpanMapRef,
31-
EagerCallInfo, ExpandError, ExpandResult, ExpandTo, ExpansionSpanMap, InFile, MacroCallId,
32-
MacroCallKind, MacroCallLoc, MacroDefId, MacroDefKind,
31+
EagerCallInfo, ExpandError, ExpandResult, ExpandTo, ExpansionSpanMap, InFile, Intern,
32+
MacroCallId, MacroCallKind, MacroCallLoc, MacroDefId, MacroDefKind,
3333
};
3434

3535
pub fn expand_eager_macro_input(
@@ -49,13 +49,14 @@ pub fn expand_eager_macro_input(
4949
// When `lazy_expand` is called, its *parent* file must already exist.
5050
// Here we store an eager macro id for the argument expanded subtree
5151
// for that purpose.
52-
let arg_id = db.intern_macro_call(MacroCallLoc {
52+
let arg_id = MacroCallLoc {
5353
def,
5454
krate,
5555
eager: None,
5656
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to: ExpandTo::Expr },
5757
call_site,
58-
});
58+
}
59+
.intern(db);
5960
let ExpandResult { value: (arg_exp, arg_exp_map), err: parse_err } =
6061
db.parse_macro_expansion(arg_id.as_macro_file());
6162

@@ -94,7 +95,7 @@ pub fn expand_eager_macro_input(
9495
call_site,
9596
};
9697

97-
ExpandResult { value: Some(db.intern_macro_call(loc)), err }
98+
ExpandResult { value: Some(loc.intern(db)), err }
9899
}
99100

100101
fn lazy_expand(

0 commit comments

Comments
 (0)