Skip to content

Commit

Permalink
Suggest array indexing when tuple indexing on an array.
Browse files Browse the repository at this point in the history
  • Loading branch information
memoryruins committed Sep 17, 2018
1 parent 0c64789 commit 30556d5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -3344,6 +3344,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
};
}
ty::Array(ty, _) if ty.is_numeric() => {
let base = self.tcx.hir.node_to_pretty_string(base.id);
let msg = format!("attempting to use tuple indexing on an array; try");
let suggestion = format!("{}[{}]", base, field);
err.span_suggestion(field.span, &msg, suggestion);
},
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
@@ -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
@@ -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`.

0 comments on commit 30556d5

Please sign in to comment.