Skip to content

fix: name the axis in slicing panic messages - #1629

Open
BenjaminLucier wants to merge 1 commit into
rust-ndarray:masterfrom
BenjaminLucier:fix/slice-axis-in-panic
Open

BenjaminLucier wants to merge 1 commit into
rust-ndarray:masterfrom
BenjaminLucier:fix/slice-axis-in-panic

Conversation

@BenjaminLucier

Copy link
Copy Markdown

Closes #1121.

Problem

When a slice is out of bounds, the panic doesn't say which axis was being sliced:

Slice begin 16744 is past end of axis of length 184

With several ranges in one s![], you have to work out which one was wrong.

Fix

Pass the axis index down to the bounds check, so the message names it:

Slice begin 16744 is past end of axis 1 of length 184

The zero-step panic now names the axis too (Slice stride must not be zero (axis 1)).

The axis is the index in the input array, so NewAxis entries in s![] don't shift it. This covers slice*, slice_axis*, slice_collapse, and the overlap check in multi_slice_*, which panics on out-of-bounds slices before slicing anything.

All the changed functions are crate-private (to_abs_slice, do_slice, slice_min_max), so the public API is unchanged. In slices_intersect the axis index comes from adding 0.. to the existing izip!.

The issue also suggests printing the whole array shape. I left that out because the bounds check only sees the one axis being sliced; the axis number is enough to find the bad range.

Tests

tests/array.rs gains five #[should_panic(expected = ...)] tests that check the full message: begin and end out of bounds, an index that has to skip a NewAxis, a zero step, and multi_slice_mut. The existing slice_min_max unit tests were updated for the new parameter.

Checked locally on stable 1.99.1 (aarch64-apple-darwin): cargo clippy -F approx,serde,rayon and cargo test -F approx,serde,rayon. Not run locally: nightly cargo fmt (formatted by hand to match), MSRV 1.87, miri, nostd, cross, cargo-careful, BLAS.

🤖 Generated with Claude Code

Out-of-bounds and zero-step slice panics now say which axis was being
sliced, e.g. "Slice end 9 is past end of axis 2 of length 5" instead of
"Slice end 9 is past end of axis of length 5". The axis is the index in
the input array, so `NewAxis` entries in `s![]` do not shift it.

This covers `slice*`, `slice_axis*`, `slice_collapse` and the overlap
check in `multi_slice_*`.

Closes rust-ndarray#1121

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

More informative messages when index out of bound

1 participant