Skip to content

Commit

Permalink
Merge pull request #18762 from davidbarsky/davidbarsky/wrap-salsa-can…
Browse files Browse the repository at this point in the history
…cellation-error

internal: wrap `salsa::Cycle`
  • Loading branch information
Veykril authored Dec 26, 2024
2 parents bae8fb5 + 637700e commit 352116c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/tools/rust-analyzer/crates/ra-salsa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,11 @@ where
#[non_exhaustive]
pub enum Cancelled {
/// The query was operating on revision R, but there is a pending write to move to revision R+1.
#[non_exhaustive]
PendingWrite,

/// The query was blocked on another thread, and that thread panicked.
#[non_exhaustive]
PropagatedPanic,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,31 @@ impl RequestDispatcher<'_> {
}
}

#[derive(Debug)]
enum HandlerCancelledError {
PropagatedPanic,
Inner(ide::Cancelled),
}

impl std::error::Error for HandlerCancelledError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
HandlerCancelledError::PropagatedPanic => None,
HandlerCancelledError::Inner(cancelled) => Some(cancelled),
}
}
}

impl fmt::Display for HandlerCancelledError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Cancelled")
}
}

fn thread_result_to_response<R>(
id: lsp_server::RequestId,
result: thread::Result<anyhow::Result<R::Result>>,
) -> Result<lsp_server::Response, Cancelled>
) -> Result<lsp_server::Response, HandlerCancelledError>
where
R: lsp_types::request::Request,
R::Params: DeserializeOwned,
Expand All @@ -331,10 +352,10 @@ where
message.push_str(panic_message)
} else if let Some(cycle) = panic.downcast_ref::<Cycle>() {
tracing::error!("Cycle propagated out of salsa! This is a bug: {cycle:?}");
return Err(Cancelled::PropagatedPanic);
return Err(HandlerCancelledError::PropagatedPanic);
} else if let Ok(cancelled) = panic.downcast::<Cancelled>() {
tracing::error!("Cancellation propagated out of salsa! This is a bug");
return Err(*cancelled);
return Err(HandlerCancelledError::Inner(*cancelled));
}

Ok(lsp_server::Response::new_err(
Expand All @@ -349,7 +370,7 @@ where
fn result_to_response<R>(
id: lsp_server::RequestId,
result: anyhow::Result<R::Result>,
) -> Result<lsp_server::Response, Cancelled>
) -> Result<lsp_server::Response, HandlerCancelledError>
where
R: lsp_types::request::Request,
R::Params: DeserializeOwned,
Expand All @@ -360,7 +381,7 @@ where
Err(e) => match e.downcast::<LspError>() {
Ok(lsp_error) => lsp_server::Response::new_err(id, lsp_error.code, lsp_error.message),
Err(e) => match e.downcast::<Cancelled>() {
Ok(cancelled) => return Err(cancelled),
Ok(cancelled) => return Err(HandlerCancelledError::Inner(cancelled)),
Err(e) => lsp_server::Response::new_err(
id,
lsp_server::ErrorCode::InternalError as i32,
Expand Down

0 comments on commit 352116c

Please sign in to comment.