Skip to content

Commit fbf11cf

Browse files
Recover from using ; as separator between fields
1 parent 6e4a9ab commit fbf11cf

File tree

5 files changed

+59
-3
lines changed

5 files changed

+59
-3
lines changed

Diff for: compiler/rustc_parse/src/parser/item.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,17 @@ impl<'a> Parser<'a> {
15261526
if self.token == token::Comma {
15271527
seen_comma = true;
15281528
}
1529+
if self.eat(&token::Semi) {
1530+
let sp = self.prev_token.span;
1531+
let mut err = self.struct_span_err(sp, format!("{adt_ty} fields are separated by `,`"));
1532+
err.span_suggestion_short(
1533+
sp,
1534+
"replace `;` with `,`",
1535+
",",
1536+
Applicability::MachineApplicable,
1537+
);
1538+
return Err(err);
1539+
}
15291540
match self.token.kind {
15301541
token::Comma => {
15311542
self.bump();

Diff for: src/test/ui/parser/recover-field-semi.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
struct Foo {
2+
foo: i32;
3+
//~^ ERROR struct fields are separated by `,`
4+
}
5+
6+
union Bar { //~ ERROR
7+
foo: i32;
8+
//~^ ERROR union fields are separated by `,`
9+
}
10+
11+
enum Baz {
12+
Qux { foo: i32; }
13+
//~^ ERROR struct fields are separated by `,`
14+
}
15+
16+
fn main() {}

Diff for: src/test/ui/parser/recover-field-semi.stderr

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: struct fields are separated by `,`
2+
--> $DIR/recover-field-semi.rs:2:13
3+
|
4+
LL | foo: i32;
5+
| ^ help: replace `;` with `,`
6+
7+
error: union fields are separated by `,`
8+
--> $DIR/recover-field-semi.rs:7:13
9+
|
10+
LL | foo: i32;
11+
| ^ help: replace `;` with `,`
12+
13+
error: struct fields are separated by `,`
14+
--> $DIR/recover-field-semi.rs:12:19
15+
|
16+
LL | Qux { foo: i32; }
17+
| ^ help: replace `;` with `,`
18+
19+
error: unions cannot have zero fields
20+
--> $DIR/recover-field-semi.rs:6:1
21+
|
22+
LL | / union Bar {
23+
LL | | foo: i32;
24+
LL | |
25+
LL | | }
26+
| |_^
27+
28+
error: aborting due to 4 previous errors
29+

Diff for: src/test/ui/parser/removed-syntax-field-semicolon.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
struct S {
22
bar: ();
3-
//~^ ERROR expected `,`, or `}`, found `;`
3+
//~^ ERROR struct fields are separated by `,`
44
}
55

66
fn main() {}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected `,`, or `}`, found `;`
1+
error: struct fields are separated by `,`
22
--> $DIR/removed-syntax-field-semicolon.rs:2:12
33
|
44
LL | bar: ();
5-
| ^
5+
| ^ help: replace `;` with `,`
66

77
error: aborting due to previous error
88

0 commit comments

Comments
 (0)