aboutsummaryrefslogtreecommitdiff
path: root/crates/base_db
diff options
context:
space:
mode:
Diffstat (limited to 'crates/base_db')
-rw-r--r--crates/base_db/Cargo.toml2
-rw-r--r--crates/base_db/src/cancellation.rs48
-rw-r--r--crates/base_db/src/fixture.rs10
-rw-r--r--crates/base_db/src/input.rs16
-rw-r--r--crates/base_db/src/lib.rs45
5 files changed, 15 insertions, 106 deletions
diff --git a/crates/base_db/Cargo.toml b/crates/base_db/Cargo.toml
index 1724d2f85..69173ad1f 100644
--- a/crates/base_db/Cargo.toml
+++ b/crates/base_db/Cargo.toml
@@ -10,7 +10,7 @@ edition = "2018"
10doctest = false 10doctest = false
11 11
12[dependencies] 12[dependencies]
13salsa = "0.16.0" 13salsa = "0.17.0-pre.1"
14rustc-hash = "1.1.0" 14rustc-hash = "1.1.0"
15 15
16syntax = { path = "../syntax", version = "0.0.0" } 16syntax = { path = "../syntax", version = "0.0.0" }
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/fixture.rs b/crates/base_db/src/fixture.rs
index 0132565e4..69ceba735 100644
--- a/crates/base_db/src/fixture.rs
+++ b/crates/base_db/src/fixture.rs
@@ -34,19 +34,13 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static {
34 34
35 fn with_position(ra_fixture: &str) -> (Self, FilePosition) { 35 fn with_position(ra_fixture: &str) -> (Self, FilePosition) {
36 let (db, file_id, range_or_offset) = Self::with_range_or_offset(ra_fixture); 36 let (db, file_id, range_or_offset) = Self::with_range_or_offset(ra_fixture);
37 let offset = match range_or_offset { 37 let offset = range_or_offset.expect_offset();
38 RangeOrOffset::Range(_) => panic!("Expected a cursor position, got a range instead"),
39 RangeOrOffset::Offset(it) => it,
40 };
41 (db, FilePosition { file_id, offset }) 38 (db, FilePosition { file_id, offset })
42 } 39 }
43 40
44 fn with_range(ra_fixture: &str) -> (Self, FileRange) { 41 fn with_range(ra_fixture: &str) -> (Self, FileRange) {
45 let (db, file_id, range_or_offset) = Self::with_range_or_offset(ra_fixture); 42 let (db, file_id, range_or_offset) = Self::with_range_or_offset(ra_fixture);
46 let range = match range_or_offset { 43 let range = range_or_offset.expect_range();
47 RangeOrOffset::Range(it) => it,
48 RangeOrOffset::Offset(_) => panic!("Expected a cursor range, got a position instead"),
49 };
50 (db, FileRange { file_id, range }) 44 (db, FileRange { file_id, range })
51 } 45 }
52 46
diff --git a/crates/base_db/src/input.rs b/crates/base_db/src/input.rs
index 0ef77ef5d..23cb0c839 100644
--- a/crates/base_db/src/input.rs
+++ b/crates/base_db/src/input.rs
@@ -53,11 +53,15 @@ impl SourceRoot {
53} 53}
54 54
55/// `CrateGraph` is a bit of information which turns a set of text files into a 55/// `CrateGraph` is a bit of information which turns a set of text files into a
56/// number of Rust crates. Each crate is defined by the `FileId` of its root module, 56/// number of Rust crates.
57/// the set of cfg flags (not yet implemented) and the set of dependencies. Note 57///
58/// that, due to cfg's, there might be several crates for a single `FileId`! As 58/// Each crate is defined by the `FileId` of its root module, the set of enabled
59/// in the rust-lang proper, a crate does not have a name. Instead, names are 59/// `cfg` flags and the set of dependencies.
60/// specified on dependency edges. That is, a crate might be known under 60///
61/// Note that, due to cfg's, there might be several crates for a single `FileId`!
62///
63/// For the purposes of analysis, a crate does not have a name. Instead, names
64/// are specified on dependency edges. That is, a crate might be known under
61/// different names in different dependent crates. 65/// different names in different dependent crates.
62/// 66///
63/// Note that `CrateGraph` is build-system agnostic: it's a concept of the Rust 67/// Note that `CrateGraph` is build-system agnostic: it's a concept of the Rust
@@ -143,7 +147,7 @@ impl CrateDisplayName {
143#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] 147#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
144pub struct ProcMacroId(pub u32); 148pub struct ProcMacroId(pub u32);
145 149
146#[derive(Copy, Clone, Eq, PartialEq, Debug)] 150#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
147pub enum ProcMacroKind { 151pub enum ProcMacroKind {
148 CustomDerive, 152 CustomDerive,
149 FuncLike, 153 FuncLike,
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>;