-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Better error message for comma after base struct #45178
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (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. |
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.
This looks like a great improvement!
I think it should have a test, though? I guess it'd be a parse-fail test?
src/libsyntax/parse/parser.rs
Outdated
"cannot use a comma after struct expansion", | ||
); | ||
err.span_suggestion_short(self.span, "remove this comma", "".to_owned()); | ||
err.note("the struct expansion should always be the last field"); |
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.
nitpick: It must be the last field, not just should, right?
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.
Alright, thanks, I will add a test. Should I call it issue-41834.rs
or struct-expansion.rs
? (not sure what's the correct term for the ..default()
)
4d5e257
to
1a0c551
Compare
@@ -0,0 +1,27 @@ | |||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT |
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.
Could you make this test an UI test?
src/libsyntax/parse/parser.rs
Outdated
@@ -2323,6 +2324,16 @@ impl<'a> Parser<'a> { | |||
self.recover_stmt(); | |||
} | |||
} | |||
if self.check(&token::Comma) { |
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.
Nit: check
adds the token into the "expected tokens" set, self.token == &token::Comma
should be enough here.
src/libsyntax/parse/parser.rs
Outdated
"cannot use a comma after struct expansion", | ||
); | ||
err.span_suggestion_short(self.span, "remove this comma", "".to_owned()); | ||
err.note("the struct expansion must always be the last field"); |
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.
"the" shouldn't be here if we talk about struct expansions in general?
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.
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.
I'm not even sure how this thing is called (..expr
specifically, the whole Struct { field1, field2, ..expr }
is usually called a "functional record update").
What the book/reference/etc say?
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.
The book refers to this as "field init shorthand", or "field init syntax", however still not sure how we call the item. The first edition of the book refers to all this as "update syntax", so I'm not even sure if that is the official name.
Anyway I suggest we just call it the "base struct":
"cannot use a comma after the base struct"
"the base struct must always be the last field"
(I use "the" because there is only one base 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.
Ok, sounds good.
1a0c551
to
ee46c26
Compare
`let x = { ..default(), } // This comma is an error`
ee46c26
to
72cfd20
Compare
@bors r+ |
📌 Commit 72cfd20 has been approved by |
🌲 The tree is currently closed for pull requests below priority 2, this pull request will be tested once the tree is reopened |
@bors rollup |
…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.
#41834
This adds a better error for commas after the base struct:
The current error is a generic
expected one of ...
which isn't beginner-friendly. My error looks like this:I even added a note for people who don't know why this isn't allowed.