rust-lang / rust-lang/rust

Suggests changing slice-like type instead of index for invalid `SliceIndex`

Open
#139,424 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.