From ede9600ab4e55a2bedea375c5486fce6b20d57fa Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 12 Feb 2024 12:58:30 -0800 Subject: [PATCH] Improve panic message for invalid anchors (#7700) Release Notes: - N/A Co-authored-by: Marshall --- crates/text/src/text.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/crates/text/src/text.rs b/crates/text/src/text.rs index fc2cf3b753..20ef6dcf1b 100644 --- a/crates/text/src/text.rs +++ b/crates/text/src/text.rs @@ -1911,8 +1911,16 @@ impl BufferSnapshot { } else { insertion_cursor.prev(&()); } - let insertion = insertion_cursor.item().expect("invalid insertion"); - assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion"); + + let Some(insertion) = insertion_cursor + .item() + .filter(|insertion| insertion.timestamp == anchor.timestamp) + else { + panic!( + "invalid anchor {:?}. buffer id: {}, version: {:?}", + anchor, self.remote_id, self.version + ); + }; let mut fragment_cursor = self.fragments.cursor::<(Option<&Locator>, usize)>(); fragment_cursor.seek(&Some(&insertion.fragment_id), Bias::Left, &None); @@ -1949,8 +1957,20 @@ impl BufferSnapshot { } else { insertion_cursor.prev(&()); } - let insertion = insertion_cursor.item().expect("invalid insertion"); - debug_assert_eq!(insertion.timestamp, anchor.timestamp, "invalid insertion"); + + let Some(insertion) = insertion_cursor.item().filter(|insertion| { + if cfg!(debug_assertions) { + insertion.timestamp == anchor.timestamp + } else { + true + } + }) else { + panic!( + "invalid anchor {:?}. buffer id: {}, version: {:?}", + anchor, self.remote_id, self.version + ); + }; + &insertion.fragment_id } }