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 to create a new const item if the fn in the array is a const fn #81503

Merged
merged 2 commits into from
Feb 16, 2021

Conversation

henryboisdequin
Copy link
Contributor

@henryboisdequin henryboisdequin commented Jan 29, 2021

Fixes #73734. If the fn in the array repeat expression is a const fn, suggest creating a new const item. On nightly, suggest creating an inline const block. This PR also removes the suggest_const_in_array_repeat_expressions as it is no longer necessary.

Example:

fn main() {
    // Should not compile but hint to create a new const item (stable) or an inline const block (nightly)
    let strings: [String; 5] = [String::new(); 5];
    println!("{:?}", strings);
}

Gives this error:

error[E0277]: the trait bound `std::string::String: std::marker::Copy` is not satisfied
 --> $DIR/const-fn-in-vec.rs:3:32
  |
2 |     let strings: [String; 5] = [String::new(); 5];
  |                             ^^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `String`
  |
  = note: the `Copy` trait is required because the repeated element will be copied

With this change, this is the error message:

error[E0277]: the trait bound `String: Copy` is not satisfied
  --> $DIR/const-fn-in-vec.rs:3:32
   |
LL |     let strings: [String; 5] = [String::new(); 5];
   |                                ^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
   |
   = help: moving the function call to a new `const` item will resolve the error

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @matthewjasper (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 29, 2021
@rust-log-analyzer

This comment has been minimized.

@henryboisdequin
Copy link
Contributor Author

I'm looking into the errors.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@henryboisdequin
Copy link
Contributor Author

r? @Aaron1011

@Aaron1011 Aaron1011 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 29, 2021
@henryboisdequin
Copy link
Contributor Author

@rustbot label -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 30, 2021
@Aaron1011
Copy link
Member

I helped with writing this PR, so let's get someone else to do a final review.

r? @estebank

@rust-highfive rust-highfive assigned estebank and unassigned Aaron1011 Jan 30, 2021
@rust-log-analyzer

This comment has been minimized.

@tesuji
Copy link
Contributor

tesuji commented Jan 30, 2021

Does this PR conflict with #80404, which removes const_in_array_repeat_expressions feature ?

@Aaron1011
Copy link
Member

Aaron1011 commented Jan 30, 2021

They touch the same files, but this PR removes the specialized diagnostic for that feature, so everything should be fine after a rebase.

@henryboisdequin henryboisdequin force-pushed the fix-const-fn-arr-err-msg branch from 9db7bc8 to 6cd36bd Compare February 2, 2021 15:05
@rust-log-analyzer

This comment has been minimized.

@bors

This comment has been minimized.

@estebank

This comment has been minimized.

@henryboisdequin henryboisdequin force-pushed the fix-const-fn-arr-err-msg branch 2 times, most recently from 6c0add5 to 275e426 Compare February 3, 2021 03:50
@rust-log-analyzer

This comment has been minimized.

@henryboisdequin henryboisdequin force-pushed the fix-const-fn-arr-err-msg branch from 275e426 to 885556d Compare February 3, 2021 04:15
@rust-log-analyzer

This comment has been minimized.

@henryboisdequin henryboisdequin force-pushed the fix-const-fn-arr-err-msg branch from 885556d to c2e849c Compare February 3, 2021 04:48
@henryboisdequin
Copy link
Contributor Author

@estebank Do I need to make any further changes?

@henryboisdequin
Copy link
Contributor Author

Friendly ping @estebank :)

@henryboisdequin
Copy link
Contributor Author

@estebank Done ✅

@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Feb 14, 2021

📌 Commit 64fe2c1 has been approved by estebank

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 14, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 16, 2021
…as-schievink

Rollup of 19 pull requests

Successful merges:

 - rust-lang#81503 (Suggest to create a new `const` item if the `fn` in the array is a `const fn`)
 - rust-lang#81897 (Add match pattern diagnostics regression test)
 - rust-lang#81975 (Seal the CommandExt, OsStrExt and OsStringExt traits)
 - rust-lang#82009 (const_generics: Dont evaluate array length const when handling errors)
 - rust-lang#82060 (Fix typos in BTreeSet::{first, last} docs)
 - rust-lang#82061 (CTFE validation: catch ReadPointerAsBytes and better error)
 - rust-lang#82063 (Fixed minor typo in catch_unwind docs)
 - rust-lang#82067 (const_generics: Fix incorrect ty::ParamEnv::empty() usage)
 - rust-lang#82077 (Edit `rustc_arena::DropArena` docs)
 - rust-lang#82096 (Fix a typo)
 - rust-lang#82106 (Remove unnecessary `Option` in `default_doc`)
 - rust-lang#82107 (expand: Some cleanup)
 - rust-lang#82118 (Add missing env!-decl variant)
 - rust-lang#82119 (Fix typo in link to CreateSymbolicLinkW documentation.)
 - rust-lang#82120 (Stabilize Arguments::as_str)
 - rust-lang#82129 (Remove redundant bool_to_option feature gate)
 - rust-lang#82133 (Update link for extern prelude.)
 - rust-lang#82141 (32-bit ARM: Emit `lr` instead of `r14` when specified as an `asm!` output register.)
 - rust-lang#82147 (:arrow_up: rust-analyzer)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit f02f7b0 into rust-lang:master Feb 16, 2021
@rustbot rustbot added this to the 1.52.0 milestone Feb 16, 2021
@henryboisdequin henryboisdequin deleted the fix-const-fn-arr-err-msg branch February 16, 2021 05:46
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 12, 2021
…r=estebank

Diagnostic cleanups

Follow up to rust-lang#81503
Helps with rust-lang#82916 (don't show note if `span` is `DUMMY_SP`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggest creating a const when calling a const function in an array initializer
10 participants