Repository navigation
fix: explain invalid s![] arguments in the compiler error - #1630
Open
BenjaminLucier wants to merge 1 commit into
Open
BenjaminLucier wants to merge 1 commit into
BenjaminLucier wants to merge 1 commit into
Conversation
Wrapping the arguments of `s![]` in brackets, e.g. `s![[1..3, ..]]`,
produced "the trait `SliceNextDim` is not implemented for
`[Range<{integer}>; 2]`", which does not mention the macro or what it
expects. Add `#[diagnostic::on_unimplemented]` to `SliceNextDim` so the
error says which argument types `s![]` accepts and that the arguments
should not be wrapped in brackets or parentheses.
Also document the mistake in the `s![]` docs with a `compile_fail`
example.
Closes rust-ndarray#1122
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 #1122.
Problem
Wrapping the arguments of
s![]in brackets is an easy mistake, and the error doesn't point at it:It names a hidden trait and never mentions
s![]or what the macro accepts.Fix
Add
#[diagnostic::on_unimplemented]toSliceNextDim, the trait thats![]requires of each argument. Nows![[1..3, 0..2]]reports:Tuples such as
s![(1..3, 2)]get the same message.#[diagnostic::on_unimplemented]has been stable since Rust 1.78, below the MSRV of 1.87.The
s!docs now also explain the mistake, with acompile_failexample, as the issue suggested.Two related mistakes are not covered, because they fail earlier with a different error:
s![[1..3, ..]]with a..element: the array literal itself has a type mismatch (E0308).1u64..3: these fail on theFrom<_> for SliceInfoElembound.Tests
The new
compile_fail,E0277doc test checks that the bracketed form is rejected. Rustdoc only checks the error code on nightly. E0277 is what the compiler reports, as in the output above.Checked locally on stable 1.99.1 (aarch64-apple-darwin):
cargo clippy -F approx,serde,rayon,cargo test --doc, andcargo doc --no-deps --all-features. Not run locally: nightlycargo fmt(formatted by hand to match), MSRV 1.87, miri, nostd, cross, cargo-careful, BLAS.🤖 Generated with Claude Code