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 diagnostics for functions in struct definitions #76808

Merged
merged 3 commits into from
May 8, 2021

Conversation

LeSeulArtichaut
Copy link
Contributor

Tries to implement #76421.
This is probably going to need unit tests, but I wanted to hear from review all the cases tests should cover.

I'd like to follow up with the "mechanically applicable suggestion here that adds an impl block" step, but I'd need guidance. My idea for now would be to try to parse a function, and if that succeeds, create a dummy ast::Item impl block to then format it using pprust. Would that be a viable approach? Is there a better alternative?

r? @matklad cc @estebank

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 16, 2020
@jyn514 jyn514 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 Sep 16, 2020
@jyn514
Copy link
Member

jyn514 commented Sep 16, 2020

It would be helpful to add a UI test for the example in #76421 - even if it doesn't hit all the cases, it helps show that the diagnostic looks roughly right.

@LeSeulArtichaut
Copy link
Contributor Author

LeSeulArtichaut commented Sep 17, 2020

@jyn514 I pushed the test

@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 8, 2020
@camelid camelid added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 23, 2020
@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 13, 2020
@Dylan-DPC-zz
Copy link

@matklad any updates?

@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 30, 2020
@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 18, 2020
@camelid camelid added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 15, 2021
@Dylan-DPC-zz
Copy link

r? @estebank

@rust-highfive rust-highfive assigned estebank and unassigned matklad Jan 31, 2021
@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 23, 2021
@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 13, 2021
@bors
Copy link
Contributor

bors commented Mar 17, 2021

☔ The latest upstream changes (presumably #83188) made this pull request unmergeable. Please resolve the merge conflicts.

@crlf0710 crlf0710 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 Apr 2, 2021
@JohnCSimon JohnCSimon added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 18, 2021
Copy link
Member

@jackh726 jackh726 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LeSeulArtichaut Do you want to just try to get the the current suggestion merged and then make a followup PR with an even better error? If so, then r=me on this, with or without an additional comment on the test.

In future PRs, I would like to see tests for unions or enums too. Or feel free to add that to this PR if it works.

compiler/rustc_parse/src/parser/item.rs Show resolved Hide resolved
@jackh726
Copy link
Member

jackh726 commented May 3, 2021

r? @jackh726

@rust-highfive rust-highfive assigned jackh726 and unassigned estebank May 3, 2021
@jackh726
Copy link
Member

jackh726 commented May 3, 2021

@bors delegate+

@bors
Copy link
Contributor

bors commented May 3, 2021

✌️ @LeSeulArtichaut can now approve this pull request

@LeSeulArtichaut LeSeulArtichaut 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 May 5, 2021
@LeSeulArtichaut LeSeulArtichaut 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 May 7, 2021
@jackh726
Copy link
Member

jackh726 commented May 7, 2021

@bors r+

@bors
Copy link
Contributor

bors commented May 7, 2021

📌 Commit 6717f81 has been approved by jackh726

@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 May 7, 2021
@LeSeulArtichaut
Copy link
Contributor Author

LeSeulArtichaut commented May 7, 2021

@jackh726 Enums did not work, I added a new commit to this PR. I also added a better test description as you requested.

Do you want to just try to get the the current suggestion merged and then make a followup PR with an even better error?

What would an "even better error" look like? Do you mean a machine-applicable suggestion that extracts the function to an impl block, as estebank suggested? If so I'd prefer that to be in a follow-up PR.

PS: you're too fast


let struct_def = if this.check(&token::OpenDelim(token::Brace)) {
// Parse a struct variant.
let (fields, recovered) = this.parse_record_struct_body()?;
let (fields, recovered) = this.parse_record_struct_body("struct")?;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this can give some slightly ankward results:

enum Foo {
    Variant {
        fn bar() {}
    }
}
error: functions are not allowed in struct definitions
 --> test.rs:5:9
  |
5 |         fn bar() {}
  |         ^^^^^^^^^^^
  |
  = help: unlike in C++, Java, and C#, functions are declared in `impl` blocks
  = help: see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information

Doesn't seem very important to me however

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I see. If it comes up, a fairly easy fix might be to add a struct flag that is true in an actual struct and false in a enum variant.

@jackh726
Copy link
Member

jackh726 commented May 7, 2021

What would an "even better error" look like? Do you mean a machine-applicable suggestion that extracts the function to an impl block, as estebank suggested? If so I'd prefer that to be in a follow-up PR.

Yeah, the machine-applicable suggestion.

PS: you're too fast

Only sometimes :)

bors added a commit to rust-lang-ci/rust that referenced this pull request May 8, 2021
Rollup of 6 pull requests

Successful merges:

 - rust-lang#76808 (Improve diagnostics for functions in `struct` definitions)
 - rust-lang#84887 (Remove SpanInterner::get)
 - rust-lang#85034 (fix null pointer error messages)
 - rust-lang#85038 (Don't stop running rustdoc-gui tests at first failure)
 - rust-lang#85044 (Use `path.exists()` instead of `fs::metadata(path).is_ok()`)
 - rust-lang#85052 (rustdoc: Link to the docs on namespaces when an unknown disambiguator is found)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit eb36bc6 into rust-lang:master May 8, 2021
@rustbot rustbot added this to the 1.54.0 milestone May 8, 2021
@LeSeulArtichaut LeSeulArtichaut deleted the diagnose-functions-struct branch May 8, 2021 10:54
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 S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.