Skip to content

Commit a265cdd

Browse files
committed
Remove FileItemTreeId
1 parent d4a3719 commit a265cdd

File tree

5 files changed

+116
-181
lines changed

5 files changed

+116
-181
lines changed

src/tools/rust-analyzer/crates/hir-def/src/attr.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -523,15 +523,10 @@ impl AttrsWithOwner {
523523
let mod_data = &def_map[module.local_id];
524524

525525
let raw_attrs = match mod_data.origin {
526-
ModuleOrigin::File {
527-
definition,
528-
declaration_tree_id,
529-
file_item_tree_id,
530-
..
531-
} => {
526+
ModuleOrigin::File { definition, declaration_tree_id, declaration, .. } => {
532527
let decl_attrs = declaration_tree_id
533528
.item_tree(db)
534-
.raw_attrs(AttrOwner::ModItem(file_item_tree_id.into()))
529+
.raw_attrs(AttrOwner::ModItem(declaration.into()))
535530
.clone();
536531
let tree = db.file_item_tree(definition.into());
537532
let def_attrs = tree.raw_attrs(AttrOwner::TopLevel).clone();
@@ -541,12 +536,10 @@ impl AttrsWithOwner {
541536
let tree = db.file_item_tree(definition.into());
542537
tree.raw_attrs(AttrOwner::TopLevel).clone()
543538
}
544-
ModuleOrigin::Inline { definition_tree_id, file_item_tree_id, .. } => {
545-
definition_tree_id
546-
.item_tree(db)
547-
.raw_attrs(AttrOwner::ModItem(file_item_tree_id.into()))
548-
.clone()
549-
}
539+
ModuleOrigin::Inline { definition_tree_id, definition } => definition_tree_id
540+
.item_tree(db)
541+
.raw_attrs(AttrOwner::ModItem(definition.into()))
542+
.clone(),
550543
ModuleOrigin::BlockExpr { id, .. } => {
551544
let tree = db.block_item_tree(id);
552545
tree.raw_attrs(AttrOwner::TopLevel).clone()

src/tools/rust-analyzer/crates/hir-def/src/item_tree.rs

Lines changed: 34 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ mod tests;
3737

3838
use std::{
3939
fmt::{self, Debug},
40-
hash::{Hash, Hasher},
41-
ops::{Index, Range},
40+
hash::Hash,
41+
ops::Index,
4242
sync::OnceLock,
4343
};
4444

@@ -51,7 +51,7 @@ use hir_expand::{
5151
name::Name,
5252
};
5353
use intern::Interned;
54-
use la_arena::{Arena, Idx, RawIdx};
54+
use la_arena::{Arena, Idx};
5555
use rustc_hash::FxHashMap;
5656
use smallvec::SmallVec;
5757
use span::{AstIdNode, Edition, FileAstId, SyntaxContext};
@@ -277,23 +277,23 @@ struct ItemVisibilities {
277277

278278
#[derive(Default, Debug, Eq, PartialEq)]
279279
struct ItemTreeData {
280-
uses: Arena<Use>,
281-
extern_crates: Arena<ExternCrate>,
282-
extern_blocks: Arena<ExternBlock>,
283-
functions: Arena<Function>,
284-
structs: Arena<Struct>,
285-
unions: Arena<Union>,
286-
enums: Arena<Enum>,
287-
consts: Arena<Const>,
288-
statics: Arena<Static>,
289-
traits: Arena<Trait>,
290-
trait_aliases: Arena<TraitAlias>,
291-
impls: Arena<Impl>,
292-
type_aliases: Arena<TypeAlias>,
293-
mods: Arena<Mod>,
294-
macro_calls: Arena<MacroCall>,
295-
macro_rules: Arena<MacroRules>,
296-
macro_defs: Arena<Macro2>,
280+
uses: FxHashMap<ItemTreeAstId<Use>, Use>,
281+
extern_crates: FxHashMap<ItemTreeAstId<ExternCrate>, ExternCrate>,
282+
extern_blocks: FxHashMap<ItemTreeAstId<ExternBlock>, ExternBlock>,
283+
functions: FxHashMap<ItemTreeAstId<Function>, Function>,
284+
structs: FxHashMap<ItemTreeAstId<Struct>, Struct>,
285+
unions: FxHashMap<ItemTreeAstId<Union>, Union>,
286+
enums: FxHashMap<ItemTreeAstId<Enum>, Enum>,
287+
consts: FxHashMap<ItemTreeAstId<Const>, Const>,
288+
statics: FxHashMap<ItemTreeAstId<Static>, Static>,
289+
traits: FxHashMap<ItemTreeAstId<Trait>, Trait>,
290+
trait_aliases: FxHashMap<ItemTreeAstId<TraitAlias>, TraitAlias>,
291+
impls: FxHashMap<ItemTreeAstId<Impl>, Impl>,
292+
type_aliases: FxHashMap<ItemTreeAstId<TypeAlias>, TypeAlias>,
293+
mods: FxHashMap<ItemTreeAstId<Mod>, Mod>,
294+
macro_calls: FxHashMap<ItemTreeAstId<MacroCall>, MacroCall>,
295+
macro_rules: FxHashMap<ItemTreeAstId<MacroRules>, MacroRules>,
296+
macro_defs: FxHashMap<ItemTreeAstId<Macro2>, Macro2>,
297297

298298
vis: ItemVisibilities,
299299
}
@@ -329,51 +329,11 @@ pub trait ItemTreeNode: Clone {
329329
fn ast_id(&self) -> FileAstId<Self::Source>;
330330

331331
/// Looks up an instance of `Self` in an item tree.
332-
fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self;
332+
fn lookup(tree: &ItemTree, index: FileAstId<Self::Source>) -> &Self;
333333
}
334334

335-
pub struct FileItemTreeId<N>(Idx<N>);
336-
337-
impl<N> FileItemTreeId<N> {
338-
pub fn range_iter(range: Range<Self>) -> impl Iterator<Item = Self> + Clone {
339-
(range.start.index().into_raw().into_u32()..range.end.index().into_raw().into_u32())
340-
.map(RawIdx::from_u32)
341-
.map(Idx::from_raw)
342-
.map(Self)
343-
}
344-
}
345-
346-
impl<N> FileItemTreeId<N> {
347-
pub fn index(&self) -> Idx<N> {
348-
self.0
349-
}
350-
}
351-
352-
impl<N> Clone for FileItemTreeId<N> {
353-
fn clone(&self) -> Self {
354-
*self
355-
}
356-
}
357-
impl<N> Copy for FileItemTreeId<N> {}
358-
359-
impl<N> PartialEq for FileItemTreeId<N> {
360-
fn eq(&self, other: &FileItemTreeId<N>) -> bool {
361-
self.0 == other.0
362-
}
363-
}
364-
impl<N> Eq for FileItemTreeId<N> {}
365-
366-
impl<N> Hash for FileItemTreeId<N> {
367-
fn hash<H: Hasher>(&self, state: &mut H) {
368-
self.0.hash(state)
369-
}
370-
}
371-
372-
impl<N> fmt::Debug for FileItemTreeId<N> {
373-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
374-
self.0.fmt(f)
375-
}
376-
}
335+
#[allow(type_alias_bounds)]
336+
pub type ItemTreeAstId<T: ItemTreeNode> = FileAstId<T::Source>;
377337

378338
/// Identifies a particular [`ItemTree`].
379339
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
@@ -409,21 +369,21 @@ macro_rules! mod_items {
409369
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
410370
pub enum ModItem {
411371
$(
412-
$typ(FileItemTreeId<$typ>),
372+
$typ(FileAstId<$ast>),
413373
)+
414374
}
415375

416376
impl ModItem {
417-
pub fn ast_id(&self, tree: &ItemTree) -> FileAstId<ast::Item> {
377+
pub fn ast_id(self) -> FileAstId<ast::Item> {
418378
match self {
419-
$(ModItem::$typ(it) => tree[it.index()].ast_id().upcast()),+
379+
$(ModItem::$typ(it) => it.upcast()),+
420380
}
421381
}
422382
}
423383

424384
$(
425-
impl From<FileItemTreeId<$typ>> for ModItem {
426-
fn from(id: FileItemTreeId<$typ>) -> ModItem {
385+
impl From<FileAstId<$ast>> for ModItem {
386+
fn from(id: FileAstId<$ast>) -> ModItem {
427387
ModItem::$typ(id)
428388
}
429389
}
@@ -433,20 +393,20 @@ macro_rules! mod_items {
433393
impl ItemTreeNode for $typ {
434394
type Source = $ast;
435395

436-
fn ast_id(&self) -> FileAstId<Self::Source> {
396+
fn ast_id(&self) -> FileAstId<$ast> {
437397
self.ast_id
438398
}
439399

440-
fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self {
441-
&tree.data().$fld[index]
400+
fn lookup(tree: &ItemTree, index: FileAstId<$ast>) -> &Self {
401+
&tree.data().$fld[&index]
442402
}
443403
}
444404

445-
impl Index<Idx<$typ>> for ItemTree {
405+
impl Index<FileAstId<$ast>> for ItemTree {
446406
type Output = $typ;
447407

448-
fn index(&self, index: Idx<$typ>) -> &Self::Output {
449-
&self.data().$fld[index]
408+
fn index(&self, index: FileAstId<$ast>) -> &Self::Output {
409+
&self.data().$fld[&index]
450410
}
451411
}
452412
)+
@@ -506,13 +466,6 @@ impl Index<RawVisibilityId> for ItemTree {
506466
}
507467
}
508468

509-
impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
510-
type Output = N;
511-
fn index(&self, id: FileItemTreeId<N>) -> &N {
512-
N::lookup(self, id.index())
513-
}
514-
}
515-
516469
#[derive(Debug, Clone, Eq, PartialEq)]
517470
pub struct Use {
518471
pub visibility: RawVisibilityId,

0 commit comments

Comments
 (0)