Skip to content

fix: explain invalid s![] arguments in the compiler error - #1630

Open
BenjaminLucier wants to merge 1 commit into
rust-ndarray:masterfrom
BenjaminLucier:fix/s-macro-bracket-diagnostic
Open

BenjaminLucier wants to merge 1 commit into
rust-ndarray:masterfrom
BenjaminLucier:fix/s-macro-bracket-diagnostic

Conversation

@BenjaminLucier

Copy link
Copy Markdown

Closes #1122.

Problem

Wrapping the arguments of s![] in brackets is an easy mistake, and the error doesn't point at it:

error[E0277]: the trait bound `[std::ops::Range<{integer}>; 1]: ndarray::SliceNextDim` is not satisfied

It names a hidden trait and never mentions s![] or what the macro accepts.

Fix

Add #[diagnostic::on_unimplemented] to SliceNextDim, the trait that s![] requires of each argument. Now s![[1..3, 0..2]] reports:

error[E0277]: `[std::ops::Range<{integer}>; 2]` cannot be used as an argument to the `s![]` macro
  |
  |     let _ = a.slice(s![[1..3, 0..2]]);
  |                     ^^^^^^^^^^^^^^^^ not an index, range, `Slice`, or `NewAxis`
  |
  = note: each comma-separated argument to `s![]` must be an index (`isize`, `usize` or `i32`), a range such as `1..3` or `..`, a `Slice`, or `NewAxis`, optionally followed by `;step`
  = note: do not wrap the arguments in brackets or parentheses: write `s![1..3, ..]`, not `s![[1..3, ..]]`

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 a compile_fail example, as the issue suggested.

Two related mistakes are not covered, because they fail earlier with a different error:

  • Mixed range types inside brackets, like s![[1..3, ..]] with a .. element: the array literal itself has a type mismatch (E0308).
  • Unsupported index types, like 1u64..3: these fail on the From<_> for SliceInfoElem bound.

Tests

The new compile_fail,E0277 doc 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, and cargo doc --no-deps --all-features. Not run locally: nightly cargo fmt (formatted by hand to match), MSRV 1.87, miri, nostd, cross, cargo-careful, BLAS.

🤖 Generated with Claude Code

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

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.

Compiler error message is unhelpful when trying ndarray::s!([2..3])

1 participant