Suggest arry::from_fn for array initialization
This commit is contained in:
parent
4cb17b4e78
commit
3ed96e35c4
9 changed files with 40 additions and 0 deletions
|
@ -3152,6 +3152,10 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
||||||
],
|
],
|
||||||
Applicability::MachineApplicable,
|
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()
|
if self.tcx.sess.is_nightly_build()
|
||||||
|
|
|
@ -5,6 +5,8 @@ LL | let headers = [Header{value: &[]}; 128];
|
||||||
| ^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `Header<'_>`
|
| ^^^^^^^^^^^^^^^^^^ 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
|
= 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)]`
|
help: consider annotating `Header<'_>` with `#[derive(Copy)]`
|
||||||
|
|
|
|
||||||
LL + #[derive(Copy)]
|
LL + #[derive(Copy)]
|
||||||
|
@ -18,6 +20,8 @@ LL | let headers = [Header{value: &[0]}; 128];
|
||||||
| ^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `Header<'_>`
|
| ^^^^^^^^^^^^^^^^^^^ 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
|
= 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)]`
|
help: consider annotating `Header<'_>` with `#[derive(Copy)]`
|
||||||
|
|
|
|
||||||
LL + #[derive(Copy)]
|
LL + #[derive(Copy)]
|
||||||
|
|
|
@ -5,6 +5,8 @@ LL | [x; { N }]
|
||||||
| ^ the trait `Copy` is not implemented for `T`
|
| ^ 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
|
= 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`
|
help: consider restricting type parameter `T`
|
||||||
|
|
|
|
||||||
LL | fn g<T: std::marker::Copy, const N: usize>(x: T) -> [T; N] {
|
LL | fn g<T: std::marker::Copy, const N: usize>(x: T) -> [T; N] {
|
||||||
|
|
|
@ -5,6 +5,8 @@ LL | [x; N]
|
||||||
| ^ the trait `Copy` is not implemented for `T`
|
| ^ 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
|
= 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`
|
help: consider restricting type parameter `T`
|
||||||
|
|
|
|
||||||
LL | fn g<T: std::marker::Copy, const N: usize>(x: T) -> [T; N] {
|
LL | fn g<T: std::marker::Copy, const N: usize>(x: T) -> [T; N] {
|
||||||
|
|
|
@ -6,6 +6,8 @@ LL | let arr: [Option<Bar>; 2] = [x; 2];
|
||||||
|
|
|
|
||||||
= note: required for `Option<Bar>` to implement `Copy`
|
= 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
|
= 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)]`
|
help: consider annotating `Bar` with `#[derive(Copy)]`
|
||||||
|
|
|
|
||||||
LL + #[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: 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
|
= 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)]`
|
help: consider annotating `Bar` with `#[derive(Copy)]`
|
||||||
|
|
|
|
||||||
LL + #[derive(Copy)]
|
LL + #[derive(Copy)]
|
||||||
|
|
|
@ -6,6 +6,8 @@ LL | let arr: [Option<Bar>; 2] = [x; 2];
|
||||||
|
|
|
|
||||||
= note: required for `Option<Bar>` to implement `Copy`
|
= 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
|
= 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)]`
|
help: consider annotating `Bar` with `#[derive(Copy)]`
|
||||||
|
|
|
|
||||||
LL + #[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: 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
|
= 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)]`
|
help: consider annotating `Bar` with `#[derive(Copy)]`
|
||||||
|
|
|
|
||||||
LL + #[derive(Copy)]
|
LL + #[derive(Copy)]
|
||||||
|
|
|
@ -5,6 +5,8 @@ LL | let _ = [ a; 5 ];
|
||||||
| ^ the trait `Copy` is not implemented for `Foo`
|
| ^ 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
|
= 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)]`
|
help: consider annotating `Foo` with `#[derive(Copy)]`
|
||||||
|
|
|
|
||||||
LL + #[derive(Copy)]
|
LL + #[derive(Copy)]
|
||||||
|
|
5
tests/ui/trait-bounds/issue-119530-sugg-from-fn.rs
Normal file
5
tests/ui/trait-bounds/issue-119530-sugg-from-fn.rs
Normal file
|
@ -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
|
||||||
|
}
|
13
tests/ui/trait-bounds/issue-119530-sugg-from-fn.stderr
Normal file
13
tests/ui/trait-bounds/issue-119530-sugg-from-fn.stderr
Normal file
|
@ -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`.
|
Loading…
Add table
Reference in a new issue