From 77972e2001d727155fe4ead78cf5e3994f24211d Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 18 Dec 2020 20:37:26 +0100 Subject: Don't look at attributes when lowering to ItemTree Resolves 2 `cfg_attr` FIXMEs --- crates/hir_def/src/item_tree.rs | 5 +---- crates/hir_def/src/item_tree/lower.rs | 7 +------ crates/hir_def/src/nameres/collector.rs | 31 ++++++++++++++++++++++++++----- 3 files changed, 28 insertions(+), 15 deletions(-) (limited to 'crates/hir_def') diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index 5eb7cae7f..100dbf5d6 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs @@ -12,7 +12,7 @@ use std::{ }; use arena::{Arena, Idx, RawId}; -use ast::{AstNode, AttrsOwner, NameOwner, StructKind}; +use ast::{AstNode, NameOwner, StructKind}; use base_db::CrateId; use either::Either; use hir_expand::{ @@ -495,7 +495,6 @@ pub struct Import { pub alias: Option, pub visibility: RawVisibilityId, pub is_glob: bool, - pub is_prelude: bool, /// AST ID of the `use` or `extern crate` item this import was derived from. Note that many /// `Import`s can map to the same `use` item. pub ast_id: FileAstId, @@ -511,8 +510,6 @@ pub struct ExternCrate { pub name: Name, pub alias: Option, pub visibility: RawVisibilityId, - /// Whether this is a `#[macro_use] extern crate ...`. - pub is_macro_use: bool, pub ast_id: FileAstId, } diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs index c8f090c22..3b206ef85 100644 --- a/crates/hir_def/src/item_tree/lower.rs +++ b/crates/hir_def/src/item_tree/lower.rs @@ -485,8 +485,6 @@ impl Ctx { } fn lower_use(&mut self, use_item: &ast::Use) -> Vec> { - // FIXME: cfg_attr - let is_prelude = use_item.has_atom_attr("prelude_import"); let visibility = self.lower_visibility(use_item); let ast_id = self.source_ast_id_map.ast_id(use_item); @@ -502,7 +500,6 @@ impl Ctx { alias, visibility, is_glob, - is_prelude, ast_id, index: imports.len(), }))); @@ -522,10 +519,8 @@ impl Ctx { }); let visibility = self.lower_visibility(extern_crate); let ast_id = self.source_ast_id_map.ast_id(extern_crate); - // FIXME: cfg_attr - let is_macro_use = extern_crate.has_atom_attr("macro_use"); - let res = ExternCrate { name, alias, visibility, is_macro_use, ast_id }; + let res = ExternCrate { name, alias, visibility, ast_id }; Some(id(self.data().extern_crates.alloc(res))) } diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index b114a6fe4..55228e480 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -136,23 +136,35 @@ struct Import { } impl Import { - fn from_use(tree: &ItemTree, id: ItemTreeId) -> Self { + fn from_use( + db: &dyn DefDatabase, + krate: CrateId, + tree: &ItemTree, + id: ItemTreeId, + ) -> Self { let it = &tree[id.value]; + let attrs = &tree.attrs(db, krate, ModItem::from(id.value).into()); let visibility = &tree[it.visibility]; Self { path: it.path.clone(), alias: it.alias.clone(), visibility: visibility.clone(), is_glob: it.is_glob, - is_prelude: it.is_prelude, + is_prelude: attrs.by_key("prelude_import").exists(), is_extern_crate: false, is_macro_use: false, source: ImportSource::Import(id), } } - fn from_extern_crate(tree: &ItemTree, id: ItemTreeId) -> Self { + fn from_extern_crate( + db: &dyn DefDatabase, + krate: CrateId, + tree: &ItemTree, + id: ItemTreeId, + ) -> Self { let it = &tree[id.value]; + let attrs = &tree.attrs(db, krate, ModItem::from(id.value).into()); let visibility = &tree[it.visibility]; Self { path: ModPath::from_segments(PathKind::Plain, iter::once(it.name.clone())), @@ -161,7 +173,7 @@ impl Import { is_glob: false, is_prelude: false, is_extern_crate: true, - is_macro_use: it.is_macro_use, + is_macro_use: attrs.by_key("macro_use").exists(), source: ImportSource::ExternCrate(id), } } @@ -930,7 +942,12 @@ impl ModCollector<'_, '_> { if attrs.cfg().map_or(true, |cfg| self.is_cfg_enabled(&cfg)) { if let ModItem::ExternCrate(id) = item { let import = self.item_tree[*id].clone(); - if import.is_macro_use { + let attrs = self.item_tree.attrs( + self.def_collector.db, + krate, + ModItem::from(*id).into(), + ); + if attrs.by_key("macro_use").exists() { self.def_collector.import_macros_from_extern_crate(self.module_id, &import); } } @@ -956,6 +973,8 @@ impl ModCollector<'_, '_> { self.def_collector.unresolved_imports.push(ImportDirective { module_id: self.module_id, import: Import::from_use( + self.def_collector.db, + krate, &self.item_tree, InFile::new(self.file_id, import_id), ), @@ -966,6 +985,8 @@ impl ModCollector<'_, '_> { self.def_collector.unresolved_imports.push(ImportDirective { module_id: self.module_id, import: Import::from_extern_crate( + self.def_collector.db, + krate, &self.item_tree, InFile::new(self.file_id, import_id), ), -- cgit v1.2.3