From 7e626ba9a08129b2c9e5c3d14b7890f7e1b5847c Mon Sep 17 00:00:00 2001 From: "gcp-cherry-pick-bot[bot]" <98988430+gcp-cherry-pick-bot[bot]@users.noreply.github.com> Date: Fri, 26 Apr 2024 14:18:00 -0600 Subject: [PATCH] Fix panic in Diagnostics (cherry-pick #11066) (#11069) Cherry-picked Fix panic in Diagnostics (#11066) cc @maxbrunsfeld Release Notes: - Fixed a panic in populating diagnostics Co-authored-by: Conrad Irwin --- crates/diagnostics/src/diagnostics.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index fc7f8de8a0d96c0e107194db716160004d27ba27..6e2f6129baf250eeba3e2f8a5f6c570221c53c0d 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -887,22 +887,27 @@ fn diagnostic_header_renderer(diagnostic: Diagnostic) -> RenderBlock { }) } -fn compare_diagnostics( - lhs: &DiagnosticEntry, - rhs: &DiagnosticEntry, +fn compare_diagnostics( + old: &DiagnosticEntry, + new: &DiagnosticEntry, snapshot: &language::BufferSnapshot, ) -> Ordering { - lhs.range + use language::ToOffset; + // The old diagnostics may point to a previously open Buffer for this file. + if !old.range.start.is_valid(snapshot) { + return Ordering::Greater; + } + old.range .start .to_offset(snapshot) - .cmp(&rhs.range.start.to_offset(snapshot)) + .cmp(&new.range.start.to_offset(snapshot)) .then_with(|| { - lhs.range + old.range .end .to_offset(snapshot) - .cmp(&rhs.range.end.to_offset(snapshot)) + .cmp(&new.range.end.to_offset(snapshot)) }) - .then_with(|| lhs.diagnostic.message.cmp(&rhs.diagnostic.message)) + .then_with(|| old.diagnostic.message.cmp(&new.diagnostic.message)) } #[cfg(test)]