Skip to content

internal: wrap salsa::Cycle #18762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions 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
31 changes: 26 additions & 5 deletions crates/rust-analyzer/src/handlers/dispatch.rs
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
Loading