From 6baf8b033b4f1916ba88277621b78f02033f0db6 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 2 Jun 2022 12:05:28 +0200 Subject: [PATCH] Don't reuse the same diagnostic group id across buffers This lets us use the group id as the key for an `ElementState`, which fixes a panic that would occur in project diagnostics when opening it while there were multiple diagnostic groups with the same id. --- crates/collab/src/integration_tests.rs | 4 ++-- crates/project/src/project.rs | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/crates/collab/src/integration_tests.rs b/crates/collab/src/integration_tests.rs index 56822d21da..65f60ed077 100644 --- a/crates/collab/src/integration_tests.rs +++ b/crates/collab/src/integration_tests.rs @@ -1359,7 +1359,7 @@ async fn test_collaborating_with_diagnostics( DiagnosticEntry { range: Point::new(0, 4)..Point::new(0, 7), diagnostic: Diagnostic { - group_id: 0, + group_id: 1, message: "message 1".to_string(), severity: lsp::DiagnosticSeverity::ERROR, is_primary: true, @@ -1369,7 +1369,7 @@ async fn test_collaborating_with_diagnostics( DiagnosticEntry { range: Point::new(0, 10)..Point::new(0, 13), diagnostic: Diagnostic { - group_id: 1, + group_id: 2, severity: lsp::DiagnosticSeverity::WARNING, message: "message 2".to_string(), is_primary: true, diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index 70156bb3a5..adeb8d37f9 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -73,6 +73,7 @@ pub struct Project { next_language_server_id: usize, client: Arc, next_entry_id: Arc, + next_diagnostic_group_id: usize, user_store: ModelHandle, fs: Arc, client_state: ProjectClientState, @@ -355,6 +356,7 @@ impl Project { user_store, fs, next_entry_id: Default::default(), + next_diagnostic_group_id: Default::default(), language_servers: Default::default(), started_language_servers: Default::default(), language_server_statuses: Default::default(), @@ -424,6 +426,7 @@ impl Project { user_store: user_store.clone(), fs, next_entry_id: Default::default(), + next_diagnostic_group_id: Default::default(), subscriptions: vec![client.add_model_for_remote_entity(remote_id, cx)], client: client.clone(), client_state: ProjectClientState::Remote { @@ -2121,7 +2124,6 @@ impl Project { .uri .to_file_path() .map_err(|_| anyhow!("URI is not a file"))?; - let mut next_group_id = 0; let mut diagnostics = Vec::default(); let mut primary_diagnostic_group_ids = HashMap::default(); let mut sources_by_group_id = HashMap::default(); @@ -2156,7 +2158,7 @@ impl Project { (diagnostic.severity, is_unnecessary), ); } else { - let group_id = post_inc(&mut next_group_id); + let group_id = post_inc(&mut self.next_diagnostic_group_id); let is_disk_based = source.map_or(false, |source| { disk_based_sources.contains(&source.as_str()) }); @@ -6270,7 +6272,7 @@ mod tests { severity: DiagnosticSeverity::WARNING, message: "unreachable statement".to_string(), is_disk_based: true, - group_id: 1, + group_id: 4, is_primary: true, ..Default::default() } @@ -6281,7 +6283,7 @@ mod tests { severity: DiagnosticSeverity::ERROR, message: "undefined variable 'A'".to_string(), is_disk_based: true, - group_id: 0, + group_id: 3, is_primary: true, ..Default::default() }, @@ -6359,7 +6361,7 @@ mod tests { severity: DiagnosticSeverity::WARNING, message: "undefined variable 'A'".to_string(), is_disk_based: true, - group_id: 1, + group_id: 6, is_primary: true, ..Default::default() } @@ -6370,7 +6372,7 @@ mod tests { severity: DiagnosticSeverity::ERROR, message: "undefined variable 'BB'".to_string(), is_disk_based: true, - group_id: 0, + group_id: 5, is_primary: true, ..Default::default() },