Slice indexing panics with valid "backwards" ranges
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I tried this code:
use core::ops::Bound;
fn main() {
let buf: [u32; 0] = [];
println!("{:?}", &buf[(Bound::Excluded(0), Bound::Included(0))]);
}
This feels perfectly valid, since the indices for the start and end of the range are both in-bounds, just with the backwards order that you'd expect. However, Rust internally converts this range into 1..1 and fails:
thread 'main' panicked at src/main.rs:4:26:
range end index 1 out of range for slice of length 0
Meta
rustc --version --verbose:
rustc 1.85.0-nightly (a4cb3c831 2024-12-17)
binary: rustc
commit-hash: a4cb3c831823d9baa56c3d90514b75b2660116fa
commit-date: 2024-12-17
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.5
Backtrace
stack backtrace:
0: rust_begin_unwind
at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/std/src/panicking.rs:665:5
1: core::panicking::panic_fmt
at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/core/src/panicking.rs:74:14
2: core::slice::index::slice_end_index_len_fail_rt
at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/core/src/slice/index.rs:64:5
3: core::slice::index::slice_end_index_len_fail
at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/core/src/slice/index.rs:58:5
4: <core::ops::range::Range<usize> as core::slice::index::SliceIndex<[T]>>::index
at ./.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/slice/index.rs:465:13
5: <(core::ops::range::Bound<usize>,core::ops::range::Bound<usize>) as core::slice::index::SliceIndex<[T]>>::index
at ./.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/slice/index.rs:1050:9
6: core::slice::index::<impl core::ops::index::Index<I> for [T]>::index
at ./.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/slice/index.rs:16:9
7: core::array::<impl core::ops::index::Index<I> for [T; N]>::index
at ./.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/array/mod.rs:375:9
8: playground::main
at ./src/main.rs:4:26
9: core::ops::function::FnOnce::call_once
at ./.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
Reason for emitting these "weird" ranges
I'm trying to create a data structure which stores a series of sorted, disjoint ranges in a Vec. When you determine where a provided range might fall into this vec, it's very natural to use the included/excluded bounds: you effectively have a similar API to that of binary_search, where "Included" means that an endpoint of a range overlaps with the range stored at an index, and "Excluded" means that the range does not overlap with the range stored at an index, but could be inserted after that index.
In this case, both the "normal" lopsided ranges (inclusive, then exclusive) happen, but also these "backwards" ones with exclusive, then inclusive. So, I think that supporting these "backward" ranges would be nice, even if it complicates the code a bit here.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the reproducer in the issue and inspect library/core/src/slice/index.rs, especially the tuple-of-Bound indexing path shown in the backtrace. Determine the intended behavior for excluded-start, included-end ranges and cover the relevant slice-indexing cases. Done means valid backward ranges no longer panic while invalid ranges retain appropriate bounds checks.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100