Skip to content

Commit 9fa647c

Browse files
committed
Update salsa
1 parent a871773 commit 9fa647c

File tree

8 files changed

+75
-33
lines changed

8 files changed

+75
-33
lines changed

Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ process-wrap = { version = "8.2.0", features = ["std"] }
131131
pulldown-cmark-to-cmark = "10.0.4"
132132
pulldown-cmark = { version = "0.9.6", default-features = false }
133133
rayon = "1.10.0"
134-
salsa = "0.20.0"
134+
salsa = "0.21.0"
135135
semver = "1.0.26"
136136
serde = { version = "1.0.219" }
137137
serde_derive = { version = "1.0.219" }

crates/base-db/src/lib.rs

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
mod change;
44
mod input;
55

6-
use std::hash::BuildHasherDefault;
6+
use std::{cell::RefCell, hash::BuildHasherDefault, panic, sync::Once};
77

88
pub use crate::{
99
change::FileChange,
@@ -60,7 +60,7 @@ impl Files {
6060
match self.files.get(&file_id) {
6161
Some(text) => *text,
6262
None => {
63-
panic!("Unable to fetch file text for `vfs::FileId`: {:?}; this is a bug", file_id)
63+
panic!("Unable to fetch file text for `vfs::FileId`: {file_id:?}; this is a bug")
6464
}
6565
}
6666
}
@@ -101,8 +101,7 @@ impl Files {
101101
let source_root = match self.source_roots.get(&source_root_id) {
102102
Some(source_root) => source_root,
103103
None => panic!(
104-
"Unable to fetch `SourceRootInput` with `SourceRootId` ({:?}); this is a bug",
105-
source_root_id
104+
"Unable to fetch `SourceRootInput` with `SourceRootId` ({source_root_id:?}); this is a bug"
106105
),
107106
};
108107

@@ -132,8 +131,7 @@ impl Files {
132131
let file_source_root = match self.file_source_roots.get(&id) {
133132
Some(file_source_root) => file_source_root,
134133
None => panic!(
135-
"Unable to get `FileSourceRootInput` with `vfs::FileId` ({:?}); this is a bug",
136-
id
134+
"Unable to get `FileSourceRootInput` with `vfs::FileId` ({id:?}); this is a bug",
137135
),
138136
};
139137
*file_source_root
@@ -384,3 +382,51 @@ fn relevant_crates(db: &dyn RootQueryDb, file_id: FileId) -> Arc<[Crate]> {
384382
let source_root = db.file_source_root(file_id);
385383
db.source_root_crates(source_root.source_root_id(db))
386384
}
385+
386+
#[must_use]
387+
pub struct DbPanicContext {
388+
// prevent arbitrary construction
389+
_priv: (),
390+
}
391+
392+
impl Drop for DbPanicContext {
393+
fn drop(&mut self) {
394+
Self::with_ctx(|ctx| assert!(ctx.pop().is_some()));
395+
}
396+
}
397+
398+
impl DbPanicContext {
399+
pub fn enter(frame: String) -> DbPanicContext {
400+
#[expect(clippy::print_stderr, reason = "already panicking anyway")]
401+
fn set_hook() {
402+
let default_hook = panic::take_hook();
403+
panic::set_hook(Box::new(move |panic_info| {
404+
DbPanicContext::with_ctx(|ctx| {
405+
if !ctx.is_empty() {
406+
eprintln!("Panic context:");
407+
for frame in ctx.iter() {
408+
eprintln!("> {frame}\n");
409+
}
410+
}
411+
});
412+
if let Some(backtrace) = salsa::Backtrace::capture() {
413+
eprintln!("{backtrace}");
414+
}
415+
default_hook(panic_info);
416+
}));
417+
}
418+
419+
static SET_HOOK: Once = Once::new();
420+
SET_HOOK.call_once(set_hook);
421+
422+
Self::with_ctx(|ctx| ctx.push(frame));
423+
DbPanicContext { _priv: () }
424+
}
425+
426+
fn with_ctx(f: impl FnOnce(&mut Vec<String>)) {
427+
thread_local! {
428+
static CTX: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
429+
}
430+
CTX.with(|ctx| f(&mut ctx.borrow_mut()));
431+
}
432+
}

crates/rust-analyzer/src/cli/rustc_tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{cell::RefCell, fs::read_to_string, panic::AssertUnwindSafe, path::Path
77

88
use hir::{ChangeWithProcMacros, Crate};
99
use ide::{AnalysisHost, DiagnosticCode, DiagnosticsConfig};
10+
use ide_db::base_db;
1011
use itertools::Either;
1112
use paths::Utf8PathBuf;
1213
use profile::StopWatch;
@@ -310,7 +311,7 @@ impl flags::RustcTests {
310311
let tester = AssertUnwindSafe(&mut tester);
311312
let p = p.clone();
312313
move || {
313-
let _guard = stdx::panic_context::enter(p.display().to_string());
314+
let _guard = base_db::DbPanicContext::enter(p.display().to_string());
314315
{ tester }.0.test(p);
315316
}
316317
}) {

crates/rust-analyzer/src/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::mem;
55

66
use cargo_metadata::PackageId;
77
use ide::FileId;
8-
use ide_db::FxHashMap;
8+
use ide_db::{FxHashMap, base_db::DbPanicContext};
99
use itertools::Itertools;
1010
use rustc_hash::FxHashSet;
1111
use stdx::iter_eq_by;
@@ -215,7 +215,7 @@ pub(crate) fn fetch_native_diagnostics(
215215
kind: NativeDiagnosticsFetchKind,
216216
) -> Vec<(FileId, Vec<lsp_types::Diagnostic>)> {
217217
let _p = tracing::info_span!("fetch_native_diagnostics").entered();
218-
let _ctx = stdx::panic_context::enter("fetch_native_diagnostics".to_owned());
218+
let _ctx = DbPanicContext::enter("fetch_native_diagnostics".to_owned());
219219

220220
// the diagnostics produced may point to different files not requested by the concrete request,
221221
// put those into here and filter later

crates/rust-analyzer/src/handlers/dispatch.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use std::{
44
panic, thread,
55
};
66

7-
use ide_db::base_db::salsa::{self, Cancelled};
7+
use ide_db::base_db::{
8+
DbPanicContext,
9+
salsa::{self, Cancelled},
10+
};
811
use lsp_server::{ExtractError, Response, ResponseError};
912
use serde::{Serialize, de::DeserializeOwned};
1013
use stdx::thread::ThreadIntent;
@@ -56,7 +59,7 @@ impl RequestDispatcher<'_> {
5659
tracing::info_span!("request", method = ?req.method, "request_id" = ?req.id).entered();
5760
tracing::debug!(?params);
5861
let result = {
59-
let _pctx = stdx::panic_context::enter(panic_context);
62+
let _pctx = DbPanicContext::enter(panic_context);
6063
f(self.global_state, params)
6164
};
6265
if let Ok(response) = result_to_response::<R>(req.id, result) {
@@ -86,7 +89,7 @@ impl RequestDispatcher<'_> {
8689
let global_state_snapshot = self.global_state.snapshot();
8790

8891
let result = panic::catch_unwind(move || {
89-
let _pctx = stdx::panic_context::enter(panic_context);
92+
let _pctx = DbPanicContext::enter(panic_context);
9093
f(global_state_snapshot, params)
9194
});
9295

@@ -257,7 +260,7 @@ impl RequestDispatcher<'_> {
257260
}
258261
.spawn(intent, move || {
259262
let result = panic::catch_unwind(move || {
260-
let _pctx = stdx::panic_context::enter(panic_context);
263+
let _pctx = DbPanicContext::enter(panic_context);
261264
f(world, params)
262265
});
263266
match thread_result_to_response::<R>(req.id.clone(), result) {
@@ -421,11 +424,8 @@ impl NotificationDispatcher<'_> {
421424

422425
tracing::debug!(?params);
423426

424-
let _pctx = stdx::panic_context::enter(format!(
425-
"\nversion: {}\nnotification: {}",
426-
version(),
427-
N::METHOD
428-
));
427+
let _pctx =
428+
DbPanicContext::enter(format!("\nversion: {}\nnotification: {}", version(), N::METHOD));
429429
if let Err(e) = f(self.global_state, params) {
430430
tracing::error!(handler = %N::METHOD, error = %e, "notification handler failed");
431431
}

crates/span/src/hygiene.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,11 @@ const _: () = {
9494
}
9595
}
9696
impl zalsa_struct_::Configuration for SyntaxContext {
97+
const LOCATION: salsa::plumbing::Location =
98+
salsa::plumbing::Location { file: file!(), line: line!() };
9799
const DEBUG_NAME: &'static str = "SyntaxContextData";
98100
type Fields<'a> = SyntaxContextData;
99101
type Struct<'a> = SyntaxContext;
100-
fn struct_from_id<'db>(id: salsa::Id) -> Self::Struct<'db> {
101-
SyntaxContext::from_salsa_id(id)
102-
}
103-
fn deref_struct(s: Self::Struct<'_>) -> salsa::Id {
104-
s.as_salsa_id()
105-
.expect("`SyntaxContext::deref_structs()` called on a root `SyntaxContext`")
106-
}
107102
}
108103
impl SyntaxContext {
109104
pub fn ingredient<Db>(db: &Db) -> &zalsa_struct_::IngredientImpl<Self>

xtask/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub(crate) fn detect_target(sh: &Shell) -> String {
3737
Ok(target) => target,
3838
_ => match cmd!(sh, "rustc --print=host-tuple").read() {
3939
Ok(target) => target,
40-
Err(e) => panic!("Failed to detect target: {}\nPlease set RA_TARGET explicitly", e),
40+
Err(e) => panic!("Failed to detect target: {e}\nPlease set RA_TARGET explicitly"),
4141
},
4242
}
4343
}

0 commit comments

Comments
 (0)