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 creating a const when calling a const function in an array initializer #73734

Closed
Aaron1011 opened this issue Jun 25, 2020 · 7 comments · Fixed by #81503
Closed

Suggest creating a const when calling a const function in an array initializer #73734

Aaron1011 opened this issue Jun 25, 2020 · 7 comments · Fixed by #81503
Assignees
Labels
A-const-eval Area: constant evaluation (mir interpretation) A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Aaron1011
Copy link
Member

Aaron1011 commented Jun 25, 2020

The following code:

fn main() {
    let strs: [String; 5] = [String::new(); 5];
}

gives the following error:

error[E0277]: the trait bound `std::string::String: std::marker::Copy` is not satisfied
 --> src/main.rs:2:29
  |
2 |     let strs: [String; 5] = [String::new(); 5];
  |                             ^^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `std::string::String`
  |
  = note: the `Copy` trait is required because the repeated element will be copied

However, String::new() is a const fn, so this code can be made to compile by creating a new const item:

fn main() {
    const EMPTY: String = String::new();
    let strs: [String; 5] = [EMPTY; 5];
}

We should suggest this to user when a const function is being called in the array initializer.

Assuming that rust-lang/rfcs#2920 is accepted, we could eventually suggest an inline const block.
On Nightly, we could suggest an inline const block.

@Aaron1011 Aaron1011 added A-diagnostics Area: Messages for errors, warnings, and lints A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. A-const-eval Area: constant evaluation (mir interpretation) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Jun 25, 2020
@JohnTitor JohnTitor added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 26, 2020
@Aaron1011 Aaron1011 added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. labels Dec 5, 2020
@Aaron1011
Copy link
Member Author

I think this would make a good first issue for someone looking to get started contributing. I'd be happy to mentor anyone who wants to work on this.

@DavidHusicka
Copy link

@rustbot claim

@henryboisdequin
Copy link
Contributor

@Aaron1011 Is this still an issue that I can work on?

@Aaron1011
Copy link
Member Author

@henryboisdequin Yes, assuming that @DavidHusicka is no longer working on this.

@henryboisdequin
Copy link
Contributor

Should I claim it?

@Aaron1011
Copy link
Member Author

@henryboisdequin: It looks like DavidHusicka hasn't had any activity this month, so I think it would be fine for you to take the issue. Feel free to reach out to me if you have any questions about how to get started implementing this.

@henryboisdequin
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: constant evaluation (mir interpretation) A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. 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.

4 participants