Suggest array indexing when tuple indexing on an array.

This commit is contained in:
memoryruins 2018-09-17 09:09:45 -04:00
parent 0c6478998e
commit 30556d592e
3 changed files with 24 additions and 0 deletions

View file

@ -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);

View file

@ -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]
}

View file

@ -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`.