Unnamed repository; edit this file 'description' to name the repository.
Ensure resolved hint's file exists
Kirill Bulatov 2023-09-02
parent 10464c7 · commit caf0185
-rw-r--r--crates/rust-analyzer/src/global_state.rs4
-rw-r--r--crates/rust-analyzer/src/handlers/request.rs2
-rw-r--r--crates/vfs/src/lib.rs5
3 files changed, 11 insertions, 0 deletions
diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs
index 22a7dd1548..869afbe907 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 dbf3a4792d..a21206b5b1 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 fe3dfe6196..06004adad3 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