Suggests changing slice-like type instead of index for invalid `SliceIndex`
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Following https://github.com/rust-lang/rust/pull/138381, ByteStr now indexes with SliceIndex instead of Index/IndexMut. This worsened some diagnostics for indexing other types.
Since SliceIndex is “backwards”, this leads it to suggest other slice-like types (the LHS), instead of suggesting other index/range types which work for the LHS. When the LHS already indexes with SliceIndex, it's unhelpful to suggest other slice-like types. Furthermore, this leaks ByteStr into stable diagnostics (confirmed with either rust.channel = "stable" in bootstrap.toml or RUSTC_BOOTSTRAP=-1)
The affected diagnostics are shown in commit https://github.com/rust-lang/rust/commit/9d379e11a6e9d8b491ce7143600495ae101e8c69.
Code
fn main() {
let x = vec![1];
x[0i32]; //~ ERROR E0277
}
Current output
error[E0277]: the type `[{integer}]` cannot be indexed by `i32`
--> tests/ui/indexing/index-help.rs:3:7
|
LL | x[0i32];
| ^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[{integer}]>` is not implemented for `i32`
= help: the following other types implement trait `SliceIndex<T>`:
`usize` implements `SliceIndex<ByteStr>`
`usize` implements `SliceIndex<[T]>`
= note: required for `Vec<{integer}>` to implement `Index<i32>`
Desired output
This was the output before that PR:
error[E0277]: the type `[{integer}]` cannot be indexed by `i32`
--> tests/ui/indexing/index-help.rs:3:7
|
LL | x[0i32];
| ^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `SliceIndex<[{integer}]>` is not implemented for `i32`
but it is implemented for `usize`
= help: for that trait implementation, expected `usize`, found `i32`
= note: required for `Vec<{integer}>` to implement `Index<i32>`
Rust Version
rustc 1.88.0 (56ffb4362 2025-04-05)
commit-hash: 56ffb43629bf58996c367073a0fa19e7d422df19
commit-date: 2025-04-05 10:18:03 +0200
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with tests/ui/indexing/index-help.rs and reproduce the E0277 diagnostic using the code in the issue. Compare the current and desired output, then trace the compiler diagnostic logic involved in SliceIndex suggestions; done means the test produces the desired suggestion without leaking ByteStr into stable diagnostics.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100