Fix panic in Diagnostics (cherry-pick #11066) (#11069)
gcp-cherry-pick-bot[bot]
and
Conrad Irwin
created
Cherry-picked Fix panic in Diagnostics (#11066)
cc @maxbrunsfeld
Release Notes:
- Fixed a panic in populating diagnostics
Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
Change summary
crates/diagnostics/src/diagnostics.rs | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)
Detailed changes
@@ -887,22 +887,27 @@ fn diagnostic_header_renderer(diagnostic: Diagnostic) -> RenderBlock {
})
}
-fn compare_diagnostics<L: language::ToOffset, R: language::ToOffset>(
- lhs: &DiagnosticEntry<L>,
- rhs: &DiagnosticEntry<R>,
+fn compare_diagnostics(
+ old: &DiagnosticEntry<language::Anchor>,
+ new: &DiagnosticEntry<language::Anchor>,
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)]