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

Improve help for const-able values used in array initialization #113912

Closed
ilyvion opened this issue Jul 21, 2023 · 2 comments · Fixed by #113925
Closed

Improve help for const-able values used in array initialization #113912

ilyvion opened this issue Jul 21, 2023 · 2 comments · Fixed by #113925
Assignees
Labels
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.

Comments

@ilyvion
Copy link

ilyvion commented Jul 21, 2023

Code

fn main() {
    let x = [None::<String>(); 10];
}

Current output

error[E0277]: the trait bound `String: Copy` is not satisfied
 --> src/main.rs:2:14
  |
2 |     let x = [None::<String>; 10];
  |              ^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
  |
  = note: required for `Option<String>` to implement `Copy`
  = note: the `Copy` trait is required because this value will be copied for each element of the array

Desired output

error[E0277]: the trait bound `String: Copy` is not satisfied
 --> src/main.rs:2:14
  |
2 |     let x = [None::<String>; 10];
  |              ^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
  |
  = note: required for `Option<String>` to implement `Copy`
  = note: the `Copy` trait is required because this value will be copied for each element of the array
  = help: consider creating a new `const` item and initializing it with the value to be used in the repeat position, like `const VAL: Type = value;` and `let x = [VAL; 42];`

Rationale and extra context

I'm basing this suggestion on two things. One, that if you follow it, the code compiles:

fn main() {
    const NONE: Option<String> = None;
    let x = [NONE; 10];
}

and two, that the diagnostic help you get when you try to use a const fn in the repeat position suggests something very similar, see "other cases." It would require the compiler to know that a value that is attempted to be repeated is constable though, which I don't know whether it can, but I figured I'd suggest this, and you can reject it if it isn't possible to do.

Alternatively, the help text could say something like "if the value can be made const, consider creating [...]"

Other cases

I was inspired to make this issue based on the output you get when you try to use a const fn as an array initializer. For instance, the code

fn main() {
    let x = [none::<String>(); 10];
}

const fn none<T>() -> Option<T> {
    None
}

produces the output

error[E0277]: the trait bound `String: Copy` is not satisfied
 --> src/main.rs:2:14
  |
2 |     let x = [none::<String>(); 10];
  |              ^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
  |
  = note: required for `Option<String>` to implement `Copy`
  = note: the `Copy` trait is required because this value will be copied for each element of the array
  = help: consider creating a new `const` item and initializing it with the result of the function call to be used in the repeat position, like `const VAL: Type = const_fn();` and `let x = [VAL; 42];`

Anything else?

No response

@ilyvion ilyvion 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 Jul 21, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 21, 2023
@chenyukang chenyukang added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. and removed E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. labels Jul 21, 2023
@AnuragNishad12

This comment was marked as off-topic.

@lcnr
Copy link
Contributor

lcnr commented Jul 21, 2023

@AnuragNishad12 I don't think your comment is related to this issue? If this is a problem you've encountered while trying to fix this issue, please reach out on zulip instead.

@Noratrieb Noratrieb removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 30, 2023
@bors bors closed this as completed in b724d9c Nov 8, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 8, 2023
Rollup merge of rust-lang#113925 - clubby789:const-ctor-repeat, r=estebank

Improve diagnostic for const ctors in array repeat expressions

Fixes rust-lang#113912
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 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.

7 participants