Remove unnecessary macro_declarations from ItemScope

This commit is contained in:
Lukas Wirth 2022-03-09 00:19:53 +01:00
parent dbada38b61
commit 55ec93a337
6 changed files with 25 additions and 57 deletions

View file

@ -1767,11 +1767,13 @@ impl Macro {
} }
pub fn name(self, _db: &dyn HirDatabase) -> Option<Name> { pub fn name(self, _db: &dyn HirDatabase) -> Option<Name> {
match self.id { // match self.id {
MacroId::Macro2Id(_id) => todo!(), // MacroId::Macro2Id(id) => db.macro2_data(id).name.clone(),
MacroId::MacroRulesId(_id) => todo!(), // MacroId::MacroRulesId(id) => db.macro_rules_data(id).name.clone(),
MacroId::ProcMacroId(_id) => todo!(), // MacroId::ProcMacroId(id) => db.proc_macro_data(id).name.clone(),
} // }
// FIXME
None
} }
pub fn kind(&self, db: &dyn HirDatabase) -> MacroKind { pub fn kind(&self, db: &dyn HirDatabase) -> MacroKind {

View file

@ -1,7 +1,6 @@
//! File symbol extraction. //! File symbol extraction.
use base_db::FileRange; use base_db::FileRange;
use either::Either;
use hir_def::{ use hir_def::{
item_tree::ItemTreeNode, src::HasSource, AdtId, AssocItemId, AssocItemLoc, DefWithBodyId, item_tree::ItemTreeNode, src::HasSource, AdtId, AssocItemId, AssocItemLoc, DefWithBodyId,
ImplId, ItemContainerId, Lookup, MacroId, ModuleDefId, ModuleId, TraitId, ImplId, ItemContainerId, Lookup, MacroId, ModuleDefId, ModuleId, TraitId,
@ -10,7 +9,7 @@ use hir_expand::{HirFileId, InFile};
use hir_ty::db::HirDatabase; use hir_ty::db::HirDatabase;
use syntax::{ast::HasName, AstNode, SmolStr, SyntaxNode, SyntaxNodePtr}; use syntax::{ast::HasName, AstNode, SmolStr, SyntaxNode, SyntaxNodePtr};
use crate::{HasSource as _, Macro, Module, Semantics}; use crate::{Module, Semantics};
/// The actual data that is stored in the index. It should be as compact as /// The actual data that is stored in the index. It should be as compact as
/// possible. /// possible.
@ -175,10 +174,6 @@ impl<'a> SymbolCollector<'a> {
for const_id in scope.unnamed_consts() { for const_id in scope.unnamed_consts() {
self.collect_from_body(const_id); self.collect_from_body(const_id);
} }
for macro_def_id in scope.macro_declarations() {
self.push_decl_macro(macro_def_id.into());
}
} }
fn collect_from_body(&mut self, body_id: impl Into<DefWithBodyId>) { fn collect_from_body(&mut self, body_id: impl Into<DefWithBodyId>) {
@ -333,29 +328,6 @@ impl<'a> SymbolCollector<'a> {
}) })
} }
fn push_decl_macro(&mut self, macro_def: Macro) {
self.push_file_symbol(|s| {
let name = macro_def.name(s.db)?.as_text()?;
let source = macro_def.source(s.db)?;
let (ptr, name_ptr) = match source.value {
Either::Left(m) => {
(SyntaxNodePtr::new(m.syntax()), SyntaxNodePtr::new(m.name()?.syntax()))
}
Either::Right(f) => {
(SyntaxNodePtr::new(f.syntax()), SyntaxNodePtr::new(f.name()?.syntax()))
}
};
Some(FileSymbol {
name,
kind: FileSymbolKind::Macro,
container_name: s.current_container_name(),
loc: DeclarationLocation { hir_file_id: source.file_id, name_ptr, ptr },
})
})
}
fn push_file_symbol(&mut self, f: impl FnOnce(&Self) -> Option<FileSymbol>) { fn push_file_symbol(&mut self, f: impl FnOnce(&Self) -> Option<FileSymbol>) {
if let Some(file_symbol) = f(self) { if let Some(file_symbol) = f(self) {
self.symbols.push(file_symbol); self.symbols.push(file_symbol);

View file

@ -93,6 +93,7 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
#[salsa::invoke(StructData::struct_data_query)] #[salsa::invoke(StructData::struct_data_query)]
fn struct_data(&self, id: StructId) -> Arc<StructData>; fn struct_data(&self, id: StructId) -> Arc<StructData>;
#[salsa::invoke(StructData::union_data_query)] #[salsa::invoke(StructData::union_data_query)]
fn union_data(&self, id: UnionId) -> Arc<StructData>; fn union_data(&self, id: UnionId) -> Arc<StructData>;

View file

@ -45,7 +45,6 @@ pub struct ItemScope {
/// The defs declared in this scope. Each def has a single scope where it is /// The defs declared in this scope. Each def has a single scope where it is
/// declared. /// declared.
declarations: Vec<ModuleDefId>, declarations: Vec<ModuleDefId>,
macro_declarations: Vec<MacroId>,
impls: Vec<ImplId>, impls: Vec<ImplId>,
unnamed_consts: Vec<ConstId>, unnamed_consts: Vec<ConstId>,
@ -109,10 +108,6 @@ impl ItemScope {
self.declarations.iter().copied() self.declarations.iter().copied()
} }
pub fn macro_declarations(&self) -> impl Iterator<Item = MacroId> + '_ {
self.macro_declarations.iter().copied()
}
pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ { pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ {
self.impls.iter().copied() self.impls.iter().copied()
} }
@ -177,10 +172,6 @@ impl ItemScope {
self.declarations.push(def) self.declarations.push(def)
} }
pub(crate) fn declare_macro(&mut self, def: MacroId) {
self.macro_declarations.push(def);
}
pub(crate) fn get_legacy_macro(&self, name: &Name) -> Option<MacroRulesId> { pub(crate) fn get_legacy_macro(&self, name: &Name) -> Option<MacroRulesId> {
self.legacy_macros.get(name).copied() self.legacy_macros.get(name).copied()
} }
@ -380,7 +371,6 @@ impl ItemScope {
macros, macros,
unresolved, unresolved,
declarations, declarations,
macro_declarations,
impls, impls,
unnamed_consts, unnamed_consts,
unnamed_trait_imports, unnamed_trait_imports,
@ -393,7 +383,6 @@ impl ItemScope {
macros.shrink_to_fit(); macros.shrink_to_fit();
unresolved.shrink_to_fit(); unresolved.shrink_to_fit();
declarations.shrink_to_fit(); declarations.shrink_to_fit();
macro_declarations.shrink_to_fit();
impls.shrink_to_fit(); impls.shrink_to_fit();
unnamed_consts.shrink_to_fit(); unnamed_consts.shrink_to_fit();
unnamed_trait_imports.shrink_to_fit(); unnamed_trait_imports.shrink_to_fit();

View file

@ -448,7 +448,7 @@ pub enum ModuleDefId {
MacroId(MacroId), MacroId(MacroId),
} }
impl_from!( impl_from!(
MacroId, MacroId(Macro2Id, MacroRulesId, ProcMacroId),
ModuleId, ModuleId,
FunctionId, FunctionId,
AdtId(StructId, EnumId, UnionId), AdtId(StructId, EnumId, UnionId),

View file

@ -606,16 +606,16 @@ impl DefCollector<'_> {
) { ) {
// Textual scoping // Textual scoping
self.define_legacy_macro(module_id, name.clone(), macro_); self.define_legacy_macro(module_id, name.clone(), macro_);
let macro_ = macro_.into();
self.def_map.modules[module_id].scope.declare_macro(macro_);
// Module scoping // Module scoping
// In Rust, `#[macro_export]` macros are unconditionally visible at the // In Rust, `#[macro_export]` macros are unconditionally visible at the
// crate root, even if the parent modules is **not** visible. // crate root, even if the parent modules is **not** visible.
if export { if export {
let module_id = self.def_map.root;
self.def_map.modules[module_id].scope.declare(macro_.into());
self.update( self.update(
self.def_map.root, module_id,
&[(Some(name), PerNs::macros(macro_, Visibility::Public))], &[(Some(name), PerNs::macros(macro_.into(), Visibility::Public))],
Visibility::Public, Visibility::Public,
ImportType::Named, ImportType::Named,
); );
@ -646,9 +646,13 @@ impl DefCollector<'_> {
) { ) {
let vis = let vis =
self.def_map.resolve_visibility(self.db, module_id, vis).unwrap_or(Visibility::Public); self.def_map.resolve_visibility(self.db, module_id, vis).unwrap_or(Visibility::Public);
let macro_ = macro_.into(); self.def_map.modules[module_id].scope.declare(macro_.into());
self.def_map.modules[module_id].scope.declare_macro(macro_); self.update(
self.update(module_id, &[(Some(name), PerNs::macros(macro_, vis))], vis, ImportType::Named); module_id,
&[(Some(name), PerNs::macros(macro_.into(), Visibility::Public))],
vis,
ImportType::Named,
);
} }
/// Define a proc macro /// Define a proc macro
@ -656,11 +660,11 @@ impl DefCollector<'_> {
/// A proc macro is similar to normal macro scope, but it would not visible in legacy textual scoped. /// A proc macro is similar to normal macro scope, but it would not visible in legacy textual scoped.
/// And unconditionally exported. /// And unconditionally exported.
fn define_proc_macro(&mut self, name: Name, macro_: ProcMacroId) { fn define_proc_macro(&mut self, name: Name, macro_: ProcMacroId) {
let macro_ = macro_.into(); let module_id = self.def_map.root;
self.def_map.modules[self.def_map.root].scope.declare_macro(macro_); self.def_map.modules[module_id].scope.declare(macro_.into());
self.update( self.update(
self.def_map.root, module_id,
&[(Some(name), PerNs::macros(macro_, Visibility::Public))], &[(Some(name), PerNs::macros(macro_.into(), Visibility::Public))],
Visibility::Public, Visibility::Public,
ImportType::Named, ImportType::Named,
); );