Skip to content

Commit 62a13cc

Browse files
bors[bot]Jonas Schievink
andauthored
Merge #11267
11267: internal: Remove `ModuleId` from `hir` reexports r=jonas-schievink a=jonas-schievink followup to #11266 bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents 16d3a7b + 18e80e6 commit 62a13cc

File tree

4 files changed

+50
-44
lines changed

4 files changed

+50
-44
lines changed

crates/hir/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use hir_def::{
4949
src::HasSource as _,
5050
AdtId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, DefWithBodyId, EnumId,
5151
FunctionId, GenericDefId, HasModule, ImplId, ItemContainerId, LifetimeParamId,
52-
LocalEnumVariantId, LocalFieldId, Lookup, StaticId, StructId, TraitId, TypeAliasId,
52+
LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
5353
TypeParamId, UnionId,
5454
};
5555
use hir_expand::{name::name, MacroCallKind, MacroDefKind};
@@ -115,7 +115,6 @@ pub use {
115115
path::{ModPath, PathKind},
116116
type_ref::{Mutability, TypeRef},
117117
visibility::Visibility,
118-
ModuleId,
119118
},
120119
hir_expand::{
121120
name::{known, Name},
@@ -183,6 +182,11 @@ impl Crate {
183182
Module { id: def_map.module_id(def_map.root()) }
184183
}
185184

185+
pub fn modules(self, db: &dyn HirDatabase) -> Vec<Module> {
186+
let def_map = db.crate_def_map(self.id);
187+
def_map.modules().map(|(id, _)| def_map.module_id(id).into()).collect()
188+
}
189+
186190
pub fn root_file(self, db: &dyn HirDatabase) -> FileId {
187191
db.crate_graph()[self.id].root_file_id
188192
}

crates/hir/src/symbols.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ pub struct SymbolCollector<'a> {
102102
/// Given a [`ModuleId`] and a [`HirDatabase`], use the DefMap for the module's crate to collect
103103
/// all symbols that should be indexed for the given module.
104104
impl<'a> SymbolCollector<'a> {
105-
pub fn collect(db: &dyn HirDatabase, module_id: ModuleId) -> Vec<FileSymbol> {
105+
pub fn collect(db: &dyn HirDatabase, module: Module) -> Vec<FileSymbol> {
106106
let mut symbol_collector = SymbolCollector {
107107
db,
108108
symbols: Default::default(),
109109
current_container_name: None,
110110
// The initial work is the root module we're collecting, additional work will
111111
// be populated as we traverse the module's definitions.
112-
work: vec![SymbolCollectorWork { module_id, parent: None }],
112+
work: vec![SymbolCollectorWork { module_id: module.into(), parent: None }],
113113
};
114114

115115
while let Some(work) = symbol_collector.work.pop() {

crates/ide_db/src/symbol_index.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ use std::{
3030

3131
use base_db::{
3232
salsa::{self, ParallelDatabase},
33-
CrateId, SourceDatabaseExt, SourceRootId, Upcast,
33+
SourceDatabaseExt, SourceRootId, Upcast,
3434
};
3535
use fst::{self, Streamer};
3636
use hir::{
37-
db::{DefDatabase, HirDatabase},
37+
db::HirDatabase,
3838
symbols::{FileSymbol, SymbolCollector},
39-
ModuleId,
39+
Crate, Module,
4040
};
4141
use rayon::prelude::*;
4242
use rustc_hash::FxHashSet;
@@ -93,7 +93,7 @@ impl Query {
9393
pub trait SymbolsDatabase: HirDatabase + SourceDatabaseExt + Upcast<dyn HirDatabase> {
9494
/// The symbol index for a given module. These modules should only be in source roots that
9595
/// are inside local_roots.
96-
fn module_symbols(&self, module_id: ModuleId) -> Arc<SymbolIndex>;
96+
fn module_symbols(&self, module: Module) -> Arc<SymbolIndex>;
9797

9898
/// The symbol index for a given source root within library_roots.
9999
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
116116
let symbols = db
117117
.source_root_crates(source_root_id)
118118
.iter()
119-
.flat_map(|&krate| module_ids_for_crate(db.upcast(), krate))
119+
.flat_map(|&krate| Crate::from(krate).modules(db.upcast()))
120120
// we specifically avoid calling SymbolsDatabase::module_symbols here, even they do the same thing,
121121
// as the index for a library is not going to really ever change, and we do not want to store each
122122
// module's index in salsa.
123-
.map(|module_id| SymbolCollector::collect(db.upcast(), module_id))
123+
.map(|module| SymbolCollector::collect(db.upcast(), module))
124124
.flatten()
125125
.collect();
126126

127127
Arc::new(SymbolIndex::new(symbols))
128128
}
129129

130-
fn module_symbols(db: &dyn SymbolsDatabase, module_id: ModuleId) -> Arc<SymbolIndex> {
130+
fn module_symbols(db: &dyn SymbolsDatabase, module: Module) -> Arc<SymbolIndex> {
131131
let _p = profile::span("module_symbols");
132-
let symbols = SymbolCollector::collect(db.upcast(), module_id);
132+
let symbols = SymbolCollector::collect(db.upcast(), module);
133133
Arc::new(SymbolIndex::new(symbols))
134134
}
135135

@@ -188,41 +188,36 @@ pub fn world_symbols(db: &RootDatabase, query: Query) -> Vec<FileSymbol> {
188188
.map_with(Snap::new(db), |snap, &root| snap.library_symbols(root))
189189
.collect()
190190
} else {
191-
let mut module_ids = Vec::new();
191+
let mut modules = Vec::new();
192192

193193
for &root in db.local_roots().iter() {
194194
let crates = db.source_root_crates(root);
195195
for &krate in crates.iter() {
196-
module_ids.extend(module_ids_for_crate(db, krate));
196+
modules.extend(Crate::from(krate).modules(db));
197197
}
198198
}
199199

200-
module_ids
200+
modules
201201
.par_iter()
202-
.map_with(Snap::new(db), |snap, &module_id| snap.module_symbols(module_id))
202+
.map_with(Snap::new(db), |snap, &module| snap.module_symbols(module))
203203
.collect()
204204
};
205205

206206
query.search(&indices)
207207
}
208208

209-
pub fn crate_symbols(db: &RootDatabase, krate: CrateId, query: Query) -> Vec<FileSymbol> {
209+
pub fn crate_symbols(db: &RootDatabase, krate: Crate, query: Query) -> Vec<FileSymbol> {
210210
let _p = profile::span("crate_symbols").detail(|| format!("{:?}", query));
211211

212-
let module_ids = module_ids_for_crate(db, krate);
213-
let indices: Vec<_> = module_ids
212+
let modules = krate.modules(db);
213+
let indices: Vec<_> = modules
214214
.par_iter()
215-
.map_with(Snap::new(db), |snap, &module_id| snap.module_symbols(module_id))
215+
.map_with(Snap::new(db), |snap, &module| snap.module_symbols(module))
216216
.collect();
217217

218218
query.search(&indices)
219219
}
220220

221-
fn module_ids_for_crate(db: &dyn DefDatabase, krate: CrateId) -> Vec<ModuleId> {
222-
let def_map = db.crate_def_map(krate);
223-
def_map.modules().map(|(id, _)| def_map.module_id(id)).collect()
224-
}
225-
226221
pub fn index_resolve(db: &RootDatabase, name: &str) -> Vec<FileSymbol> {
227222
let mut query = Query::new(name.to_string());
228223
query.exact();
@@ -427,7 +422,8 @@ struct StructInModB;
427422
"#,
428423
);
429424

430-
let symbols: Vec<_> = module_ids_for_crate(db.upcast(), db.test_crate())
425+
let symbols: Vec<_> = Crate::from(db.test_crate())
426+
.modules(&db)
431427
.into_iter()
432428
.map(|module_id| (module_id, SymbolCollector::collect(&db, module_id)))
433429
.collect();

crates/ide_db/src/test_data/test_symbol_index_collection.txt

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
[
22
(
3-
ModuleId {
4-
krate: CrateId(
5-
0,
6-
),
7-
block: None,
8-
local_id: Idx::<ModuleData>(0),
3+
Module {
4+
id: ModuleId {
5+
krate: CrateId(
6+
0,
7+
),
8+
block: None,
9+
local_id: Idx::<ModuleData>(0),
10+
},
911
},
1012
[
1113
FileSymbol {
@@ -459,12 +461,14 @@
459461
],
460462
),
461463
(
462-
ModuleId {
463-
krate: CrateId(
464-
0,
465-
),
466-
block: None,
467-
local_id: Idx::<ModuleData>(1),
464+
Module {
465+
id: ModuleId {
466+
krate: CrateId(
467+
0,
468+
),
469+
block: None,
470+
local_id: Idx::<ModuleData>(1),
471+
},
468472
},
469473
[
470474
FileSymbol {
@@ -492,12 +496,14 @@
492496
],
493497
),
494498
(
495-
ModuleId {
496-
krate: CrateId(
497-
0,
498-
),
499-
block: None,
500-
local_id: Idx::<ModuleData>(2),
499+
Module {
500+
id: ModuleId {
501+
krate: CrateId(
502+
0,
503+
),
504+
block: None,
505+
local_id: Idx::<ModuleData>(2),
506+
},
501507
},
502508
[
503509
FileSymbol {

0 commit comments

Comments
 (0)