diff --git a/manual/src/snippets/slices.md b/manual/src/snippets/slices.md index 88296dce..b4997de9 100644 --- a/manual/src/snippets/slices.md +++ b/manual/src/snippets/slices.md @@ -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. diff --git a/ndc_stdlib/src/index.rs b/ndc_stdlib/src/index.rs index 88cf0fc6..968c4ced 100644 --- a/ndc_stdlib/src/index.rs +++ b/ndc_stdlib/src/index.rs @@ -212,6 +212,14 @@ fn to_forward_index(i: i64, size: usize, for_slice: bool) -> Result i64 { + if i < 0 { size as i64 + i } else { i } +} + enum VmOffset { Element(usize), Range(usize, usize), @@ -223,6 +231,17 @@ fn extract_vm_offset(index_value: &Value, size: usize) -> Result