Skip to content

Commit fe6eb6a

Browse files
committed
Suggest arry::from_fn for array initialization
1 parent e927184 commit fe6eb6a

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -3144,6 +3144,10 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
31443144
],
31453145
Applicability::MachineApplicable,
31463146
);
3147+
} else {
3148+
// FIXME: we may suggest array::repeat instead
3149+
err.help("consider using `core::array::from_fn` to initialize the array");
3150+
err.help("see https://doc.rust-lang.org/stable/std/array/fn.from_fn.html# for more information");
31473151
}
31483152

31493153
if self.tcx.sess.is_nightly_build()
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: considering 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)