Skip to content

Commit 777fdac

Browse files
authored
Merge pull request #19982 from Veykril/push-uptnmqtlylsx
Simplify and optimize `ItemTree`
2 parents 43d8618 + 63e1707 commit 777fdac

File tree

9 files changed

+558
-755
lines changed

9 files changed

+558
-755
lines changed

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
AdtId, AstIdLoc, AttrDefId, GenericParamId, HasModule, LocalFieldId, Lookup, MacroId,
2727
VariantId,
2828
db::DefDatabase,
29-
item_tree::AttrOwner,
29+
item_tree::block_item_tree_query,
3030
lang_item::LangItem,
3131
nameres::{ModuleOrigin, ModuleSource},
3232
src::{HasChildSource, HasSource},
@@ -523,26 +523,25 @@ impl AttrsWithOwner {
523523
let mod_data = &def_map[module.local_id];
524524

525525
let raw_attrs = match mod_data.origin {
526-
ModuleOrigin::File { definition, declaration_tree_id, .. } => {
526+
ModuleOrigin::File { definition, declaration_tree_id, declaration, .. } => {
527527
let decl_attrs = declaration_tree_id
528528
.item_tree(db)
529-
.raw_attrs(AttrOwner::ModItem(declaration_tree_id.value.into()))
529+
.raw_attrs(declaration.upcast())
530530
.clone();
531531
let tree = db.file_item_tree(definition.into());
532-
let def_attrs = tree.raw_attrs(AttrOwner::TopLevel).clone();
532+
let def_attrs = tree.top_level_raw_attrs().clone();
533533
decl_attrs.merge(def_attrs)
534534
}
535535
ModuleOrigin::CrateRoot { definition } => {
536536
let tree = db.file_item_tree(definition.into());
537-
tree.raw_attrs(AttrOwner::TopLevel).clone()
537+
tree.top_level_raw_attrs().clone()
538+
}
539+
ModuleOrigin::Inline { definition_tree_id, definition } => {
540+
definition_tree_id.item_tree(db).raw_attrs(definition.upcast()).clone()
538541
}
539-
ModuleOrigin::Inline { definition_tree_id, .. } => definition_tree_id
540-
.item_tree(db)
541-
.raw_attrs(AttrOwner::ModItem(definition_tree_id.value.into()))
542-
.clone(),
543542
ModuleOrigin::BlockExpr { id, .. } => {
544-
let tree = db.block_item_tree(id);
545-
tree.raw_attrs(AttrOwner::TopLevel).clone()
543+
let tree = block_item_tree_query(db, id);
544+
tree.top_level_raw_attrs().clone()
546545
}
547546
};
548547
Attrs::expand_cfg_attr(db, module.krate, raw_attrs)

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::{
2424
},
2525
hir::generics::GenericParams,
2626
import_map::ImportMap,
27-
item_tree::{AttrOwner, ItemTree},
27+
item_tree::{ItemTree, file_item_tree_query},
2828
lang_item::{self, LangItem},
2929
nameres::{
3030
assoc::{ImplItems, TraitItems},
@@ -108,11 +108,9 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + SourceDatabase {
108108
fn expand_proc_attr_macros(&self) -> bool;
109109

110110
/// Computes an [`ItemTree`] for the given file or macro expansion.
111-
#[salsa::invoke(ItemTree::file_item_tree_query)]
112-
fn file_item_tree(&self, file_id: HirFileId) -> Arc<ItemTree>;
113-
114-
#[salsa::invoke(ItemTree::block_item_tree_query)]
115-
fn block_item_tree(&self, block_id: BlockId) -> Arc<ItemTree>;
111+
#[salsa::invoke(file_item_tree_query)]
112+
#[salsa::transparent]
113+
fn file_item_tree(&self, file_id: HirFileId) -> &ItemTree;
116114

117115
/// Turns a MacroId into a MacroDefId, describing the macro's definition post name resolution.
118116
#[salsa::invoke(macro_def)]
@@ -376,7 +374,7 @@ fn include_macro_invoc(
376374
fn crate_supports_no_std(db: &dyn DefDatabase, crate_id: Crate) -> bool {
377375
let file = crate_id.data(db).root_file_id(db);
378376
let item_tree = db.file_item_tree(file.into());
379-
let attrs = item_tree.raw_attrs(AttrOwner::TopLevel);
377+
let attrs = item_tree.top_level_raw_attrs();
380378
for attr in &**attrs {
381379
match attr.path().as_ident() {
382380
Some(ident) if *ident == sym::no_std => return true,

0 commit comments

Comments
 (0)