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

Suggest array indexing when tuple indexing on an array #54292

Merged
merged 3 commits into from
Sep 21, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3344,6 +3344,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
};
}
ty::Array(ty, _) if ty.is_numeric() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if you can get the numeric value of field and check wether it is smaller than ty::Array(_, len). That would give us a great degree of confidence that the suggestion is correct, we could even switch between Applicability levels given that... 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason to limit this suggestion to only [{integer}; _] and [{float}; _]? I'm not against it, as being conservative in suggestions is not bad, I just want to hear what was your reasoning.

let base = self.tcx.hir.node_to_pretty_string(base.id);
let msg = format!("attempting to use tuple indexing on an array; try");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change the wording to something along the lines of instead of using tuple indexing use array indexing.

let suggestion = format!("{}[{}]", base, field);
err.span_suggestion(field.span, &msg, suggestion);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use span_suggestion_with_applicability and Applicability::MaybeIncorrect.

},
ty::RawPtr(..) => {
let base = self.tcx.hir.node_to_pretty_string(base.id);
let msg = format!("`{}` is a native pointer; try dereferencing it", base);
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/issues/issue-53712.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// issue #53712: make the error generated by using tuple indexing on an array more specific

fn main() {
let arr = [10, 20, 30, 40, 50];
arr.0;
//~^ ERROR no field `0` on type `[{integer}; 5]` [E0609]
//~| HELP attempting to use tuple indexing on an array; try
//~| SUGGESTION arr[0]
}
9 changes: 9 additions & 0 deletions src/test/ui/issues/issue-53712.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0609]: no field `0` on type `[{integer}; 5]`
--> $DIR/issue-53712.rs:5:9
|
LL | arr.0;
| ^ help: attempting to use tuple indexing on an array; try: `arr[0]`

error: aborting due to previous error

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