-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Improve diagnostics for functions in struct
definitions
#76808
Conversation
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. |
@jyn514 I pushed the test |
@matklad any updates? |
r? @estebank |
☔ The latest upstream changes (presumably #83188) made this pull request unmergeable. Please resolve the merge conflicts. |
There was a problem hiding this 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.
r? @jackh726 |
@bors delegate+ |
✌️ @LeSeulArtichaut can now approve this pull request |
644975f
to
6717f81
Compare
@bors r+ |
📌 Commit 6717f81 has been approved by |
@jackh726 Enums did not work, I added a new commit to this PR. I also added a better test description as you requested.
What would an "even better error" look like? Do you mean a machine-applicable suggestion that extracts the function to an 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")?; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Yeah, the machine-applicable suggestion.
Only sometimes :) |
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
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 usingpprust
. Would that be a viable approach? Is there a better alternative?r? @matklad cc @estebank