Skip to content

Commit dd538b5

Browse files
authored
Rollup merge of #119805 - chenyukang:yukang-fix-119530, r=davidtwco
Suggest array::from_fn for array initialization Fixes #119530
2 parents e8f9e54 + 3ed96e3 commit dd538b5

File tree

9 files changed

+40
-0
lines changed

9 files changed

+40
-0
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+4
Original file line numberDiff line numberDiff line change
@@ -3152,6 +3152,10 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
31523152
],
31533153
Applicability::MachineApplicable,
31543154
);
3155+
} else {
3156+
// FIXME: we may suggest array::repeat instead
3157+
err.help("consider using `core::array::from_fn` to initialize the array");
3158+
err.help("see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information");
31553159
}
31563160

31573161
if self.tcx.sess.is_nightly_build()

tests/ui/array-slice-vec/repeat_empty_ok.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | let headers = [Header{value: &[]}; 128];
55
| ^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `Header<'_>`
66
|
77
= note: the `Copy` trait is required because this value will be copied for each element of the array
8+
= help: consider using `core::array::from_fn` to initialize the array
9+
= help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information
810
help: consider annotating `Header<'_>` with `#[derive(Copy)]`
911
|
1012
LL + #[derive(Copy)]
@@ -18,6 +20,8 @@ LL | let headers = [Header{value: &[0]}; 128];
1820
| ^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `Header<'_>`
1921
|
2022
= note: the `Copy` trait is required because this value will be copied for each element of the array
23+
= help: consider using `core::array::from_fn` to initialize the array
24+
= help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information
2125
help: consider annotating `Header<'_>` with `#[derive(Copy)]`
2226
|
2327
LL + #[derive(Copy)]

tests/ui/const-generics/issues/issue-61336-2.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | [x; { N }]
55
| ^ the trait `Copy` is not implemented for `T`
66
|
77
= note: the `Copy` trait is required because this value will be copied for each element of the array
8+
= help: consider using `core::array::from_fn` to initialize the array
9+
= help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information
810
help: consider restricting type parameter `T`
911
|
1012
LL | fn g<T: std::marker::Copy, const N: usize>(x: T) -> [T; N] {

tests/ui/const-generics/issues/issue-61336.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | [x; N]
55
| ^ the trait `Copy` is not implemented for `T`
66
|
77
= note: the `Copy` trait is required because this value will be copied for each element of the array
8+
= help: consider using `core::array::from_fn` to initialize the array
9+
= help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information
810
help: consider restricting type parameter `T`
911
|
1012
LL | fn g<T: std::marker::Copy, const N: usize>(x: T) -> [T; N] {

tests/ui/consts/const-blocks/migrate-fail.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LL | let arr: [Option<Bar>; 2] = [x; 2];
66
|
77
= note: required for `Option<Bar>` to implement `Copy`
88
= note: the `Copy` trait is required because this value will be copied for each element of the array
9+
= help: consider using `core::array::from_fn` to initialize the array
10+
= help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information
911
help: consider annotating `Bar` with `#[derive(Copy)]`
1012
|
1113
LL + #[derive(Copy)]
@@ -20,6 +22,8 @@ LL | let arr: [Option<Bar>; 2] = [x; 2];
2022
|
2123
= note: required for `Option<Bar>` to implement `Copy`
2224
= note: the `Copy` trait is required because this value will be copied for each element of the array
25+
= help: consider using `core::array::from_fn` to initialize the array
26+
= help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information
2327
help: consider annotating `Bar` with `#[derive(Copy)]`
2428
|
2529
LL + #[derive(Copy)]

tests/ui/consts/const-blocks/nll-fail.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LL | let arr: [Option<Bar>; 2] = [x; 2];
66
|
77
= note: required for `Option<Bar>` to implement `Copy`
88
= note: the `Copy` trait is required because this value will be copied for each element of the array
9+
= help: consider using `core::array::from_fn` to initialize the array
10+
= help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information
911
help: consider annotating `Bar` with `#[derive(Copy)]`
1012
|
1113
LL + #[derive(Copy)]
@@ -20,6 +22,8 @@ LL | let arr: [Option<Bar>; 2] = [x; 2];
2022
|
2123
= note: required for `Option<Bar>` to implement `Copy`
2224
= note: the `Copy` trait is required because this value will be copied for each element of the array
25+
= help: consider using `core::array::from_fn` to initialize the array
26+
= help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information
2327
help: consider annotating `Bar` with `#[derive(Copy)]`
2428
|
2529
LL + #[derive(Copy)]

tests/ui/repeat-expr/repeat-to-run-dtor-twice.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | let _ = [ a; 5 ];
55
| ^ the trait `Copy` is not implemented for `Foo`
66
|
77
= note: the `Copy` trait is required because this value will be copied for each element of the array
8+
= help: consider using `core::array::from_fn` to initialize the array
9+
= help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information
810
help: consider annotating `Foo` with `#[derive(Copy)]`
911
|
1012
LL + #[derive(Copy)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn foo() -> String { String::new() }
2+
3+
fn main() {
4+
let string_arr = [foo(); 64]; //~ ERROR the trait bound `String: Copy` is not satisfied
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0277]: the trait bound `String: Copy` is not satisfied
2+
--> $DIR/issue-119530-sugg-from-fn.rs:4:23
3+
|
4+
LL | let string_arr = [foo(); 64];
5+
| ^^^^^ the trait `Copy` is not implemented for `String`
6+
|
7+
= note: the `Copy` trait is required because this value will be copied for each element of the array
8+
= help: consider using `core::array::from_fn` to initialize the array
9+
= help: see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information
10+
11+
error: aborting due to 1 previous error
12+
13+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)