Skip to content

Commit dfa9d68

Browse files
committed
Remove AttrOwner
1 parent 762efdf commit dfa9d68

File tree

6 files changed

+59
-68
lines changed

6 files changed

+59
-68
lines changed

src/tools/rust-analyzer/crates/hir-def/src/attr.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use crate::{
2626
AdtId, AstIdLoc, AttrDefId, GenericParamId, HasModule, LocalFieldId, Lookup, MacroId,
2727
VariantId,
2828
db::DefDatabase,
29-
item_tree::AttrOwner,
3029
lang_item::LangItem,
3130
nameres::{ModuleOrigin, ModuleSource},
3231
src::{HasChildSource, HasSource},
@@ -526,23 +525,22 @@ impl AttrsWithOwner {
526525
ModuleOrigin::File { definition, declaration_tree_id, declaration, .. } => {
527526
let decl_attrs = declaration_tree_id
528527
.item_tree(db)
529-
.raw_attrs(AttrOwner::Item(declaration.erase()))
528+
.raw_attrs(declaration.upcast())
530529
.clone();
531530
let tree = db.file_item_tree(definition.into());
532-
let def_attrs = tree.raw_attrs(AttrOwner::TopLevel).clone();
531+
let def_attrs = tree.top_level_raw_attrs().clone();
533532
decl_attrs.merge(def_attrs)
534533
}
535534
ModuleOrigin::CrateRoot { definition } => {
536535
let tree = db.file_item_tree(definition.into());
537-
tree.raw_attrs(AttrOwner::TopLevel).clone()
536+
tree.top_level_raw_attrs().clone()
537+
}
538+
ModuleOrigin::Inline { definition_tree_id, definition } => {
539+
definition_tree_id.item_tree(db).raw_attrs(definition.upcast()).clone()
538540
}
539-
ModuleOrigin::Inline { definition_tree_id, definition } => definition_tree_id
540-
.item_tree(db)
541-
.raw_attrs(AttrOwner::Item(definition.erase()))
542-
.clone(),
543541
ModuleOrigin::BlockExpr { id, .. } => {
544542
let tree = db.block_item_tree(id);
545-
tree.raw_attrs(AttrOwner::TopLevel).clone()
543+
tree.top_level_raw_attrs().clone()
546544
}
547545
};
548546
Attrs::expand_cfg_attr(db, module.krate, raw_attrs)

src/tools/rust-analyzer/crates/hir-def/src/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ fn include_macro_invoc(
376376
fn crate_supports_no_std(db: &dyn DefDatabase, crate_id: Crate) -> bool {
377377
let file = crate_id.data(db).root_file_id(db);
378378
let item_tree = db.file_item_tree(file.into());
379-
let attrs = item_tree.raw_attrs(crate::item_tree::AttrOwner::TopLevel);
379+
let attrs = item_tree.top_level_raw_attrs();
380380
for attr in &**attrs {
381381
match attr.path().as_ident() {
382382
Some(ident) if *ident == sym::no_std => return true,

src/tools/rust-analyzer/crates/hir-def/src/item_tree.rs

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use hir_expand::{
5353
use intern::Interned;
5454
use la_arena::Idx;
5555
use rustc_hash::FxHashMap;
56-
use span::{AstIdNode, Edition, ErasedFileAstId, FileAstId, SyntaxContext};
56+
use span::{AstIdNode, Edition, FileAstId, SyntaxContext};
5757
use stdx::never;
5858
use syntax::{SyntaxKind, ast, match_ast};
5959
use triomphe::Arc;
@@ -89,9 +89,8 @@ impl fmt::Debug for RawVisibilityId {
8989
#[derive(Debug, Default, Eq, PartialEq)]
9090
pub struct ItemTree {
9191
top_level: Box<[ModItemId]>,
92-
// Consider splitting this into top level RawAttrs and the map?
93-
attrs: FxHashMap<AttrOwner, RawAttrs>,
94-
92+
top_attrs: RawAttrs,
93+
attrs: FxHashMap<FileAstId<ast::Item>, RawAttrs>,
9594
vis: ItemVisibilities,
9695
// FIXME: They values store the key, turn this into a FxHashSet<ModItem> instead?
9796
data: FxHashMap<FileAstId<ast::Item>, ModItem>,
@@ -104,12 +103,13 @@ impl ItemTree {
104103

105104
let ctx = lower::Ctx::new(db, file_id);
106105
let syntax = db.parse_or_expand(file_id);
107-
let mut top_attrs = None;
108106
let mut item_tree = match_ast! {
109107
match syntax {
110108
ast::SourceFile(file) => {
111-
top_attrs = Some(RawAttrs::new(db, &file, ctx.span_map()));
112-
ctx.lower_module_items(&file)
109+
let top_attrs = RawAttrs::new(db, &file, ctx.span_map());
110+
let mut item_tree = ctx.lower_module_items(&file);
111+
item_tree.top_attrs = top_attrs;
112+
item_tree
113113
},
114114
ast::MacroItems(items) => {
115115
ctx.lower_module_items(&items)
@@ -128,17 +128,18 @@ impl ItemTree {
128128
}
129129
};
130130

131-
if let Some(attrs) = top_attrs {
132-
item_tree.attrs.insert(AttrOwner::TopLevel, attrs);
133-
}
134-
if item_tree.data.is_empty() && item_tree.top_level.is_empty() && item_tree.attrs.is_empty()
131+
if item_tree.data.is_empty()
132+
&& item_tree.top_level.is_empty()
133+
&& item_tree.attrs.is_empty()
134+
&& item_tree.top_attrs.is_empty()
135135
{
136136
EMPTY
137137
.get_or_init(|| {
138138
Arc::new(ItemTree {
139139
top_level: Box::new([]),
140140
attrs: FxHashMap::default(),
141141
data: FxHashMap::default(),
142+
top_attrs: RawAttrs::EMPTY,
142143
vis: ItemVisibilities { arena: Box::new([]) },
143144
})
144145
})
@@ -158,14 +159,18 @@ impl ItemTree {
158159

159160
let ctx = lower::Ctx::new(db, loc.ast_id.file_id);
160161
let mut item_tree = ctx.lower_block(&block);
161-
if item_tree.data.is_empty() && item_tree.top_level.is_empty() && item_tree.attrs.is_empty()
162+
if item_tree.data.is_empty()
163+
&& item_tree.top_level.is_empty()
164+
&& item_tree.attrs.is_empty()
165+
&& item_tree.top_attrs.is_empty()
162166
{
163167
EMPTY
164168
.get_or_init(|| {
165169
Arc::new(ItemTree {
166170
top_level: Box::new([]),
167171
attrs: FxHashMap::default(),
168172
data: FxHashMap::default(),
173+
top_attrs: RawAttrs::EMPTY,
169174
vis: ItemVisibilities { arena: Box::new([]) },
170175
})
171176
})
@@ -182,20 +187,26 @@ impl ItemTree {
182187
&self.top_level
183188
}
184189

190+
/// Returns the inner attributes of the source file.
191+
pub fn top_level_raw_attrs(&self) -> &RawAttrs {
192+
&self.top_attrs
193+
}
194+
185195
/// Returns the inner attributes of the source file.
186196
pub fn top_level_attrs(&self, db: &dyn DefDatabase, krate: Crate) -> Attrs {
187-
Attrs::expand_cfg_attr(
188-
db,
189-
krate,
190-
self.attrs.get(&AttrOwner::TopLevel).unwrap_or(&RawAttrs::EMPTY).clone(),
191-
)
197+
Attrs::expand_cfg_attr(db, krate, self.top_attrs.clone())
192198
}
193199

194-
pub(crate) fn raw_attrs(&self, of: AttrOwner) -> &RawAttrs {
200+
pub(crate) fn raw_attrs(&self, of: FileAstId<ast::Item>) -> &RawAttrs {
195201
self.attrs.get(&of).unwrap_or(&RawAttrs::EMPTY)
196202
}
197203

198-
pub(crate) fn attrs(&self, db: &dyn DefDatabase, krate: Crate, of: AttrOwner) -> Attrs {
204+
pub(crate) fn attrs(
205+
&self,
206+
db: &dyn DefDatabase,
207+
krate: Crate,
208+
of: FileAstId<ast::Item>,
209+
) -> Attrs {
199210
Attrs::expand_cfg_attr(db, krate, self.raw_attrs(of).clone())
200211
}
201212

@@ -226,7 +237,7 @@ impl ItemTree {
226237
}
227238

228239
fn shrink_to_fit(&mut self) {
229-
let ItemTree { top_level: _, attrs, data, vis: _ } = self;
240+
let ItemTree { top_level: _, attrs, data, vis: _, top_attrs: _ } = self;
230241
attrs.shrink_to_fit();
231242
data.shrink_to_fit();
232243
}
@@ -266,21 +277,6 @@ pub struct ItemTreeDataStats {
266277
pub macro_rules: usize,
267278
}
268279

269-
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
270-
pub enum AttrOwner {
271-
/// Attributes on an item.
272-
Item(ErasedFileAstId),
273-
/// Inner attributes of the source file.
274-
TopLevel,
275-
}
276-
277-
impl From<ModItemId> for AttrOwner {
278-
#[inline]
279-
fn from(value: ModItemId) -> Self {
280-
AttrOwner::Item(value.ast_id().erase())
281-
}
282-
}
283-
284280
/// Trait implemented by all nodes in the item tree.
285281
pub trait ItemTreeNode: Clone {
286282
type Source: AstIdNode;

src/tools/rust-analyzer/crates/hir-def/src/item_tree/lower.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use hir_expand::{
1010
span_map::{SpanMap, SpanMapRef},
1111
};
1212
use la_arena::Arena;
13-
use span::{AstIdMap, SyntaxContext};
13+
use span::{AstIdMap, FileAstId, SyntaxContext};
1414
use syntax::{
1515
AstNode,
1616
ast::{self, HasModuleItem, HasName},
@@ -20,10 +20,10 @@ use triomphe::Arc;
2020
use crate::{
2121
db::DefDatabase,
2222
item_tree::{
23-
AttrOwner, Const, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl, ImportAlias,
24-
Interned, ItemTree, ItemTreeAstId, Macro2, MacroCall, MacroRules, Mod, ModItem, ModItemId,
25-
ModKind, ModPath, RawAttrs, RawVisibility, RawVisibilityId, Static, Struct, StructKind,
26-
Trait, TraitAlias, TypeAlias, Union, Use, UseTree, UseTreeKind, VisibilityExplicitness,
23+
Const, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl, ImportAlias, Interned,
24+
ItemTree, ItemTreeAstId, Macro2, MacroCall, MacroRules, Mod, ModItem, ModItemId, ModKind,
25+
ModPath, RawAttrs, RawVisibility, RawVisibilityId, Static, Struct, StructKind, Trait,
26+
TraitAlias, TypeAlias, Union, Use, UseTree, UseTreeKind, VisibilityExplicitness,
2727
},
2828
};
2929

@@ -97,7 +97,7 @@ impl<'a> Ctx<'a> {
9797
}
9898

9999
pub(super) fn lower_block(mut self, block: &ast::BlockExpr) -> ItemTree {
100-
self.tree.attrs.insert(AttrOwner::TopLevel, RawAttrs::new(self.db, block, self.span_map()));
100+
self.tree.top_attrs = RawAttrs::new(self.db, block, self.span_map());
101101
self.top_level = block
102102
.statements()
103103
.filter_map(|stmt| match stmt {
@@ -144,12 +144,12 @@ impl<'a> Ctx<'a> {
144144
ast::Item::ExternBlock(ast) => self.lower_extern_block(ast).into(),
145145
};
146146
let attrs = RawAttrs::new(self.db, item, self.span_map());
147-
self.add_attrs(mod_item.into(), attrs);
147+
self.add_attrs(mod_item.ast_id(), attrs);
148148

149149
Some(mod_item)
150150
}
151151

152-
fn add_attrs(&mut self, item: AttrOwner, attrs: RawAttrs) {
152+
fn add_attrs(&mut self, item: FileAstId<ast::Item>, attrs: RawAttrs) {
153153
if !attrs.is_empty() {
154154
match self.tree.attrs.entry(item) {
155155
Entry::Occupied(mut entry) => {
@@ -365,7 +365,7 @@ impl<'a> Ctx<'a> {
365365
ast::ExternItem::MacroCall(call) => self.lower_macro_call(call)?.into(),
366366
};
367367
let attrs = RawAttrs::new(self.db, &item, self.span_map());
368-
self.add_attrs(mod_item.into(), attrs);
368+
self.add_attrs(mod_item.ast_id(), attrs);
369369
Some(mod_item)
370370
})
371371
.collect()

src/tools/rust-analyzer/crates/hir-def/src/item_tree/pretty.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ use span::{Edition, ErasedFileAstId};
66

77
use crate::{
88
item_tree::{
9-
AttrOwner, Const, DefDatabase, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl,
10-
ItemTree, Macro2, MacroCall, MacroRules, Mod, ModItemId, ModKind, RawAttrs,
11-
RawVisibilityId, Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use, UseTree,
12-
UseTreeKind,
9+
Const, DefDatabase, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl, ItemTree,
10+
Macro2, MacroCall, MacroRules, Mod, ModItemId, ModKind, RawAttrs, RawVisibilityId, Static,
11+
Struct, Trait, TraitAlias, TypeAlias, Union, Use, UseTree, UseTreeKind,
1312
},
1413
visibility::RawVisibility,
1514
};
@@ -18,9 +17,7 @@ pub(super) fn print_item_tree(db: &dyn DefDatabase, tree: &ItemTree, edition: Ed
1817
let mut p =
1918
Printer { db, tree, buf: String::new(), indent_level: 0, needs_indent: true, edition };
2019

21-
if let Some(attrs) = tree.attrs.get(&AttrOwner::TopLevel) {
22-
p.print_attrs(attrs, true, "\n");
23-
}
20+
p.print_attrs(&tree.top_attrs, true, "\n");
2421
p.blank();
2522

2623
for item in tree.top_level_items() {
@@ -102,8 +99,8 @@ impl Printer<'_> {
10299
}
103100
}
104101

105-
fn print_attrs_of(&mut self, of: impl Into<AttrOwner>, separated_by: &str) {
106-
if let Some(attrs) = self.tree.attrs.get(&of.into()) {
102+
fn print_attrs_of(&mut self, of: ModItemId, separated_by: &str) {
103+
if let Some(attrs) = self.tree.attrs.get(&of.ast_id()) {
107104
self.print_attrs(attrs, false, separated_by);
108105
}
109106
}

src/tools/rust-analyzer/crates/hir-def/src/nameres/collector.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ use crate::{
3535
db::DefDatabase,
3636
item_scope::{GlobId, ImportId, ImportOrExternCrate, PerNsGlobImports},
3737
item_tree::{
38-
self, AttrOwner, FieldsShape, ImportAlias, ImportKind, ItemTree, ItemTreeAstId,
39-
ItemTreeNode, Macro2, MacroCall, MacroRules, Mod, ModItemId, ModKind, TreeId, UseTreeKind,
38+
self, FieldsShape, ImportAlias, ImportKind, ItemTree, ItemTreeAstId, ItemTreeNode, Macro2,
39+
MacroCall, MacroRules, Mod, ModItemId, ModKind, TreeId, UseTreeKind,
4040
},
4141
macro_call_as_call_id,
4242
nameres::{
@@ -1727,7 +1727,7 @@ impl ModCollector<'_, '_> {
17271727
};
17281728

17291729
let mut process_mod_item = |item: ModItemId| {
1730-
let attrs = self.item_tree.attrs(db, krate, item.into());
1730+
let attrs = self.item_tree.attrs(db, krate, item.ast_id());
17311731
if let Some(cfg) = attrs.cfg() {
17321732
if !self.is_cfg_enabled(&cfg) {
17331733
let ast_id = item.ast_id().erase();
@@ -2298,7 +2298,7 @@ impl ModCollector<'_, '_> {
22982298
fn collect_macro_rules(&mut self, id: ItemTreeAstId<MacroRules>, module: ModuleId) {
22992299
let krate = self.def_collector.def_map.krate;
23002300
let mac = &self.item_tree[id];
2301-
let attrs = self.item_tree.attrs(self.def_collector.db, krate, AttrOwner::Item(id.erase()));
2301+
let attrs = self.item_tree.attrs(self.def_collector.db, krate, id.upcast());
23022302
let ast_id = InFile::new(self.file_id(), mac.ast_id.upcast());
23032303

23042304
let export_attr = || attrs.by_key(sym::macro_export);
@@ -2387,7 +2387,7 @@ impl ModCollector<'_, '_> {
23872387

23882388
// Case 1: builtin macros
23892389
let mut helpers_opt = None;
2390-
let attrs = self.item_tree.attrs(self.def_collector.db, krate, AttrOwner::Item(id.erase()));
2390+
let attrs = self.item_tree.attrs(self.def_collector.db, krate, id.upcast());
23912391
let expander = if attrs.by_key(sym::rustc_builtin_macro).exists() {
23922392
if let Some(expander) = find_builtin_macro(&mac.name) {
23932393
match expander {

0 commit comments

Comments
 (0)