Skip to content

Commit 30556d5

Browse files
committed
Suggest array indexing when tuple indexing on an array.
1 parent 0c64789 commit 30556d5

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3344,6 +3344,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
33443344
}
33453345
};
33463346
}
3347+
ty::Array(ty, _) if ty.is_numeric() => {
3348+
let base = self.tcx.hir.node_to_pretty_string(base.id);
3349+
let msg = format!("attempting to use tuple indexing on an array; try");
3350+
let suggestion = format!("{}[{}]", base, field);
3351+
err.span_suggestion(field.span, &msg, suggestion);
3352+
},
33473353
ty::RawPtr(..) => {
33483354
let base = self.tcx.hir.node_to_pretty_string(base.id);
33493355
let msg = format!("`{}` is a native pointer; try dereferencing it", base);

src/test/ui/issues/issue-53712.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// issue #53712: make the error generated by using tuple indexing on an array more specific
2+
3+
fn main() {
4+
let arr = [10, 20, 30, 40, 50];
5+
arr.0;
6+
//~^ ERROR no field `0` on type `[{integer}; 5]` [E0609]
7+
//~| HELP attempting to use tuple indexing on an array; try
8+
//~| SUGGESTION arr[0]
9+
}

src/test/ui/issues/issue-53712.stderr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0609]: no field `0` on type `[{integer}; 5]`
2+
--> $DIR/issue-53712.rs:5:9
3+
|
4+
LL | arr.0;
5+
| ^ help: attempting to use tuple indexing on an array; try: `arr[0]`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0609`.

0 commit comments

Comments
 (0)