AttrOwner needs no ModItem

This commit is contained in:
Lukas Wirth 2025-06-12 10:25:21 +02:00
parent f3434f537f
commit 80996dca35
4 changed files with 36 additions and 47 deletions

View File

@ -526,7 +526,7 @@ impl AttrsWithOwner {
ModuleOrigin::File { definition, declaration_tree_id, declaration, .. } => {
let decl_attrs = declaration_tree_id
.item_tree(db)
.raw_attrs(AttrOwner::ModItem(declaration.into()))
.raw_attrs(AttrOwner::Item(declaration.erase()))
.clone();
let tree = db.file_item_tree(definition.into());
let def_attrs = tree.raw_attrs(AttrOwner::TopLevel).clone();
@ -538,7 +538,7 @@ impl AttrsWithOwner {
}
ModuleOrigin::Inline { definition_tree_id, definition } => definition_tree_id
.item_tree(db)
.raw_attrs(AttrOwner::ModItem(definition.into()))
.raw_attrs(AttrOwner::Item(definition.erase()))
.clone(),
ModuleOrigin::BlockExpr { id, .. } => {
let tree = db.block_item_tree(id);

View File

@ -24,7 +24,7 @@ use crate::{
},
hir::generics::GenericParams,
import_map::ImportMap,
item_tree::{AttrOwner, ItemTree},
item_tree::ItemTree,
lang_item::{self, LangItem},
nameres::{
assoc::{ImplItems, TraitItems},
@ -376,7 +376,7 @@ fn include_macro_invoc(
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(AttrOwner::TopLevel);
let attrs = item_tree.raw_attrs(crate::item_tree::AttrOwner::TopLevel);
for attr in &**attrs {
match attr.path().as_ident() {
Some(ident) if *ident == sym::no_std => return true,

View File

@ -51,10 +51,10 @@ use hir_expand::{
name::Name,
};
use intern::Interned;
use la_arena::{Arena, Idx};
use la_arena::Idx;
use rustc_hash::FxHashMap;
use smallvec::SmallVec;
use span::{AstIdNode, Edition, FileAstId, SyntaxContext};
use span::{AstIdNode, Edition, ErasedFileAstId, FileAstId, SyntaxContext};
use stdx::never;
use syntax::{SyntaxKind, ast, match_ast};
use triomphe::Arc;
@ -90,6 +90,7 @@ impl fmt::Debug for RawVisibilityId {
#[derive(Debug, Default, Eq, PartialEq)]
pub struct ItemTree {
top_level: SmallVec<[ModItem; 1]>,
// Consider splitting this into top level RawAttrs and the map?
attrs: FxHashMap<AttrOwner, RawAttrs>,
data: Option<Box<ItemTreeData>>,
@ -174,7 +175,7 @@ impl ItemTree {
/// Returns an iterator over all items located at the top level of the `HirFileId` this
/// `ItemTree` was created from.
pub fn top_level_items(&self) -> &[ModItem] {
pub(crate) fn top_level_items(&self) -> &[ModItem] {
&self.top_level
}
@ -310,7 +311,7 @@ pub struct ItemTreeDataStats {
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum AttrOwner {
/// Attributes on an item.
ModItem(ModItem),
Item(ErasedFileAstId),
/// Inner attributes of the source file.
TopLevel,
}
@ -318,7 +319,7 @@ pub enum AttrOwner {
impl From<ModItem> for AttrOwner {
#[inline]
fn from(value: ModItem) -> Self {
AttrOwner::ModItem(value)
AttrOwner::Item(value.ast_id().erase())
}
}
@ -365,25 +366,25 @@ impl TreeId {
}
macro_rules! mod_items {
( $( $typ:ident in $fld:ident -> $ast:ty ),+ $(,)? ) => {
($mod_item:ident -> $( $typ:ident in $fld:ident -> $ast:ty ),+ $(,)? ) => {
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum ModItem {
pub(crate) enum $mod_item {
$(
$typ(FileAstId<$ast>),
)+
}
impl ModItem {
pub fn ast_id(self) -> FileAstId<ast::Item> {
impl $mod_item {
pub(crate) fn ast_id(self) -> FileAstId<ast::Item> {
match self {
$(ModItem::$typ(it) => it.upcast()),+
$($mod_item::$typ(it) => it.upcast()),+
}
}
}
$(
impl From<FileAstId<$ast>> for ModItem {
fn from(id: FileAstId<$ast>) -> ModItem {
impl From<FileAstId<$ast>> for $mod_item {
fn from(id: FileAstId<$ast>) -> $mod_item {
ModItem::$typ(id)
}
}
@ -414,6 +415,7 @@ macro_rules! mod_items {
}
mod_items! {
ModItem ->
Use in uses -> ast::Use,
ExternCrate in extern_crates -> ast::ExternCrate,
ExternBlock in extern_blocks -> ast::ExternBlock,
@ -539,7 +541,7 @@ pub struct ExternCrate {
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ExternBlock {
pub ast_id: FileAstId<ast::ExternBlock>,
pub children: Box<[ModItem]>,
pub(crate) children: Box<[ModItem]>,
}
#[derive(Debug, Clone, Eq, PartialEq)]
@ -647,12 +649,12 @@ pub struct TypeAlias {
pub struct Mod {
pub name: Name,
pub visibility: RawVisibilityId,
pub kind: ModKind,
pub(crate) kind: ModKind,
pub ast_id: FileAstId<ast::Module>,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ModKind {
pub(crate) enum ModKind {
/// `mod m { ... }`
Inline { items: Box<[ModItem]> },
/// `mod m;`

View File

@ -35,8 +35,8 @@ use crate::{
db::DefDatabase,
item_scope::{GlobId, ImportId, ImportOrExternCrate, PerNsGlobImports},
item_tree::{
self, FieldsShape, ImportAlias, ImportKind, ItemTree, ItemTreeAstId, ItemTreeNode, Macro2,
MacroCall, MacroRules, Mod, ModItem, ModKind, TreeId, UseTreeKind,
self, AttrOwner, FieldsShape, ImportAlias, ImportKind, ItemTree, ItemTreeAstId,
ItemTreeNode, Macro2, MacroCall, MacroRules, Mod, ModItem, ModKind, TreeId, UseTreeKind,
},
macro_call_as_call_id,
nameres::{
@ -246,7 +246,7 @@ struct DefCollector<'a> {
/// This also stores the attributes to skip when we resolve derive helpers and non-macro
/// non-builtin attributes in general.
// FIXME: There has to be a better way to do this
skip_attrs: FxHashMap<InFile<ModItem>, AttrId>,
skip_attrs: FxHashMap<InFile<FileAstId<ast::Item>>, AttrId>,
}
impl DefCollector<'_> {
@ -472,7 +472,7 @@ impl DefCollector<'_> {
attr.path().clone(),
));
self.skip_attrs.insert(ast_id.ast_id.with_value(*mod_item), attr.id);
self.skip_attrs.insert(ast_id.ast_id.with_value(mod_item.ast_id()), attr.id);
Some((idx, directive, *mod_item, *tree))
}
@ -1371,7 +1371,9 @@ impl DefCollector<'_> {
let mut recollect_without = |collector: &mut Self| {
// Remove the original directive since we resolved it.
let mod_dir = collector.mod_dirs[&directive.module_id].clone();
collector.skip_attrs.insert(InFile::new(file_id, *mod_item), attr.id);
collector
.skip_attrs
.insert(InFile::new(file_id, mod_item.ast_id()), attr.id);
let item_tree = tree.item_tree(self.db);
ModCollector {
@ -1728,25 +1730,7 @@ impl ModCollector<'_, '_> {
let attrs = self.item_tree.attrs(db, krate, item.into());
if let Some(cfg) = attrs.cfg() {
if !self.is_cfg_enabled(&cfg) {
let ast_id = match item {
ModItem::Use(it) => self.item_tree[it].ast_id.erase(),
ModItem::ExternCrate(it) => self.item_tree[it].ast_id.erase(),
ModItem::ExternBlock(it) => self.item_tree[it].ast_id.erase(),
ModItem::Function(it) => self.item_tree[it].ast_id.erase(),
ModItem::Struct(it) => self.item_tree[it].ast_id.erase(),
ModItem::Union(it) => self.item_tree[it].ast_id.erase(),
ModItem::Enum(it) => self.item_tree[it].ast_id.erase(),
ModItem::Const(it) => self.item_tree[it].ast_id.erase(),
ModItem::Static(it) => self.item_tree[it].ast_id.erase(),
ModItem::Trait(it) => self.item_tree[it].ast_id.erase(),
ModItem::TraitAlias(it) => self.item_tree[it].ast_id.erase(),
ModItem::Impl(it) => self.item_tree[it].ast_id.erase(),
ModItem::TypeAlias(it) => self.item_tree[it].ast_id.erase(),
ModItem::Mod(it) => self.item_tree[it].ast_id.erase(),
ModItem::MacroCall(it) => self.item_tree[it].ast_id.erase(),
ModItem::MacroRules(it) => self.item_tree[it].ast_id.erase(),
ModItem::Macro2(it) => self.item_tree[it].ast_id.erase(),
};
let ast_id = item.ast_id().erase();
self.emit_unconfigured_diagnostic(InFile::new(self.file_id(), ast_id), &cfg);
return;
}
@ -2256,8 +2240,11 @@ impl ModCollector<'_, '_> {
mod_item: ModItem,
container: ItemContainerId,
) -> Result<(), ()> {
let mut ignore_up_to =
self.def_collector.skip_attrs.get(&InFile::new(self.file_id(), mod_item)).copied();
let mut ignore_up_to = self
.def_collector
.skip_attrs
.get(&InFile::new(self.file_id(), mod_item.ast_id()))
.copied();
let iter = attrs
.iter()
.dedup_by(|a, b| {
@ -2309,7 +2296,7 @@ impl ModCollector<'_, '_> {
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, ModItem::from(id).into());
let attrs = self.item_tree.attrs(self.def_collector.db, krate, AttrOwner::Item(id.erase()));
let ast_id = InFile::new(self.file_id(), mac.ast_id.upcast());
let export_attr = || attrs.by_key(sym::macro_export);
@ -2398,7 +2385,7 @@ impl ModCollector<'_, '_> {
// Case 1: builtin macros
let mut helpers_opt = None;
let attrs = self.item_tree.attrs(self.def_collector.db, krate, ModItem::from(id).into());
let attrs = self.item_tree.attrs(self.def_collector.db, krate, AttrOwner::Item(id.erase()));
let expander = if attrs.by_key(sym::rustc_builtin_macro).exists() {
if let Some(expander) = find_builtin_macro(&mac.name) {
match expander {