Skip to content

Commit 352116c

Browse files
authored
Merge pull request #18762 from davidbarsky/davidbarsky/wrap-salsa-cancellation-error
internal: wrap `salsa::Cycle`
2 parents bae8fb5 + 637700e commit 352116c

File tree

2 files changed

+28
-5
lines changed
  • src/tools/rust-analyzer/crates

2 files changed

+28
-5
lines changed

src/tools/rust-analyzer/crates/ra-salsa/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,11 @@ where
610610
#[non_exhaustive]
611611
pub enum Cancelled {
612612
/// The query was operating on revision R, but there is a pending write to move to revision R+1.
613+
#[non_exhaustive]
613614
PendingWrite,
614615

615616
/// The query was blocked on another thread, and that thread panicked.
617+
#[non_exhaustive]
616618
PropagatedPanic,
617619
}
618620

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,31 @@ impl RequestDispatcher<'_> {
308308
}
309309
}
310310

311+
#[derive(Debug)]
312+
enum HandlerCancelledError {
313+
PropagatedPanic,
314+
Inner(ide::Cancelled),
315+
}
316+
317+
impl std::error::Error for HandlerCancelledError {
318+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
319+
match self {
320+
HandlerCancelledError::PropagatedPanic => None,
321+
HandlerCancelledError::Inner(cancelled) => Some(cancelled),
322+
}
323+
}
324+
}
325+
326+
impl fmt::Display for HandlerCancelledError {
327+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
328+
write!(f, "Cancelled")
329+
}
330+
}
331+
311332
fn thread_result_to_response<R>(
312333
id: lsp_server::RequestId,
313334
result: thread::Result<anyhow::Result<R::Result>>,
314-
) -> Result<lsp_server::Response, Cancelled>
335+
) -> Result<lsp_server::Response, HandlerCancelledError>
315336
where
316337
R: lsp_types::request::Request,
317338
R::Params: DeserializeOwned,
@@ -331,10 +352,10 @@ where
331352
message.push_str(panic_message)
332353
} else if let Some(cycle) = panic.downcast_ref::<Cycle>() {
333354
tracing::error!("Cycle propagated out of salsa! This is a bug: {cycle:?}");
334-
return Err(Cancelled::PropagatedPanic);
355+
return Err(HandlerCancelledError::PropagatedPanic);
335356
} else if let Ok(cancelled) = panic.downcast::<Cancelled>() {
336357
tracing::error!("Cancellation propagated out of salsa! This is a bug");
337-
return Err(*cancelled);
358+
return Err(HandlerCancelledError::Inner(*cancelled));
338359
}
339360

340361
Ok(lsp_server::Response::new_err(
@@ -349,7 +370,7 @@ where
349370
fn result_to_response<R>(
350371
id: lsp_server::RequestId,
351372
result: anyhow::Result<R::Result>,
352-
) -> Result<lsp_server::Response, Cancelled>
373+
) -> Result<lsp_server::Response, HandlerCancelledError>
353374
where
354375
R: lsp_types::request::Request,
355376
R::Params: DeserializeOwned,
@@ -360,7 +381,7 @@ where
360381
Err(e) => match e.downcast::<LspError>() {
361382
Ok(lsp_error) => lsp_server::Response::new_err(id, lsp_error.code, lsp_error.message),
362383
Err(e) => match e.downcast::<Cancelled>() {
363-
Ok(cancelled) => return Err(cancelled),
384+
Ok(cancelled) => return Err(HandlerCancelledError::Inner(cancelled)),
364385
Err(e) => lsp_server::Response::new_err(
365386
id,
366387
lsp_server::ErrorCode::InternalError as i32,

0 commit comments

Comments
 (0)