Skip to content

Make the error generated by using tuple indexing on an array more specific. #53712

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

Closed
Tom1380 opened this issue Aug 25, 2018 · 3 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

Comments

@Tom1380
Copy link

Tom1380 commented Aug 25, 2018

Attempting to compile this:

let a = [100, 100];
a.0;

will give this error:

error[E0609]: no field `0` on type `[{integer}; 2]`

I was thinking that a clearer error could be something along the lines of:

error[E----]: attempting to use tuple indexing on an array.

and give this hint:

help: change this to `a[0]`
@varkor varkor added A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. labels Aug 25, 2018
@varkor
Copy link
Member

varkor commented Aug 25, 2018

This should be straightforward to add a help message for. This error message is emitted here:
https://github.com/rust-lang/rust/blob/352b852f5b199dad36b5dc62a24e9bad4c18a671/src/librustc_typeck/check/mod.rs#L3318-L3320
The match immediately afterwards is responsible for giving suggestions relevant to the particular type. You'd need to add a new case for ty::Array (defined here) and emit an appropriate message (there are examples for other types in the match expression), if the field is numeric.

If anyone'd like to tackle this, just post a reply here — I can give more details as needed (but it's always useful to try working things out yourself first, to get a feel for where to find things).

@Tom1380
Copy link
Author

Tom1380 commented Aug 25, 2018

I don't have any experience with Rustc's codebase, but I would be willing to give it a try.

@varkor
Copy link
Member

varkor commented Aug 25, 2018

@Tom1380: great! If you need any help, you can post a comment here, or ask on Discord or #rustc. Once you add the help message, you'll need to add a test for it (there are plenty of examples in src/test/ui — you can run ./x.py test [filename] --bless to test and produce the output).

kennytm added a commit to kennytm/rust that referenced this issue Sep 20, 2018
Suggest array indexing when tuple indexing on an array

Closes rust-lang#53712

r? @varkor
cc @estebank
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 E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
Projects
None yet
Development

No branches or pull requests

2 participants