Repository: rust-analyzer/rust-analyzer
Pull request #11267 (2022-01-12 19:22:00)

internal: Remove `ModuleId` from `hir` reexports
followup to https://github.com/rust-analyzer/rust-analyzer/pull/11266

bors r+


Patch:
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 22789e0897..8714406925 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -49,7 +49,7 @@ use hir_def::{
     src::HasSource as _,
     AdtId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, DefWithBodyId, EnumId,
     FunctionId, GenericDefId, HasModule, ImplId, ItemContainerId, LifetimeParamId,
-    LocalEnumVariantId, LocalFieldId, Lookup, StaticId, StructId, TraitId, TypeAliasId,
+    LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
     TypeParamId, UnionId,
 };
 use hir_expand::{name::name, MacroCallKind, MacroDefKind};
@@ -115,7 +115,6 @@ pub use {
         path::{ModPath, PathKind},
         type_ref::{Mutability, TypeRef},
         visibility::Visibility,
-        ModuleId,
     },
     hir_expand::{
         name::{known, Name},
@@ -183,6 +182,11 @@ impl Crate {
         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 {
         db.crate_graph()[self.id].root_file_id
     }
diff --git a/crates/hir/src/symbols.rs b/crates/hir/src/symbols.rs
index b9d2f0cdca..f1669d5288 100644
--- a/crates/hir/src/symbols.rs
+++ b/crates/hir/src/symbols.rs
@@ -102,14 +102,14 @@ pub struct SymbolCollector<'a> {
 /// 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.
 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 {
             db,
             symbols: Default::default(),
             current_container_name: None,
             // The initial work is the root module we're collecting, additional work will
             // 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() {
diff --git a/crates/ide_db/src/symbol_index.rs b/crates/ide_db/src/symbol_index.rs
index e82230b2f3..9697ba24b8 100644
--- a/crates/ide_db/src/symbol_index.rs
+++ b/crates/ide_db/src/symbol_index.rs
@@ -30,13 +30,13 @@ use std::{
 
 use base_db::{
     salsa::{self, ParallelDatabase},
-    CrateId, SourceDatabaseExt, SourceRootId, Upcast,
+    SourceDatabaseExt, SourceRootId, Upcast,
 };
 use fst::{self, Streamer};
 use hir::{
-    db::{DefDatabase, HirDatabase},
+    db::HirDatabase,
     symbols::{FileSymbol, SymbolCollector},
-    ModuleId,
+    Crate, Module,
 };
 use rayon::prelude::*;
 use rustc_hash::FxHashSet;
@@ -93,7 +93,7 @@ impl Query {
 pub trait SymbolsDatabase: HirDatabase + SourceDatabaseExt + Upcast<dyn HirDatabase> {
     /// The symbol index for a given module. These modules should only be in source roots that
     /// 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.
     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
         .source_root_crates(source_root_id)
         .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,
         // 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.
-        .map(|module_id| SymbolCollector::collect(db.upcast(), module_id))
+        .map(|module| SymbolCollector::collect(db.upcast(), module))
         .flatten()
         .collect();
 
     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 symbols = SymbolCollector::collect(db.upcast(), module_id);
+    let symbols = SymbolCollector::collect(db.upcast(), module);
     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))
             .collect()
     } else {
-        let mut module_ids = Vec::new();
+        let mut modules = Vec::new();
 
         for &root in db.local_roots().iter() {
             let crates = db.source_root_crates(root);
             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()
-            .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()
     };
 
     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 module_ids = module_ids_for_crate(db, krate);
-    let indices: Vec<_> = module_ids
+    let modules = krate.modules(db);
+    let indices: Vec<_> = modules
         .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();
 
     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> {
     let mut query = Query::new(name.to_string());
     query.exact();
@@ -427,7 +422,8 @@ struct StructInModB;
         "#,
         );
 
-        let symbols: Vec<_> = module_ids_for_crate(db.upcast(), db.test_crate())
+        let symbols: Vec<_> = Crate::from(db.test_crate())
+            .modules(&db)
             .into_iter()
             .map(|module_id| (module_id, SymbolCollector::collect(&db, module_id)))
             .collect();


Test patch:
diff --git a/crates/ide_db/src/test_data/test_symbol_index_collection.txt b/crates/ide_db/src/test_data/test_symbol_index_collection.txt
index 176d7e1579..cc51d85da7 100644
--- a/crates/ide_db/src/test_data/test_symbol_index_collection.txt
+++ b/crates/ide_db/src/test_data/test_symbol_index_collection.txt
@@ -1,11 +1,13 @@
 [
     (
-        ModuleId {
-            krate: CrateId(
-                0,
-            ),
-            block: None,
-            local_id: Idx::<ModuleData>(0),
+        Module {
+            id: ModuleId {
+                krate: CrateId(
+                    0,
+                ),
+                block: None,
+                local_id: Idx::<ModuleData>(0),
+            },
         },
         [
             FileSymbol {
@@ -459,12 +461,14 @@
         ],
     ),
     (
-        ModuleId {
-            krate: CrateId(
-                0,
-            ),
-            block: None,
-            local_id: Idx::<ModuleData>(1),
+        Module {
+            id: ModuleId {
+                krate: CrateId(
+                    0,
+                ),
+                block: None,
+                local_id: Idx::<ModuleData>(1),
+            },
         },
         [
             FileSymbol {
@@ -492,12 +496,14 @@
         ],
     ),
     (
-        ModuleId {
-            krate: CrateId(
-                0,
-            ),
-            block: None,
-            local_id: Idx::<ModuleData>(2),
+        Module {
+            id: ModuleId {
+                krate: CrateId(
+                    0,
+                ),
+                block: None,
+                local_id: Idx::<ModuleData>(2),
+            },
         },
         [
             FileSymbol {