Skip to content

Commit 00303c3

Browse files
committed
Abstract over ItemTreeLoc
1 parent 2ebf0c8 commit 00303c3

File tree

11 files changed

+161
-230
lines changed

11 files changed

+161
-230
lines changed

crates/hir-def/src/attr.rs

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ use triomphe::Arc;
2424

2525
use crate::{
2626
db::DefDatabase,
27-
item_tree::{AttrOwner, Fields, ItemTreeId, ItemTreeModItemNode},
27+
item_tree::{AttrOwner, Fields, ItemTreeNode},
2828
lang_item::LangItem,
2929
nameres::{ModuleOrigin, ModuleSource},
3030
src::{HasChildSource, HasSource},
31-
AdtId, AssocItemLoc, AttrDefId, GenericParamId, ItemLoc, LocalFieldId, Lookup, MacroId,
32-
VariantId,
31+
AdtId, AttrDefId, GenericParamId, ItemTreeLoc, LocalFieldId, Lookup, MacroId, VariantId,
3332
};
3433

3534
/// Desugared attributes of an item post `cfg_attr` expansion.
@@ -356,11 +355,7 @@ impl AttrsWithOwner {
356355
AttrDefId::FieldId(it) => {
357356
return db.fields_attrs(it.parent)[it.local_id].clone();
358357
}
359-
AttrDefId::EnumVariantId(it) => {
360-
let id = it.lookup(db).id;
361-
let tree = id.item_tree(db);
362-
tree.raw_attrs(id.value.into()).clone()
363-
}
358+
AttrDefId::EnumVariantId(it) => attrs_from_item_tree_loc(db, it),
364359
AttrDefId::AdtId(it) => match it {
365360
AdtId::StructId(it) => attrs_from_item_tree_loc(db, it),
366361
AdtId::EnumId(it) => attrs_from_item_tree_loc(db, it),
@@ -369,15 +364,15 @@ impl AttrsWithOwner {
369364
AttrDefId::TraitId(it) => attrs_from_item_tree_loc(db, it),
370365
AttrDefId::TraitAliasId(it) => attrs_from_item_tree_loc(db, it),
371366
AttrDefId::MacroId(it) => match it {
372-
MacroId::Macro2Id(it) => attrs_from_item_tree(db, it.lookup(db).id),
373-
MacroId::MacroRulesId(it) => attrs_from_item_tree(db, it.lookup(db).id),
374-
MacroId::ProcMacroId(it) => attrs_from_item_tree(db, it.lookup(db).id),
367+
MacroId::Macro2Id(it) => attrs_from_item_tree_loc(db, it),
368+
MacroId::MacroRulesId(it) => attrs_from_item_tree_loc(db, it),
369+
MacroId::ProcMacroId(it) => attrs_from_item_tree_loc(db, it),
375370
},
376371
AttrDefId::ImplId(it) => attrs_from_item_tree_loc(db, it),
377-
AttrDefId::ConstId(it) => attrs_from_item_tree_assoc(db, it),
378-
AttrDefId::StaticId(it) => attrs_from_item_tree_assoc(db, it),
379-
AttrDefId::FunctionId(it) => attrs_from_item_tree_assoc(db, it),
380-
AttrDefId::TypeAliasId(it) => attrs_from_item_tree_assoc(db, it),
372+
AttrDefId::ConstId(it) => attrs_from_item_tree_loc(db, it),
373+
AttrDefId::StaticId(it) => attrs_from_item_tree_loc(db, it),
374+
AttrDefId::FunctionId(it) => attrs_from_item_tree_loc(db, it),
375+
AttrDefId::TypeAliasId(it) => attrs_from_item_tree_loc(db, it),
381376
AttrDefId::GenericParamId(it) => match it {
382377
GenericParamId::ConstParamId(it) => {
383378
let src = it.parent().child_source(db);
@@ -602,29 +597,14 @@ fn any_has_attrs<'db>(
602597
id.lookup(db).source(db).map(ast::AnyHasAttrs::new)
603598
}
604599

605-
fn attrs_from_item_tree<N: ItemTreeModItemNode>(
606-
db: &dyn DefDatabase,
607-
id: ItemTreeId<N>,
608-
) -> RawAttrs {
609-
let tree = id.item_tree(db);
610-
let mod_item = N::id_to_mod_item(id.value);
611-
tree.raw_attrs(mod_item.into()).clone()
612-
}
613-
614-
fn attrs_from_item_tree_loc<'db, N: ItemTreeModItemNode>(
600+
fn attrs_from_item_tree_loc<'db, N: ItemTreeNode>(
615601
db: &(dyn DefDatabase + 'db),
616-
lookup: impl Lookup<Database<'db> = dyn DefDatabase + 'db, Data = ItemLoc<N>>,
602+
lookup: impl Lookup<Database<'db> = dyn DefDatabase + 'db, Data = impl ItemTreeLoc<Id = N>>,
617603
) -> RawAttrs {
618-
let id = lookup.lookup(db).id;
619-
attrs_from_item_tree(db, id)
620-
}
621-
622-
fn attrs_from_item_tree_assoc<'db, N: ItemTreeModItemNode>(
623-
db: &(dyn DefDatabase + 'db),
624-
lookup: impl Lookup<Database<'db> = dyn DefDatabase + 'db, Data = AssocItemLoc<N>>,
625-
) -> RawAttrs {
626-
let id = lookup.lookup(db).id;
627-
attrs_from_item_tree(db, id)
604+
let id = lookup.lookup(db).item_tree_id();
605+
let tree = id.item_tree(db);
606+
let attr_owner = N::attr_owner(id.value);
607+
tree.raw_attrs(attr_owner).clone()
628608
}
629609

630610
pub(crate) fn fields_attrs_source_map(

crates/hir-def/src/child_by_source.rs

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ use crate::{
1414
DynMap,
1515
},
1616
item_scope::ItemScope,
17-
item_tree::ItemTreeModItemNode,
17+
item_tree::ItemTreeNode,
1818
nameres::DefMap,
1919
src::{HasChildSource, HasSource},
20-
AdtId, AssocItemId, AssocItemLoc, DefWithBodyId, EnumId, FieldId, ImplId, ItemLoc, Lookup,
21-
MacroId, ModuleDefId, ModuleId, TraitId, VariantId,
20+
AdtId, AssocItemId, DefWithBodyId, EnumId, FieldId, ImplId, ItemTreeLoc, Lookup, MacroId,
21+
ModuleDefId, ModuleId, TraitId, VariantId,
2222
};
2323

2424
pub trait ChildBySource {
@@ -61,13 +61,9 @@ impl ChildBySource for ImplId {
6161

6262
fn add_assoc_item(db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId, item: AssocItemId) {
6363
match item {
64-
AssocItemId::FunctionId(func) => {
65-
insert_assoc_item_loc(db, res, file_id, func, keys::FUNCTION)
66-
}
67-
AssocItemId::ConstId(konst) => insert_assoc_item_loc(db, res, file_id, konst, keys::CONST),
68-
AssocItemId::TypeAliasId(ty) => {
69-
insert_assoc_item_loc(db, res, file_id, ty, keys::TYPE_ALIAS)
70-
}
64+
AssocItemId::FunctionId(func) => insert_item_loc(db, res, file_id, func, keys::FUNCTION),
65+
AssocItemId::ConstId(konst) => insert_item_loc(db, res, file_id, konst, keys::CONST),
66+
AssocItemId::TypeAliasId(ty) => insert_item_loc(db, res, file_id, ty, keys::TYPE_ALIAS),
7167
}
7268
}
7369

@@ -87,7 +83,7 @@ impl ChildBySource for ItemScope {
8783
.for_each(|ext| insert_item_loc(db, res, file_id, ext, keys::EXTERN_CRATE));
8884
self.use_decls().for_each(|ext| insert_item_loc(db, res, file_id, ext, keys::USE));
8985
self.unnamed_consts(db)
90-
.for_each(|konst| insert_assoc_item_loc(db, res, file_id, konst, keys::CONST));
86+
.for_each(|konst| insert_item_loc(db, res, file_id, konst, keys::CONST));
9187
self.attr_macro_invocs().filter(|(id, _)| id.file_id == file_id).for_each(
9288
|(ast_id, call_id)| {
9389
res[keys::ATTR_MACRO_CALL].insert(ast_id.to_node(db.upcast()), call_id);
@@ -132,17 +128,13 @@ impl ChildBySource for ItemScope {
132128
}
133129
match item {
134130
ModuleDefId::FunctionId(id) => {
135-
insert_assoc_item_loc(db, map, file_id, id, keys::FUNCTION)
136-
}
137-
ModuleDefId::ConstId(id) => {
138-
insert_assoc_item_loc(db, map, file_id, id, keys::CONST)
131+
insert_item_loc(db, map, file_id, id, keys::FUNCTION)
139132
}
133+
ModuleDefId::ConstId(id) => insert_item_loc(db, map, file_id, id, keys::CONST),
140134
ModuleDefId::TypeAliasId(id) => {
141-
insert_assoc_item_loc(db, map, file_id, id, keys::TYPE_ALIAS)
142-
}
143-
ModuleDefId::StaticId(id) => {
144-
insert_assoc_item_loc(db, map, file_id, id, keys::STATIC)
135+
insert_item_loc(db, map, file_id, id, keys::TYPE_ALIAS)
145136
}
137+
ModuleDefId::StaticId(id) => insert_item_loc(db, map, file_id, id, keys::STATIC),
146138
ModuleDefId::TraitId(id) => insert_item_loc(db, map, file_id, id, keys::TRAIT),
147139
ModuleDefId::TraitAliasId(id) => {
148140
insert_item_loc(db, map, file_id, id, keys::TRAIT_ALIAS)
@@ -215,36 +207,20 @@ impl ChildBySource for DefWithBodyId {
215207
}
216208
}
217209

218-
fn insert_item_loc<ID, N>(
219-
db: &dyn DefDatabase,
220-
res: &mut DynMap,
221-
file_id: HirFileId,
222-
id: ID,
223-
key: Key<N::Source, ID>,
224-
) where
225-
ID: for<'db> Lookup<Database<'db> = dyn DefDatabase + 'db, Data = ItemLoc<N>> + 'static,
226-
N: ItemTreeModItemNode,
227-
N::Source: 'static,
228-
{
229-
let loc = id.lookup(db);
230-
if loc.id.file_id() == file_id {
231-
res[key].insert(loc.source(db).value, id)
232-
}
233-
}
234-
235-
fn insert_assoc_item_loc<ID, N>(
210+
fn insert_item_loc<ID, N, Data>(
236211
db: &dyn DefDatabase,
237212
res: &mut DynMap,
238213
file_id: HirFileId,
239214
id: ID,
240215
key: Key<N::Source, ID>,
241216
) where
242-
ID: for<'db> Lookup<Database<'db> = dyn DefDatabase + 'db, Data = AssocItemLoc<N>> + 'static,
243-
N: ItemTreeModItemNode,
217+
ID: for<'db> Lookup<Database<'db> = dyn DefDatabase + 'db, Data = Data> + 'static,
218+
Data: ItemTreeLoc<Id = N>,
219+
N: ItemTreeNode,
244220
N::Source: 'static,
245221
{
246222
let loc = id.lookup(db);
247-
if loc.id.file_id() == file_id {
223+
if loc.item_tree_id().file_id() == file_id {
248224
res[key].insert(loc.source(db).value, id)
249225
}
250226
}

crates/hir-def/src/generics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ use crate::{
2121
db::DefDatabase,
2222
dyn_map::{keys, DynMap},
2323
expander::Expander,
24-
item_tree::{ItemTree, ItemTreeModItemNode},
24+
item_tree::ItemTree,
2525
lower::LowerCtx,
2626
nameres::{DefMap, MacroSubNs},
2727
src::{HasChildSource, HasSource},
2828
type_ref::{ConstRef, LifetimeRef, TypeBound, TypeRef},
29-
AdtId, AssocItemLoc, ConstParamId, GenericDefId, HasModule, ItemLoc, LifetimeParamId,
30-
LocalLifetimeParamId, LocalTypeOrConstParamId, Lookup, TypeOrConstParamId, TypeParamId,
29+
AdtId, ConstParamId, GenericDefId, HasModule, LifetimeParamId, LocalLifetimeParamId,
30+
LocalTypeOrConstParamId, Lookup, TypeOrConstParamId, TypeParamId,
3131
};
3232

3333
/// Data about a generic type parameter (to a function, struct, impl, ...).

crates/hir-def/src/item_tree.rs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -337,20 +337,15 @@ from_attrs!(
337337
LifetimeParamData(Idx<LifetimeParamData>),
338338
);
339339

340-
/// Trait implemented by all item nodes in the item tree.
341-
pub trait ItemTreeModItemNode: Clone {
342-
type Source: AstIdNode + Into<ast::Item>;
340+
/// Trait implemented by all nodes in the item tree.
341+
pub trait ItemTreeNode: Clone {
342+
type Source: AstIdNode;
343343

344344
fn ast_id(&self) -> FileAstId<Self::Source>;
345345

346346
/// Looks up an instance of `Self` in an item tree.
347347
fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self;
348-
349-
/// Downcasts a `ModItem` to a `FileItemTreeId` specific to this type.
350-
fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>>;
351-
352-
/// Upcasts a `FileItemTreeId` to a generic `ModItem`.
353-
fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem;
348+
fn attr_owner(id: FileItemTreeId<Self>) -> AttrOwner;
354349
}
355350

356351
pub struct FileItemTreeId<N>(Idx<N>);
@@ -495,7 +490,7 @@ macro_rules! mod_items {
495490
)+
496491

497492
$(
498-
impl ItemTreeModItemNode for $typ {
493+
impl ItemTreeNode for $typ {
499494
type Source = $ast;
500495

501496
fn ast_id(&self) -> FileAstId<Self::Source> {
@@ -506,15 +501,8 @@ macro_rules! mod_items {
506501
&tree.data().$fld[index]
507502
}
508503

509-
fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> {
510-
match mod_item {
511-
ModItem::$typ(id) => Some(id),
512-
_ => None,
513-
}
514-
}
515-
516-
fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem {
517-
ModItem::$typ(id)
504+
fn attr_owner(id: FileItemTreeId<Self>) -> AttrOwner {
505+
AttrOwner::ModItem(ModItem::$typ(id))
518506
}
519507
}
520508

@@ -578,17 +566,26 @@ impl Index<RawVisibilityId> for ItemTree {
578566
}
579567
}
580568

581-
impl<N: ItemTreeModItemNode> Index<FileItemTreeId<N>> for ItemTree {
569+
impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
582570
type Output = N;
583571
fn index(&self, id: FileItemTreeId<N>) -> &N {
584572
N::lookup(self, id.index())
585573
}
586574
}
587575

588-
impl Index<FileItemTreeId<Variant>> for ItemTree {
589-
type Output = Variant;
590-
fn index(&self, id: FileItemTreeId<Variant>) -> &Variant {
591-
&self[id.index()]
576+
impl ItemTreeNode for Variant {
577+
type Source = ast::Variant;
578+
579+
fn ast_id(&self) -> FileAstId<Self::Source> {
580+
self.ast_id
581+
}
582+
583+
fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self {
584+
&tree.data().variants[index]
585+
}
586+
587+
fn attr_owner(id: FileItemTreeId<Self>) -> AttrOwner {
588+
AttrOwner::Variant(id)
592589
}
593590
}
594591

@@ -1027,7 +1024,7 @@ impl AssocItem {
10271024
}
10281025
}
10291026

1030-
#[derive(Debug, Eq, PartialEq)]
1027+
#[derive(Debug, Clone, PartialEq, Eq)]
10311028
pub struct Variant {
10321029
pub name: Name,
10331030
pub fields: Fields,

crates/hir-def/src/item_tree/lower.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313

1414
use super::*;
1515

16-
fn id<N: ItemTreeModItemNode>(index: Idx<N>) -> FileItemTreeId<N> {
16+
fn id<N: ItemTreeNode>(index: Idx<N>) -> FileItemTreeId<N> {
1717
FileItemTreeId(index)
1818
}
1919

@@ -267,7 +267,7 @@ impl<'a> Ctx<'a> {
267267
if let Some(data) = self.lower_variant(&variant) {
268268
let idx = self.data().variants.alloc(data);
269269
self.add_attrs(
270-
FileItemTreeId(idx).into(),
270+
id(idx).into(),
271271
RawAttrs::new(self.db.upcast(), &variant, self.span_map()),
272272
);
273273
}

0 commit comments

Comments
 (0)