Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 58ac823

Browse files
committed
Less eager parsing for module sources
1 parent b5e0452 commit 58ac823

File tree

7 files changed

+37
-12
lines changed

7 files changed

+37
-12
lines changed

crates/hir-def/src/nameres.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ mod tests;
6060
use std::{cmp::Ord, ops::Deref};
6161

6262
use base_db::{CrateId, Edition, FileId, ProcMacroKind};
63-
use hir_expand::{name::Name, InFile, MacroCallId, MacroDefId};
63+
use hir_expand::{name::Name, HirFileId, InFile, MacroCallId, MacroDefId};
6464
use itertools::Itertools;
6565
use la_arena::Arena;
6666
use profile::Count;
@@ -626,6 +626,17 @@ impl ModuleData {
626626
self.origin.definition_source(db)
627627
}
628628

629+
/// Same as [`definition_source`] but only returns the file id to prevent parsing the ASt.
630+
pub fn definition_source_file_id(&self) -> HirFileId {
631+
match self.origin {
632+
ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition } => {
633+
definition.into()
634+
}
635+
ModuleOrigin::Inline { definition, .. } => definition.file_id,
636+
ModuleOrigin::BlockExpr { block } => block.file_id,
637+
}
638+
}
639+
629640
/// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
630641
/// `None` for the crate root or block.
631642
pub fn declaration_source(&self, db: &dyn DefDatabase) -> Option<InFile<ast::Module>> {

crates/hir/src/has_source.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
//! Provides set of implementation for hir's objects that allows get back location in file.
22
3+
use base_db::FileId;
34
use either::Either;
45
use hir_def::{
56
nameres::{ModuleOrigin, ModuleSource},
67
src::{HasChildSource, HasSource as _},
78
Lookup, MacroId, VariantId,
89
};
9-
use hir_expand::InFile;
10+
use hir_expand::{HirFileId, InFile};
1011
use syntax::ast;
1112

1213
use crate::{
@@ -32,6 +33,11 @@ impl Module {
3233
def_map[self.id.local_id].definition_source(db.upcast())
3334
}
3435

36+
pub fn definition_source_file_id(self, db: &dyn HirDatabase) -> HirFileId {
37+
let def_map = self.id.def_map(db.upcast());
38+
def_map[self.id.local_id].definition_source_file_id()
39+
}
40+
3541
pub fn is_mod_rs(self, db: &dyn HirDatabase) -> bool {
3642
let def_map = self.id.def_map(db.upcast());
3743
match def_map[self.id.local_id].origin {
@@ -40,6 +46,16 @@ impl Module {
4046
}
4147
}
4248

49+
pub fn as_source_file_id(self, db: &dyn HirDatabase) -> Option<FileId> {
50+
let def_map = self.id.def_map(db.upcast());
51+
match def_map[self.id.local_id].origin {
52+
ModuleOrigin::File { definition, .. } | ModuleOrigin::CrateRoot { definition, .. } => {
53+
Some(definition)
54+
}
55+
_ => None,
56+
}
57+
}
58+
4359
pub fn is_inline(self, db: &dyn HirDatabase) -> bool {
4460
let def_map = self.id.def_map(db.upcast());
4561
def_map[self.id.local_id].origin.is_inline()

crates/ide-completion/src/completions/mod_.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub(crate) fn complete_mod(
4242
}
4343

4444
let module_definition_file =
45-
current_module.definition_source(ctx.db).file_id.original_file(ctx.db);
45+
current_module.definition_source_file_id(ctx.db).original_file(ctx.db);
4646
let source_root = ctx.db.source_root(ctx.db.file_source_root(module_definition_file));
4747
let directory_to_look_for_submodules = directory_to_look_for_submodules(
4848
current_module,

crates/ide-db/src/search.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,8 @@ impl SearchScope {
149149

150150
let mut to_visit: Vec<_> = module.children(db).collect();
151151
while let Some(module) = to_visit.pop() {
152-
if let InFile { file_id, value: ModuleSource::SourceFile(_) } =
153-
module.definition_source(db)
154-
{
155-
entries.insert(file_id.original_file(db), None);
152+
if let Some(file_id) = module.as_source_file_id(db) {
153+
entries.insert(file_id, None);
156154
}
157155
to_visit.extend(module.children(db));
158156
}

crates/ide/src/static_index.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl StaticIndex<'_> {
187187
pub fn compute(analysis: &Analysis) -> StaticIndex<'_> {
188188
let db = &*analysis.db;
189189
let work = all_modules(db).into_iter().filter(|module| {
190-
let file_id = module.definition_source(db).file_id.original_file(db);
190+
let file_id = module.definition_source_file_id(db).original_file(db);
191191
let source_root = db.file_source_root(file_id);
192192
let source_root = db.source_root(source_root);
193193
!source_root.is_library
@@ -201,7 +201,7 @@ impl StaticIndex<'_> {
201201
};
202202
let mut visited_files = FxHashSet::default();
203203
for module in work {
204-
let file_id = module.definition_source(db).file_id.original_file(db);
204+
let file_id = module.definition_source_file_id(db).original_file(db);
205205
if visited_files.contains(&file_id) {
206206
continue;
207207
}

crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl flags::AnalysisStats {
105105
}
106106
for krate in krates {
107107
let module = krate.root_module(db);
108-
let file_id = module.definition_source(db).file_id;
108+
let file_id = module.definition_source_file_id(db);
109109
let file_id = file_id.original_file(db);
110110
let source_root = db.file_source_root(file_id);
111111
let source_root = db.source_root(source_root);

crates/rust-analyzer/src/cli/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ impl flags::Diagnostics {
3737
let mut visited_files = FxHashSet::default();
3838

3939
let work = all_modules(db).into_iter().filter(|module| {
40-
let file_id = module.definition_source(db).file_id.original_file(db);
40+
let file_id = module.definition_source_file_id(db).original_file(db);
4141
let source_root = db.file_source_root(file_id);
4242
let source_root = db.source_root(source_root);
4343
!source_root.is_library
4444
});
4545

4646
for module in work {
47-
let file_id = module.definition_source(db).file_id.original_file(db);
47+
let file_id = module.definition_source_file_id(db).original_file(db);
4848
if !visited_files.contains(&file_id) {
4949
let crate_name =
5050
module.krate().display_name(db).as_deref().unwrap_or("unknown").to_string();

0 commit comments

Comments
 (0)