Skip to content

Commit fd336d1

Browse files
bors[bot]matklad
andcommitted
Merge #147
147: Cancelation r=matklad a=matklad This series of commits switch cancellation strategy from `JobToken` (which are cancellation tokens, explicitly controlled by the called) to salsa built-in auto cancellation. "Auto" means that, as soon as we advance the revision, all pending queries are cancelled automatically, and this looks like a semantic we actually want. "client-side" cancellation is a rare event, and it's ok to just punt on it. Automatic cancellation after the user types something in happens all the time. Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 73dd870 + 0102a01 commit fd336d1

File tree

16 files changed

+212
-262
lines changed

16 files changed

+212
-262
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_analysis/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ authors = ["Aleksey Kladov <[email protected]>"]
77
[dependencies]
88
relative-path = "0.3.7"
99
log = "0.4.2"
10-
crossbeam-channel = "0.2.4"
1110
parking_lot = "0.6.3"
1211
once_cell = "0.1.5"
1312
rayon = "1.0.2"

crates/ra_analysis/src/db.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
use crate::{
2-
module_map::{ModuleDescriptorQuery, ModuleTreeQuery, ModulesDatabase},
3-
symbol_index::SymbolIndex,
4-
FileId, FileResolverImp,
1+
use std::{
2+
fmt,
3+
hash::{Hash, Hasher},
4+
sync::Arc,
55
};
6+
67
use ra_editor::LineIndex;
78
use ra_syntax::File;
89
use rustc_hash::FxHashSet;
910
use salsa;
1011

11-
use std::{
12-
fmt,
13-
hash::{Hash, Hasher},
14-
sync::Arc,
12+
use crate::{
13+
db,
14+
Cancelable, Canceled,
15+
module_map::{ModuleDescriptorQuery, ModuleTreeQuery, ModulesDatabase},
16+
symbol_index::SymbolIndex,
17+
FileId, FileResolverImp,
1518
};
1619

1720
#[derive(Default)]
@@ -31,6 +34,14 @@ impl salsa::Database for RootDatabase {
3134
}
3235
}
3336

37+
pub(crate) fn check_canceled(db: &impl salsa::Database) -> Cancelable<()> {
38+
if db.salsa_runtime().is_current_revision_canceled() {
39+
Err(Canceled)
40+
} else {
41+
Ok(())
42+
}
43+
}
44+
3445
impl salsa::ParallelDatabase for RootDatabase {
3546
fn fork(&self) -> Self {
3647
RootDatabase {
@@ -98,7 +109,7 @@ salsa::query_group! {
98109
fn file_lines(file_id: FileId) -> Arc<LineIndex> {
99110
type FileLinesQuery;
100111
}
101-
fn file_symbols(file_id: FileId) -> Arc<SymbolIndex> {
112+
fn file_symbols(file_id: FileId) -> Cancelable<Arc<SymbolIndex>> {
102113
type FileSymbolsQuery;
103114
}
104115
}
@@ -112,7 +123,8 @@ fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<LineIndex> {
112123
let text = db.file_text(file_id);
113124
Arc::new(LineIndex::new(&*text))
114125
}
115-
fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<SymbolIndex> {
126+
fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Cancelable<Arc<SymbolIndex>> {
127+
db::check_canceled(db)?;
116128
let syntax = db.file_syntax(file_id);
117-
Arc::new(SymbolIndex::for_file(file_id, syntax))
129+
Ok(Arc::new(SymbolIndex::for_file(file_id, syntax)))
118130
}

crates/ra_analysis/src/descriptors.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
use crate::{imp::FileResolverImp, FileId};
1+
use std::collections::BTreeMap;
2+
23
use ra_syntax::{
34
ast::{self, AstNode, NameOwner},
45
text_utils::is_subrange,
56
SmolStr,
67
};
78
use relative_path::RelativePathBuf;
89

9-
use std::collections::BTreeMap;
10+
use crate::{imp::FileResolverImp, FileId};
1011

1112
#[derive(Debug, PartialEq, Eq, Hash)]
1213
pub struct ModuleDescriptor {

crates/ra_analysis/src/imp.rs

+42-36
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use rustc_hash::FxHashSet;
1919
use crate::{
2020
descriptors::{FnDescriptor, ModuleTreeDescriptor, Problem},
2121
roots::{ReadonlySourceRoot, SourceRoot, WritableSourceRoot},
22-
CrateGraph, CrateId, Diagnostic, FileId, FileResolver, FileSystemEdit, JobToken, Position,
23-
Query, SourceChange, SourceFileEdit,
22+
CrateGraph, CrateId, Diagnostic, FileId, FileResolver, FileSystemEdit, Position,
23+
Query, SourceChange, SourceFileEdit, Cancelable,
2424
};
2525

2626
#[derive(Clone, Debug)]
@@ -148,19 +148,21 @@ impl AnalysisImpl {
148148
pub fn file_line_index(&self, file_id: FileId) -> Arc<LineIndex> {
149149
self.root(file_id).lines(file_id)
150150
}
151-
pub fn world_symbols(&self, query: Query, token: &JobToken) -> Vec<(FileId, FileSymbol)> {
151+
pub fn world_symbols(&self, query: Query) -> Cancelable<Vec<(FileId, FileSymbol)>> {
152152
let mut buf = Vec::new();
153153
if query.libs {
154-
self.data.libs.iter().for_each(|it| it.symbols(&mut buf));
154+
for lib in self.data.libs.iter() {
155+
lib.symbols(&mut buf)?;
156+
}
155157
} else {
156-
self.data.root.symbols(&mut buf);
158+
self.data.root.symbols(&mut buf)?;
157159
}
158-
query.search(&buf, token)
160+
Ok(query.search(&buf))
159161
}
160-
pub fn parent_module(&self, file_id: FileId) -> Vec<(FileId, FileSymbol)> {
162+
pub fn parent_module(&self, file_id: FileId) -> Cancelable<Vec<(FileId, FileSymbol)>> {
161163
let root = self.root(file_id);
162-
let module_tree = root.module_tree();
163-
module_tree
164+
let module_tree = root.module_tree()?;
165+
let res = module_tree
164166
.parent_modules(file_id)
165167
.iter()
166168
.map(|link| {
@@ -174,10 +176,11 @@ impl AnalysisImpl {
174176
};
175177
(file_id, sym)
176178
})
177-
.collect()
179+
.collect();
180+
Ok(res)
178181
}
179-
pub fn crate_for(&self, file_id: FileId) -> Vec<CrateId> {
180-
let module_tree = self.root(file_id).module_tree();
182+
pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
183+
let module_tree = self.root(file_id).module_tree()?;
181184
let crate_graph = &self.data.crate_graph;
182185
let mut res = Vec::new();
183186
let mut work = VecDeque::new();
@@ -195,7 +198,7 @@ impl AnalysisImpl {
195198
.filter(|&id| visited.insert(id));
196199
work.extend(parents);
197200
}
198-
res
201+
Ok(res)
199202
}
200203
pub fn crate_root(&self, crate_id: CrateId) -> FileId {
201204
self.data.crate_graph.crate_roots[&crate_id]
@@ -204,15 +207,14 @@ impl AnalysisImpl {
204207
&self,
205208
file_id: FileId,
206209
offset: TextUnit,
207-
token: &JobToken,
208-
) -> Vec<(FileId, FileSymbol)> {
210+
) -> Cancelable<Vec<(FileId, FileSymbol)>> {
209211
let root = self.root(file_id);
210-
let module_tree = root.module_tree();
212+
let module_tree = root.module_tree()?;
211213
let file = root.syntax(file_id);
212214
let syntax = file.syntax();
213215
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) {
214216
// First try to resolve the symbol locally
215-
if let Some((name, range)) = resolve_local_name(&file, offset, name_ref) {
217+
return if let Some((name, range)) = resolve_local_name(&file, offset, name_ref) {
216218
let mut vec = vec![];
217219
vec.push((
218220
file_id,
@@ -222,12 +224,11 @@ impl AnalysisImpl {
222224
kind: NAME,
223225
},
224226
));
225-
226-
return vec;
227+
Ok(vec)
227228
} else {
228229
// If that fails try the index based approach.
229-
return self.index_resolve(name_ref, token);
230-
}
230+
self.index_resolve(name_ref)
231+
};
231232
}
232233
if let Some(name) = find_node_at_offset::<ast::Name>(syntax, offset) {
233234
if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) {
@@ -250,14 +251,14 @@ impl AnalysisImpl {
250251
})
251252
.collect();
252253

253-
return res;
254+
return Ok(res);
254255
}
255256
}
256257
}
257-
vec![]
258+
Ok(vec![])
258259
}
259260

260-
pub fn find_all_refs(&self, file_id: FileId, offset: TextUnit, _token: &JobToken) -> Vec<(FileId, TextRange)> {
261+
pub fn find_all_refs(&self, file_id: FileId, offset: TextUnit) -> Vec<(FileId, TextRange)> {
261262
let root = self.root(file_id);
262263
let file = root.syntax(file_id);
263264
let syntax = file.syntax();
@@ -289,9 +290,9 @@ impl AnalysisImpl {
289290
ret
290291
}
291292

292-
pub fn diagnostics(&self, file_id: FileId) -> Vec<Diagnostic> {
293+
pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
293294
let root = self.root(file_id);
294-
let module_tree = root.module_tree();
295+
let module_tree = root.module_tree()?;
295296
let syntax = root.syntax(file_id);
296297

297298
let mut res = ra_editor::diagnostics(&syntax)
@@ -346,7 +347,7 @@ impl AnalysisImpl {
346347
};
347348
res.push(diag)
348349
}
349-
res
350+
Ok(res)
350351
}
351352

352353
pub fn assists(&self, file_id: FileId, range: TextRange) -> Vec<SourceChange> {
@@ -379,18 +380,23 @@ impl AnalysisImpl {
379380
&self,
380381
file_id: FileId,
381382
offset: TextUnit,
382-
token: &JobToken,
383-
) -> Option<(FnDescriptor, Option<usize>)> {
383+
) -> Cancelable<Option<(FnDescriptor, Option<usize>)>> {
384384
let root = self.root(file_id);
385385
let file = root.syntax(file_id);
386386
let syntax = file.syntax();
387387

388388
// Find the calling expression and it's NameRef
389-
let calling_node = FnCallNode::with_node(syntax, offset)?;
390-
let name_ref = calling_node.name_ref()?;
389+
let calling_node = match FnCallNode::with_node(syntax, offset) {
390+
Some(node) => node,
391+
None => return Ok(None),
392+
};
393+
let name_ref = match calling_node.name_ref() {
394+
Some(name) => name,
395+
None => return Ok(None),
396+
};
391397

392398
// Resolve the function's NameRef (NOTE: this isn't entirely accurate).
393-
let file_symbols = self.index_resolve(name_ref, token);
399+
let file_symbols = self.index_resolve(name_ref)?;
394400
for (_, fs) in file_symbols {
395401
if fs.kind == FN_DEF {
396402
if let Some(fn_def) = find_node_at_offset(syntax, fs.node_range.start()) {
@@ -432,21 +438,21 @@ impl AnalysisImpl {
432438
}
433439
}
434440

435-
return Some((descriptor, current_parameter));
441+
return Ok(Some((descriptor, current_parameter)));
436442
}
437443
}
438444
}
439445
}
440446

441-
None
447+
Ok(None)
442448
}
443449

444-
fn index_resolve(&self, name_ref: ast::NameRef, token: &JobToken) -> Vec<(FileId, FileSymbol)> {
450+
fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable<Vec<(FileId, FileSymbol)>> {
445451
let name = name_ref.text();
446452
let mut query = Query::new(name.to_string());
447453
query.exact();
448454
query.limit(4);
449-
self.world_symbols(query, token)
455+
self.world_symbols(query)
450456
}
451457

452458
fn resolve_module(

crates/ra_analysis/src/job.rs

-53
This file was deleted.

0 commit comments

Comments
 (0)