Repository navigation
fix: name the axis in slicing panic messages - #1629
Open
BenjaminLucier wants to merge 1 commit into
Open
BenjaminLucier wants to merge 1 commit into
BenjaminLucier wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1121.
Problem
When a slice is out of bounds, the panic doesn't say which axis was being sliced:
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:
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
NewAxisentries ins![]don't shift it. This coversslice*,slice_axis*,slice_collapse, and the overlap check inmulti_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. Inslices_intersectthe axis index comes from adding0..to the existingizip!.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.rsgains five#[should_panic(expected = ...)]tests that check the full message: begin and end out of bounds, an index that has to skip aNewAxis, a zero step, andmulti_slice_mut. The existingslice_min_maxunit tests were updated for the new parameter.Checked locally on stable 1.99.1 (aarch64-apple-darwin):
cargo clippy -F approx,serde,rayonandcargo test -F approx,serde,rayon. Not run locally: nightlycargo fmt(formatted by hand to match), MSRV 1.87, miri, nostd, cross, cargo-careful, BLAS.🤖 Generated with Claude Code