Skip to content

Commit

Permalink
Suggest arry::from_fn for array initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Jan 12, 2024
1 parent e927184 commit fe6eb6a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3144,6 +3144,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()
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/trait-bounds/issue-119530-sugg-from-fn.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions tests/ui/trait-bounds/issue-119530-sugg-from-fn.stderr
Original file line number Diff line number Diff line change
@@ -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: considering 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`.

0 comments on commit fe6eb6a

Please sign in to comment.