Skip to content

Commit 23d1db0

Browse files
committed
Recover from semicolon field separator
1 parent 6809ec1 commit 23d1db0

File tree

5 files changed

+23
-32
lines changed

5 files changed

+23
-32
lines changed

compiler/rustc_parse/src/parser/item.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,22 +1917,23 @@ impl<'a> Parser<'a> {
19171917
attrs: AttrVec,
19181918
) -> PResult<'a, FieldDef> {
19191919
let a_var = self.parse_name_and_ty(adt_ty, lo, vis, safety, attrs)?;
1920-
if self.eat(exp!(Semi)) {
1921-
let sp = self.prev_token.span;
1922-
let mut err =
1923-
self.dcx().struct_span_err(sp, format!("{adt_ty} fields are separated by `,`"));
1924-
err.span_suggestion_short(
1925-
sp,
1926-
"replace `;` with `,`",
1927-
",",
1928-
Applicability::MachineApplicable,
1929-
);
1930-
return Err(err);
1931-
}
19321920
match self.token.kind {
19331921
token::Comma => {
19341922
self.bump();
19351923
}
1924+
token::Semi => {
1925+
self.bump();
1926+
let sp = self.prev_token.span;
1927+
let mut err =
1928+
self.dcx().struct_span_err(sp, format!("{adt_ty} fields are separated by `,`"));
1929+
err.span_suggestion_short(
1930+
sp,
1931+
"replace `;` with `,`",
1932+
",",
1933+
Applicability::MachineApplicable,
1934+
);
1935+
err.emit();
1936+
}
19361937
token::CloseBrace => {}
19371938
token::DocComment(..) => {
19381939
let previous_span = self.prev_token.span;

tests/ui/parser/recover/recover-field-semi.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ struct Foo {
33
//~^ ERROR struct fields are separated by `,`
44
}
55

6-
union Bar { //~ ERROR
6+
union Bar {
77
foo: i32;
88
//~^ ERROR union fields are separated by `,`
99
}
@@ -13,4 +13,6 @@ enum Baz {
1313
//~^ ERROR struct fields are separated by `,`
1414
}
1515

16-
fn main() {}
16+
fn main() {
17+
let _ = Foo { foo: "" }; //~ ERROR mismatched types
18+
}
Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
11
error: struct fields are separated by `,`
22
--> $DIR/recover-field-semi.rs:2:13
33
|
4-
LL | struct Foo {
5-
| --- while parsing this struct
64
LL | foo: i32;
75
| ^ help: replace `;` with `,`
86

97
error: union fields are separated by `,`
108
--> $DIR/recover-field-semi.rs:7:13
119
|
12-
LL | union Bar {
13-
| --- while parsing this union
1410
LL | foo: i32;
1511
| ^ help: replace `;` with `,`
1612

1713
error: struct fields are separated by `,`
1814
--> $DIR/recover-field-semi.rs:12:19
1915
|
2016
LL | Qux { foo: i32; }
21-
| --- ^ help: replace `;` with `,`
22-
| |
23-
| while parsing this struct
17+
| ^ help: replace `;` with `,`
2418

25-
error: unions cannot have zero fields
26-
--> $DIR/recover-field-semi.rs:6:1
19+
error[E0308]: mismatched types
20+
--> $DIR/recover-field-semi.rs:17:24
2721
|
28-
LL | / union Bar {
29-
LL | | foo: i32;
30-
LL | |
31-
LL | | }
32-
| |_^
22+
LL | let _ = Foo { foo: "" };
23+
| ^^ expected `i32`, found `&str`
3324

3425
error: aborting due to 4 previous errors
3526

27+
For more information about this error, try `rustc --explain E0308`.

tests/ui/parser/removed-syntax/removed-syntax-field-semicolon.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
error: struct fields are separated by `,`
22
--> $DIR/removed-syntax-field-semicolon.rs:2:12
33
|
4-
LL | struct S {
5-
| - while parsing this struct
64
LL | bar: ();
75
| ^ help: replace `;` with `,`
86

tests/ui/pattern/struct-parser-recovery-issue-126344.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
error: struct fields are separated by `,`
22
--> $DIR/struct-parser-recovery-issue-126344.rs:2:11
33
|
4-
LL | struct Wrong {
5-
| ----- while parsing this struct
64
LL | x: i32;
75
| ^ help: replace `;` with `,`
86

0 commit comments

Comments
 (0)