diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index 22a7dd1548f7..869afbe907db 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -489,6 +489,10 @@ impl GlobalStateSnapshot { pub(crate) fn vfs_memory_usage(&self) -> usize { self.vfs.read().0.memory_usage() } + + pub(crate) fn file_exists(&self, file_id: FileId) -> bool { + self.vfs.read().0.exists(file_id) + } } pub(crate) fn file_id_to_url(vfs: &vfs::Vfs, id: FileId) -> Url { diff --git a/crates/rust-analyzer/src/handlers/request.rs b/crates/rust-analyzer/src/handlers/request.rs index dbf3a4792d60..a21206b5b132 100644 --- a/crates/rust-analyzer/src/handlers/request.rs +++ b/crates/rust-analyzer/src/handlers/request.rs @@ -1438,6 +1438,8 @@ pub(crate) fn handle_inlay_hints_resolve( let resolve_data: lsp_ext::InlayHintResolveData = serde_json::from_value(data)?; let file_id = FileId(resolve_data.file_id); + anyhow::ensure!(snap.file_exists(file_id), "Invalid LSP resolve data"); + let line_index = snap.file_line_index(file_id)?; let range = from_proto::text_range( &line_index, diff --git a/crates/vfs/src/lib.rs b/crates/vfs/src/lib.rs index fe3dfe619686..06004adad34a 100644 --- a/crates/vfs/src/lib.rs +++ b/crates/vfs/src/lib.rs @@ -184,6 +184,11 @@ impl Vfs { mem::take(&mut self.changes) } + /// Provides a panic-less way to verify file_id validity. + pub fn exists(&self, file_id: FileId) -> bool { + self.get(file_id).is_some() + } + /// Returns the id associated with `path` /// /// - If `path` does not exists in the `Vfs`, allocate a new id for it, associated with a