Skip to content

Commit 0081ef3

Browse files
committed
Use ItemTree for modules in attrs_query
1 parent 5f9a582 commit 0081ef3

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

crates/hir-def/src/attr.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -374,30 +374,23 @@ impl AttrsWithOwner {
374374
let mod_data = &def_map[module.local_id];
375375

376376
match mod_data.origin {
377-
// FIXME: We should be able to leverage the item tree instead of parsing declaration here
378-
// but we don't save the module's item tree id anywhere
379-
ModuleOrigin::File { definition, declaration, .. } => {
380-
let value = declaration.to_node(db.upcast());
381-
let it = InFile { file_id: declaration.file_id, value };
382-
let raw_attrs = RawAttrs::from_attrs_owner(
383-
db,
384-
it.as_ref().map(|it| it as &dyn ast::HasAttrs),
385-
);
377+
ModuleOrigin::File { definition, declaration_tree_id, .. } => {
378+
let decl_attrs = declaration_tree_id
379+
.item_tree(db)
380+
.raw_attrs(AttrOwner::ModItem(declaration_tree_id.value.into()))
381+
.clone();
386382
let tree = db.file_item_tree(definition.into());
387-
raw_attrs.merge(tree.raw_attrs(AttrOwner::TopLevel).clone())
383+
let def_attrs = tree.raw_attrs(AttrOwner::TopLevel).clone();
384+
decl_attrs.merge(def_attrs)
388385
}
389386
ModuleOrigin::CrateRoot { definition } => {
390387
let tree = db.file_item_tree(definition.into());
391388
tree.raw_attrs(AttrOwner::TopLevel).clone()
392389
}
393-
// FIXME: We should be able to leverage the item tree instead of parsing here
394-
// but we don't save the module's item tree id anywhere
395-
ModuleOrigin::Inline { definition } => RawAttrs::from_attrs_owner(
396-
db,
397-
InFile::new(definition.file_id, definition.to_node(db.upcast()))
398-
.as_ref()
399-
.map(|it| it as &dyn ast::HasAttrs),
400-
),
390+
ModuleOrigin::Inline { definition_tree_id, .. } => definition_tree_id
391+
.item_tree(db)
392+
.raw_attrs(AttrOwner::ModItem(definition_tree_id.value.into()))
393+
.clone(),
401394
ModuleOrigin::BlockExpr { block } => RawAttrs::from_attrs_owner(
402395
db,
403396
InFile::new(block.file_id, block.to_node(db.upcast()))

crates/hir-def/src/nameres.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use syntax::{ast, SmolStr};
7070
use crate::{
7171
db::DefDatabase,
7272
item_scope::{BuiltinShadowMode, ItemScope},
73-
item_tree::TreeId,
73+
item_tree::{ItemTreeId, Mod, TreeId},
7474
nameres::{diagnostics::DefDiagnostic, path_resolution::ResolveMode},
7575
path::ModPath,
7676
per_ns::PerNs,
@@ -141,9 +141,11 @@ pub enum ModuleOrigin {
141141
File {
142142
is_mod_rs: bool,
143143
declaration: AstId<ast::Module>,
144+
declaration_tree_id: ItemTreeId<Mod>,
144145
definition: FileId,
145146
},
146147
Inline {
148+
definition_tree_id: ItemTreeId<Mod>,
147149
definition: AstId<ast::Module>,
148150
},
149151
/// Pseudo-module introduced by a block scope (contains only inner items).
@@ -186,7 +188,7 @@ impl ModuleOrigin {
186188
let sf = db.parse(file_id).tree();
187189
InFile::new(file_id.into(), ModuleSource::SourceFile(sf))
188190
}
189-
ModuleOrigin::Inline { definition } => InFile::new(
191+
ModuleOrigin::Inline { definition, .. } => InFile::new(
190192
definition.file_id,
191193
ModuleSource::Module(definition.to_node(db.upcast())),
192194
),

crates/hir-def/src/nameres/collector.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,7 @@ impl ModCollector<'_, '_> {
15251525
};
15261526

15271527
match item {
1528-
ModItem::Mod(m) => self.collect_module(&self.item_tree[m], &attrs),
1528+
ModItem::Mod(m) => self.collect_module(m, &attrs),
15291529
ModItem::Import(import_id) => {
15301530
let imports = Import::from_use(
15311531
db,
@@ -1700,9 +1700,10 @@ impl ModCollector<'_, '_> {
17001700
}
17011701
}
17021702

1703-
fn collect_module(&mut self, module: &Mod, attrs: &Attrs) {
1703+
fn collect_module(&mut self, module_id: FileItemTreeId<Mod>, attrs: &Attrs) {
17041704
let path_attr = attrs.by_key("path").string_value();
17051705
let is_macro_use = attrs.by_key("macro_use").exists();
1706+
let module = &self.item_tree[module_id];
17061707
match &module.kind {
17071708
// inline module, just recurse
17081709
ModKind::Inline { items } => {
@@ -1711,6 +1712,7 @@ impl ModCollector<'_, '_> {
17111712
AstId::new(self.file_id(), module.ast_id),
17121713
None,
17131714
&self.item_tree[module.visibility],
1715+
module_id,
17141716
);
17151717

17161718
if let Some(mod_dir) = self.mod_dir.descend_into_definition(&module.name, path_attr)
@@ -1748,6 +1750,7 @@ impl ModCollector<'_, '_> {
17481750
ast_id,
17491751
Some((file_id, is_mod_rs)),
17501752
&self.item_tree[module.visibility],
1753+
module_id,
17511754
);
17521755
ModCollector {
17531756
def_collector: self.def_collector,
@@ -1774,6 +1777,7 @@ impl ModCollector<'_, '_> {
17741777
ast_id,
17751778
None,
17761779
&self.item_tree[module.visibility],
1780+
module_id,
17771781
);
17781782
self.def_collector.def_map.diagnostics.push(
17791783
DefDiagnostic::unresolved_module(self.module_id, ast_id, candidates),
@@ -1790,17 +1794,24 @@ impl ModCollector<'_, '_> {
17901794
declaration: AstId<ast::Module>,
17911795
definition: Option<(FileId, bool)>,
17921796
visibility: &crate::visibility::RawVisibility,
1797+
mod_tree_id: FileItemTreeId<Mod>,
17931798
) -> LocalModuleId {
17941799
let def_map = &mut self.def_collector.def_map;
17951800
let vis = def_map
17961801
.resolve_visibility(self.def_collector.db, self.module_id, visibility)
17971802
.unwrap_or(Visibility::Public);
17981803
let modules = &mut def_map.modules;
17991804
let origin = match definition {
1800-
None => ModuleOrigin::Inline { definition: declaration },
1801-
Some((definition, is_mod_rs)) => {
1802-
ModuleOrigin::File { declaration, definition, is_mod_rs }
1803-
}
1805+
None => ModuleOrigin::Inline {
1806+
definition: declaration,
1807+
definition_tree_id: ItemTreeId::new(self.tree_id, mod_tree_id),
1808+
},
1809+
Some((definition, is_mod_rs)) => ModuleOrigin::File {
1810+
declaration,
1811+
definition,
1812+
is_mod_rs,
1813+
declaration_tree_id: ItemTreeId::new(self.tree_id, mod_tree_id),
1814+
},
18041815
};
18051816

18061817
let res = modules.alloc(ModuleData::new(origin, vis));

0 commit comments

Comments
 (0)