Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions manual/src/snippets/slices.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ assert_eq([30, 40, 50, 60], my_list[3..=6]);
// Negative indices: Counting from the end of the list
assert_eq([80, 90], my_list[-3..-1]);
```

A range whose start lands after its end (for example `my_list[6..3]`) is out of
bounds and raises an error rather than returning a reversed or empty slice. This
holds for lists, strings, tuples and deques alike.
5 changes: 5 additions & 0 deletions ndc_stdlib/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ fn extract_vm_offset(index_value: &Value, size: usize) -> Result<VmOffset, VmErr
} else {
to_idx
};
if to_idx < from_idx {
Comment thread
timfennis marked this conversation as resolved.
Outdated
return Err(VmError::native(format!(
"{from_idx}..{to_idx} out of bounds"
)));
}
return Ok(VmOffset::Range(from_idx, to_idx));
}
if let Some(start) = iter_ref.unbounded_range_start() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// expect-error: 3..1 out of bounds
// A reversed range on a string used to panic with "attempt to subtract with
// overflow"; it must now error gracefully like list/tuple slices do.
"hello"[3..1]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// expect-error: 1..0 out of bounds
// A reversed range on a deque used to panic with "attempt to subtract with
// overflow"; it must now error gracefully.
let d = Deque();
d.push_back(1);
d.push_back(2);
d.push_back(3);
d[1..0]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// expect-error: 3..1 out of bounds
// Assigning into a reversed slice used to panic with "attempt to subtract with
// overflow"; it must now error gracefully.
let x = [1, 2, 3, 4];
x[3..1] = [9]
Loading