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 all commits
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
18 changes: 18 additions & 0 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3344,6 +3344,24 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
};
}
ty::Array(_, len) => {
if let (Some(len), Ok(user_index)) = (
len.assert_usize(self.tcx),
field.as_str().parse::<u64>()
) {
let base = self.tcx.hir.node_to_pretty_string(base.id);
let help = "instead of using tuple indexing, use array indexing";
let suggestion = format!("{}[{}]", base, field);
let applicability = if len < user_index {
Applicability::MachineApplicable
} else {
Applicability::MaybeIncorrect
};
err.span_suggestion_with_applicability(
expr.span, help, suggestion, applicability
);
}
}
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 instead of using tuple indexing, use array indexing
//~| SUGGESTION arr[0]
}
11 changes: 11 additions & 0 deletions src/test/ui/issues/issue-53712.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0609]: no field `0` on type `[{integer}; 5]`
--> $DIR/issue-53712.rs:5:9
|
LL | arr.0;
| ----^
| |
| help: instead of using tuple indexing, use array indexing: `arr[0]`

error: aborting due to previous error

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