-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
let
's not needed in struct field definitions
#101789
Conversation
r? @estebank (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
You will have to run |
@@ -1725,7 +1725,12 @@ impl<'a> Parser<'a> { | |||
err.help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information"); | |||
err | |||
} else { | |||
self.expected_ident_found() | |||
let mut err = self.expected_ident_found(); | |||
if ident.name == kw::Let { |
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.
Ideally we would do N=1 lookahead to check if the next element is an identifier, and then do self.bump();
(to consume the let
) and then get that ident, emit the error and return Ok(ident)
(so that the parser can properly recover and continue creating the struct
with the expected field name, and even emit errors if the mistyped field isn't included elsewhere). In all other cases, we keep the current behavior (returning err
) and let the other parser recovery kick in.
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.
We might want to extend the existing test to include something like
struct S {
let s: (),
y: (),
}
fn main() {
let _ = S { // should complain about missing `y`
s: (),
};
let _ = S {
y: (), // should complain about missing `s`
};
let _ = S {
let s: (), // should complain about missing `y` and the spurious `let`
};
let _ = S {
let y: (), // should complain about missing `s` and the spurious `let`
};
let _ = S {
let s = (), // should complain about missing `y`, the `:` to `=` typo and the spurious `let`
};
let _ = S {
let y = (), // should complain about missing `s`, the `:` to `=` typo and the spurious `let`
};
}
Keep in mind that the later four recoveries are not included in this PR (but I would like to have it in the future).
Yah, will bless it a in a while. Currently struggling with git branches again... (why does github not allow multiple forks )😮💨 |
(From CI logs) CI seems to be complaining about the code formatting, maybe try blessing again with tidy? Not a hundred percent sure though. |
Oh ye, forgot to tidy, aside from that I meant the ICE causing test bork |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@rustbot ready |
This comment has been minimized.
This comment has been minimized.
Co-authored-by: jyn514 <jyn514@gmail.com> Co-authored-by: Esteban Kuber <estebank@users.noreply.github.com>
@bors r=estebank |
`let`'s not needed in struct field definitions Fixes rust-lang#101683
`let`'s not needed in struct field definitions Fixes rust-lang#101683
@@ -1788,7 +1788,23 @@ impl<'a> Parser<'a> { | |||
} | |||
} | |||
} else { | |||
self.expected_ident_found() | |||
let mut err = self.expected_ident_found(); | |||
if let Some((ident, _)) = self.token.ident() && ident.as_str() == "let" { |
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 probably could've been self.token.is_keyword(kw::Let)
, see Token::is_keyword
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.
Alternatively, this could've used Parser::eat_keyword_noexpect
to avoid the bump call below
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.
Awesome, will send an improvement PR later on. 👍🏻
if let Some((ident, _)) = self.token.ident() && ident.as_str() == "let" { | ||
self.bump(); // `let` | ||
let span = self.prev_token.span.until(self.token.span); | ||
err.span_suggestion( |
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.
@Xiretza this is a good example of a #[derive(Subdiagnostic)]
candidate that both emits notes and a suggestion!
self.bump(); | ||
return Ok(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.
@gimbles for future reference, why is this bumping twice? This should perhaps be:
self.bump(); | |
return Ok(ident); | |
let ident = self.parse_ident()?; | |
return Ok(ident); |
Otherwise, it seems to me like it's returning let
as the struct field name?
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.
Look at the earlier reviews on wonky spans ^^
…iaskrgr Rollup of 6 pull requests Successful merges: - rust-lang#101360 (Point out incompatible closure bounds) - rust-lang#101789 (`let`'s not needed in struct field definitions) - rust-lang#102846 (update to syn-1.0.102) - rust-lang#102871 (rustdoc: clean up overly complex `.trait-impl` CSS selectors) - rust-lang#102876 (suggest candidates for unresolved import) - rust-lang#102888 (Improve rustdoc-gui search-color test) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Fixes #101683