From fbf11cfc1314e577bfdae7d53953220798ffa12b Mon Sep 17 00:00:00 2001 From: Chayim Refael Friedman Date: Mon, 5 Sep 2022 16:09:57 +0000 Subject: [PATCH] Recover from using `;` as separator between fields --- compiler/rustc_parse/src/parser/item.rs | 11 +++++++ src/test/ui/parser/recover-field-semi.rs | 16 ++++++++++ src/test/ui/parser/recover-field-semi.stderr | 29 +++++++++++++++++++ .../parser/removed-syntax-field-semicolon.rs | 2 +- .../removed-syntax-field-semicolon.stderr | 4 +-- 5 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/parser/recover-field-semi.rs create mode 100644 src/test/ui/parser/recover-field-semi.stderr diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 5b75d1d5f221d..29b484a3954c9 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1526,6 +1526,17 @@ impl<'a> Parser<'a> { 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(); diff --git a/src/test/ui/parser/recover-field-semi.rs b/src/test/ui/parser/recover-field-semi.rs new file mode 100644 index 0000000000000..b703578860ec1 --- /dev/null +++ b/src/test/ui/parser/recover-field-semi.rs @@ -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() {} diff --git a/src/test/ui/parser/recover-field-semi.stderr b/src/test/ui/parser/recover-field-semi.stderr new file mode 100644 index 0000000000000..657366db9b4b7 --- /dev/null +++ b/src/test/ui/parser/recover-field-semi.stderr @@ -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 + diff --git a/src/test/ui/parser/removed-syntax-field-semicolon.rs b/src/test/ui/parser/removed-syntax-field-semicolon.rs index ac28e21ae0329..808f2a5cc381c 100644 --- a/src/test/ui/parser/removed-syntax-field-semicolon.rs +++ b/src/test/ui/parser/removed-syntax-field-semicolon.rs @@ -1,6 +1,6 @@ struct S { bar: (); - //~^ ERROR expected `,`, or `}`, found `;` + //~^ ERROR struct fields are separated by `,` } fn main() {} diff --git a/src/test/ui/parser/removed-syntax-field-semicolon.stderr b/src/test/ui/parser/removed-syntax-field-semicolon.stderr index fbefeb26a501f..e4f75f672063c 100644 --- a/src/test/ui/parser/removed-syntax-field-semicolon.stderr +++ b/src/test/ui/parser/removed-syntax-field-semicolon.stderr @@ -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