diff options
Diffstat (limited to 'crates/base_db/src/lib.rs')
-rw-r--r-- | crates/base_db/src/lib.rs | 45 |
1 files changed, 2 insertions, 43 deletions
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. |
2 | mod cancellation; | ||
3 | mod input; | 2 | mod input; |
4 | mod change; | 3 | mod change; |
5 | pub mod fixture; | 4 | pub mod fixture; |
@@ -10,14 +9,13 @@ use rustc_hash::FxHashSet; | |||
10 | use syntax::{ast, Parse, SourceFile, TextRange, TextSize}; | 9 | use syntax::{ast, Parse, SourceFile, TextRange, TextSize}; |
11 | 10 | ||
12 | pub use crate::{ | 11 | pub 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 | }; |
20 | pub use salsa; | 18 | pub use salsa::{self, Cancelled}; |
21 | pub use vfs::{file_set::FileSet, AnchoredPath, AnchoredPathBuf, FileId, VfsPath}; | 19 | pub 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 | ||
41 | pub 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 | |||
71 | impl<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)] |
81 | pub struct FilePosition { | 40 | pub 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)] |
104 | pub trait SourceDatabase: CheckCanceled + FileLoader + std::fmt::Debug { | 63 | pub 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>; |