diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index aa27fe528e1fd..a38bd505da9e7 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -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::() + ) { + 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); diff --git a/src/test/ui/issues/issue-53712.rs b/src/test/ui/issues/issue-53712.rs new file mode 100644 index 0000000000000..2353904d79d75 --- /dev/null +++ b/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 instead of using tuple indexing, use array indexing + //~| SUGGESTION arr[0] +} diff --git a/src/test/ui/issues/issue-53712.stderr b/src/test/ui/issues/issue-53712.stderr new file mode 100644 index 0000000000000..db85919afcb55 --- /dev/null +++ b/src/test/ui/issues/issue-53712.stderr @@ -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`.