-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Issue #50974 : Suboptimal error in case of duplicate ,
in struct constructor
#51184
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. |
src/libsyntax/parse/parser.rs
Outdated
@@ -767,6 +767,9 @@ impl<'a> Parser<'a> { | |||
err.span_label(self.span, format!("expected identifier, found {}", token_descr)); | |||
} else { | |||
err.span_label(self.span, "expected identifier"); | |||
if self.token == token::Comma { | |||
err.span_suggestion(self.span, "remove this comma", ",".into()); |
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 third argument here should be an empty string. This will change the output to help: remove this comma
.
src/libsyntax/parse/parser.rs
Outdated
@@ -767,6 +767,9 @@ impl<'a> Parser<'a> { | |||
err.span_label(self.span, format!("expected identifier, found {}", token_descr)); | |||
} else { | |||
err.span_label(self.span, "expected identifier"); | |||
if self.token == 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.
I'm not sure this is the best thing to do, as is. I would only add this suggestion if the next token is indeed an ident:
if self.token == token::Comma && self.look_ahead(1, |t| *t.is_ident()) {
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 think it will still be useful even if the next token is not an identifier. We should not ever have 2 commas in a row in a struct constructor.
struct Bar {
a: u8,
b: u8
}
let foo = Bar {
a: 42,
b: 42,,
};
In this example, the user still has to remove the duplicate comma to get his code to compile.
src/libsyntax/parse/parser.rs
Outdated
self.recover_stmt(); | ||
break; | ||
|
||
if self.token != 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.
This confuses me a bit — when does this ,
token get dropped, if not by recover_stmt
?
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.
It gets dropped afterwards at line 2541 :
self.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Brace)])
Should I add a comment to clarify this?
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.
Ah, I see now. Yes, put a comment if you don't mind.
This comment has been minimized.
This comment has been minimized.
@lambtowolf think you can add that explanatory comment? =) |
@bors r+ |
📌 Commit 01559fb has been approved by |
Issue rust-lang#50974 : Suboptimal error in case of duplicate `,` in struct constructor Fixes rust-lang#50974
🔒 Merge conflict |
@lambtowolf Needs rebase 😿 |
@lambtowolf you should rebase on master, even if you don't see any conflict at first glance. |
Ping from triage @lambtowolf! It's been a while since we heard from you, will you have time to work on this again? |
🔒 Merge conflict This pull request and the master branch diverged in a way that cannot be automatically merged. Please rebase on top of the latest master branch, and let the reviewer approve again. How do I rebase?Assuming
You may also read Git Rebasing to Resolve Conflicts by Drew Blessing for a short tutorial. Please avoid the "Resolve conflicts" button on GitHub. It uses Sometimes step 4 will complete without asking for resolution. This is usually due to difference between how Error message
|
1 similar comment
🔒 Merge conflict This pull request and the master branch diverged in a way that cannot be automatically merged. Please rebase on top of the latest master branch, and let the reviewer approve again. How do I rebase?Assuming
You may also read Git Rebasing to Resolve Conflicts by Drew Blessing for a short tutorial. Please avoid the "Resolve conflicts" button on GitHub. It uses Sometimes step 4 will complete without asking for resolution. This is usually due to difference between how Error message
|
Only display the "remove this comma" suggestion when followed by an identifier
Rebased this on top of the latest master. @bors r=nikomatsakis |
📌 Commit a99767f has been approved by |
☀️ Test successful - status-appveyor, status-travis |
Fixes #50974