Repository navigation
Conversation
`partition` picks the slice fast path when the first lane is contiguous, then calls `as_slice_mut().unwrap()` on each lane. A lane along an inverted axis (e.g. after `invert_axis` or `s![..;-1]`) is contiguous but has stride -1, so `as_slice_mut()` returns `None` and the unwrap panics. Check `as_slice_mut().is_some()` instead, so such lanes go through the copying path that already handles arbitrary strides. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
by-ae
left a comment
There was a problem hiding this comment.
Checked this out at c4aba74 and ran the partition tests in the lib (cargo test -p ndarray --lib test_partition): all 8 pass, including the new test_partition_negative_stride.
I also ran a randomized probe outside the test suite: 3000 random 1-3D i32 arrays (C and F order), with each axis randomly inverted or sliced with step -2 or 2, partitioning along a random axis at a random kth. For every lane it checked that the result is a permutation of the input lane, that p[kth] equals the kth element of the sorted lane, and that everything before or after it is <= or >= respectively. On master the same probe panics at the as_slice_mut().unwrap() inside the fast path (impl_methods.rs:3324). On this branch all 3000 cases pass.
Checking only the first lane is fine, since every lane along axis has the same stride, so if as_slice_mut() works for the first lane it works for all of them.
partitionpanics when the lanes alongaxishave a negative stride:partitioncopies the array withto_owned(), which keeps the negative stride, and then takes the slice fast path if the first laneis_contiguous(). A lane with stride -1 counts as contiguous, butas_slice_mut()only returns a slice for standard layout, so theunwrap()in the fast path fails.This checks
as_slice_mut().is_some()instead, which is exactly what the fast path needs. Lanes with a negative stride now go through the existing copy path, which already handles any stride.Added
test_partition_negative_stridecovering an inverted axis and a reversed view; it panics without the change.cargo test -p ndarray -F approx,serde,rayon,cargo clippy -F approx,serde,rayonandcargo +nightly fmt --all --checkpass locally.I used Claude Code to write this change.