about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs4
-rw-r--r--tests/ui/array-slice-vec/repeat_empty_ok.stderr4
-rw-r--r--tests/ui/const-generics/issues/issue-61336-2.stderr2
-rw-r--r--tests/ui/const-generics/issues/issue-61336.stderr2
-rw-r--r--tests/ui/consts/const-blocks/migrate-fail.stderr4
-rw-r--r--tests/ui/consts/const-blocks/nll-fail.stderr4
-rw-r--r--tests/ui/repeat-expr/repeat-to-run-dtor-twice.stderr2
-rw-r--r--tests/ui/trait-bounds/issue-119530-sugg-from-fn.rs5
-rw-r--r--tests/ui/trait-bounds/issue-119530-sugg-from-fn.stderr13
9 files changed, 40 insertions, 0 deletions
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 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
                         ],
                         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<T: std::marker::Copy, const N: usize>(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<T: std::marker::Copy, const N: usize>(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<Bar>; 2] = [x; 2];
    |
    = note: required for `Option<Bar>` 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<Bar>; 2] = [x; 2];
    |
    = note: required for `Option<Bar>` 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<Bar>; 2] = [x; 2];
    |
    = note: required for `Option<Bar>` 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<Bar>; 2] = [x; 2];
    |
    = note: required for `Option<Bar>` 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`.