From 3ed96e35c422941535cf0b19e5207572c8f38157 Mon Sep 17 00:00:00 2001 From: yukang Date: Fri, 5 Jan 2024 10:25:20 +0800 Subject: [PATCH] Suggest arry::from_fn for array initialization --- .../src/traits/error_reporting/suggestions.rs | 4 ++++ tests/ui/array-slice-vec/repeat_empty_ok.stderr | 4 ++++ tests/ui/const-generics/issues/issue-61336-2.stderr | 2 ++ tests/ui/const-generics/issues/issue-61336.stderr | 2 ++ tests/ui/consts/const-blocks/migrate-fail.stderr | 4 ++++ tests/ui/consts/const-blocks/nll-fail.stderr | 4 ++++ .../ui/repeat-expr/repeat-to-run-dtor-twice.stderr | 2 ++ tests/ui/trait-bounds/issue-119530-sugg-from-fn.rs | 5 +++++ .../trait-bounds/issue-119530-sugg-from-fn.stderr | 13 +++++++++++++ 9 files changed, 40 insertions(+) create mode 100644 tests/ui/trait-bounds/issue-119530-sugg-from-fn.rs create mode 100644 tests/ui/trait-bounds/issue-119530-sugg-from-fn.stderr diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 0e33e9cd790..e31aaaa1969 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -3152,6 +3152,10 @@ fn note_obligation_cause_code( ], Applicability::MachineApplicable, ); + } else { + // FIXME: we may suggest array::repeat instead + err.help("consider using `core::array::from_fn` to initialize the array"); + err.help("see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information"); } if self.tcx.sess.is_nightly_build() diff --git a/tests/ui/array-slice-vec/repeat_empty_ok.stderr b/tests/ui/array-slice-vec/repeat_empty_ok.stderr index e8bac04ac45..bc3a68c905d 100644 --- a/tests/ui/array-slice-vec/repeat_empty_ok.stderr +++ b/tests/ui/array-slice-vec/repeat_empty_ok.stderr @@ -5,6 +5,8 @@ LL | let headers = [Header{value: &[]}; 128]; | ^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `Header<'_>` | = note: the `Copy` trait is required because this value will be copied for each element of the array + = help: consider using `core::array::from_fn` to initialize the array + = help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information help: consider annotating `Header<'_>` with `#[derive(Copy)]` | LL + #[derive(Copy)] @@ -18,6 +20,8 @@ LL | let headers = [Header{value: &[0]}; 128]; | ^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `Header<'_>` | = note: the `Copy` trait is required because this value will be copied for each element of the array + = help: consider using `core::array::from_fn` to initialize the array + = help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information help: consider annotating `Header<'_>` with `#[derive(Copy)]` | LL + #[derive(Copy)] diff --git a/tests/ui/const-generics/issues/issue-61336-2.stderr b/tests/ui/const-generics/issues/issue-61336-2.stderr index 0af6f87a68d..9064c2d0b94 100644 --- a/tests/ui/const-generics/issues/issue-61336-2.stderr +++ b/tests/ui/const-generics/issues/issue-61336-2.stderr @@ -5,6 +5,8 @@ LL | [x; { N }] | ^ the trait `Copy` is not implemented for `T` | = note: the `Copy` trait is required because this value will be copied for each element of the array + = help: consider using `core::array::from_fn` to initialize the array + = help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information help: consider restricting type parameter `T` | LL | fn g(x: T) -> [T; N] { diff --git a/tests/ui/const-generics/issues/issue-61336.stderr b/tests/ui/const-generics/issues/issue-61336.stderr index 4132e511240..9935d6c1689 100644 --- a/tests/ui/const-generics/issues/issue-61336.stderr +++ b/tests/ui/const-generics/issues/issue-61336.stderr @@ -5,6 +5,8 @@ LL | [x; N] | ^ the trait `Copy` is not implemented for `T` | = note: the `Copy` trait is required because this value will be copied for each element of the array + = help: consider using `core::array::from_fn` to initialize the array + = help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information help: consider restricting type parameter `T` | LL | fn g(x: T) -> [T; N] { diff --git a/tests/ui/consts/const-blocks/migrate-fail.stderr b/tests/ui/consts/const-blocks/migrate-fail.stderr index d1896f755d5..95fece0ae8a 100644 --- a/tests/ui/consts/const-blocks/migrate-fail.stderr +++ b/tests/ui/consts/const-blocks/migrate-fail.stderr @@ -6,6 +6,8 @@ LL | let arr: [Option; 2] = [x; 2]; | = note: required for `Option` to implement `Copy` = note: the `Copy` trait is required because this value will be copied for each element of the array + = help: consider using `core::array::from_fn` to initialize the array + = help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information help: consider annotating `Bar` with `#[derive(Copy)]` | LL + #[derive(Copy)] @@ -20,6 +22,8 @@ LL | let arr: [Option; 2] = [x; 2]; | = note: required for `Option` to implement `Copy` = note: the `Copy` trait is required because this value will be copied for each element of the array + = help: consider using `core::array::from_fn` to initialize the array + = help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information help: consider annotating `Bar` with `#[derive(Copy)]` | LL + #[derive(Copy)] diff --git a/tests/ui/consts/const-blocks/nll-fail.stderr b/tests/ui/consts/const-blocks/nll-fail.stderr index 807c964a51d..ed1dc78f77d 100644 --- a/tests/ui/consts/const-blocks/nll-fail.stderr +++ b/tests/ui/consts/const-blocks/nll-fail.stderr @@ -6,6 +6,8 @@ LL | let arr: [Option; 2] = [x; 2]; | = note: required for `Option` to implement `Copy` = note: the `Copy` trait is required because this value will be copied for each element of the array + = help: consider using `core::array::from_fn` to initialize the array + = help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information help: consider annotating `Bar` with `#[derive(Copy)]` | LL + #[derive(Copy)] @@ -20,6 +22,8 @@ LL | let arr: [Option; 2] = [x; 2]; | = note: required for `Option` to implement `Copy` = note: the `Copy` trait is required because this value will be copied for each element of the array + = help: consider using `core::array::from_fn` to initialize the array + = help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information help: consider annotating `Bar` with `#[derive(Copy)]` | LL + #[derive(Copy)] diff --git a/tests/ui/repeat-expr/repeat-to-run-dtor-twice.stderr b/tests/ui/repeat-expr/repeat-to-run-dtor-twice.stderr index 2e3ede46eca..f3fe8c10c02 100644 --- a/tests/ui/repeat-expr/repeat-to-run-dtor-twice.stderr +++ b/tests/ui/repeat-expr/repeat-to-run-dtor-twice.stderr @@ -5,6 +5,8 @@ LL | let _ = [ a; 5 ]; | ^ the trait `Copy` is not implemented for `Foo` | = note: the `Copy` trait is required because this value will be copied for each element of the array + = help: consider using `core::array::from_fn` to initialize the array + = help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information help: consider annotating `Foo` with `#[derive(Copy)]` | LL + #[derive(Copy)] diff --git a/tests/ui/trait-bounds/issue-119530-sugg-from-fn.rs b/tests/ui/trait-bounds/issue-119530-sugg-from-fn.rs new file mode 100644 index 00000000000..cfe378f55b1 --- /dev/null +++ b/tests/ui/trait-bounds/issue-119530-sugg-from-fn.rs @@ -0,0 +1,5 @@ +fn foo() -> String { String::new() } + +fn main() { + let string_arr = [foo(); 64]; //~ ERROR the trait bound `String: Copy` is not satisfied +} diff --git a/tests/ui/trait-bounds/issue-119530-sugg-from-fn.stderr b/tests/ui/trait-bounds/issue-119530-sugg-from-fn.stderr new file mode 100644 index 00000000000..f394c4cf027 --- /dev/null +++ b/tests/ui/trait-bounds/issue-119530-sugg-from-fn.stderr @@ -0,0 +1,13 @@ +error[E0277]: the trait bound `String: Copy` is not satisfied + --> $DIR/issue-119530-sugg-from-fn.rs:4:23 + | +LL | let string_arr = [foo(); 64]; + | ^^^^^ the trait `Copy` is not implemented for `String` + | + = note: the `Copy` trait is required because this value will be copied for each element of the array + = help: consider using `core::array::from_fn` to initialize the array + = help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0277`.