Skip to content

Commit 77972e2

Browse files
Don't look at attributes when lowering to ItemTree
Resolves 2 `cfg_attr` FIXMEs
1 parent 25185c1 commit 77972e2

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

crates/hir_def/src/item_tree.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::{
1212
};
1313

1414
use arena::{Arena, Idx, RawId};
15-
use ast::{AstNode, AttrsOwner, NameOwner, StructKind};
15+
use ast::{AstNode, NameOwner, StructKind};
1616
use base_db::CrateId;
1717
use either::Either;
1818
use hir_expand::{
@@ -495,7 +495,6 @@ pub struct Import {
495495
pub alias: Option<ImportAlias>,
496496
pub visibility: RawVisibilityId,
497497
pub is_glob: bool,
498-
pub is_prelude: bool,
499498
/// AST ID of the `use` or `extern crate` item this import was derived from. Note that many
500499
/// `Import`s can map to the same `use` item.
501500
pub ast_id: FileAstId<ast::Use>,
@@ -511,8 +510,6 @@ pub struct ExternCrate {
511510
pub name: Name,
512511
pub alias: Option<ImportAlias>,
513512
pub visibility: RawVisibilityId,
514-
/// Whether this is a `#[macro_use] extern crate ...`.
515-
pub is_macro_use: bool,
516513
pub ast_id: FileAstId<ast::ExternCrate>,
517514
}
518515

crates/hir_def/src/item_tree/lower.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,6 @@ impl Ctx {
485485
}
486486

487487
fn lower_use(&mut self, use_item: &ast::Use) -> Vec<FileItemTreeId<Import>> {
488-
// FIXME: cfg_attr
489-
let is_prelude = use_item.has_atom_attr("prelude_import");
490488
let visibility = self.lower_visibility(use_item);
491489
let ast_id = self.source_ast_id_map.ast_id(use_item);
492490

@@ -502,7 +500,6 @@ impl Ctx {
502500
alias,
503501
visibility,
504502
is_glob,
505-
is_prelude,
506503
ast_id,
507504
index: imports.len(),
508505
})));
@@ -522,10 +519,8 @@ impl Ctx {
522519
});
523520
let visibility = self.lower_visibility(extern_crate);
524521
let ast_id = self.source_ast_id_map.ast_id(extern_crate);
525-
// FIXME: cfg_attr
526-
let is_macro_use = extern_crate.has_atom_attr("macro_use");
527522

528-
let res = ExternCrate { name, alias, visibility, is_macro_use, ast_id };
523+
let res = ExternCrate { name, alias, visibility, ast_id };
529524
Some(id(self.data().extern_crates.alloc(res)))
530525
}
531526

crates/hir_def/src/nameres/collector.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,35 @@ struct Import {
136136
}
137137

138138
impl Import {
139-
fn from_use(tree: &ItemTree, id: ItemTreeId<item_tree::Import>) -> Self {
139+
fn from_use(
140+
db: &dyn DefDatabase,
141+
krate: CrateId,
142+
tree: &ItemTree,
143+
id: ItemTreeId<item_tree::Import>,
144+
) -> Self {
140145
let it = &tree[id.value];
146+
let attrs = &tree.attrs(db, krate, ModItem::from(id.value).into());
141147
let visibility = &tree[it.visibility];
142148
Self {
143149
path: it.path.clone(),
144150
alias: it.alias.clone(),
145151
visibility: visibility.clone(),
146152
is_glob: it.is_glob,
147-
is_prelude: it.is_prelude,
153+
is_prelude: attrs.by_key("prelude_import").exists(),
148154
is_extern_crate: false,
149155
is_macro_use: false,
150156
source: ImportSource::Import(id),
151157
}
152158
}
153159

154-
fn from_extern_crate(tree: &ItemTree, id: ItemTreeId<item_tree::ExternCrate>) -> Self {
160+
fn from_extern_crate(
161+
db: &dyn DefDatabase,
162+
krate: CrateId,
163+
tree: &ItemTree,
164+
id: ItemTreeId<item_tree::ExternCrate>,
165+
) -> Self {
155166
let it = &tree[id.value];
167+
let attrs = &tree.attrs(db, krate, ModItem::from(id.value).into());
156168
let visibility = &tree[it.visibility];
157169
Self {
158170
path: ModPath::from_segments(PathKind::Plain, iter::once(it.name.clone())),
@@ -161,7 +173,7 @@ impl Import {
161173
is_glob: false,
162174
is_prelude: false,
163175
is_extern_crate: true,
164-
is_macro_use: it.is_macro_use,
176+
is_macro_use: attrs.by_key("macro_use").exists(),
165177
source: ImportSource::ExternCrate(id),
166178
}
167179
}
@@ -930,7 +942,12 @@ impl ModCollector<'_, '_> {
930942
if attrs.cfg().map_or(true, |cfg| self.is_cfg_enabled(&cfg)) {
931943
if let ModItem::ExternCrate(id) = item {
932944
let import = self.item_tree[*id].clone();
933-
if import.is_macro_use {
945+
let attrs = self.item_tree.attrs(
946+
self.def_collector.db,
947+
krate,
948+
ModItem::from(*id).into(),
949+
);
950+
if attrs.by_key("macro_use").exists() {
934951
self.def_collector.import_macros_from_extern_crate(self.module_id, &import);
935952
}
936953
}
@@ -956,6 +973,8 @@ impl ModCollector<'_, '_> {
956973
self.def_collector.unresolved_imports.push(ImportDirective {
957974
module_id: self.module_id,
958975
import: Import::from_use(
976+
self.def_collector.db,
977+
krate,
959978
&self.item_tree,
960979
InFile::new(self.file_id, import_id),
961980
),
@@ -966,6 +985,8 @@ impl ModCollector<'_, '_> {
966985
self.def_collector.unresolved_imports.push(ImportDirective {
967986
module_id: self.module_id,
968987
import: Import::from_extern_crate(
988+
self.def_collector.db,
989+
krate,
969990
&self.item_tree,
970991
InFile::new(self.file_id, import_id),
971992
),

0 commit comments

Comments
 (0)