Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

diagnostics: suggest dereferencing when trying to use a reference as index #96678

Closed
matthiaskrgr opened this issue May 3, 2022 · 2 comments · Fixed by #99671
Closed

diagnostics: suggest dereferencing when trying to use a reference as index #96678

matthiaskrgr opened this issue May 3, 2022 · 2 comments · Fixed by #99671
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented May 3, 2022

Given the following code:

fn main() {
    let i: &usize = &1;
    let one_item_please: i32 = [1, 2, 3][i];
}

The current output is:

error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the type `[{integer}]` cannot be indexed by `&usize`
--> src/main.rs:3:32 |
3 |     let one_item_please: i32 = [1, 2, 3][i];
  |                                ^^^^^^^^^^^^ slice indices are of type `usize` or ranges of `usize`
  |
  = help: the trait `SliceIndex<[{integer}]>` is not implemented for `&usize`
  = note: required because of the requirements on the impl of `Index<&usize>` for `[{integer}]`

For more information about this error, try `rustc --explain E0277`.

Ideally the output should look like:
It would be great if rustc could suggest dereferencing: [i] -> [*i] which fixes the build

@matthiaskrgr matthiaskrgr added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 3, 2022
@TaKO8Ki TaKO8Ki self-assigned this May 4, 2022
@compiler-errors
Copy link
Member

This got reported again in #99125, @TaKO8Ki are you still working on this?

@TaKO8Ki
Copy link
Member

TaKO8Ki commented Jul 11, 2022

@compiler-errors I have recently started working on it again and will create a pull request as soon as possible!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants