11267: internal: Remove `ModuleId` from `hir` reexports r=jonas-schievink a=jonas-schievink

followup to https://github.com/rust-analyzer/rust-analyzer/pull/11266

bors r+

Co-authored-by: Jonas Schievink <jonas.schievink@ferrous-systems.com>
This commit is contained in:
bors[bot] 2022-01-12 19:22:15 +00:00 committed by GitHub
commit 62a13cce9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 44 deletions

View File

@ -49,7 +49,7 @@
src::HasSource as _, src::HasSource as _,
AdtId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, DefWithBodyId, EnumId, AdtId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, DefWithBodyId, EnumId,
FunctionId, GenericDefId, HasModule, ImplId, ItemContainerId, LifetimeParamId, FunctionId, GenericDefId, HasModule, ImplId, ItemContainerId, LifetimeParamId,
LocalEnumVariantId, LocalFieldId, Lookup, StaticId, StructId, TraitId, TypeAliasId, LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
TypeParamId, UnionId, TypeParamId, UnionId,
}; };
use hir_expand::{name::name, MacroCallKind, MacroDefKind}; use hir_expand::{name::name, MacroCallKind, MacroDefKind};
@ -115,7 +115,6 @@
path::{ModPath, PathKind}, path::{ModPath, PathKind},
type_ref::{Mutability, TypeRef}, type_ref::{Mutability, TypeRef},
visibility::Visibility, visibility::Visibility,
ModuleId,
}, },
hir_expand::{ hir_expand::{
name::{known, Name}, name::{known, Name},
@ -183,6 +182,11 @@ pub fn root_module(self, db: &dyn HirDatabase) -> Module {
Module { id: def_map.module_id(def_map.root()) } Module { id: def_map.module_id(def_map.root()) }
} }
pub fn modules(self, db: &dyn HirDatabase) -> Vec<Module> {
let def_map = db.crate_def_map(self.id);
def_map.modules().map(|(id, _)| def_map.module_id(id).into()).collect()
}
pub fn root_file(self, db: &dyn HirDatabase) -> FileId { pub fn root_file(self, db: &dyn HirDatabase) -> FileId {
db.crate_graph()[self.id].root_file_id db.crate_graph()[self.id].root_file_id
} }

View File

@ -102,14 +102,14 @@ pub struct SymbolCollector<'a> {
/// Given a [`ModuleId`] and a [`HirDatabase`], use the DefMap for the module's crate to collect /// Given a [`ModuleId`] and a [`HirDatabase`], use the DefMap for the module's crate to collect
/// all symbols that should be indexed for the given module. /// all symbols that should be indexed for the given module.
impl<'a> SymbolCollector<'a> { impl<'a> SymbolCollector<'a> {
pub fn collect(db: &dyn HirDatabase, module_id: ModuleId) -> Vec<FileSymbol> { pub fn collect(db: &dyn HirDatabase, module: Module) -> Vec<FileSymbol> {
let mut symbol_collector = SymbolCollector { let mut symbol_collector = SymbolCollector {
db, db,
symbols: Default::default(), symbols: Default::default(),
current_container_name: None, current_container_name: None,
// The initial work is the root module we're collecting, additional work will // The initial work is the root module we're collecting, additional work will
// be populated as we traverse the module's definitions. // be populated as we traverse the module's definitions.
work: vec![SymbolCollectorWork { module_id, parent: None }], work: vec![SymbolCollectorWork { module_id: module.into(), parent: None }],
}; };
while let Some(work) = symbol_collector.work.pop() { while let Some(work) = symbol_collector.work.pop() {

View File

@ -30,13 +30,13 @@
use base_db::{ use base_db::{
salsa::{self, ParallelDatabase}, salsa::{self, ParallelDatabase},
CrateId, SourceDatabaseExt, SourceRootId, Upcast, SourceDatabaseExt, SourceRootId, Upcast,
}; };
use fst::{self, Streamer}; use fst::{self, Streamer};
use hir::{ use hir::{
db::{DefDatabase, HirDatabase}, db::HirDatabase,
symbols::{FileSymbol, SymbolCollector}, symbols::{FileSymbol, SymbolCollector},
ModuleId, Crate, Module,
}; };
use rayon::prelude::*; use rayon::prelude::*;
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
@ -93,7 +93,7 @@ pub fn limit(&mut self, limit: usize) {
pub trait SymbolsDatabase: HirDatabase + SourceDatabaseExt + Upcast<dyn HirDatabase> { pub trait SymbolsDatabase: HirDatabase + SourceDatabaseExt + Upcast<dyn HirDatabase> {
/// The symbol index for a given module. These modules should only be in source roots that /// The symbol index for a given module. These modules should only be in source roots that
/// are inside local_roots. /// are inside local_roots.
fn module_symbols(&self, module_id: ModuleId) -> Arc<SymbolIndex>; fn module_symbols(&self, module: Module) -> Arc<SymbolIndex>;
/// The symbol index for a given source root within library_roots. /// The symbol index for a given source root within library_roots.
fn library_symbols(&self, source_root_id: SourceRootId) -> Arc<SymbolIndex>; fn library_symbols(&self, source_root_id: SourceRootId) -> Arc<SymbolIndex>;
@ -116,20 +116,20 @@ fn library_symbols(db: &dyn SymbolsDatabase, source_root_id: SourceRootId) -> Ar
let symbols = db let symbols = db
.source_root_crates(source_root_id) .source_root_crates(source_root_id)
.iter() .iter()
.flat_map(|&krate| module_ids_for_crate(db.upcast(), krate)) .flat_map(|&krate| Crate::from(krate).modules(db.upcast()))
// we specifically avoid calling SymbolsDatabase::module_symbols here, even they do the same thing, // we specifically avoid calling SymbolsDatabase::module_symbols here, even they do the same thing,
// as the index for a library is not going to really ever change, and we do not want to store each // as the index for a library is not going to really ever change, and we do not want to store each
// module's index in salsa. // module's index in salsa.
.map(|module_id| SymbolCollector::collect(db.upcast(), module_id)) .map(|module| SymbolCollector::collect(db.upcast(), module))
.flatten() .flatten()
.collect(); .collect();
Arc::new(SymbolIndex::new(symbols)) Arc::new(SymbolIndex::new(symbols))
} }
fn module_symbols(db: &dyn SymbolsDatabase, module_id: ModuleId) -> Arc<SymbolIndex> { fn module_symbols(db: &dyn SymbolsDatabase, module: Module) -> Arc<SymbolIndex> {
let _p = profile::span("module_symbols"); let _p = profile::span("module_symbols");
let symbols = SymbolCollector::collect(db.upcast(), module_id); let symbols = SymbolCollector::collect(db.upcast(), module);
Arc::new(SymbolIndex::new(symbols)) Arc::new(SymbolIndex::new(symbols))
} }
@ -188,41 +188,36 @@ pub fn world_symbols(db: &RootDatabase, query: Query) -> Vec<FileSymbol> {
.map_with(Snap::new(db), |snap, &root| snap.library_symbols(root)) .map_with(Snap::new(db), |snap, &root| snap.library_symbols(root))
.collect() .collect()
} else { } else {
let mut module_ids = Vec::new(); let mut modules = Vec::new();
for &root in db.local_roots().iter() { for &root in db.local_roots().iter() {
let crates = db.source_root_crates(root); let crates = db.source_root_crates(root);
for &krate in crates.iter() { for &krate in crates.iter() {
module_ids.extend(module_ids_for_crate(db, krate)); modules.extend(Crate::from(krate).modules(db));
} }
} }
module_ids modules
.par_iter() .par_iter()
.map_with(Snap::new(db), |snap, &module_id| snap.module_symbols(module_id)) .map_with(Snap::new(db), |snap, &module| snap.module_symbols(module))
.collect() .collect()
}; };
query.search(&indices) query.search(&indices)
} }
pub fn crate_symbols(db: &RootDatabase, krate: CrateId, query: Query) -> Vec<FileSymbol> { pub fn crate_symbols(db: &RootDatabase, krate: Crate, query: Query) -> Vec<FileSymbol> {
let _p = profile::span("crate_symbols").detail(|| format!("{:?}", query)); let _p = profile::span("crate_symbols").detail(|| format!("{:?}", query));
let module_ids = module_ids_for_crate(db, krate); let modules = krate.modules(db);
let indices: Vec<_> = module_ids let indices: Vec<_> = modules
.par_iter() .par_iter()
.map_with(Snap::new(db), |snap, &module_id| snap.module_symbols(module_id)) .map_with(Snap::new(db), |snap, &module| snap.module_symbols(module))
.collect(); .collect();
query.search(&indices) query.search(&indices)
} }
fn module_ids_for_crate(db: &dyn DefDatabase, krate: CrateId) -> Vec<ModuleId> {
let def_map = db.crate_def_map(krate);
def_map.modules().map(|(id, _)| def_map.module_id(id)).collect()
}
pub fn index_resolve(db: &RootDatabase, name: &str) -> Vec<FileSymbol> { pub fn index_resolve(db: &RootDatabase, name: &str) -> Vec<FileSymbol> {
let mut query = Query::new(name.to_string()); let mut query = Query::new(name.to_string());
query.exact(); query.exact();
@ -427,7 +422,8 @@ mod a_mod {
"#, "#,
); );
let symbols: Vec<_> = module_ids_for_crate(db.upcast(), db.test_crate()) let symbols: Vec<_> = Crate::from(db.test_crate())
.modules(&db)
.into_iter() .into_iter()
.map(|module_id| (module_id, SymbolCollector::collect(&db, module_id))) .map(|module_id| (module_id, SymbolCollector::collect(&db, module_id)))
.collect(); .collect();

View File

@ -1,12 +1,14 @@
[ [
( (
ModuleId { Module {
id: ModuleId {
krate: CrateId( krate: CrateId(
0, 0,
), ),
block: None, block: None,
local_id: Idx::<ModuleData>(0), local_id: Idx::<ModuleData>(0),
}, },
},
[ [
FileSymbol { FileSymbol {
name: "StructFromMacro", name: "StructFromMacro",
@ -459,13 +461,15 @@
], ],
), ),
( (
ModuleId { Module {
id: ModuleId {
krate: CrateId( krate: CrateId(
0, 0,
), ),
block: None, block: None,
local_id: Idx::<ModuleData>(1), local_id: Idx::<ModuleData>(1),
}, },
},
[ [
FileSymbol { FileSymbol {
name: "StructInModA", name: "StructInModA",
@ -492,13 +496,15 @@
], ],
), ),
( (
ModuleId { Module {
id: ModuleId {
krate: CrateId( krate: CrateId(
0, 0,
), ),
block: None, block: None,
local_id: Idx::<ModuleData>(2), local_id: Idx::<ModuleData>(2),
}, },
},
[ [
FileSymbol { FileSymbol {
name: "StructInModB", name: "StructInModB",