Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest array::from_fn in array initialization #119530

Closed
est31 opened this issue Jan 2, 2024 · 4 comments · Fixed by #119805
Closed

Suggest array::from_fn in array initialization #119530

est31 opened this issue Jan 2, 2024 · 4 comments · Fixed by #119805
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-help-wanted Call for participation: Help is requested to fix this issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@est31
Copy link
Member

est31 commented Jan 2, 2024

For this snippet (playground):

fn foo() -> String { String::new() }

fn main() {
    let string_arr = [foo(); 64];
}

rustc gives:

error[E0277]: the trait bound `String: Copy` is not satisfied
 --> src/main.rs:3:23
  |
3 |     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

Since #113912, for const-able values, rustc suggests the creation of a constant binding. Here this is not possible because foo is not const fn. However, we can suggest array::from_fn instead.

@est31 est31 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 2, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 2, 2024
@est31
Copy link
Member Author

est31 commented Jan 2, 2024

Personally, I prefer array::from_fn over a separate const binding. Even in the const case, the generated code will call the initialization function many times. Maybe in the case where const bindings are possible, we can suggest both? Where they are not possible, we don't suggest anything right now, so at the least we should suggest array::from_fn there.

@Nilstrieb @lcnr what do you think?

@Noratrieb Noratrieb removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 2, 2024
@Noratrieb
Copy link
Member

Once inline_const is stable we can suggest that :3. But until then array::from_fn sounds reasonable.

@est31
Copy link
Member Author

est31 commented Jan 2, 2024

Agreed, good point about inline_const. It seems to have a stabilization PR open with an FCP, which is blocked: #104087 (comment) . Maybe there might be progress on it this month: #104087 (comment) .

Even if we stabilize inline const, it might make sense to wait with suggesting it for a while so that we don't make code depend on the latest MSRV immediately. So therefore, I think that it might still make sense to implement this in the meantime.

@est31 est31 added the E-help-wanted Call for participation: Help is requested to fix this issue. label Jan 2, 2024
@chenyukang chenyukang self-assigned this Jan 5, 2024
@joboet
Copy link
Member

joboet commented Jan 5, 2024

I think this should (in the future) suggest array::repeat instead, since the function would only be called once if String were Copy, whereas from_fn calls it for every element.

fmease added a commit to fmease/rust that referenced this issue Jan 23, 2024
…vidtwco

Suggest array::from_fn for array initialization

Fixes rust-lang#119530
@bors bors closed this as completed in dd538b5 Jan 23, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 23, 2024
Rollup merge of rust-lang#119805 - chenyukang:yukang-fix-119530, r=davidtwco

Suggest array::from_fn for array initialization

Fixes rust-lang#119530
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-help-wanted Call for participation: Help is requested to fix this issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants