-
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
Comma after struct expansion is a syntax error #41834
Comments
I agree this is annoying and need to be fixed. |
Just for remark: the |
So we discussed this some in the @rust-lang/lang meeting. I would say that opinions were mixed, but in general I think that we are inclined (for now) to leave this as is. The arguments basically were as follows. First, everyone agreed that we actively do not want to allow Given that, the argument in favor of allowing commas is essentially that people might expect to put it there because they always end lines with a comma (habit, essentially). The argument against is that a trailing comma's main purpose is to avoid messy diffs and simplify macro authors lives, and @petrochenkov made a convincing case that neither applies here. Moreover, a trailing comma suggests that you can move things around in the list, and that you might put something afterwards, neither of which are true here. So including the comma is somewhat misleading. Therefore, I think we're inclined to leave the grammar as is, and prohibit trailing commas after a |
Leaving this open to track improving the diagnostic here. |
In order to avoid multiple errors after finding this comma, as it happens now, the comma has to be specifically accepted and a diagnostic generated for this case in particular, along the lines of
The method |
…henkov Better error message for comma after base struct rust-lang#41834 This adds a better error for commas after the base struct: ``` let foo = Foo { one: 111, ..Foo::default(), // This comma is a syntax error }; ``` The current error is a generic `expected one of ...` which isn't beginner-friendly. My error looks like this: ``` error: cannot use a comma after the base struct --> tmp/example.rs:26:9 | 26 | ..Foo::default(), | ^^^^^^^^^^^^^^^^- help: remove this comma | = note: the base struct expansion must always be the last field ``` I even added a note for people who don't know why this isn't allowed.
Triage: I think this should be closed now, because #45178 has now been merged into master as of 3 days ago. |
On rustc 1.17:
(playground link: https://is.gd/2wvenA)
This feels very inconsistent, as Rust has no problems with extra commas anywhere else. It is also an extremely "natural" mistake to make, so even if it's rightly signaled & easily corrected, it is still quite annoying.
The text was updated successfully, but these errors were encountered: