rust-lang / rust-lang/rust

Fix suggestion to deference reference index

Open
#162,419 0 comments 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

Code
fn main() {
    let i = 0 as usize;
    let arr = [false];
    println!("{}", arr[&i]);
}
Current output
--> src/main.rs:4:24
    |
  4 |     println!("{}", arr[&i]);
    |                        ^^ slice indices are of type `usize` or ranges of `usize`
    |
    = help: the trait `SliceIndex<[bool]>` is not implemented for `&usize`
help: `usize` implements trait `SliceIndex<T>`
   --> /home/moosama76/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/slice/index.rs:179:1
    |
179 | const unsafe impl<T> SliceIndex<[T]> for usize {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `SliceIndex<[T]>`
    |
   ::: /home/moosama76/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/bstr/traits.rs:197:1
    |
197 | unsafe impl SliceIndex<ByteStr> for usize {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `SliceIndex<ByteStr>`
    = note: required for `[bool]` to implement `Index<&usize>`
    = note: 1 redundant requirement hidden
    = note: required for `[bool; 2]` to implement `Index<&usize>`
help: dereference this index
    |
  4 |     println!("{}", arr[*&i]);
    |                        +
Desired output
help: remove the reference, 
  |
4 |     println!("{}", arr[&i]);
    |                      -
Rationale and extra context

I want to improve this diagnostic

Other cases

Rust Version
rustc 1.98.1 (48a229cea 2026-09-01)
binary: rustc
commit-hash: 48a229ceaefd4985c50990b14116b6d856af0985
commit-date: 2026-09-01
host: x86_64-unknown-linux-gnu
release: 1.98.1
LLVM version: 22.1.8
Anything else?

No response

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

Reproduce the example in src/main.rs with rustc 1.98.1 and compare the current indexing diagnostic with the desired output. Trace the compiler diagnostic handling for the reference index case; the work is done when it suggests removing the reference and shows the requested source replacement.

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
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.