From 76098940466fa7549a5bbdf1a1322b4f19e9016f Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Fri, 25 Apr 2025 11:12:16 -0600 Subject: [PATCH] Maybe fix panic (#29352) Since around the time we shipped block diagnostics, we've been seeing an out of range panic in the editor. Although the code is heavily inlined, so the stacktrace is missing, this seems like a likely place that indexing may have gone wrong. Release Notes: - Fixed a rare panic in the editor --- crates/editor/src/element.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 6521e67ab30f8fcd36268ab5e82509ee46bad918..085dda195e7be17ccf954eeb552b767f9139fb06 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -2661,19 +2661,21 @@ impl EditorElement { text_x + layout.width, )) }; - x_position = if rows.contains(&align_to.row()) { - x_and_width(&line_layouts[align_to.row().minus(rows.start) as usize]) - } else { - x_and_width(&layout_line( - align_to.row(), - snapshot, - &self.style, - editor_width, - is_row_soft_wrapped, - window, - cx, - )) - }; + let line_ix = align_to.row().0.checked_sub(rows.start.0); + x_position = + if let Some(layout) = line_ix.and_then(|ix| line_layouts.get(ix as usize)) { + x_and_width(&layout) + } else { + x_and_width(&layout_line( + align_to.row(), + snapshot, + &self.style, + editor_width, + is_row_soft_wrapped, + window, + cx, + )) + }; let anchor_x = x_position.unwrap().0;