Skip to content

Commit 8cf1b87

Browse files
authored
Rename to SessionSnapshot, move unwind assertion closer (#19177)
This PR addresses the post-merge review comments from #19041, specifically it: - Rename `WorkspaceSnapshot` to `SessionSnapshot` - Rename `take_workspace_snapshot` to `take_session_snapshot` - Rename `take_snapshot` to `take_document_snapshot` - Move `AssertUnwindSafe` closer to the `catch_unwind` call which requires the assertion
1 parent 1fd4812 commit 8cf1b87

File tree

5 files changed

+28
-22
lines changed

5 files changed

+28
-22
lines changed

crates/ty_server/src/server/api.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use lsp_server as server;
66
use lsp_server::RequestId;
77
use lsp_types::notification::Notification;
88
use lsp_types::request::Request;
9-
use std::panic::UnwindSafe;
9+
use std::panic::{AssertUnwindSafe, UnwindSafe};
1010

1111
mod diagnostics;
1212
mod notifications;
@@ -157,7 +157,9 @@ where
157157
.cancellation_token(&id)
158158
.expect("request should have been tested for cancellation before scheduling");
159159

160-
let snapshot = session.take_workspace_snapshot();
160+
// SAFETY: The `snapshot` is safe to move across the unwind boundary because it is not used
161+
// after unwinding.
162+
let snapshot = AssertUnwindSafe(session.take_session_snapshot());
161163

162164
Box::new(move |client| {
163165
let _span = tracing::debug_span!("request", %id, method = R::METHOD).entered();
@@ -216,7 +218,7 @@ where
216218
AnySystemPath::SystemVirtual(_) => session.default_project_db().clone(),
217219
};
218220

219-
let Some(snapshot) = session.take_snapshot(url) else {
221+
let Some(snapshot) = session.take_document_snapshot(url) else {
220222
tracing::warn!("Ignoring request because snapshot for path `{path:?}` doesn't exist");
221223
return Box::new(|_| {});
222224
};
@@ -317,7 +319,7 @@ where
317319
let (id, params) = cast_notification::<N>(req)?;
318320
Ok(Task::background(schedule, move |session: &Session| {
319321
let url = N::document_url(&params);
320-
let Some(snapshot) = session.take_snapshot((*url).clone()) else {
322+
let Some(snapshot) = session.take_document_snapshot((*url).clone()) else {
321323
tracing::debug!(
322324
"Ignoring notification because snapshot for url `{url}` doesn't exist."
323325
);

crates/ty_server/src/server/api/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub(super) fn publish_diagnostics(
8080
let path = key.path();
8181

8282
let snapshot = session
83-
.take_snapshot(url.clone())
83+
.take_document_snapshot(url.clone())
8484
.ok_or_else(|| anyhow::anyhow!("Unable to take snapshot for document with URL {url}"))
8585
.with_failure_code(lsp_server::ErrorCode::InternalError)?;
8686

crates/ty_server/src/server/api/requests/workspace_diagnostic.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::panic::AssertUnwindSafe;
2+
13
use lsp_types::request::WorkspaceDiagnosticRequest;
24
use lsp_types::{
35
FullDocumentDiagnosticReport, Url, WorkspaceDiagnosticParams, WorkspaceDiagnosticReport,
@@ -12,7 +14,7 @@ use crate::server::api::diagnostics::to_lsp_diagnostic;
1214
use crate::server::api::traits::{
1315
BackgroundRequestHandler, RequestHandler, RetriableRequestHandler,
1416
};
15-
use crate::session::WorkspaceSnapshot;
17+
use crate::session::SessionSnapshot;
1618
use crate::session::client::Client;
1719
use crate::system::file_to_url;
1820

@@ -24,7 +26,7 @@ impl RequestHandler for WorkspaceDiagnosticRequestHandler {
2426

2527
impl BackgroundRequestHandler for WorkspaceDiagnosticRequestHandler {
2628
fn run(
27-
snapshot: WorkspaceSnapshot,
29+
snapshot: AssertUnwindSafe<SessionSnapshot>,
2830
_client: &Client,
2931
_params: WorkspaceDiagnosticParams,
3032
) -> Result<WorkspaceDiagnosticReportResult> {

crates/ty_server/src/server/api/traits.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! A stateful LSP implementation that calls into the ty API.
22
3+
use std::panic::AssertUnwindSafe;
4+
35
use crate::session::client::Client;
4-
use crate::session::{DocumentSnapshot, Session, WorkspaceSnapshot};
6+
use crate::session::{DocumentSnapshot, Session, SessionSnapshot};
57

68
use lsp_types::notification::Notification as LSPNotification;
79
use lsp_types::request::Request;
@@ -58,7 +60,7 @@ pub(super) trait BackgroundDocumentRequestHandler: RetriableRequestHandler {
5860
/// A request handler that can be run on a background thread.
5961
pub(super) trait BackgroundRequestHandler: RetriableRequestHandler {
6062
fn run(
61-
snapshot: WorkspaceSnapshot,
63+
snapshot: AssertUnwindSafe<SessionSnapshot>,
6264
client: &Client,
6365
params: <<Self as RequestHandler>::RequestType as Request>::Params,
6466
) -> super::Result<<<Self as RequestHandler>::RequestType as Request>::Result>;

crates/ty_server/src/session.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use std::collections::{BTreeMap, VecDeque};
44
use std::ops::{Deref, DerefMut};
5-
use std::panic::AssertUnwindSafe;
65
use std::sync::Arc;
76

87
use anyhow::{Context, anyhow};
@@ -224,14 +223,6 @@ impl Session {
224223
self.index().key_from_url(url)
225224
}
226225

227-
pub(crate) fn take_workspace_snapshot(&self) -> WorkspaceSnapshot {
228-
WorkspaceSnapshot {
229-
projects: AssertUnwindSafe(self.projects.values().cloned().collect()),
230-
index: self.index.clone().unwrap(),
231-
position_encoding: self.position_encoding,
232-
}
233-
}
234-
235226
pub(crate) fn initialize_workspaces(&mut self, workspace_settings: Vec<(Url, ClientOptions)>) {
236227
assert!(!self.workspaces.all_initialized());
237228

@@ -289,7 +280,7 @@ impl Session {
289280
/// Creates a document snapshot with the URL referencing the document to snapshot.
290281
///
291282
/// Returns `None` if the url can't be converted to a document key or if the document isn't open.
292-
pub fn take_snapshot(&self, url: Url) -> Option<DocumentSnapshot> {
283+
pub(crate) fn take_document_snapshot(&self, url: Url) -> Option<DocumentSnapshot> {
293284
let key = self.key_from_url(url).ok()?;
294285
Some(DocumentSnapshot {
295286
resolved_client_capabilities: self.resolved_client_capabilities.clone(),
@@ -299,6 +290,15 @@ impl Session {
299290
})
300291
}
301292

293+
/// Creates a snapshot of the current state of the [`Session`].
294+
pub(crate) fn take_session_snapshot(&self) -> SessionSnapshot {
295+
SessionSnapshot {
296+
projects: self.projects.values().cloned().collect(),
297+
index: self.index.clone().unwrap(),
298+
position_encoding: self.position_encoding,
299+
}
300+
}
301+
302302
/// Iterates over the document keys for all open text documents.
303303
pub(super) fn text_document_keys(&self) -> impl Iterator<Item = DocumentKey> + '_ {
304304
self.index()
@@ -467,13 +467,13 @@ impl DocumentSnapshot {
467467
}
468468

469469
/// An immutable snapshot of the current state of [`Session`].
470-
pub(crate) struct WorkspaceSnapshot {
471-
projects: AssertUnwindSafe<Vec<ProjectDatabase>>,
470+
pub(crate) struct SessionSnapshot {
471+
projects: Vec<ProjectDatabase>,
472472
index: Arc<index::Index>,
473473
position_encoding: PositionEncoding,
474474
}
475475

476-
impl WorkspaceSnapshot {
476+
impl SessionSnapshot {
477477
pub(crate) fn projects(&self) -> &[ProjectDatabase] {
478478
&self.projects
479479
}

0 commit comments

Comments
 (0)