Recover from using ; as separator between fields

This commit is contained in:
Chayim Refael Friedman 2022-09-05 16:09:57 +00:00
parent 6e4a9ab650
commit fbf11cfc13
5 changed files with 59 additions and 3 deletions

View File

@ -1526,6 +1526,17 @@ fn parse_single_struct_field(
if self.token == token::Comma {
seen_comma = true;
}
if self.eat(&token::Semi) {
let sp = self.prev_token.span;
let mut err = self.struct_span_err(sp, format!("{adt_ty} fields are separated by `,`"));
err.span_suggestion_short(
sp,
"replace `;` with `,`",
",",
Applicability::MachineApplicable,
);
return Err(err);
}
match self.token.kind {
token::Comma => {
self.bump();

View File

@ -0,0 +1,16 @@
struct Foo {
foo: i32;
//~^ ERROR struct fields are separated by `,`
}
union Bar { //~ ERROR
foo: i32;
//~^ ERROR union fields are separated by `,`
}
enum Baz {
Qux { foo: i32; }
//~^ ERROR struct fields are separated by `,`
}
fn main() {}

View File

@ -0,0 +1,29 @@
error: struct fields are separated by `,`
--> $DIR/recover-field-semi.rs:2:13
|
LL | foo: i32;
| ^ help: replace `;` with `,`
error: union fields are separated by `,`
--> $DIR/recover-field-semi.rs:7:13
|
LL | foo: i32;
| ^ help: replace `;` with `,`
error: struct fields are separated by `,`
--> $DIR/recover-field-semi.rs:12:19
|
LL | Qux { foo: i32; }
| ^ help: replace `;` with `,`
error: unions cannot have zero fields
--> $DIR/recover-field-semi.rs:6:1
|
LL | / union Bar {
LL | | foo: i32;
LL | |
LL | | }
| |_^
error: aborting due to 4 previous errors

View File

@ -1,6 +1,6 @@
struct S {
bar: ();
//~^ ERROR expected `,`, or `}`, found `;`
//~^ ERROR struct fields are separated by `,`
}
fn main() {}

View File

@ -1,8 +1,8 @@
error: expected `,`, or `}`, found `;`
error: struct fields are separated by `,`
--> $DIR/removed-syntax-field-semicolon.rs:2:12
|
LL | bar: ();
| ^
| ^ help: replace `;` with `,`
error: aborting due to previous error