Coalesce item tree data maps

This commit is contained in:
Lukas Wirth 2025-06-12 11:06:32 +02:00
parent 80996dca35
commit 8930c58d30
4 changed files with 138 additions and 181 deletions

View file

@ -89,11 +89,12 @@ impl fmt::Debug for RawVisibilityId {
/// The item tree of a source file. /// The item tree of a source file.
#[derive(Debug, Default, Eq, PartialEq)] #[derive(Debug, Default, Eq, PartialEq)]
pub struct ItemTree { pub struct ItemTree {
top_level: SmallVec<[ModItem; 1]>, top_level: SmallVec<[ModItemId; 1]>,
// Consider splitting this into top level RawAttrs and the map? // Consider splitting this into top level RawAttrs and the map?
attrs: FxHashMap<AttrOwner, RawAttrs>, attrs: FxHashMap<AttrOwner, RawAttrs>,
data: Option<Box<ItemTreeData>>, vis: ItemVisibilities,
data: FxHashMap<FileAstId<ast::Item>, ModItem>,
} }
impl ItemTree { impl ItemTree {
@ -130,14 +131,15 @@ impl ItemTree {
if let Some(attrs) = top_attrs { if let Some(attrs) = top_attrs {
item_tree.attrs.insert(AttrOwner::TopLevel, attrs); item_tree.attrs.insert(AttrOwner::TopLevel, attrs);
} }
if item_tree.data.is_none() && 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()
{ {
EMPTY EMPTY
.get_or_init(|| { .get_or_init(|| {
Arc::new(ItemTree { Arc::new(ItemTree {
top_level: SmallVec::new_const(), top_level: SmallVec::new_const(),
attrs: FxHashMap::default(), attrs: FxHashMap::default(),
data: None, data: FxHashMap::default(),
vis: ItemVisibilities { arena: Box::new([]) },
}) })
}) })
.clone() .clone()
@ -156,14 +158,15 @@ impl ItemTree {
let ctx = lower::Ctx::new(db, loc.ast_id.file_id); let ctx = lower::Ctx::new(db, loc.ast_id.file_id);
let mut item_tree = ctx.lower_block(&block); let mut item_tree = ctx.lower_block(&block);
if item_tree.data.is_none() && 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()
{ {
EMPTY EMPTY
.get_or_init(|| { .get_or_init(|| {
Arc::new(ItemTree { Arc::new(ItemTree {
top_level: SmallVec::new_const(), top_level: SmallVec::new_const(),
attrs: FxHashMap::default(), attrs: FxHashMap::default(),
data: None, data: FxHashMap::default(),
vis: ItemVisibilities { arena: Box::new([]) },
}) })
}) })
.clone() .clone()
@ -175,7 +178,7 @@ impl ItemTree {
/// Returns an iterator over all items located at the top level of the `HirFileId` this /// Returns an iterator over all items located at the top level of the `HirFileId` this
/// `ItemTree` was created from. /// `ItemTree` was created from.
pub(crate) fn top_level_items(&self) -> &[ModItem] { pub(crate) fn top_level_items(&self) -> &[ModItemId] {
&self.top_level &self.top_level
} }
@ -200,74 +203,33 @@ impl ItemTree {
/// ///
/// For more detail, see [`ItemTreeDataStats`]. /// For more detail, see [`ItemTreeDataStats`].
pub fn item_tree_stats(&self) -> ItemTreeDataStats { pub fn item_tree_stats(&self) -> ItemTreeDataStats {
match self.data { let mut traits = 0;
Some(ref data) => ItemTreeDataStats { let mut impls = 0;
traits: data.traits.len(), let mut mods = 0;
impls: data.impls.len(), let mut macro_calls = 0;
mods: data.mods.len(), let mut macro_rules = 0;
macro_calls: data.macro_calls.len(), for item in self.data.values() {
macro_rules: data.macro_rules.len(), match item {
}, ModItem::Trait(_) => traits += 1,
None => ItemTreeDataStats::default(), ModItem::Impl(_) => impls += 1,
ModItem::Mod(_) => mods += 1,
ModItem::MacroCall(_) => macro_calls += 1,
ModItem::MacroRules(_) => macro_rules += 1,
_ => {}
}
} }
ItemTreeDataStats { traits, impls, mods, macro_calls, macro_rules }
} }
pub fn pretty_print(&self, db: &dyn DefDatabase, edition: Edition) -> String { pub fn pretty_print(&self, db: &dyn DefDatabase, edition: Edition) -> String {
pretty::print_item_tree(db, self, edition) pretty::print_item_tree(db, self, edition)
} }
fn data(&self) -> &ItemTreeData {
self.data.as_ref().expect("attempted to access data of empty ItemTree")
}
fn data_mut(&mut self) -> &mut ItemTreeData {
self.data.get_or_insert_with(Box::default)
}
fn shrink_to_fit(&mut self) { fn shrink_to_fit(&mut self) {
let ItemTree { top_level, attrs, data } = self; let ItemTree { top_level, attrs, data, vis: _ } = self;
top_level.shrink_to_fit(); top_level.shrink_to_fit();
attrs.shrink_to_fit(); attrs.shrink_to_fit();
if let Some(data) = data { data.shrink_to_fit();
let ItemTreeData {
uses,
extern_crates,
extern_blocks,
functions,
structs,
unions,
enums,
consts,
statics,
traits,
trait_aliases,
impls,
type_aliases,
mods,
macro_calls,
macro_rules,
macro_defs,
vis: _,
} = &mut **data;
uses.shrink_to_fit();
extern_crates.shrink_to_fit();
extern_blocks.shrink_to_fit();
functions.shrink_to_fit();
structs.shrink_to_fit();
unions.shrink_to_fit();
enums.shrink_to_fit();
consts.shrink_to_fit();
statics.shrink_to_fit();
traits.shrink_to_fit();
trait_aliases.shrink_to_fit();
impls.shrink_to_fit();
type_aliases.shrink_to_fit();
mods.shrink_to_fit();
macro_calls.shrink_to_fit();
macro_rules.shrink_to_fit();
macro_defs.shrink_to_fit();
}
} }
} }
@ -276,29 +238,26 @@ struct ItemVisibilities {
arena: Box<[RawVisibility]>, arena: Box<[RawVisibility]>,
} }
#[derive(Default, Debug, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
struct ItemTreeData { enum ModItem {
uses: FxHashMap<ItemTreeAstId<Use>, Use>, Const(Const),
extern_crates: FxHashMap<ItemTreeAstId<ExternCrate>, ExternCrate>, Enum(Enum),
extern_blocks: FxHashMap<ItemTreeAstId<ExternBlock>, ExternBlock>, ExternBlock(ExternBlock),
functions: FxHashMap<ItemTreeAstId<Function>, Function>, ExternCrate(ExternCrate),
structs: FxHashMap<ItemTreeAstId<Struct>, Struct>, Function(Function),
unions: FxHashMap<ItemTreeAstId<Union>, Union>, Impl(Impl),
enums: FxHashMap<ItemTreeAstId<Enum>, Enum>, Macro2(Macro2),
consts: FxHashMap<ItemTreeAstId<Const>, Const>, MacroCall(MacroCall),
statics: FxHashMap<ItemTreeAstId<Static>, Static>, MacroRules(MacroRules),
traits: FxHashMap<ItemTreeAstId<Trait>, Trait>, Mod(Mod),
trait_aliases: FxHashMap<ItemTreeAstId<TraitAlias>, TraitAlias>, Static(Static),
impls: FxHashMap<ItemTreeAstId<Impl>, Impl>, Struct(Struct),
type_aliases: FxHashMap<ItemTreeAstId<TypeAlias>, TypeAlias>, Trait(Trait),
mods: FxHashMap<ItemTreeAstId<Mod>, Mod>, TraitAlias(TraitAlias),
macro_calls: FxHashMap<ItemTreeAstId<MacroCall>, MacroCall>, TypeAlias(TypeAlias),
macro_rules: FxHashMap<ItemTreeAstId<MacroRules>, MacroRules>, Union(Union),
macro_defs: FxHashMap<ItemTreeAstId<Macro2>, Macro2>, Use(Use),
vis: ItemVisibilities,
} }
#[derive(Default, Debug, Eq, PartialEq)] #[derive(Default, Debug, Eq, PartialEq)]
pub struct ItemTreeDataStats { pub struct ItemTreeDataStats {
pub traits: usize, pub traits: usize,
@ -316,9 +275,9 @@ pub enum AttrOwner {
TopLevel, TopLevel,
} }
impl From<ModItem> for AttrOwner { impl From<ModItemId> for AttrOwner {
#[inline] #[inline]
fn from(value: ModItem) -> Self { fn from(value: ModItemId) -> Self {
AttrOwner::Item(value.ast_id().erase()) AttrOwner::Item(value.ast_id().erase())
} }
} }
@ -385,7 +344,7 @@ macro_rules! mod_items {
$( $(
impl From<FileAstId<$ast>> for $mod_item { impl From<FileAstId<$ast>> for $mod_item {
fn from(id: FileAstId<$ast>) -> $mod_item { fn from(id: FileAstId<$ast>) -> $mod_item {
ModItem::$typ(id) ModItemId::$typ(id)
} }
} }
)+ )+
@ -399,7 +358,10 @@ macro_rules! mod_items {
} }
fn lookup(tree: &ItemTree, index: FileAstId<$ast>) -> &Self { fn lookup(tree: &ItemTree, index: FileAstId<$ast>) -> &Self {
&tree.data().$fld[&index] match &tree.data[&index.upcast()] {
ModItem::$typ(item) => item,
_ => panic!("expected item of type `{}` at index `{:?}`", stringify!($typ), index),
}
} }
} }
@ -407,7 +369,10 @@ macro_rules! mod_items {
type Output = $typ; type Output = $typ;
fn index(&self, index: FileAstId<$ast>) -> &Self::Output { fn index(&self, index: FileAstId<$ast>) -> &Self::Output {
&self.data().$fld[&index] match &self.data[&index.upcast()] {
ModItem::$typ(item) => item,
_ => panic!("expected item of type `{}` at index `{:?}`", stringify!($typ), index),
}
} }
} }
)+ )+
@ -415,7 +380,7 @@ macro_rules! mod_items {
} }
mod_items! { mod_items! {
ModItem -> ModItemId ->
Use in uses -> ast::Use, Use in uses -> ast::Use,
ExternCrate in extern_crates -> ast::ExternCrate, ExternCrate in extern_crates -> ast::ExternCrate,
ExternBlock in extern_blocks -> ast::ExternBlock, ExternBlock in extern_blocks -> ast::ExternBlock,
@ -463,7 +428,7 @@ impl Index<RawVisibilityId> for ItemTree {
VisibilityExplicitness::Explicit, VisibilityExplicitness::Explicit,
) )
}), }),
_ => &self.data().vis.arena[index.0 as usize], _ => &self.vis.arena[index.0 as usize],
} }
} }
} }
@ -541,7 +506,7 @@ pub struct ExternCrate {
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct ExternBlock { pub struct ExternBlock {
pub ast_id: FileAstId<ast::ExternBlock>, pub ast_id: FileAstId<ast::ExternBlock>,
pub(crate) children: Box<[ModItem]>, pub(crate) children: Box<[ModItemId]>,
} }
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
@ -656,7 +621,7 @@ pub struct Mod {
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) enum ModKind { pub(crate) enum ModKind {
/// `mod m { ... }` /// `mod m { ... }`
Inline { items: Box<[ModItem]> }, Inline { items: Box<[ModItemId]> },
/// `mod m;` /// `mod m;`
Outline, Outline,
} }

View file

@ -21,10 +21,9 @@ use crate::{
db::DefDatabase, db::DefDatabase,
item_tree::{ item_tree::{
AttrOwner, Const, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl, ImportAlias, AttrOwner, Const, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl, ImportAlias,
Interned, ItemTree, ItemTreeAstId, ItemTreeData, Macro2, MacroCall, MacroRules, Mod, Interned, ItemTree, ItemTreeAstId, Macro2, MacroCall, MacroRules, Mod, ModItem, ModItemId,
ModItem, ModKind, ModPath, RawAttrs, RawVisibility, RawVisibilityId, Static, Struct, ModKind, ModPath, RawAttrs, RawVisibility, RawVisibilityId, Static, Struct, StructKind,
StructKind, Trait, TraitAlias, TypeAlias, Union, Use, UseTree, UseTreeKind, Trait, TraitAlias, TypeAlias, Union, Use, UseTree, UseTreeKind, VisibilityExplicitness,
VisibilityExplicitness,
}, },
}; };
@ -56,9 +55,7 @@ impl<'a> Ctx<'a> {
pub(super) fn lower_module_items(mut self, item_owner: &dyn HasModuleItem) -> ItemTree { pub(super) fn lower_module_items(mut self, item_owner: &dyn HasModuleItem) -> ItemTree {
self.tree.top_level = self.tree.top_level =
item_owner.items().flat_map(|item| self.lower_mod_item(&item)).collect(); item_owner.items().flat_map(|item| self.lower_mod_item(&item)).collect();
if let Some(data) = &mut self.tree.data { self.tree.vis.arena = self.visibilities.into_iter().collect();
data.vis.arena = self.visibilities.into_iter().collect();
}
self.tree self.tree
} }
@ -92,9 +89,7 @@ impl<'a> Ctx<'a> {
} }
} }
if let Some(data) = &mut self.tree.data { self.tree.vis.arena = self.visibilities.into_iter().collect();
data.vis.arena = self.visibilities.into_iter().collect();
}
self.tree self.tree
} }
@ -120,18 +115,12 @@ impl<'a> Ctx<'a> {
} }
} }
} }
if let Some(data) = &mut self.tree.data { self.tree.vis.arena = self.visibilities.into_iter().collect();
data.vis.arena = self.visibilities.into_iter().collect();
}
self.tree self.tree
} }
fn data(&mut self) -> &mut ItemTreeData { fn lower_mod_item(&mut self, item: &ast::Item) -> Option<ModItemId> {
self.tree.data_mut() let mod_item: ModItemId = match item {
}
fn lower_mod_item(&mut self, item: &ast::Item) -> Option<ModItem> {
let mod_item: ModItem = match item {
ast::Item::Struct(ast) => self.lower_struct(ast)?.into(), ast::Item::Struct(ast) => self.lower_struct(ast)?.into(),
ast::Item::Union(ast) => self.lower_union(ast)?.into(), ast::Item::Union(ast) => self.lower_union(ast)?.into(),
ast::Item::Enum(ast) => self.lower_enum(ast)?.into(), ast::Item::Enum(ast) => self.lower_enum(ast)?.into(),
@ -175,7 +164,7 @@ impl<'a> Ctx<'a> {
let ast_id = self.source_ast_id_map.ast_id(strukt); let ast_id = self.source_ast_id_map.ast_id(strukt);
let shape = adt_shape(strukt.kind()); let shape = adt_shape(strukt.kind());
let res = Struct { name, visibility, shape, ast_id }; let res = Struct { name, visibility, shape, ast_id };
self.data().structs.insert(ast_id, res); self.tree.data.insert(ast_id.upcast(), ModItem::Struct(res));
Some(ast_id) Some(ast_id)
} }
@ -185,7 +174,7 @@ impl<'a> Ctx<'a> {
let name = union.name()?.as_name(); let name = union.name()?.as_name();
let ast_id = self.source_ast_id_map.ast_id(union); let ast_id = self.source_ast_id_map.ast_id(union);
let res = Union { name, visibility, ast_id }; let res = Union { name, visibility, ast_id };
self.data().unions.insert(ast_id, res); self.tree.data.insert(ast_id.upcast(), ModItem::Union(res));
Some(ast_id) Some(ast_id)
} }
@ -194,7 +183,7 @@ impl<'a> Ctx<'a> {
let name = enum_.name()?.as_name(); let name = enum_.name()?.as_name();
let ast_id = self.source_ast_id_map.ast_id(enum_); let ast_id = self.source_ast_id_map.ast_id(enum_);
let res = Enum { name, visibility, ast_id }; let res = Enum { name, visibility, ast_id };
self.data().enums.insert(ast_id, res); self.tree.data.insert(ast_id.upcast(), ModItem::Enum(res));
Some(ast_id) Some(ast_id)
} }
@ -206,7 +195,7 @@ impl<'a> Ctx<'a> {
let res = Function { name, visibility, ast_id }; let res = Function { name, visibility, ast_id };
self.data().functions.insert(ast_id, res); self.tree.data.insert(ast_id.upcast(), ModItem::Function(res));
Some(ast_id) Some(ast_id)
} }
@ -218,7 +207,7 @@ impl<'a> Ctx<'a> {
let visibility = self.lower_visibility(type_alias); let visibility = self.lower_visibility(type_alias);
let ast_id = self.source_ast_id_map.ast_id(type_alias); let ast_id = self.source_ast_id_map.ast_id(type_alias);
let res = TypeAlias { name, visibility, ast_id }; let res = TypeAlias { name, visibility, ast_id };
self.data().type_aliases.insert(ast_id, res); self.tree.data.insert(ast_id.upcast(), ModItem::TypeAlias(res));
Some(ast_id) Some(ast_id)
} }
@ -227,7 +216,7 @@ impl<'a> Ctx<'a> {
let visibility = self.lower_visibility(static_); let visibility = self.lower_visibility(static_);
let ast_id = self.source_ast_id_map.ast_id(static_); let ast_id = self.source_ast_id_map.ast_id(static_);
let res = Static { name, visibility, ast_id }; let res = Static { name, visibility, ast_id };
self.data().statics.insert(ast_id, res); self.tree.data.insert(ast_id.upcast(), ModItem::Static(res));
Some(ast_id) Some(ast_id)
} }
@ -236,7 +225,7 @@ impl<'a> Ctx<'a> {
let visibility = self.lower_visibility(konst); let visibility = self.lower_visibility(konst);
let ast_id = self.source_ast_id_map.ast_id(konst); let ast_id = self.source_ast_id_map.ast_id(konst);
let res = Const { name, visibility, ast_id }; let res = Const { name, visibility, ast_id };
self.data().consts.insert(ast_id, res); self.tree.data.insert(ast_id.upcast(), ModItem::Const(res));
ast_id ast_id
} }
@ -258,7 +247,7 @@ impl<'a> Ctx<'a> {
}; };
let ast_id = self.source_ast_id_map.ast_id(module); let ast_id = self.source_ast_id_map.ast_id(module);
let res = Mod { name, visibility, kind, ast_id }; let res = Mod { name, visibility, kind, ast_id };
self.data().mods.insert(ast_id, res); self.tree.data.insert(ast_id.upcast(), ModItem::Mod(res));
Some(ast_id) Some(ast_id)
} }
@ -268,7 +257,7 @@ impl<'a> Ctx<'a> {
let ast_id = self.source_ast_id_map.ast_id(trait_def); let ast_id = self.source_ast_id_map.ast_id(trait_def);
let def = Trait { name, visibility, ast_id }; let def = Trait { name, visibility, ast_id };
self.data().traits.insert(ast_id, def); self.tree.data.insert(ast_id.upcast(), ModItem::Trait(def));
Some(ast_id) Some(ast_id)
} }
@ -281,7 +270,7 @@ impl<'a> Ctx<'a> {
let ast_id = self.source_ast_id_map.ast_id(trait_alias_def); let ast_id = self.source_ast_id_map.ast_id(trait_alias_def);
let alias = TraitAlias { name, visibility, ast_id }; let alias = TraitAlias { name, visibility, ast_id };
self.data().trait_aliases.insert(ast_id, alias); self.tree.data.insert(ast_id.upcast(), ModItem::TraitAlias(alias));
Some(ast_id) Some(ast_id)
} }
@ -290,7 +279,7 @@ impl<'a> Ctx<'a> {
// Note that trait impls don't get implicit `Self` unlike traits, because here they are a // Note that trait impls don't get implicit `Self` unlike traits, because here they are a
// type alias rather than a type parameter, so this is handled by the resolver. // type alias rather than a type parameter, so this is handled by the resolver.
let res = Impl { ast_id }; let res = Impl { ast_id };
self.data().impls.insert(ast_id, res); self.tree.data.insert(ast_id.upcast(), ModItem::Impl(res));
ast_id ast_id
} }
@ -302,7 +291,7 @@ impl<'a> Ctx<'a> {
})?; })?;
let res = Use { visibility, ast_id, use_tree }; let res = Use { visibility, ast_id, use_tree };
self.data().uses.insert(ast_id, res); self.tree.data.insert(ast_id.upcast(), ModItem::Use(res));
Some(ast_id) Some(ast_id)
} }
@ -318,7 +307,7 @@ impl<'a> Ctx<'a> {
let ast_id = self.source_ast_id_map.ast_id(extern_crate); let ast_id = self.source_ast_id_map.ast_id(extern_crate);
let res = ExternCrate { name, alias, visibility, ast_id }; let res = ExternCrate { name, alias, visibility, ast_id };
self.data().extern_crates.insert(ast_id, res); self.tree.data.insert(ast_id.upcast(), ModItem::ExternCrate(res));
Some(ast_id) Some(ast_id)
} }
@ -332,7 +321,7 @@ impl<'a> Ctx<'a> {
let ast_id = self.source_ast_id_map.ast_id(m); let ast_id = self.source_ast_id_map.ast_id(m);
let expand_to = hir_expand::ExpandTo::from_call_site(m); let expand_to = hir_expand::ExpandTo::from_call_site(m);
let res = MacroCall { path, ast_id, expand_to, ctxt: span_map.span_for_range(range).ctx }; let res = MacroCall { path, ast_id, expand_to, ctxt: span_map.span_for_range(range).ctx };
self.data().macro_calls.insert(ast_id, res); self.tree.data.insert(ast_id.upcast(), ModItem::MacroCall(res));
Some(ast_id) Some(ast_id)
} }
@ -341,7 +330,7 @@ impl<'a> Ctx<'a> {
let ast_id = self.source_ast_id_map.ast_id(m); let ast_id = self.source_ast_id_map.ast_id(m);
let res = MacroRules { name: name.as_name(), ast_id }; let res = MacroRules { name: name.as_name(), ast_id };
self.data().macro_rules.insert(ast_id, res); self.tree.data.insert(ast_id.upcast(), ModItem::MacroRules(res));
Some(ast_id) Some(ast_id)
} }
@ -352,7 +341,7 @@ impl<'a> Ctx<'a> {
let visibility = self.lower_visibility(m); let visibility = self.lower_visibility(m);
let res = Macro2 { name: name.as_name(), ast_id, visibility }; let res = Macro2 { name: name.as_name(), ast_id, visibility };
self.data().macro_defs.insert(ast_id, res); self.tree.data.insert(ast_id.upcast(), ModItem::Macro2(res));
Some(ast_id) Some(ast_id)
} }
@ -365,7 +354,7 @@ impl<'a> Ctx<'a> {
// (in other words, the knowledge that they're in an extern block must not be used). // (in other words, the knowledge that they're in an extern block must not be used).
// This is because an extern block can contain macros whose ItemTree's top-level items // This is because an extern block can contain macros whose ItemTree's top-level items
// should be considered to be in an extern block too. // should be considered to be in an extern block too.
let mod_item: ModItem = match &item { let mod_item: ModItemId = match &item {
ast::ExternItem::Fn(ast) => self.lower_function(ast)?.into(), ast::ExternItem::Fn(ast) => self.lower_function(ast)?.into(),
ast::ExternItem::Static(ast) => self.lower_static(ast)?.into(), ast::ExternItem::Static(ast) => self.lower_static(ast)?.into(),
ast::ExternItem::TypeAlias(ty) => self.lower_type_alias(ty)?.into(), ast::ExternItem::TypeAlias(ty) => self.lower_type_alias(ty)?.into(),
@ -379,7 +368,7 @@ impl<'a> Ctx<'a> {
}); });
let res = ExternBlock { ast_id, children }; let res = ExternBlock { ast_id, children };
self.data().extern_blocks.insert(ast_id, res); self.tree.data.insert(ast_id.upcast(), ModItem::ExternBlock(res));
ast_id ast_id
} }

