text: Downgrade some more offset panics to error logs (#43925)

Release Notes:

- N/A *or* Added/Fixed/Improved ...
This commit is contained in:
Lukas Wirth
2025-12-01 18:04:01 +00:00
committed by GitHub
parent c8166abbcb
commit 61ae59708d
3 changed files with 36 additions and 14 deletions
+5 -1
View File
@@ -220,7 +220,11 @@ impl<'a> ChunkSlice<'a> {
range.start = self.text.ceil_char_boundary(range.start); range.start = self.text.ceil_char_boundary(range.start);
} }
if !self.assert_char_boundary::<false>(range.end) { if !self.assert_char_boundary::<false>(range.end) {
range.end = self.text.floor_char_boundary(range.end); range.end = if range.end < range.start {
range.start
} else {
self.text.floor_char_boundary(range.end)
};
} }
let mask = (1 as Bitmap) let mask = (1 as Bitmap)
.unbounded_shl(range.end as u32) .unbounded_shl(range.end as u32)
+12 -4
View File
@@ -50,23 +50,31 @@ impl Rope {
#[track_caller] #[track_caller]
#[inline(always)] #[inline(always)]
pub fn assert_char_boundary(&self, offset: usize) { pub fn assert_char_boundary<const PANIC: bool>(&self, offset: usize) -> bool {
if self.chunks.is_empty() && offset == 0 { if self.chunks.is_empty() && offset == 0 {
return; return true;
} }
let (start, _, item) = self.chunks.find::<usize, _>((), &offset, Bias::Left); let (start, _, item) = self.chunks.find::<usize, _>((), &offset, Bias::Left);
match item { match item {
Some(chunk) => { Some(chunk) => {
let chunk_offset = offset - start; let chunk_offset = offset - start;
chunk.assert_char_boundary::<true>(chunk_offset); chunk.assert_char_boundary::<PANIC>(chunk_offset)
} }
None => { None if PANIC => {
panic!( panic!(
"byte index {} is out of bounds of rope (length: {})", "byte index {} is out of bounds of rope (length: {})",
offset, offset,
self.len() self.len()
); );
} }
None => {
log::error!(
"byte index {} is out of bounds of rope (length: {})",
offset,
self.len()
);
false
}
} }
} }
+19 -9
View File
@@ -2435,16 +2435,22 @@ impl BufferSnapshot {
self.anchor_at_offset(position.to_offset(self), bias) self.anchor_at_offset(position.to_offset(self), bias)
} }
fn anchor_at_offset(&self, offset: usize, bias: Bias) -> Anchor { fn anchor_at_offset(&self, mut offset: usize, bias: Bias) -> Anchor {
if bias == Bias::Left && offset == 0 { if bias == Bias::Left && offset == 0 {
Anchor::min_for_buffer(self.remote_id) Anchor::min_for_buffer(self.remote_id)
} else if bias == Bias::Right && offset == self.len() { } else if bias == Bias::Right
&& ((cfg!(debug_assertions) && offset >= self.len()) || offset == self.len())
{
Anchor::max_for_buffer(self.remote_id) Anchor::max_for_buffer(self.remote_id)
} else { } else {
if cfg!(debug_assertions) { if self
self.visible_text.assert_char_boundary(offset); .visible_text
} else if offset > self.visible_text.len() { .assert_char_boundary::<{ cfg!(debug_assertions) }>(offset)
panic!("offset {} is out of bounds", offset) {
offset = match bias {
Bias::Left => self.visible_text.floor_char_boundary(offset),
Bias::Right => self.visible_text.ceil_char_boundary(offset),
};
} }
let (start, _, item) = self.fragments.find::<usize, _>(&None, &offset, bias); let (start, _, item) = self.fragments.find::<usize, _>(&None, &offset, bias);
let fragment = item.unwrap(); let fragment = item.unwrap();
@@ -3136,10 +3142,14 @@ impl ToOffset for Point {
impl ToOffset for usize { impl ToOffset for usize {
fn to_offset(&self, snapshot: &BufferSnapshot) -> usize { fn to_offset(&self, snapshot: &BufferSnapshot) -> usize {
if cfg!(debug_assertions) { if snapshot
snapshot.as_rope().assert_char_boundary(*self); .as_rope()
.assert_char_boundary::<{ cfg!(debug_assertions) }>(*self)
{
snapshot.as_rope().floor_char_boundary(*self)
} else {
*self
} }
*self
} }
} }