From 30556d592e5f2702fd578d289bed7a7a4facbf98 Mon Sep 17 00:00:00 2001 From: memoryruins Date: Mon, 17 Sep 2018 09:09:45 -0400 Subject: [PATCH 1/3] Suggest array indexing when tuple indexing on an array. --- src/librustc_typeck/check/mod.rs | 6 ++++++ src/test/ui/issues/issue-53712.rs | 9 +++++++++ src/test/ui/issues/issue-53712.stderr | 9 +++++++++ 3 files changed, 24 insertions(+) create mode 100644 src/test/ui/issues/issue-53712.rs create mode 100644 src/test/ui/issues/issue-53712.stderr diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index aa27fe528e1fd..89ab085b03481 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -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); diff --git a/src/test/ui/issues/issue-53712.rs b/src/test/ui/issues/issue-53712.rs new file mode 100644 index 0000000000000..c8b54c2d4beb4 --- /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 attempting to use tuple indexing on an array; try + //~| 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..ef885a438c827 --- /dev/null +++ b/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`. From dff1bc1260e5c1c13be20b5402d1b4b783cc1218 Mon Sep 17 00:00:00 2001 From: memoryruins Date: Tue, 18 Sep 2018 14:41:34 -0400 Subject: [PATCH 2/3] Set diagnostic applicability based on array length --- src/librustc_typeck/check/mod.rs | 24 ++++++++++++++++++------ src/test/ui/issues/issue-53712.rs | 2 +- src/test/ui/issues/issue-53712.stderr | 2 +- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 89ab085b03481..f8628801d2519 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3344,12 +3344,24 @@ 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::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( + field.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 index c8b54c2d4beb4..2353904d79d75 100644 --- a/src/test/ui/issues/issue-53712.rs +++ b/src/test/ui/issues/issue-53712.rs @@ -4,6 +4,6 @@ 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 + //~| 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 index ef885a438c827..06c19e19d5229 100644 --- a/src/test/ui/issues/issue-53712.stderr +++ b/src/test/ui/issues/issue-53712.stderr @@ -2,7 +2,7 @@ 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]` + | ^ help: instead of using tuple indexing, use array indexing: `arr[0]` error: aborting due to previous error From 73fdc81b39138d3526c3629e7d83f581901dce58 Mon Sep 17 00:00:00 2001 From: memoryruins Date: Tue, 18 Sep 2018 15:35:04 -0400 Subject: [PATCH 3/3] Use expr's span --- src/librustc_typeck/check/mod.rs | 2 +- src/test/ui/issues/issue-53712.stderr | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index f8628801d2519..a38bd505da9e7 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3358,7 +3358,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { Applicability::MaybeIncorrect }; err.span_suggestion_with_applicability( - field.span, help, suggestion, applicability + expr.span, help, suggestion, applicability ); } } diff --git a/src/test/ui/issues/issue-53712.stderr b/src/test/ui/issues/issue-53712.stderr index 06c19e19d5229..db85919afcb55 100644 --- a/src/test/ui/issues/issue-53712.stderr +++ b/src/test/ui/issues/issue-53712.stderr @@ -2,7 +2,9 @@ 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]` + | ----^ + | | + | help: instead of using tuple indexing, use array indexing: `arr[0]` error: aborting due to previous error