View file

@ -7,8 +7,9 @@ use span::{Edition, ErasedFileAstId};
use crate::{ use crate::{
item_tree::{ item_tree::{
AttrOwner, Const, DefDatabase, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl, AttrOwner, Const, DefDatabase, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl,
ItemTree, Macro2, MacroCall, MacroRules, Mod, ModItem, ModKind, RawAttrs, RawVisibilityId, ItemTree, Macro2, MacroCall, MacroRules, Mod, ModItemId, ModKind, RawAttrs,
Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use, UseTree, UseTreeKind, RawVisibilityId, Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use, UseTree,
UseTreeKind,
}, },
visibility::RawVisibility, visibility::RawVisibility,
}; };
@ -159,11 +160,11 @@ impl Printer<'_> {
} }
} }
fn print_mod_item(&mut self, item: ModItem) { fn print_mod_item(&mut self, item: ModItemId) {
self.print_attrs_of(item, "\n"); self.print_attrs_of(item, "\n");
match item { match item {
ModItem::Use(it) => { ModItemId::Use(it) => {
let Use { visibility, use_tree, ast_id } = &self.tree[it]; let Use { visibility, use_tree, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase()); self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
@ -171,7 +172,7 @@ impl Printer<'_> {
self.print_use_tree(use_tree); self.print_use_tree(use_tree);
wln!(self, ";"); wln!(self, ";");
} }
ModItem::ExternCrate(it) => { ModItemId::ExternCrate(it) => {
let ExternCrate { name, alias, visibility, ast_id } = &self.tree[it]; let ExternCrate { name, alias, visibility, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase()); self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
@ -181,7 +182,7 @@ impl Printer<'_> {
} }
wln!(self, ";"); wln!(self, ";");
} }
ModItem::ExternBlock(it) => { ModItemId::ExternBlock(it) => {
let ExternBlock { ast_id, children } = &self.tree[it]; let ExternBlock { ast_id, children } = &self.tree[it];
self.print_ast_id(ast_id.erase()); self.print_ast_id(ast_id.erase());
w!(self, "extern {{"); w!(self, "extern {{");
@ -192,13 +193,13 @@ impl Printer<'_> {
}); });
wln!(self, "}}"); wln!(self, "}}");
} }
ModItem::Function(it) => { ModItemId::Function(it) => {
let Function { name, visibility, ast_id } = &self.tree[it]; let Function { name, visibility, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase()); self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
wln!(self, "fn {};", name.display(self.db, self.edition)); wln!(self, "fn {};", name.display(self.db, self.edition));
} }
ModItem::Struct(it) => { ModItemId::Struct(it) => {
let Struct { visibility, name, shape: kind, ast_id } = &self.tree[it]; let Struct { visibility, name, shape: kind, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase()); self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
@ -210,7 +211,7 @@ impl Printer<'_> {
wln!(self, ";"); wln!(self, ";");
} }
} }
ModItem::Union(it) => { ModItemId::Union(it) => {
let Union { name, visibility, ast_id } = &self.tree[it]; let Union { name, visibility, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase()); self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
@ -218,13 +219,13 @@ impl Printer<'_> {
self.print_fields(FieldsShape::Record); self.print_fields(FieldsShape::Record);
wln!(self); wln!(self);
} }
ModItem::Enum(it) => { ModItemId::Enum(it) => {
let Enum { name, visibility, ast_id } = &self.tree[it]; let Enum { name, visibility, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase()); self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
w!(self, "enum {} {{ ... }}", name.display(self.db, self.edition)); w!(self, "enum {} {{ ... }}", name.display(self.db, self.edition));
} }
ModItem::Const(it) => { ModItemId::Const(it) => {
let Const { name, visibility, ast_id } = &self.tree[it]; let Const { name, visibility, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase()); self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
@ -235,7 +236,7 @@ impl Printer<'_> {
} }
wln!(self, " = _;"); wln!(self, " = _;");
} }
ModItem::Static(it) => { ModItemId::Static(it) => {
let Static { name, visibility, ast_id } = &self.tree[it]; let Static { name, visibility, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase()); self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
@ -244,24 +245,24 @@ impl Printer<'_> {
w!(self, " = _;"); w!(self, " = _;");
wln!(self); wln!(self);
} }
ModItem::Trait(it) => { ModItemId::Trait(it) => {
let Trait { name, visibility, ast_id } = &self.tree[it]; let Trait { name, visibility, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase()); self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
w!(self, "trait {} {{ ... }}", name.display(self.db, self.edition)); w!(self, "trait {} {{ ... }}", name.display(self.db, self.edition));
} }
ModItem::TraitAlias(it) => { ModItemId::TraitAlias(it) => {
let TraitAlias { name, visibility, ast_id } = &self.tree[it]; let TraitAlias { name, visibility, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase()); self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
wln!(self, "trait {} = ..;", name.display(self.db, self.edition)); wln!(self, "trait {} = ..;", name.display(self.db, self.edition));
} }
ModItem::Impl(it) => { ModItemId::Impl(it) => {
let Impl { ast_id } = &self.tree[it]; let Impl { ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase()); self.print_ast_id(ast_id.erase());
w!(self, "impl {{ ... }}"); w!(self, "impl {{ ... }}");
} }
ModItem::TypeAlias(it) => { ModItemId::TypeAlias(it) => {
let TypeAlias { name, visibility, ast_id } = &self.tree[it]; let TypeAlias { name, visibility, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase()); self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
@ -269,7 +270,7 @@ impl Printer<'_> {
w!(self, ";"); w!(self, ";");
wln!(self); wln!(self);
} }
ModItem::Mod(it) => { ModItemId::Mod(it) => {
let Mod { name, visibility, kind, ast_id } = &self.tree[it]; let Mod { name, visibility, kind, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase()); self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);
@ -289,7 +290,7 @@ impl Printer<'_> {
} }
} }
} }
ModItem::MacroCall(it) => { ModItemId::MacroCall(it) => {
let MacroCall { path, ast_id, expand_to, ctxt } = &self.tree[it]; let MacroCall { path, ast_id, expand_to, ctxt } = &self.tree[it];
let _ = writeln!( let _ = writeln!(
self, self,
@ -300,12 +301,12 @@ impl Printer<'_> {
); );
wln!(self, "{}!(...);", path.display(self.db, self.edition)); wln!(self, "{}!(...);", path.display(self.db, self.edition));
} }
ModItem::MacroRules(it) => { ModItemId::MacroRules(it) => {
let MacroRules { name, ast_id } = &self.tree[it]; let MacroRules { name, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase()); self.print_ast_id(ast_id.erase());
wln!(self, "macro_rules! {} {{ ... }}", name.display(self.db, self.edition)); wln!(self, "macro_rules! {} {{ ... }}", name.display(self.db, self.edition));
} }
ModItem::Macro2(it) => { ModItemId::Macro2(it) => {
let Macro2 { name, visibility, ast_id } = &self.tree[it]; let Macro2 { name, visibility, ast_id } = &self.tree[it];
self.print_ast_id(ast_id.erase()); self.print_ast_id(ast_id.erase());
self.print_visibility(*visibility); self.print_visibility(*visibility);

View file

@ -36,7 +36,7 @@ use crate::{
item_scope::{GlobId, ImportId, ImportOrExternCrate, PerNsGlobImports}, item_scope::{GlobId, ImportId, ImportOrExternCrate, PerNsGlobImports},
item_tree::{ item_tree::{
self, AttrOwner, FieldsShape, ImportAlias, ImportKind, ItemTree, ItemTreeAstId, self, AttrOwner, FieldsShape, ImportAlias, ImportKind, ItemTree, ItemTreeAstId,
ItemTreeNode, Macro2, MacroCall, MacroRules, Mod, ModItem, ModKind, TreeId, UseTreeKind, ItemTreeNode, Macro2, MacroCall, MacroRules, Mod, ModItemId, ModKind, TreeId, UseTreeKind,
}, },
macro_call_as_call_id, macro_call_as_call_id,
nameres::{ nameres::{
@ -208,7 +208,7 @@ enum MacroDirectiveKind {
Attr { Attr {
ast_id: AstIdWithPath<ast::Item>, ast_id: AstIdWithPath<ast::Item>,
attr: Attr, attr: Attr,
mod_item: ModItem, mod_item: ModItemId,
/* is this needed? */ tree: TreeId, /* is this needed? */ tree: TreeId,
}, },
} }
@ -1436,9 +1436,9 @@ impl DefCollector<'_> {
let item_tree = tree.item_tree(self.db); let item_tree = tree.item_tree(self.db);
let ast_adt_id: FileAstId<ast::Adt> = match *mod_item { let ast_adt_id: FileAstId<ast::Adt> = match *mod_item {
ModItem::Struct(strukt) => item_tree[strukt].ast_id().upcast(), ModItemId::Struct(strukt) => item_tree[strukt].ast_id().upcast(),
ModItem::Union(union) => item_tree[union].ast_id().upcast(), ModItemId::Union(union) => item_tree[union].ast_id().upcast(),
ModItem::Enum(enum_) => item_tree[enum_].ast_id().upcast(), ModItemId::Enum(enum_) => item_tree[enum_].ast_id().upcast(),
_ => { _ => {
let diag = DefDiagnostic::invalid_derive_target( let diag = DefDiagnostic::invalid_derive_target(
directive.module_id, directive.module_id,
@ -1682,12 +1682,12 @@ struct ModCollector<'a, 'b> {
} }
impl ModCollector<'_, '_> { impl ModCollector<'_, '_> {
fn collect_in_top_module(&mut self, items: &[ModItem]) { fn collect_in_top_module(&mut self, items: &[ModItemId]) {
let module = self.def_collector.def_map.module_id(self.module_id); let module = self.def_collector.def_map.module_id(self.module_id);
self.collect(items, module.into()) self.collect(items, module.into())
} }
fn collect(&mut self, items: &[ModItem], container: ItemContainerId) { fn collect(&mut self, items: &[ModItemId], container: ItemContainerId) {
let krate = self.def_collector.def_map.krate; let krate = self.def_collector.def_map.krate;
let is_crate_root = let is_crate_root =
self.module_id == DefMap::ROOT && self.def_collector.def_map.block.is_none(); self.module_id == DefMap::ROOT && self.def_collector.def_map.block.is_none();
@ -1726,7 +1726,7 @@ impl ModCollector<'_, '_> {
.unwrap_or(Visibility::Public) .unwrap_or(Visibility::Public)
}; };
let mut process_mod_item = |item: ModItem| { 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.into());
if let Some(cfg) = attrs.cfg() { if let Some(cfg) = attrs.cfg() {
if !self.is_cfg_enabled(&cfg) { if !self.is_cfg_enabled(&cfg) {
@ -1748,8 +1748,8 @@ impl ModCollector<'_, '_> {
self.def_collector.crate_local_def_map.unwrap_or(&self.def_collector.local_def_map); self.def_collector.crate_local_def_map.unwrap_or(&self.def_collector.local_def_map);
match item { match item {
ModItem::Mod(m) => self.collect_module(m, &attrs), ModItemId::Mod(m) => self.collect_module(m, &attrs),
ModItem::Use(item_tree_id) => { ModItemId::Use(item_tree_id) => {
let id = UseLoc { let id = UseLoc {
container: module, container: module,
id: InFile::new(self.file_id(), self.item_tree[item_tree_id].ast_id), id: InFile::new(self.file_id(), self.item_tree[item_tree_id].ast_id),
@ -1771,7 +1771,7 @@ impl ModCollector<'_, '_> {
}, },
) )
} }
ModItem::ExternCrate(item_tree_id) => { ModItemId::ExternCrate(item_tree_id) => {
let item_tree::ExternCrate { name, visibility, alias, ast_id } = let item_tree::ExternCrate { name, visibility, alias, ast_id } =
&self.item_tree[item_tree_id]; &self.item_tree[item_tree_id];
@ -1845,7 +1845,7 @@ impl ModCollector<'_, '_> {
); );
} }
} }
ModItem::ExternBlock(block) => { ModItemId::ExternBlock(block) => {
let extern_block_id = ExternBlockLoc { let extern_block_id = ExternBlockLoc {
container: module, container: module,
id: InFile::new(self.file_id(), self.item_tree[block].ast_id), id: InFile::new(self.file_id(), self.item_tree[block].ast_id),
@ -1859,10 +1859,12 @@ impl ModCollector<'_, '_> {
ItemContainerId::ExternBlockId(extern_block_id), ItemContainerId::ExternBlockId(extern_block_id),
) )
} }
ModItem::MacroCall(mac) => self.collect_macro_call(&self.item_tree[mac], container), ModItemId::MacroCall(mac) => {
ModItem::MacroRules(id) => self.collect_macro_rules(id, module), self.collect_macro_call(&self.item_tree[mac], container)
ModItem::Macro2(id) => self.collect_macro_def(id, module), }
ModItem::Impl(imp) => { ModItemId::MacroRules(id) => self.collect_macro_rules(id, module),
ModItemId::Macro2(id) => self.collect_macro_def(id, module),
ModItemId::Impl(imp) => {
let impl_id = ImplLoc { let impl_id = ImplLoc {
container: module, container: module,
id: InFile::new(self.file_id(), self.item_tree[imp].ast_id), id: InFile::new(self.file_id(), self.item_tree[imp].ast_id),
@ -1870,7 +1872,7 @@ impl ModCollector<'_, '_> {
.intern(db); .intern(db);
self.def_collector.def_map.modules[self.module_id].scope.define_impl(impl_id) self.def_collector.def_map.modules[self.module_id].scope.define_impl(impl_id)
} }
ModItem::Function(id) => { ModItemId::Function(id) => {
let it = &self.item_tree[id]; let it = &self.item_tree[id];
let fn_id = FunctionLoc { let fn_id = FunctionLoc {
container, container,
@ -1895,7 +1897,7 @@ impl ModCollector<'_, '_> {
update_def(self.def_collector, fn_id.into(), &it.name, vis, false); update_def(self.def_collector, fn_id.into(), &it.name, vis, false);
} }
ModItem::Struct(id) => { ModItemId::Struct(id) => {
let it = &self.item_tree[id]; let it = &self.item_tree[id];
let vis = resolve_vis(def_map, local_def_map, &self.item_tree[it.visibility]); let vis = resolve_vis(def_map, local_def_map, &self.item_tree[it.visibility]);
@ -1909,7 +1911,7 @@ impl ModCollector<'_, '_> {
!matches!(it.shape, FieldsShape::Record), !matches!(it.shape, FieldsShape::Record),
); );
} }
ModItem::Union(id) => { ModItemId::Union(id) => {
let it = &self.item_tree[id]; let it = &self.item_tree[id];
let vis = resolve_vis(def_map, local_def_map, &self.item_tree[it.visibility]); let vis = resolve_vis(def_map, local_def_map, &self.item_tree[it.visibility]);
@ -1923,7 +1925,7 @@ impl ModCollector<'_, '_> {
false, false,
); );
} }
ModItem::Enum(id) => { ModItemId::Enum(id) => {
let it = &self.item_tree[id]; let it = &self.item_tree[id];
let enum_ = EnumLoc { let enum_ = EnumLoc {
container: module, container: module,
@ -1934,7 +1936,7 @@ impl ModCollector<'_, '_> {
let vis = resolve_vis(def_map, local_def_map, &self.item_tree[it.visibility]); let vis = resolve_vis(def_map, local_def_map, &self.item_tree[it.visibility]);
update_def(self.def_collector, enum_.into(), &it.name, vis, false); update_def(self.def_collector, enum_.into(), &it.name, vis, false);
} }
ModItem::Const(id) => { ModItemId::Const(id) => {
let it = &self.item_tree[id]; let it = &self.item_tree[id];
let const_id = let const_id =
ConstLoc { container, id: InFile::new(self.tree_id.file_id(), it.ast_id) } ConstLoc { container, id: InFile::new(self.tree_id.file_id(), it.ast_id) }
@ -1954,7 +1956,7 @@ impl ModCollector<'_, '_> {
} }
} }
} }
ModItem::Static(id) => { ModItemId::Static(id) => {
let it = &self.item_tree[id]; let it = &self.item_tree[id];
let vis = resolve_vis(def_map, local_def_map, &self.item_tree[it.visibility]); let vis = resolve_vis(def_map, local_def_map, &self.item_tree[it.visibility]);
@ -1968,7 +1970,7 @@ impl ModCollector<'_, '_> {
false, false,
); );
} }
ModItem::Trait(id) => { ModItemId::Trait(id) => {
let it = &self.item_tree[id]; let it = &self.item_tree[id];
let vis = resolve_vis(def_map, local_def_map, &self.item_tree[it.visibility]); let vis = resolve_vis(def_map, local_def_map, &self.item_tree[it.visibility]);
@ -1982,7 +1984,7 @@ impl ModCollector<'_, '_> {
false, false,
); );
} }
ModItem::TraitAlias(id) => { ModItemId::TraitAlias(id) => {
let it = &self.item_tree[id]; let it = &self.item_tree[id];
let vis = resolve_vis(def_map, local_def_map, &self.item_tree[it.visibility]); let vis = resolve_vis(def_map, local_def_map, &self.item_tree[it.visibility]);
@ -1999,7 +2001,7 @@ impl ModCollector<'_, '_> {
false, false,
); );
} }
ModItem::TypeAlias(id) => { ModItemId::TypeAlias(id) => {
let it = &self.item_tree[id]; let it = &self.item_tree[id];
let vis = resolve_vis(def_map, local_def_map, &self.item_tree[it.visibility]); let vis = resolve_vis(def_map, local_def_map, &self.item_tree[it.visibility]);
@ -2022,12 +2024,12 @@ impl ModCollector<'_, '_> {
if is_crate_root { if is_crate_root {
items items
.iter() .iter()
.filter(|it| matches!(it, ModItem::ExternCrate(..))) .filter(|it| matches!(it, ModItemId::ExternCrate(..)))
.copied() .copied()
.for_each(&mut process_mod_item); .for_each(&mut process_mod_item);
items items
.iter() .iter()
.filter(|it| !matches!(it, ModItem::ExternCrate(..))) .filter(|it| !matches!(it, ModItemId::ExternCrate(..)))
.copied() .copied()
.for_each(process_mod_item); .for_each(process_mod_item);
} else { } else {
@ -2237,7 +2239,7 @@ impl ModCollector<'_, '_> {
fn resolve_attributes( fn resolve_attributes(
&mut self, &mut self,
attrs: &Attrs, attrs: &Attrs,
mod_item: ModItem, mod_item: ModItemId,
container: ItemContainerId, container: ItemContainerId,
) -> Result<(), ()> { ) -> Result<(), ()> {
let mut ignore_up_to = self let mut ignore_up_to = self