aboutsummaryrefslogtreecommitdiff
path: root/crates/base_db
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-05-17 18:07:10 +0100
committerJonas Schievink <[email protected]>2021-05-27 14:05:41 +0100
commit33debc40654be9e9061c53784f6c762b2fd21eba (patch)
treedef63d3cc42fe48b1dfdf716c2e8e56de5324dab /crates/base_db
parentd0a4ba294ccf0c925a5ff1115c19a60c6a24b734 (diff)
Update salsa
Diffstat (limited to 'crates/base_db')
-rw-r--r--crates/base_db/src/cancellation.rs48
-rw-r--r--crates/base_db/src/lib.rs45
2 files changed, 2 insertions, 91 deletions
diff --git a/crates/base_db/src/cancellation.rs b/crates/base_db/src/cancellation.rs
deleted file mode 100644
index 7420a1976..000000000
--- a/crates/base_db/src/cancellation.rs
+++ /dev/null
@@ -1,48 +0,0 @@
1//! Utility types to support cancellation.
2//!
3//! In a typical IDE use-case, requests and modification happen concurrently, as
4//! in the following scenario:
5//!
6//! * user types a character,
7//! * a syntax highlighting process is started
8//! * user types next character, while syntax highlighting *is still in
9//! progress*.
10//!
11//! In this situation, we want to react to modification as quickly as possible.
12//! At the same time, in-progress results are not very interesting, because they
13//! are invalidated by the edit anyway. So, we first cancel all in-flight
14//! requests, and then apply modification knowing that it won't interfere with
15//! any background processing (this bit is handled by salsa, see the
16//! `BaseDatabase::check_canceled` method).
17
18/// An "error" signifying that the operation was canceled.
19#[derive(Clone, PartialEq, Eq, Hash)]
20pub struct Canceled {
21 _private: (),
22}
23
24impl Canceled {
25 pub(crate) fn new() -> Canceled {
26 Canceled { _private: () }
27 }
28
29 pub fn throw() -> ! {
30 // We use resume and not panic here to avoid running the panic
31 // hook (that is, to avoid collecting and printing backtrace).
32 std::panic::resume_unwind(Box::new(Canceled::new()))
33 }
34}
35
36impl std::fmt::Display for Canceled {
37 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38 fmt.write_str("canceled")
39 }
40}
41
42impl std::fmt::Debug for Canceled {
43 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 write!(fmt, "Canceled")
45 }
46}
47
48impl std::error::Error for Canceled {}
diff --git a/crates/base_db/src/lib.rs b/crates/base_db/src/lib.rs
index 980a0ed98..62bf2a4b2 100644
--- a/crates/base_db/src/lib.rs
+++ b/crates/base_db/src/lib.rs
@@ -1,5 +1,4 @@
1//! base_db defines basic database traits. The concrete DB is defined by ide. 1//! base_db defines basic database traits. The concrete DB is defined by ide.
2mod cancellation;
3mod input; 2mod input;
4mod change; 3mod change;
5pub mod fixture; 4pub mod fixture;
@@ -10,14 +9,13 @@ use rustc_hash::FxHashSet;
10use syntax::{ast, Parse, SourceFile, TextRange, TextSize}; 9use syntax::{ast, Parse, SourceFile, TextRange, TextSize};
11 10
12pub use crate::{ 11pub use crate::{
13 cancellation::Canceled,
14 change::Change, 12 change::Change,
15 input::{ 13 input::{
16 CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, Dependency, Edition, Env, 14 CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, Dependency, Edition, Env,
17 ProcMacro, ProcMacroExpander, ProcMacroId, ProcMacroKind, SourceRoot, SourceRootId, 15 ProcMacro, ProcMacroExpander, ProcMacroId, ProcMacroKind, SourceRoot, SourceRootId,
18 }, 16 },
19}; 17};
20pub use salsa; 18pub use salsa::{self, Cancelled};
21pub use vfs::{file_set::FileSet, AnchoredPath, AnchoredPathBuf, FileId, VfsPath}; 19pub use vfs::{file_set::FileSet, AnchoredPath, AnchoredPathBuf, FileId, VfsPath};
22 20
23#[macro_export] 21#[macro_export]
@@ -38,45 +36,6 @@ pub trait Upcast<T: ?Sized> {
38 fn upcast(&self) -> &T; 36 fn upcast(&self) -> &T;
39} 37}
40 38
41pub trait CheckCanceled {
42 /// Aborts current query if there are pending changes.
43 ///
44 /// rust-analyzer needs to be able to answer semantic questions about the
45 /// code while the code is being modified. A common problem is that a
46 /// long-running query is being calculated when a new change arrives.
47 ///
48 /// We can't just apply the change immediately: this will cause the pending
49 /// query to see inconsistent state (it will observe an absence of
50 /// repeatable read). So what we do is we **cancel** all pending queries
51 /// before applying the change.
52 ///
53 /// We implement cancellation by panicking with a special value and catching
54 /// it on the API boundary. Salsa explicitly supports this use-case.
55 fn check_canceled(&self);
56
57 fn catch_canceled<F, T>(&self, f: F) -> Result<T, Canceled>
58 where
59 Self: Sized + panic::RefUnwindSafe,
60 F: FnOnce(&Self) -> T + panic::UnwindSafe,
61 {
62 // Uncomment to debug missing cancellations.
63 // let _span = profile::heartbeat_span();
64 panic::catch_unwind(|| f(self)).map_err(|err| match err.downcast::<Canceled>() {
65 Ok(canceled) => *canceled,
66 Err(payload) => panic::resume_unwind(payload),
67 })
68 }
69}
70
71impl<T: salsa::Database> CheckCanceled for T {
72 fn check_canceled(&self) {
73 // profile::heartbeat();
74 if self.salsa_runtime().is_current_revision_canceled() {
75 Canceled::throw()
76 }
77 }
78}
79
80#[derive(Clone, Copy, Debug)] 39#[derive(Clone, Copy, Debug)]
81pub struct FilePosition { 40pub struct FilePosition {
82 pub file_id: FileId, 41 pub file_id: FileId,
@@ -101,7 +60,7 @@ pub trait FileLoader {
101/// Database which stores all significant input facts: source code and project 60/// Database which stores all significant input facts: source code and project
102/// model. Everything else in rust-analyzer is derived from these queries. 61/// model. Everything else in rust-analyzer is derived from these queries.
103#[salsa::query_group(SourceDatabaseStorage)] 62#[salsa::query_group(SourceDatabaseStorage)]
104pub trait SourceDatabase: CheckCanceled + FileLoader + std::fmt::Debug { 63pub trait SourceDatabase: FileLoader + std::fmt::Debug {
105 // Parses the file into the syntax tree. 64 // Parses the file into the syntax tree.
106 #[salsa::invoke(parse_query)] 65 #[salsa::invoke(parse_query)]
107 fn parse(&self, file_id: FileId) -> Parse<ast::SourceFile>; 66 fn parse(&self, file_id: FileId) -> Parse<ast::SourceFile>;