Skip to content

Commit 998ed13

Browse files
bors[bot]matklad
andcommitted
Merge #735
735: make HirDatabase object-safe r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 3959653 + dbf9820 commit 998ed13

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

crates/ra_db/src/lib.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub use crate::{
1919
loc2id::LocationIntener,
2020
};
2121

22-
pub trait CheckCanceled: salsa::Database + panic::RefUnwindSafe {
22+
pub trait CheckCanceled: panic::RefUnwindSafe {
2323
/// Aborts current query if there are pending changes.
2424
///
2525
/// rust-analyzer needs to be able to answer semantic questions about the
@@ -33,23 +33,28 @@ pub trait CheckCanceled: salsa::Database + panic::RefUnwindSafe {
3333
///
3434
/// We implement cancellation by panicking with a special value and catching
3535
/// it on the API boundary. Salsa explicitly supports this use-case.
36-
fn check_canceled(&self) {
37-
if self.salsa_runtime().is_current_revision_canceled() {
38-
Canceled::throw()
39-
}
40-
}
36+
fn check_canceled(&self);
4137

42-
fn catch_canceled<F: FnOnce(&Self) -> T + panic::UnwindSafe, T>(
43-
&self,
44-
f: F,
45-
) -> Result<T, Canceled> {
38+
fn catch_canceled<F, T>(&self, f: F) -> Result<T, Canceled>
39+
where
40+
Self: Sized,
41+
F: FnOnce(&Self) -> T + panic::UnwindSafe,
42+
{
4643
panic::catch_unwind(|| f(self)).map_err(|err| match err.downcast::<Canceled>() {
4744
Ok(canceled) => *canceled,
4845
Err(payload) => panic::resume_unwind(payload),
4946
})
5047
}
5148
}
5249

50+
impl<T: salsa::Database + panic::RefUnwindSafe> CheckCanceled for T {
51+
fn check_canceled(&self) {
52+
if self.salsa_runtime().is_current_revision_canceled() {
53+
Canceled::throw()
54+
}
55+
}
56+
}
57+
5358
#[derive(Clone, Copy, Debug)]
5459
pub struct FilePosition {
5560
pub file_id: FileId,
@@ -65,7 +70,7 @@ pub struct FileRange {
6570
/// Database which stores all significant input facts: source code and project
6671
/// model. Everything else in rust-analyzer is derived from these queries.
6772
#[salsa::query_group(SourceDatabaseStorage)]
68-
pub trait SourceDatabase: salsa::Database + CheckCanceled {
73+
pub trait SourceDatabase: CheckCanceled {
6974
/// Text of the file.
7075
#[salsa::input]
7176
fn file_text(&self, file_id: FileId) -> Arc<String>;

crates/ra_hir/src/db.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,8 @@ pub trait HirDatabase: PersistentHirDatabase {
103103
#[salsa::invoke(crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query)]
104104
fn impls_in_crate(&self, krate: Crate) -> Arc<CrateImplBlocks>;
105105
}
106+
107+
#[test]
108+
fn hir_database_is_object_safe() {
109+
fn _assert_object_safe(_: &dyn HirDatabase) {}
110+
}

crates/ra_hir/src/mock.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{sync::Arc, panic};
22

33
use parking_lot::Mutex;
44
use ra_db::{
5-
CheckCanceled, FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId, SourceDatabase, salsa,
5+
FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId, SourceDatabase, salsa,
66
};
77
use relative_path::RelativePathBuf;
88
use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset};
@@ -159,8 +159,6 @@ impl salsa::ParallelDatabase for MockDatabase {
159159
}
160160
}
161161

162-
impl CheckCanceled for MockDatabase {}
163-
164162
impl AsRef<HirInterner> for MockDatabase {
165163
fn as_ref(&self) -> &HirInterner {
166164
&self.interner

crates/ra_ide_api/src/db.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ impl salsa::ParallelDatabase for RootDatabase {
6060
}
6161
}
6262

63-
impl CheckCanceled for RootDatabase {}
64-
6563
impl AsRef<hir::HirInterner> for RootDatabase {
6664
fn as_ref(&self) -> &hir::HirInterner {
6765
&self.interner

crates/ra_ide_api/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
//!
1010
//! The sibling `ra_ide_api_light` handles thouse bits of IDE functionality
1111
//! which are restricted to a single file and need only syntax.
12+
13+
// For proving that RootDatabase is RefUnwindSafe.
14+
#![recursion_limit = "128"]
15+
1216
mod db;
1317
mod imp;
1418
pub mod mock_analysis;

0 commit comments

Comments
 (0)