Skip to content

Commit e9db061

Browse files
authored
Rollup merge of #69180 - Aaron1011:feature/comma-struct-init, r=petrochenkov
Suggest a comma if a struct initializer field fails to parse Currently, we emit a "try adding a comma" suggestion if a comma is missing in a struct definition. However, we emit no such suggestion if a comma is missing in a struct initializer. This commit adds a "try adding a comma" suggestion when we don't find a comma during the parsing of a struct initializer field. The change to `src/test/ui/parser/removed-syntax-with-1.stderr` isn't great, but I don't see a good way of avoiding it.
2 parents 09d6a65 + 98757f1 commit e9db061

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

src/librustc_parse/parser/expr.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1832,10 +1832,16 @@ impl<'a> Parser<'a> {
18321832
}
18331833
}
18341834
Err(mut e) => {
1835+
e.span_label(struct_sp, "while parsing this struct");
18351836
if let Some(f) = recovery_field {
18361837
fields.push(f);
1838+
e.span_suggestion(
1839+
self.prev_span.shrink_to_hi(),
1840+
"try adding a comma",
1841+
",".into(),
1842+
Applicability::MachineApplicable,
1843+
);
18371844
}
1838-
e.span_label(struct_sp, "while parsing this struct");
18391845
e.emit();
18401846
self.recover_stmt_(SemiColonMode::Comma, BlockMode::Ignore);
18411847
self.eat(&token::Comma);

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ error: expected one of `,`, `.`, `?`, `}`, or an operator, found `with`
22
--> $DIR/removed-syntax-with-1.rs:8:25
33
|
44
LL | let b = S { foo: () with a, bar: () };
5-
| - ^^^^ expected one of `,`, `.`, `?`, `}`, or an operator
6-
| |
5+
| - -^^^^ expected one of `,`, `.`, `?`, `}`, or an operator
6+
| | |
7+
| | help: try adding a comma: `,`
78
| while parsing this struct
89

910
error: aborting due to previous error
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
struct Foo {
2+
first: bool,
3+
second: u8,
4+
}
5+
6+
fn main() {
7+
let a = Foo {
8+
//~^ ERROR missing field
9+
first: true
10+
second: 25
11+
//~^ ERROR expected one of
12+
};
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: expected one of `,`, `.`, `?`, `}`, or an operator, found `second`
2+
--> $DIR/struct-initializer-comma.rs:10:9
3+
|
4+
LL | let a = Foo {
5+
| --- while parsing this struct
6+
LL |
7+
LL | first: true
8+
| -
9+
| |
10+
| expected one of `,`, `.`, `?`, `}`, or an operator
11+
| help: try adding a comma: `,`
12+
LL | second: 25
13+
| ^^^^^^ unexpected token
14+
15+
error[E0063]: missing field `second` in initializer of `Foo`
16+
--> $DIR/struct-initializer-comma.rs:7:13
17+
|
18+
LL | let a = Foo {
19+
| ^^^ missing `second`
20+
21+
error: aborting due to 2 previous errors
22+
23+
For more information about this error, try `rustc --explain E0063`.

0 commit comments

Comments
 (0)