Skip to content

Commit 5f9a582

Browse files
committed
Use ItemTree for crate root attr_query collection
1 parent 1aadd9d commit 5f9a582

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

crates/hir-def/src/attr.rs

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use tt::Subtree;
1919
use crate::{
2020
db::DefDatabase,
2121
intern::Interned,
22-
item_tree::{Fields, ItemTreeId, ItemTreeNode},
23-
nameres::ModuleSource,
22+
item_tree::{AttrOwner, Fields, ItemTreeId, ItemTreeNode},
23+
nameres::{ModuleOrigin, ModuleSource},
2424
path::{ModPath, PathKind},
2525
src::{HasChildSource, HasSource},
2626
AdtId, AttrDefId, EnumId, GenericParamId, LocalEnumVariantId, LocalFieldId, Lookup, MacroId,
@@ -201,6 +201,7 @@ impl Attrs {
201201
db: &dyn DefDatabase,
202202
e: EnumId,
203203
) -> Arc<ArenaMap<LocalEnumVariantId, Attrs>> {
204+
// FIXME: There should be some proper form of mapping between item tree enum variant ids and hir enum variant ids
204205
let mut res = ArenaMap::default();
205206

206207
let loc = e.lookup(db);
@@ -226,6 +227,7 @@ impl Attrs {
226227
db: &dyn DefDatabase,
227228
v: VariantId,
228229
) -> Arc<ArenaMap<LocalFieldId, Attrs>> {
230+
// FIXME: There should be some proper form of mapping between item tree field ids and hir field ids
229231
let mut res = ArenaMap::default();
230232

231233
let crate_graph = db.crate_graph();
@@ -295,11 +297,14 @@ impl Attrs {
295297

296298
impl Attrs {
297299
pub fn cfg(&self) -> Option<CfgExpr> {
298-
let mut cfgs = self.by_key("cfg").tt_values().map(CfgExpr::parse).collect::<Vec<_>>();
299-
match cfgs.len() {
300-
0 => None,
301-
1 => Some(cfgs.pop().unwrap()),
302-
_ => Some(CfgExpr::All(cfgs)),
300+
let mut cfgs = self.by_key("cfg").tt_values().map(CfgExpr::parse);
301+
let first = cfgs.next()?;
302+
match cfgs.next() {
303+
Some(second) => {
304+
let cfgs = [first, second].into_iter().chain(cfgs);
305+
Some(CfgExpr::All(cfgs.collect()))
306+
}
307+
None => Some(first),
303308
}
304309
}
305310
pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> bool {
@@ -367,25 +372,37 @@ impl AttrsWithOwner {
367372
AttrDefId::ModuleId(module) => {
368373
let def_map = module.def_map(db);
369374
let mod_data = &def_map[module.local_id];
370-
match mod_data.declaration_source(db) {
371-
Some(it) => {
375+
376+
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 };
372382
let raw_attrs = RawAttrs::from_attrs_owner(
373383
db,
374384
it.as_ref().map(|it| it as &dyn ast::HasAttrs),
375385
);
376-
match mod_data.definition_source(db) {
377-
InFile { file_id, value: ModuleSource::SourceFile(file) } => raw_attrs
378-
.merge(RawAttrs::from_attrs_owner(db, InFile::new(file_id, &file))),
379-
_ => raw_attrs,
380-
}
386+
let tree = db.file_item_tree(definition.into());
387+
raw_attrs.merge(tree.raw_attrs(AttrOwner::TopLevel).clone())
388+
}
389+
ModuleOrigin::CrateRoot { definition } => {
390+
let tree = db.file_item_tree(definition.into());
391+
tree.raw_attrs(AttrOwner::TopLevel).clone()
381392
}
382-
None => RawAttrs::from_attrs_owner(
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(
383396
db,
384-
mod_data.definition_source(db).as_ref().map(|src| match src {
385-
ModuleSource::SourceFile(file) => file as &dyn ast::HasAttrs,
386-
ModuleSource::Module(module) => module as &dyn ast::HasAttrs,
387-
ModuleSource::BlockExpr(block) => block as &dyn ast::HasAttrs,
388-
}),
397+
InFile::new(definition.file_id, definition.to_node(db.upcast()))
398+
.as_ref()
399+
.map(|it| it as &dyn ast::HasAttrs),
400+
),
401+
ModuleOrigin::BlockExpr { block } => RawAttrs::from_attrs_owner(
402+
db,
403+
InFile::new(block.file_id, block.to_node(db.upcast()))
404+
.as_ref()
405+
.map(|it| it as &dyn ast::HasAttrs),
389406
),
390407
}
391408
}

0 commit comments

Comments
 (0)