diagnostics: suggest naming a field after failing to parse

This commit is contained in:
Michael Howell 2022-07-07 15:20:08 -07:00
parent d496a4f8bb
commit 6713dde898
3 changed files with 27 additions and 5 deletions

View File

@ -3037,6 +3037,19 @@ impl<'a> Parser<'a> {
",",
Applicability::MachineApplicable,
);
} else if is_shorthand
&& (AssocOp::from_token(&self.token).is_some()
|| matches!(&self.token.kind, token::OpenDelim(_))
|| self.token.kind == token::Dot)
{
// Looks like they tried to write a shorthand, complex expression.
let ident = parsed_field.expect("is_shorthand implies Some").ident;
e.span_suggestion(
ident.span.shrink_to_lo(),
"try naming a field",
&format!("{ident}: "),
Applicability::HasPlaceholders,
);
}
}
if !recover {

View File

@ -8,8 +8,9 @@ error: expected one of `,`, `:`, or `}`, found `.`
--> $DIR/issue-52496.rs:8:22
|
LL | let _ = Foo { bar.into(), bat: -1, . };
| --- ^ expected one of `,`, `:`, or `}`
| |
| --- - ^ expected one of `,`, `:`, or `}`
| | |
| | help: try naming a field: `bar:`
| while parsing this struct
error: expected identifier, found `.`

View File

@ -24,11 +24,19 @@ error: expected one of `,`, `:`, or `}`, found `{`
--> $DIR/issue-62973.rs:6:8
|
LL | fn p() { match s { v, E { [) {) }
| ^ - -^ expected one of `,`, `:`, or `}`
| | | |
| | | help: `}` may belong here
| ^ - ^ expected one of `,`, `:`, or `}`
| | |
| | while parsing this struct
| unclosed delimiter
|
help: `}` may belong here
|
LL | fn p() { match s { v, E} { [) {) }
| +
help: try naming a field
|
LL | fn p() { match s { v, E: E { [) {) }
| ++
error: struct literals are not allowed here
--> $DIR/issue-62973.rs:6:16