Skip to content

Commit 6713dde

Browse files
committed
diagnostics: suggest naming a field after failing to parse
1 parent d496a4f commit 6713dde

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

compiler/rustc_parse/src/parser/expr.rs

+13
Original file line numberDiff line numberDiff line change
@@ -3037,6 +3037,19 @@ impl<'a> Parser<'a> {
30373037
",",
30383038
Applicability::MachineApplicable,
30393039
);
3040+
} else if is_shorthand
3041+
&& (AssocOp::from_token(&self.token).is_some()
3042+
|| matches!(&self.token.kind, token::OpenDelim(_))
3043+
|| self.token.kind == token::Dot)
3044+
{
3045+
// Looks like they tried to write a shorthand, complex expression.
3046+
let ident = parsed_field.expect("is_shorthand implies Some").ident;
3047+
e.span_suggestion(
3048+
ident.span.shrink_to_lo(),
3049+
"try naming a field",
3050+
&format!("{ident}: "),
3051+
Applicability::HasPlaceholders,
3052+
);
30403053
}
30413054
}
30423055
if !recover {

src/test/ui/parser/issues/issue-52496.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ error: expected one of `,`, `:`, or `}`, found `.`
88
--> $DIR/issue-52496.rs:8:22
99
|
1010
LL | let _ = Foo { bar.into(), bat: -1, . };
11-
| --- ^ expected one of `,`, `:`, or `}`
12-
| |
11+
| --- - ^ expected one of `,`, `:`, or `}`
12+
| | |
13+
| | help: try naming a field: `bar:`
1314
| while parsing this struct
1415

1516
error: expected identifier, found `.`

src/test/ui/parser/issues/issue-62973.stderr

+11-3
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,19 @@ error: expected one of `,`, `:`, or `}`, found `{`
2424
--> $DIR/issue-62973.rs:6:8
2525
|
2626
LL | fn p() { match s { v, E { [) {) }
27-
| ^ - -^ expected one of `,`, `:`, or `}`
28-
| | | |
29-
| | | help: `}` may belong here
27+
| ^ - ^ expected one of `,`, `:`, or `}`
28+
| | |
3029
| | while parsing this struct
3130
| unclosed delimiter
31+
|
32+
help: `}` may belong here
33+
|
34+
LL | fn p() { match s { v, E} { [) {) }
35+
| +
36+
help: try naming a field
37+
|
38+
LL | fn p() { match s { v, E: E { [) {) }
39+
| ++
3240

3341
error: struct literals are not allowed here
3442
--> $DIR/issue-62973.rs:6:16

0 commit comments

Comments
 (0)