Skip to content

Commit 4d4c05d

Browse files
committed
Auto merge of rust-lang#13428 - Veykril:fmt-stuck, r=Veykril
fix: Fix formatting requests hanging when r-a is still starting The reason for that was that we were calculating the crate defmaps of the file we are saving by accident causing us to get stuck waiting on their expensive computation, while we only need the relevant crate id. Closes rust-lang/rust-analyzer#4054 Closes rust-lang/rust-analyzer#11654
2 parents 106285b + a762bac commit 4d4c05d

File tree

8 files changed

+17
-24
lines changed

8 files changed

+17
-24
lines changed

crates/ide/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,8 @@ impl Analysis {
482482
}
483483

484484
/// Returns crates this file belongs too.
485-
pub fn crate_for(&self, file_id: FileId) -> Cancellable<Vec<CrateId>> {
486-
self.with_db(|db| parent_module::crate_for(db, file_id))
485+
pub fn crates_for(&self, file_id: FileId) -> Cancellable<Vec<CrateId>> {
486+
self.with_db(|db| parent_module::crates_for(db, file_id))
487487
}
488488

489489
/// Returns the edition of the given crate.

crates/ide/src/parent_module.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use hir::Semantics;
22
use ide_db::{
3-
base_db::{CrateId, FileId, FilePosition},
3+
base_db::{CrateId, FileId, FileLoader, FilePosition},
44
RootDatabase,
55
};
6-
use itertools::Itertools;
76
use syntax::{
87
algo::find_node_at_offset,
98
ast::{self, AstNode},
@@ -55,9 +54,8 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<Na
5554
}
5655

5756
/// Returns `Vec` for the same reason as `parent_module`
58-
pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
59-
let sema = Semantics::new(db);
60-
sema.to_module_defs(file_id).map(|module| module.krate().into()).unique().collect()
57+
pub(crate) fn crates_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
58+
db.relevant_crates(file_id).iter().copied().collect()
6159
}
6260

6361
#[cfg(test)]
@@ -147,7 +145,7 @@ $0
147145
mod foo;
148146
"#,
149147
);
150-
assert_eq!(analysis.crate_for(file_id).unwrap().len(), 1);
148+
assert_eq!(analysis.crates_for(file_id).unwrap().len(), 1);
151149
}
152150

153151
#[test]
@@ -162,6 +160,6 @@ mod baz;
162160
mod baz;
163161
"#,
164162
);
165-
assert_eq!(analysis.crate_for(file_id).unwrap().len(), 2);
163+
assert_eq!(analysis.crates_for(file_id).unwrap().len(), 2);
166164
}
167165
}

crates/ide/src/status.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub(crate) fn status(db: &RootDatabase, file_id: Option<FileId>) -> String {
4545

4646
if let Some(file_id) = file_id {
4747
format_to!(buf, "\nFile info:\n");
48-
let crates = crate::parent_module::crate_for(db, file_id);
48+
let crates = crate::parent_module::crates_for(db, file_id);
4949
if crates.is_empty() {
5050
format_to!(buf, "Does not belong to any crate");
5151
}

crates/rust-analyzer/src/cargo_target_spec.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,7 @@ impl CargoTargetSpec {
118118
global_state_snapshot: &GlobalStateSnapshot,
119119
file_id: FileId,
120120
) -> Result<Option<CargoTargetSpec>> {
121-
let crate_id = match &*global_state_snapshot.analysis.crate_for(file_id)? {
122-
&[crate_id, ..] => crate_id,
123-
_ => return Ok(None),
124-
};
125-
let (cargo_ws, target) = match global_state_snapshot.cargo_target_for_crate_root(crate_id) {
121+
let (cargo_ws, target) = match global_state_snapshot.cargo_target_for_file_id(file_id) {
126122
Some(it) => it,
127123
None => return Ok(None),
128124
};

crates/rust-analyzer/src/dispatch.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<'a> RequestDispatcher<'a> {
5252
let _pctx = stdx::panic_context::enter(panic_context);
5353
f(self.global_state, params)
5454
};
55-
if let Ok(response) = result_to_response::<R>(req.id.clone(), result) {
55+
if let Ok(response) = result_to_response::<R>(req.id, result) {
5656
self.global_state.respond(response);
5757
}
5858

@@ -80,7 +80,7 @@ impl<'a> RequestDispatcher<'a> {
8080
f(global_state_snapshot, params)
8181
});
8282

83-
if let Ok(response) = thread_result_to_response::<R>(req.id.clone(), result) {
83+
if let Ok(response) = thread_result_to_response::<R>(req.id, result) {
8484
self.global_state.respond(response);
8585
}
8686

crates/rust-analyzer/src/global_state.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{sync::Arc, time::Instant};
88
use crossbeam_channel::{unbounded, Receiver, Sender};
99
use flycheck::FlycheckHandle;
1010
use ide::{Analysis, AnalysisHost, Cancellable, Change, FileId};
11-
use ide_db::base_db::{CrateId, FileLoader, SourceDatabase};
11+
use ide_db::base_db::{FileLoader, SourceDatabase};
1212
use lsp_types::{SemanticTokens, Url};
1313
use parking_lot::{Mutex, RwLock};
1414
use proc_macro_api::ProcMacroServer;
@@ -398,11 +398,10 @@ impl GlobalStateSnapshot {
398398
url_from_abs_path(path)
399399
}
400400

401-
pub(crate) fn cargo_target_for_crate_root(
401+
pub(crate) fn cargo_target_for_file_id(
402402
&self,
403-
crate_id: CrateId,
403+
file_id: FileId,
404404
) -> Option<(&CargoWorkspace, Target)> {
405-
let file_id = self.analysis.crate_root(crate_id).ok()?;
406405
let path = self.vfs.read().0.file_path(file_id);
407406
let path = path.as_path()?;
408407
self.workspaces.iter().find_map(|ws| match ws {

crates/rust-analyzer/src/handlers.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ pub(crate) fn handle_parent_module(
658658

659659
// check if invoked at the crate root
660660
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
661-
let crate_id = match snap.analysis.crate_for(file_id)?.first() {
661+
let crate_id = match snap.analysis.crates_for(file_id)?.first() {
662662
Some(&crate_id) => crate_id,
663663
None => return Ok(None),
664664
};
@@ -1782,7 +1782,7 @@ fn run_rustfmt(
17821782
) -> Result<Option<Vec<lsp_types::TextEdit>>> {
17831783
let file_id = from_proto::file_id(snap, &text_document.uri)?;
17841784
let file = snap.analysis.file_text(file_id)?;
1785-
let crate_ids = snap.analysis.crate_for(file_id)?;
1785+
let crate_ids = snap.analysis.crates_for(file_id)?;
17861786

17871787
let line_index = snap.file_line_index(file_id)?;
17881788

crates/rust-analyzer/src/main_loop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ impl GlobalState {
783783
let analysis = this.analysis_host.analysis();
784784
// Crates containing or depending on the saved file
785785
let crate_ids: Vec<_> = analysis
786-
.crate_for(file_id)?
786+
.crates_for(file_id)?
787787
.into_iter()
788788
.flat_map(|id| {
789789
this.analysis_host

0 commit comments

Comments
 (0)