Remove `AttrOwner`
diff --git a/crates/hir-def/src/attr.rs b/crates/hir-def/src/attr.rs
index 08986d6..d63bd39 100644
--- a/crates/hir-def/src/attr.rs
+++ b/crates/hir-def/src/attr.rs
@@ -26,7 +26,6 @@
AdtId, AstIdLoc, AttrDefId, GenericParamId, HasModule, LocalFieldId, Lookup, MacroId,
VariantId,
db::DefDatabase,
- item_tree::AttrOwner,
lang_item::LangItem,
nameres::{ModuleOrigin, ModuleSource},
src::{HasChildSource, HasSource},
@@ -526,23 +525,22 @@
ModuleOrigin::File { definition, declaration_tree_id, declaration, .. } => {
let decl_attrs = declaration_tree_id
.item_tree(db)
- .raw_attrs(AttrOwner::Item(declaration.erase()))
+ .raw_attrs(declaration.upcast())
.clone();
let tree = db.file_item_tree(definition.into());
- let def_attrs = tree.raw_attrs(AttrOwner::TopLevel).clone();
+ let def_attrs = tree.top_level_raw_attrs().clone();
decl_attrs.merge(def_attrs)
}
ModuleOrigin::CrateRoot { definition } => {
let tree = db.file_item_tree(definition.into());
- tree.raw_attrs(AttrOwner::TopLevel).clone()
+ tree.top_level_raw_attrs().clone()
}
- ModuleOrigin::Inline { definition_tree_id, definition } => definition_tree_id
- .item_tree(db)
- .raw_attrs(AttrOwner::Item(definition.erase()))
- .clone(),
+ ModuleOrigin::Inline { definition_tree_id, definition } => {
+ definition_tree_id.item_tree(db).raw_attrs(definition.upcast()).clone()
+ }
ModuleOrigin::BlockExpr { id, .. } => {
let tree = db.block_item_tree(id);
- tree.raw_attrs(AttrOwner::TopLevel).clone()
+ tree.top_level_raw_attrs().clone()
}
};
Attrs::expand_cfg_attr(db, module.krate, raw_attrs)
diff --git a/crates/hir-def/src/db.rs b/crates/hir-def/src/db.rs
index 95e33dd..5f99693 100644
--- a/crates/hir-def/src/db.rs
+++ b/crates/hir-def/src/db.rs
@@ -376,7 +376,7 @@
fn crate_supports_no_std(db: &dyn DefDatabase, crate_id: Crate) -> bool {
let file = crate_id.data(db).root_file_id(db);
let item_tree = db.file_item_tree(file.into());
- let attrs = item_tree.raw_attrs(crate::item_tree::AttrOwner::TopLevel);
+ let attrs = item_tree.top_level_raw_attrs();
for attr in &**attrs {
match attr.path().as_ident() {
Some(ident) if *ident == sym::no_std => return true,
diff --git a/crates/hir-def/src/item_tree.rs b/crates/hir-def/src/item_tree.rs
index 9f8d7b9..79f4b17 100644
--- a/crates/hir-def/src/item_tree.rs
+++ b/crates/hir-def/src/item_tree.rs
@@ -53,7 +53,7 @@
use intern::Interned;
use la_arena::Idx;
use rustc_hash::FxHashMap;
-use span::{AstIdNode, Edition, ErasedFileAstId, FileAstId, SyntaxContext};
+use span::{AstIdNode, Edition, FileAstId, SyntaxContext};
use stdx::never;
use syntax::{SyntaxKind, ast, match_ast};
use triomphe::Arc;
@@ -89,9 +89,8 @@
#[derive(Debug, Default, Eq, PartialEq)]
pub struct ItemTree {
top_level: Box<[ModItemId]>,
- // Consider splitting this into top level RawAttrs and the map?
- attrs: FxHashMap<AttrOwner, RawAttrs>,
-
+ top_attrs: RawAttrs,
+ attrs: FxHashMap<FileAstId<ast::Item>, RawAttrs>,
vis: ItemVisibilities,
// FIXME: They values store the key, turn this into a FxHashSet<ModItem> instead?
data: FxHashMap<FileAstId<ast::Item>, ModItem>,
@@ -104,12 +103,13 @@
let ctx = lower::Ctx::new(db, file_id);
let syntax = db.parse_or_expand(file_id);
- let mut top_attrs = None;
let mut item_tree = match_ast! {
match syntax {
ast::SourceFile(file) => {
- top_attrs = Some(RawAttrs::new(db, &file, ctx.span_map()));
- ctx.lower_module_items(&file)
+ let top_attrs = RawAttrs::new(db, &file, ctx.span_map());
+ let mut item_tree = ctx.lower_module_items(&file);
+ item_tree.top_attrs = top_attrs;
+ item_tree
},
ast::MacroItems(items) => {
ctx.lower_module_items(&items)
@@ -128,10 +128,10 @@
}
};
- if let Some(attrs) = top_attrs {
- item_tree.attrs.insert(AttrOwner::TopLevel, attrs);
- }
- if item_tree.data.is_empty() && item_tree.top_level.is_empty() && item_tree.attrs.is_empty()
+ if item_tree.data.is_empty()
+ && item_tree.top_level.is_empty()
+ && item_tree.attrs.is_empty()
+ && item_tree.top_attrs.is_empty()
{
EMPTY
.get_or_init(|| {
@@ -139,6 +139,7 @@
top_level: Box::new([]),
attrs: FxHashMap::default(),
data: FxHashMap::default(),
+ top_attrs: RawAttrs::EMPTY,
vis: ItemVisibilities { arena: Box::new([]) },
})
})
@@ -158,7 +159,10 @@
let ctx = lower::Ctx::new(db, loc.ast_id.file_id);
let mut item_tree = ctx.lower_block(&block);
- if item_tree.data.is_empty() && item_tree.top_level.is_empty() && item_tree.attrs.is_empty()
+ if item_tree.data.is_empty()
+ && item_tree.top_level.is_empty()
+ && item_tree.attrs.is_empty()
+ && item_tree.top_attrs.is_empty()
{
EMPTY
.get_or_init(|| {
@@ -166,6 +170,7 @@
top_level: Box::new([]),
attrs: FxHashMap::default(),
data: FxHashMap::default(),
+ top_attrs: RawAttrs::EMPTY,
vis: ItemVisibilities { arena: Box::new([]) },
})
})
@@ -183,19 +188,25 @@
}
/// Returns the inner attributes of the source file.
- pub fn top_level_attrs(&self, db: &dyn DefDatabase, krate: Crate) -> Attrs {
- Attrs::expand_cfg_attr(
- db,
- krate,
- self.attrs.get(&AttrOwner::TopLevel).unwrap_or(&RawAttrs::EMPTY).clone(),
- )
+ pub fn top_level_raw_attrs(&self) -> &RawAttrs {
+ &self.top_attrs
}
- pub(crate) fn raw_attrs(&self, of: AttrOwner) -> &RawAttrs {
+ /// Returns the inner attributes of the source file.
+ pub fn top_level_attrs(&self, db: &dyn DefDatabase, krate: Crate) -> Attrs {
+ Attrs::expand_cfg_attr(db, krate, self.top_attrs.clone())
+ }
+
+ pub(crate) fn raw_attrs(&self, of: FileAstId<ast::Item>) -> &RawAttrs {
self.attrs.get(&of).unwrap_or(&RawAttrs::EMPTY)
}
- pub(crate) fn attrs(&self, db: &dyn DefDatabase, krate: Crate, of: AttrOwner) -> Attrs {
+ pub(crate) fn attrs(
+ &self,
+ db: &dyn DefDatabase,
+ krate: Crate,
+ of: FileAstId<ast::Item>,
+ ) -> Attrs {
Attrs::expand_cfg_attr(db, krate, self.raw_attrs(of).clone())
}
@@ -226,7 +237,7 @@
}
fn shrink_to_fit(&mut self) {
- let ItemTree { top_level: _, attrs, data, vis: _ } = self;
+ let ItemTree { top_level: _, attrs, data, vis: _, top_attrs: _ } = self;
attrs.shrink_to_fit();
data.shrink_to_fit();
}
@@ -266,21 +277,6 @@
pub macro_rules: usize,
}
-#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
-pub enum AttrOwner {
- /// Attributes on an item.
- Item(ErasedFileAstId),
- /// Inner attributes of the source file.
- TopLevel,
-}
-
-impl From<ModItemId> for AttrOwner {
- #[inline]
- fn from(value: ModItemId) -> Self {
- AttrOwner::Item(value.ast_id().erase())
- }
-}
-
/// Trait implemented by all nodes in the item tree.
pub trait ItemTreeNode: Clone {
type Source: AstIdNode;
diff --git a/crates/hir-def/src/item_tree/lower.rs b/crates/hir-def/src/item_tree/lower.rs
index a703835..c3089ee 100644
--- a/crates/hir-def/src/item_tree/lower.rs
+++ b/crates/hir-def/src/item_tree/lower.rs
@@ -10,7 +10,7 @@
span_map::{SpanMap, SpanMapRef},
};
use la_arena::Arena;
-use span::{AstIdMap, SyntaxContext};
+use span::{AstIdMap, FileAstId, SyntaxContext};
use syntax::{
AstNode,
ast::{self, HasModuleItem, HasName},
@@ -20,10 +20,10 @@
use crate::{
db::DefDatabase,
item_tree::{
- AttrOwner, Const, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl, ImportAlias,
- Interned, ItemTree, ItemTreeAstId, Macro2, MacroCall, MacroRules, Mod, ModItem, ModItemId,
- ModKind, ModPath, RawAttrs, RawVisibility, RawVisibilityId, Static, Struct, StructKind,
- Trait, TraitAlias, TypeAlias, Union, Use, UseTree, UseTreeKind, VisibilityExplicitness,
+ Const, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl, ImportAlias, Interned,
+ ItemTree, ItemTreeAstId, Macro2, MacroCall, MacroRules, Mod, ModItem, ModItemId, ModKind,
+ ModPath, RawAttrs, RawVisibility, RawVisibilityId, Static, Struct, StructKind, Trait,
+ TraitAlias, TypeAlias, Union, Use, UseTree, UseTreeKind, VisibilityExplicitness,
},
};
@@ -97,7 +97,7 @@
}
pub(super) fn lower_block(mut self, block: &ast::BlockExpr) -> ItemTree {
- self.tree.attrs.insert(AttrOwner::TopLevel, RawAttrs::new(self.db, block, self.span_map()));
+ self.tree.top_attrs = RawAttrs::new(self.db, block, self.span_map());
self.top_level = block
.statements()
.filter_map(|stmt| match stmt {
@@ -144,12 +144,12 @@
ast::Item::ExternBlock(ast) => self.lower_extern_block(ast).into(),
};
let attrs = RawAttrs::new(self.db, item, self.span_map());
- self.add_attrs(mod_item.into(), attrs);
+ self.add_attrs(mod_item.ast_id(), attrs);
Some(mod_item)
}
- fn add_attrs(&mut self, item: AttrOwner, attrs: RawAttrs) {
+ fn add_attrs(&mut self, item: FileAstId<ast::Item>, attrs: RawAttrs) {
if !attrs.is_empty() {
match self.tree.attrs.entry(item) {
Entry::Occupied(mut entry) => {
@@ -365,7 +365,7 @@
ast::ExternItem::MacroCall(call) => self.lower_macro_call(call)?.into(),
};
let attrs = RawAttrs::new(self.db, &item, self.span_map());
- self.add_attrs(mod_item.into(), attrs);
+ self.add_attrs(mod_item.ast_id(), attrs);
Some(mod_item)
})
.collect()
diff --git a/crates/hir-def/src/item_tree/pretty.rs b/crates/hir-def/src/item_tree/pretty.rs
index 9a5ca73..11b9156 100644
--- a/crates/hir-def/src/item_tree/pretty.rs
+++ b/crates/hir-def/src/item_tree/pretty.rs
@@ -6,10 +6,9 @@
use crate::{
item_tree::{
- AttrOwner, Const, DefDatabase, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl,
- ItemTree, Macro2, MacroCall, MacroRules, Mod, ModItemId, ModKind, RawAttrs,
- RawVisibilityId, Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use, UseTree,
- UseTreeKind,
+ Const, DefDatabase, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl, ItemTree,
+ Macro2, MacroCall, MacroRules, Mod, ModItemId, ModKind, RawAttrs, RawVisibilityId, Static,
+ Struct, Trait, TraitAlias, TypeAlias, Union, Use, UseTree, UseTreeKind,
},
visibility::RawVisibility,
};
@@ -18,9 +17,7 @@
let mut p =
Printer { db, tree, buf: String::new(), indent_level: 0, needs_indent: true, edition };
- if let Some(attrs) = tree.attrs.get(&AttrOwner::TopLevel) {
- p.print_attrs(attrs, true, "\n");
- }
+ p.print_attrs(&tree.top_attrs, true, "\n");
p.blank();
for item in tree.top_level_items() {
@@ -102,8 +99,8 @@
}
}
- fn print_attrs_of(&mut self, of: impl Into<AttrOwner>, separated_by: &str) {
- if let Some(attrs) = self.tree.attrs.get(&of.into()) {
+ fn print_attrs_of(&mut self, of: ModItemId, separated_by: &str) {
+ if let Some(attrs) = self.tree.attrs.get(&of.ast_id()) {
self.print_attrs(attrs, false, separated_by);
}
}
diff --git a/crates/hir-def/src/nameres/collector.rs b/crates/hir-def/src/nameres/collector.rs
index 3dc76dc..a6be3f1 100644
--- a/crates/hir-def/src/nameres/collector.rs
+++ b/crates/hir-def/src/nameres/collector.rs
@@ -35,8 +35,8 @@
db::DefDatabase,
item_scope::{GlobId, ImportId, ImportOrExternCrate, PerNsGlobImports},
item_tree::{
- self, AttrOwner, FieldsShape, ImportAlias, ImportKind, ItemTree, ItemTreeAstId,
- ItemTreeNode, Macro2, MacroCall, MacroRules, Mod, ModItemId, ModKind, TreeId, UseTreeKind,
+ self, FieldsShape, ImportAlias, ImportKind, ItemTree, ItemTreeAstId, ItemTreeNode, Macro2,
+ MacroCall, MacroRules, Mod, ModItemId, ModKind, TreeId, UseTreeKind,
},
macro_call_as_call_id,
nameres::{
@@ -1727,7 +1727,7 @@
};
let mut process_mod_item = |item: ModItemId| {
- let attrs = self.item_tree.attrs(db, krate, item.into());
+ let attrs = self.item_tree.attrs(db, krate, item.ast_id());
if let Some(cfg) = attrs.cfg() {
if !self.is_cfg_enabled(&cfg) {
let ast_id = item.ast_id().erase();
@@ -2298,7 +2298,7 @@
fn collect_macro_rules(&mut self, id: ItemTreeAstId<MacroRules>, module: ModuleId) {
let krate = self.def_collector.def_map.krate;
let mac = &self.item_tree[id];
- let attrs = self.item_tree.attrs(self.def_collector.db, krate, AttrOwner::Item(id.erase()));
+ let attrs = self.item_tree.attrs(self.def_collector.db, krate, id.upcast());
let ast_id = InFile::new(self.file_id(), mac.ast_id.upcast());
let export_attr = || attrs.by_key(sym::macro_export);
@@ -2387,7 +2387,7 @@
// Case 1: builtin macros
let mut helpers_opt = None;
- let attrs = self.item_tree.attrs(self.def_collector.db, krate, AttrOwner::Item(id.erase()));
+ let attrs = self.item_tree.attrs(self.def_collector.db, krate, id.upcast());
let expander = if attrs.by_key(sym::rustc_builtin_macro).exists() {
if let Some(expander) = find_builtin_macro(&mac.name) {
match expander {