Skip to content

Commit 834579f

Browse files
committed
AttrOwner needs no ModItem
1 parent a265cdd commit 834579f

File tree

4 files changed

+36
-47
lines changed

4 files changed

+36
-47
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ impl AttrsWithOwner {
526526
ModuleOrigin::File { definition, declaration_tree_id, declaration, .. } => {
527527
let decl_attrs = declaration_tree_id
528528
.item_tree(db)
529-
.raw_attrs(AttrOwner::ModItem(declaration.into()))
529+
.raw_attrs(AttrOwner::Item(declaration.erase()))
530530
.clone();
531531
let tree = db.file_item_tree(definition.into());
532532
let def_attrs = tree.raw_attrs(AttrOwner::TopLevel).clone();
@@ -538,7 +538,7 @@ impl AttrsWithOwner {
538538
}
539539
ModuleOrigin::Inline { definition_tree_id, definition } => definition_tree_id
540540
.item_tree(db)
541-
.raw_attrs(AttrOwner::ModItem(definition.into()))
541+
.raw_attrs(AttrOwner::Item(definition.erase()))
542542
.clone(),
543543
ModuleOrigin::BlockExpr { id, .. } => {
544544
let tree = db.block_item_tree(id);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::{
2424
},
2525
hir::generics::GenericParams,
2626
import_map::ImportMap,
27-
item_tree::{AttrOwner, ItemTree},
27+
item_tree::ItemTree,
2828
lang_item::{self, LangItem},
2929
nameres::{
3030
assoc::{ImplItems, TraitItems},
@@ -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(AttrOwner::TopLevel);
379+
let attrs = item_tree.raw_attrs(crate::item_tree::AttrOwner::TopLevel);
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: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ use hir_expand::{
5151
name::Name,
5252
};
5353
use intern::Interned;
54-
use la_arena::{Arena, Idx};
54+
use la_arena::Idx;
5555
use rustc_hash::FxHashMap;
5656
use smallvec::SmallVec;
57-
use span::{AstIdNode, Edition, FileAstId, SyntaxContext};
57+
use span::{AstIdNode, Edition, ErasedFileAstId, FileAstId, SyntaxContext};
5858
use stdx::never;
5959
use syntax::{SyntaxKind, ast, match_ast};
6060
use triomphe::Arc;
@@ -90,6 +90,7 @@ impl fmt::Debug for RawVisibilityId {
9090
#[derive(Debug, Default, Eq, PartialEq)]
9191
pub struct ItemTree {
9292
top_level: SmallVec<[ModItem; 1]>,
93+
// Consider splitting this into top level RawAttrs and the map?
9394
attrs: FxHashMap<AttrOwner, RawAttrs>,
9495

9596
data: Option<Box<ItemTreeData>>,
@@ -174,7 +175,7 @@ impl ItemTree {
174175

175176
/// Returns an iterator over all items located at the top level of the `HirFileId` this
176177
/// `ItemTree` was created from.
177-
pub fn top_level_items(&self) -> &[ModItem] {
178+
pub(crate) fn top_level_items(&self) -> &[ModItem] {
178179
&self.top_level
179180
}
180181

@@ -310,15 +311,15 @@ pub struct ItemTreeDataStats {
310311
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
311312
pub enum AttrOwner {
312313
/// Attributes on an item.
313-
ModItem(ModItem),
314+
Item(ErasedFileAstId),
314315
/// Inner attributes of the source file.
315316
TopLevel,
316317
}
317318

318319
impl From<ModItem> for AttrOwner {
319320
#[inline]
320321
fn from(value: ModItem) -> Self {
321-
AttrOwner::ModItem(value)
322+
AttrOwner::Item(value.ast_id().erase())
322323
}
323324
}
324325

@@ -365,25 +366,25 @@ impl TreeId {
365366
}
366367

367368
macro_rules! mod_items {
368-
( $( $typ:ident in $fld:ident -> $ast:ty ),+ $(,)? ) => {
369+
($mod_item:ident -> $( $typ:ident in $fld:ident -> $ast:ty ),+ $(,)? ) => {
369370
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
370-
pub enum ModItem {
371+
pub(crate) enum $mod_item {
371372
$(
372373
$typ(FileAstId<$ast>),
373374
)+
374375
}
375376

376-
impl ModItem {
377-
pub fn ast_id(self) -> FileAstId<ast::Item> {
377+
impl $mod_item {
378+
pub(crate) fn ast_id(self) -> FileAstId<ast::Item> {
378379
match self {
379-
$(ModItem::$typ(it) => it.upcast()),+
380+
$($mod_item::$typ(it) => it.upcast()),+
380381
}
381382
}
382383
}
383384

384385
$(
385-
impl From<FileAstId<$ast>> for ModItem {
386-
fn from(id: FileAstId<$ast>) -> ModItem {
386+
impl From<FileAstId<$ast>> for $mod_item {
387+
fn from(id: FileAstId<$ast>) -> $mod_item {
387388
ModItem::$typ(id)
388389
}
389390
}
@@ -414,6 +415,7 @@ macro_rules! mod_items {
414415
}
415416

416417
mod_items! {
418+
ModItem ->
417419
Use in uses -> ast::Use,
418420
ExternCrate in extern_crates -> ast::ExternCrate,
419421
ExternBlock in extern_blocks -> ast::ExternBlock,
@@ -539,7 +541,7 @@ pub struct ExternCrate {
539541
#[derive(Debug, Clone, Eq, PartialEq)]
540542
pub struct ExternBlock {
541543
pub ast_id: FileAstId<ast::ExternBlock>,
542-
pub children: Box<[ModItem]>,
544+
pub(crate) children: Box<[ModItem]>,
543545
}
544546

545547
#[derive(Debug, Clone, Eq, PartialEq)]
@@ -647,12 +649,12 @@ pub struct TypeAlias {
647649
pub struct Mod {
648650
pub name: Name,
649651
pub visibility: RawVisibilityId,
650-
pub kind: ModKind,
652+
pub(crate) kind: ModKind,
651653
pub ast_id: FileAstId<ast::Module>,
652654
}
653655

654656
#[derive(Debug, Clone, Eq, PartialEq)]
655-
pub enum ModKind {
657+
pub(crate) enum ModKind {
656658
/// `mod m { ... }`
657659
Inline { items: Box<[ModItem]> },
658660
/// `mod m;`

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

Lines changed: 15 additions & 28 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, FieldsShape, ImportAlias, ImportKind, ItemTree, ItemTreeAstId, ItemTreeNode, Macro2,
39-
MacroCall, MacroRules, Mod, ModItem, ModKind, TreeId, UseTreeKind,
38+
self, AttrOwner, FieldsShape, ImportAlias, ImportKind, ItemTree, ItemTreeAstId,
39+
ItemTreeNode, Macro2, MacroCall, MacroRules, Mod, ModItem, ModKind, TreeId, UseTreeKind,
4040
},
4141
macro_call_as_call_id,
4242
nameres::{
@@ -246,7 +246,7 @@ struct DefCollector<'a> {
246246
/// This also stores the attributes to skip when we resolve derive helpers and non-macro
247247
/// non-builtin attributes in general.
248248
// FIXME: There has to be a better way to do this
249-
skip_attrs: FxHashMap<InFile<ModItem>, AttrId>,
249+
skip_attrs: FxHashMap<InFile<FileAstId<ast::Item>>, AttrId>,
250250
}
251251

252252
impl DefCollector<'_> {
@@ -472,7 +472,7 @@ impl DefCollector<'_> {
472472
attr.path().clone(),
473473
));
474474

475-
self.skip_attrs.insert(ast_id.ast_id.with_value(*mod_item), attr.id);
475+
self.skip_attrs.insert(ast_id.ast_id.with_value(mod_item.ast_id()), attr.id);
476476

477477
Some((idx, directive, *mod_item, *tree))
478478
}
@@ -1371,7 +1371,9 @@ impl DefCollector<'_> {
13711371
let mut recollect_without = |collector: &mut Self| {
13721372
// Remove the original directive since we resolved it.
13731373
let mod_dir = collector.mod_dirs[&directive.module_id].clone();
1374-
collector.skip_attrs.insert(InFile::new(file_id, *mod_item), attr.id);
1374+
collector
1375+
.skip_attrs
1376+
.insert(InFile::new(file_id, mod_item.ast_id()), attr.id);
13751377

13761378
let item_tree = tree.item_tree(self.db);
13771379
ModCollector {
@@ -1728,25 +1730,7 @@ impl ModCollector<'_, '_> {
17281730
let attrs = self.item_tree.attrs(db, krate, item.into());
17291731
if let Some(cfg) = attrs.cfg() {
17301732
if !self.is_cfg_enabled(&cfg) {
1731-
let ast_id = match item {
1732-
ModItem::Use(it) => self.item_tree[it].ast_id.erase(),
1733-
ModItem::ExternCrate(it) => self.item_tree[it].ast_id.erase(),
1734-
ModItem::ExternBlock(it) => self.item_tree[it].ast_id.erase(),
1735-
ModItem::Function(it) => self.item_tree[it].ast_id.erase(),
1736-
ModItem::Struct(it) => self.item_tree[it].ast_id.erase(),
1737-
ModItem::Union(it) => self.item_tree[it].ast_id.erase(),
1738-
ModItem::Enum(it) => self.item_tree[it].ast_id.erase(),
1739-
ModItem::Const(it) => self.item_tree[it].ast_id.erase(),
1740-
ModItem::Static(it) => self.item_tree[it].ast_id.erase(),
1741-
ModItem::Trait(it) => self.item_tree[it].ast_id.erase(),
1742-
ModItem::TraitAlias(it) => self.item_tree[it].ast_id.erase(),
1743-
ModItem::Impl(it) => self.item_tree[it].ast_id.erase(),
1744-
ModItem::TypeAlias(it) => self.item_tree[it].ast_id.erase(),
1745-
ModItem::Mod(it) => self.item_tree[it].ast_id.erase(),
1746-
ModItem::MacroCall(it) => self.item_tree[it].ast_id.erase(),
1747-
ModItem::MacroRules(it) => self.item_tree[it].ast_id.erase(),
1748-
ModItem::Macro2(it) => self.item_tree[it].ast_id.erase(),
1749-
};
1733+
let ast_id = item.ast_id().erase();
17501734
self.emit_unconfigured_diagnostic(InFile::new(self.file_id(), ast_id), &cfg);
17511735
return;
17521736
}
@@ -2256,8 +2240,11 @@ impl ModCollector<'_, '_> {
22562240
mod_item: ModItem,
22572241
container: ItemContainerId,
22582242
) -> Result<(), ()> {
2259-
let mut ignore_up_to =
2260-
self.def_collector.skip_attrs.get(&InFile::new(self.file_id(), mod_item)).copied();
2243+
let mut ignore_up_to = self
2244+
.def_collector
2245+
.skip_attrs
2246+
.get(&InFile::new(self.file_id(), mod_item.ast_id()))
2247+
.copied();
22612248
let iter = attrs
22622249
.iter()
22632250
.dedup_by(|a, b| {
@@ -2309,7 +2296,7 @@ impl ModCollector<'_, '_> {
23092296
fn collect_macro_rules(&mut self, id: ItemTreeAstId<MacroRules>, module: ModuleId) {
23102297
let krate = self.def_collector.def_map.krate;
23112298
let mac = &self.item_tree[id];
2312-
let attrs = self.item_tree.attrs(self.def_collector.db, krate, ModItem::from(id).into());
2299+
let attrs = self.item_tree.attrs(self.def_collector.db, krate, AttrOwner::Item(id.erase()));
23132300
let ast_id = InFile::new(self.file_id(), mac.ast_id.upcast());
23142301

23152302
let export_attr = || attrs.by_key(sym::macro_export);
@@ -2398,7 +2385,7 @@ impl ModCollector<'_, '_> {
23982385

23992386
// Case 1: builtin macros
24002387
let mut helpers_opt = None;
2401-
let attrs = self.item_tree.attrs(self.def_collector.db, krate, ModItem::from(id).into());
2388+
let attrs = self.item_tree.attrs(self.def_collector.db, krate, AttrOwner::Item(id.erase()));
24022389
let expander = if attrs.by_key(sym::rustc_builtin_macro).exists() {
24032390
if let Some(expander) = find_builtin_macro(&mac.name) {
24042391
match expander {

0 commit comments

Comments
 (0)