-
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
suggest ..
for erroneous ...
in struct pattern; don't discard successfully-parsed fields on error
#46721
suggest ..
for erroneous ...
in struct pattern; don't discard successfully-parsed fields on error
#46721
Conversation
Previously, if an error occured while parsing field patterns, we would just return the error, discarding any field patterns that did successfully parse, which sometimes resulted in spurious diagnostics. At the cost of a little boilerplate (the repetitious `.map_err(|err| ((fields.clone(), etc), err))` is annoying and the present author is sorry to say she could not do better), we can pack the successfully-parsed field patterns into the error and use them. This was inspired by rust-lang#46718.
I wonder if it wouldn't be better to not print a message about missing field at all here. Using |
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 like it. Agree with @xfix's suggestion but I'd accept a follow up PR for it.
let lo = self.span; | ||
let hi; | ||
|
||
if self.check(&token::DotDot) { | ||
self.bump(); | ||
if self.token != token::CloseDelim(token::Brace) { | ||
let token_str = self.this_token_to_string(); | ||
return Err(self.fatal(&format!("expected `{}`, found `{}`", "}", | ||
token_str))) | ||
let err = self.fatal(&format!("expected `{}`, found `{}`", "}", |
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.
format!("expected
{}...", "}", ...)
is a bit silly, could you change it? :)
--> $DIR/issue-46718-expected-dotdot-found-dotdotdot.rs:21:13 | ||
| | ||
21 | PersonalityInventory { expressivity: exp, ... } => exp | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing field `instrumentality` |
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 agree with @xfix's comment, it'd be nice if this error wasn't shown. IIRC, this error happens during type check, so you'd have to modify the ast::StructPatKind
and its equivalent in the hir
to include a boolean recovered
and only emit this error if it's true (I'm doing the same for type errors involving tail expressions and recovered if blocks in #46732). I'm sure that there're are other errors that could benefit from this signal too, but I wouldn't go looking to fix them on this PR. This change would probably cause many more files to be modified, so we could land this first and have a follow up PR if you prefer.
// accept trailing commas | ||
if self.check(&token::CloseDelim(token::Brace)) { break } | ||
} | ||
|
||
let attrs = self.parse_outer_attributes()?; | ||
let attrs = self.parse_outer_attributes() | ||
.map_err(|err| ((fields.clone(), etc), err))?; | ||
let lo = self.span; | ||
let hi; | ||
|
||
if self.check(&token::DotDot) { |
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 only thing this PR needs to do is to replace this self.check(&token::DotDot)
with self.check(&token::DotDot) || self.token == token::DotDotDot
, and report a non-fatal error (self.span_err(...)
) if it's actually a DotDotDot
.
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 see ...
(I might claim that the extra machinery in this PR is still potentially useful for taking successfully-parsed field patterns into account when there's some other kind of syntax error ... but maybe that can be a separate, future PR if I find this PartialPResult
device being useful elsewhere.)
Closing in favor of simpler alternative #46763; thanks to @petrochenkov for the suggestion. (The |
…_pattern_ellipsis, r=petrochenkov in which `..` is suggested for erroneous `...` in struct field patterns Resolves #46718. Supersedes #46721. r? @petrochenkov
Regarding #46718.
r? @pnkfelix