Skip to content

Commit d496a4f

Browse files
committed
diagnostics: mention the : token when struct fields fail to parse
1 parent 0f573a0 commit d496a4f

File tree

5 files changed

+12
-7
lines changed

5 files changed

+12
-7
lines changed

compiler/rustc_parse/src/parser/expr.rs

+5
Original file line numberDiff line numberDiff line change
@@ -3012,6 +3012,11 @@ impl<'a> Parser<'a> {
30123012
}
30133013
};
30143014

3015+
let is_shorthand = parsed_field.as_ref().map_or(false, |f| f.ident.span == f.expr.span);
3016+
// A shorthand field can be turned into a full field with `:`.
3017+
// We should point this out.
3018+
self.check_or_expected(!is_shorthand, TokenType::Token(token::Colon));
3019+
30153020
match self.expect_one_of(&[token::Comma], &[token::CloseDelim(close_delim)]) {
30163021
Ok(_) => {
30173022
if let Some(f) = parsed_field.or(recovery_field) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error: float literals must have an integer part
44
LL | let _ = Foo { bar: .5, baz: 42 };
55
| ^^ help: must have an integer part: `0.5`
66

7-
error: expected one of `,` or `}`, found `.`
7+
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 `}`
11+
| --- ^ expected one of `,`, `:`, or `}`
1212
| |
1313
| while parsing this struct
1414

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ LL |
2020
LL |
2121
| ^
2222

23-
error: expected one of `,` or `}`, found `{`
23+
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 `}`
27+
| ^ - -^ expected one of `,`, `:`, or `}`
2828
| | | |
2929
| | | help: `}` may belong here
3030
| | while parsing this struct

src/test/ui/parser/removed-syntax-with-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ fn main() {
66

77
let a = S { foo: (), bar: () };
88
let b = S { foo: (), with a };
9-
//~^ ERROR expected one of `,` or `}`, found `a`
9+
//~^ ERROR expected one of `,`, `:`, or `}`, found `a`
1010
//~| ERROR missing field `bar` in initializer of `S`
1111
}

src/test/ui/parser/removed-syntax-with-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected one of `,` or `}`, found `a`
1+
error: expected one of `,`, `:`, or `}`, found `a`
22
--> $DIR/removed-syntax-with-2.rs:8:31
33
|
44
LL | let b = S { foo: (), with a };
5-
| - ^ expected one of `,` or `}`
5+
| - ^ expected one of `,`, `:`, or `}`
66
| |
77
| while parsing this struct
88

0 commit comments

Comments
 (0)