From e123a612977145e6ea47bd85307d13cfa52455d5 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Mon, 26 Sep 2022 23:13:09 +0900 Subject: [PATCH 1/3] create a new local var --- compiler/rustc_parse/src/parser/item.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 34f25bd0716ab..297efd8dd465a 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1291,12 +1291,10 @@ impl<'a> Parser<'a> { /// Parses an enum declaration. fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> { if self.token.is_keyword(kw::Struct) { - let mut err = self.struct_span_err( - self.prev_token.span.to(self.token.span), - "`enum` and `struct` are mutually exclusive", - ); + let span = self.prev_token.span.to(self.token.span); + let mut err = self.struct_span_err(span, "`enum` and `struct` are mutually exclusive"); err.span_suggestion( - self.prev_token.span.to(self.token.span), + span, "replace `enum struct` with", "enum", Applicability::MachineApplicable, From 4f44dee50129ebd5da77a14a786849973f47b38f Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Mon, 26 Sep 2022 23:13:38 +0900 Subject: [PATCH 2/3] add a label to struct/enum/union ident name --- compiler/rustc_parse/src/parser/item.rs | 36 +++++++++++++------ ...edition-error-in-non-macro-position.stderr | 4 ++- .../parser/doc-before-struct-rbrace-1.stderr | 3 ++ .../ui/parser/fn-field-parse-error-ice.stderr | 2 ++ src/test/ui/parser/issues/issue-48636.stderr | 2 ++ ...0-unicode-ident-after-missing-comma.stderr | 2 ++ src/test/ui/parser/macro/issue-37113.stderr | 2 ++ .../missing-close-brace-in-struct.stderr | 3 ++ ...osing-angle-bracket-struct-field-ty.stderr | 3 ++ src/test/ui/parser/recover-enum2.stderr | 2 ++ src/test/ui/parser/recover-field-semi.stderr | 8 ++++- src/test/ui/parser/recover-struct.stderr | 2 ++ .../ui/parser/recovered-struct-variant.stderr | 4 ++- .../parser/removed-syntax-enum-newtype.stderr | 4 ++- .../ui/parser/removed-syntax-field-let.stderr | 2 ++ .../removed-syntax-field-semicolon.stderr | 2 ++ ...ue-66270-pat-struct-parser-recovery.stderr | 2 ++ src/test/ui/proc-macro/derive-bad.stderr | 5 ++- src/test/ui/pub/pub-restricted-error.stderr | 2 ++ .../ui/structs/struct-fn-in-definition.stderr | 9 +++++ ...t-field-type-including-single-colon.stderr | 4 +++ ...on-invalid-type-node-after-recovery.stderr | 4 ++- 22 files changed, 91 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 297efd8dd465a..6c4cfcf6ddf4c 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1318,7 +1318,8 @@ impl<'a> Parser<'a> { (vec![], false) } else { self.parse_delim_comma_seq(Delimiter::Brace, |p| p.parse_enum_variant()).map_err( - |e| { + |mut e| { + e.span_label(id.span, "while parsing this enum"); self.recover_stmt(); e }, @@ -1345,7 +1346,8 @@ impl<'a> Parser<'a> { let struct_def = if this.check(&token::OpenDelim(Delimiter::Brace)) { // Parse a struct variant. - let (fields, recovered) = this.parse_record_struct_body("struct", false)?; + let (fields, recovered) = + this.parse_record_struct_body("struct", ident.span, false)?; VariantData::Struct(fields, recovered) } else if this.check(&token::OpenDelim(Delimiter::Parenthesis)) { VariantData::Tuple(this.parse_tuple_struct_body()?, DUMMY_NODE_ID) @@ -1399,8 +1401,11 @@ impl<'a> Parser<'a> { VariantData::Unit(DUMMY_NODE_ID) } else { // If we see: `struct Foo where T: Copy { ... }` - let (fields, recovered) = - self.parse_record_struct_body("struct", generics.where_clause.has_where_token)?; + let (fields, recovered) = self.parse_record_struct_body( + "struct", + class_name.span, + generics.where_clause.has_where_token, + )?; VariantData::Struct(fields, recovered) } // No `where` so: `struct Foo;` @@ -1408,8 +1413,11 @@ impl<'a> Parser<'a> { VariantData::Unit(DUMMY_NODE_ID) // Record-style struct definition } else if self.token == token::OpenDelim(Delimiter::Brace) { - let (fields, recovered) = - self.parse_record_struct_body("struct", generics.where_clause.has_where_token)?; + let (fields, recovered) = self.parse_record_struct_body( + "struct", + class_name.span, + generics.where_clause.has_where_token, + )?; VariantData::Struct(fields, recovered) // Tuple-style struct definition with optional where-clause. } else if self.token == token::OpenDelim(Delimiter::Parenthesis) { @@ -1438,12 +1446,18 @@ impl<'a> Parser<'a> { let vdata = if self.token.is_keyword(kw::Where) { generics.where_clause = self.parse_where_clause()?; - let (fields, recovered) = - self.parse_record_struct_body("union", generics.where_clause.has_where_token)?; + let (fields, recovered) = self.parse_record_struct_body( + "union", + class_name.span, + generics.where_clause.has_where_token, + )?; VariantData::Struct(fields, recovered) } else if self.token == token::OpenDelim(Delimiter::Brace) { - let (fields, recovered) = - self.parse_record_struct_body("union", generics.where_clause.has_where_token)?; + let (fields, recovered) = self.parse_record_struct_body( + "union", + class_name.span, + generics.where_clause.has_where_token, + )?; VariantData::Struct(fields, recovered) } else { let token_str = super::token_descr(&self.token); @@ -1459,6 +1473,7 @@ impl<'a> Parser<'a> { fn parse_record_struct_body( &mut self, adt_ty: &str, + ident_span: Span, parsed_where: bool, ) -> PResult<'a, (Vec, /* recovered */ bool)> { let mut fields = Vec::new(); @@ -1473,6 +1488,7 @@ impl<'a> Parser<'a> { match field { Ok(field) => fields.push(field), Err(mut err) => { + err.span_label(ident_span, format!("while parsing this {adt_ty}")); err.emit(); break; } diff --git a/src/test/ui/async-await/await-keyword/2018-edition-error-in-non-macro-position.stderr b/src/test/ui/async-await/await-keyword/2018-edition-error-in-non-macro-position.stderr index ccbaa1f2af0d8..6bd8f671d7393 100644 --- a/src/test/ui/async-await/await-keyword/2018-edition-error-in-non-macro-position.stderr +++ b/src/test/ui/async-await/await-keyword/2018-edition-error-in-non-macro-position.stderr @@ -46,7 +46,9 @@ error: expected identifier, found keyword `await` --> $DIR/2018-edition-error-in-non-macro-position.rs:13:14 | LL | struct Foo { await: () } - | ^^^^^ expected identifier, found keyword + | --- ^^^^^ expected identifier, found keyword + | | + | while parsing this struct | help: escape `await` to use it as an identifier | diff --git a/src/test/ui/parser/doc-before-struct-rbrace-1.stderr b/src/test/ui/parser/doc-before-struct-rbrace-1.stderr index 19f90677398ea..92b5133b74dd6 100644 --- a/src/test/ui/parser/doc-before-struct-rbrace-1.stderr +++ b/src/test/ui/parser/doc-before-struct-rbrace-1.stderr @@ -1,6 +1,9 @@ error[E0585]: found a documentation comment that doesn't document anything --> $DIR/doc-before-struct-rbrace-1.rs:3:5 | +LL | struct X { + | - while parsing this struct +LL | a: u8, LL | /// document | ^^^^^^^^^^^^ | diff --git a/src/test/ui/parser/fn-field-parse-error-ice.stderr b/src/test/ui/parser/fn-field-parse-error-ice.stderr index d582f61cc97a9..e9583f55b8efb 100644 --- a/src/test/ui/parser/fn-field-parse-error-ice.stderr +++ b/src/test/ui/parser/fn-field-parse-error-ice.stderr @@ -7,6 +7,8 @@ LL | inner : dyn fn () error: functions are not allowed in struct definitions --> $DIR/fn-field-parse-error-ice.rs:4:17 | +LL | struct Baz { + | --- while parsing this struct LL | inner : dyn fn () | ^^ | diff --git a/src/test/ui/parser/issues/issue-48636.stderr b/src/test/ui/parser/issues/issue-48636.stderr index 462723d1d931e..1a6e4cfd2b203 100644 --- a/src/test/ui/parser/issues/issue-48636.stderr +++ b/src/test/ui/parser/issues/issue-48636.stderr @@ -1,6 +1,8 @@ error[E0585]: found a documentation comment that doesn't document anything --> $DIR/issue-48636.rs:7:5 | +LL | struct S { + | - while parsing this struct LL | x: u8 | - help: missing comma here: `,` LL | /// The ID of the parent core diff --git a/src/test/ui/parser/issues/issue-68000-unicode-ident-after-missing-comma.stderr b/src/test/ui/parser/issues/issue-68000-unicode-ident-after-missing-comma.stderr index ef365a616437b..adabb68593c09 100644 --- a/src/test/ui/parser/issues/issue-68000-unicode-ident-after-missing-comma.stderr +++ b/src/test/ui/parser/issues/issue-68000-unicode-ident-after-missing-comma.stderr @@ -7,6 +7,8 @@ LL | pub bar: Vecö error: expected `:`, found `}` --> $DIR/issue-68000-unicode-ident-after-missing-comma.rs:4:1 | +LL | pub struct Foo { + | --- while parsing this struct LL | pub bar: Vecö | - expected `:` LL | diff --git a/src/test/ui/parser/macro/issue-37113.stderr b/src/test/ui/parser/macro/issue-37113.stderr index 0912858ddc4a6..b1f8674fbdf38 100644 --- a/src/test/ui/parser/macro/issue-37113.stderr +++ b/src/test/ui/parser/macro/issue-37113.stderr @@ -1,6 +1,8 @@ error: expected identifier, found `String` --> $DIR/issue-37113.rs:4:16 | +LL | enum SomeEnum { + | -------- while parsing this enum LL | $( $t, )* | ^^ expected identifier ... diff --git a/src/test/ui/parser/mismatched-braces/missing-close-brace-in-struct.stderr b/src/test/ui/parser/mismatched-braces/missing-close-brace-in-struct.stderr index a47d5506ef0b8..ad1e90e43ec4e 100644 --- a/src/test/ui/parser/mismatched-braces/missing-close-brace-in-struct.stderr +++ b/src/test/ui/parser/mismatched-braces/missing-close-brace-in-struct.stderr @@ -10,6 +10,9 @@ LL | fn main() {} error: expected identifier, found keyword `trait` --> $DIR/missing-close-brace-in-struct.rs:4:1 | +LL | pub(crate) struct Bar { + | --- while parsing this struct +... LL | trait T { | ^^^^^ expected identifier, found keyword diff --git a/src/test/ui/parser/missing-closing-angle-bracket-struct-field-ty.stderr b/src/test/ui/parser/missing-closing-angle-bracket-struct-field-ty.stderr index 46ca1f06be67b..6d8b0c3fccde3 100644 --- a/src/test/ui/parser/missing-closing-angle-bracket-struct-field-ty.stderr +++ b/src/test/ui/parser/missing-closing-angle-bracket-struct-field-ty.stderr @@ -1,6 +1,9 @@ error: expected one of `>`, a const expression, lifetime, or type, found `}` --> $DIR/missing-closing-angle-bracket-struct-field-ty.rs:9:1 | +LL | pub struct Foo { + | --- while parsing this struct +... LL | c: Arc>, | - expected one of `>`, a const expression, lifetime, or type LL | } diff --git a/src/test/ui/parser/recover-enum2.stderr b/src/test/ui/parser/recover-enum2.stderr index ee29f06638f11..7634bca921c86 100644 --- a/src/test/ui/parser/recover-enum2.stderr +++ b/src/test/ui/parser/recover-enum2.stderr @@ -1,6 +1,8 @@ error: expected type, found `{` --> $DIR/recover-enum2.rs:6:18 | +LL | Var3 { + | ---- while parsing this struct LL | abc: {}, | ^ expected type diff --git a/src/test/ui/parser/recover-field-semi.stderr b/src/test/ui/parser/recover-field-semi.stderr index 657366db9b4b7..3cf4847488c05 100644 --- a/src/test/ui/parser/recover-field-semi.stderr +++ b/src/test/ui/parser/recover-field-semi.stderr @@ -1,12 +1,16 @@ error: struct fields are separated by `,` --> $DIR/recover-field-semi.rs:2:13 | +LL | struct Foo { + | --- while parsing this struct LL | foo: i32; | ^ help: replace `;` with `,` error: union fields are separated by `,` --> $DIR/recover-field-semi.rs:7:13 | +LL | union Bar { + | --- while parsing this union LL | foo: i32; | ^ help: replace `;` with `,` @@ -14,7 +18,9 @@ error: struct fields are separated by `,` --> $DIR/recover-field-semi.rs:12:19 | LL | Qux { foo: i32; } - | ^ help: replace `;` with `,` + | --- ^ help: replace `;` with `,` + | | + | while parsing this struct error: unions cannot have zero fields --> $DIR/recover-field-semi.rs:6:1 diff --git a/src/test/ui/parser/recover-struct.stderr b/src/test/ui/parser/recover-struct.stderr index 1b72184b0c886..9f6fb06caa34d 100644 --- a/src/test/ui/parser/recover-struct.stderr +++ b/src/test/ui/parser/recover-struct.stderr @@ -1,6 +1,8 @@ error: expected `:`, found `Bad` --> $DIR/recover-struct.rs:4:9 | +LL | struct Test { + | ---- while parsing this struct LL | Very | - expected `:` LL | Bad diff --git a/src/test/ui/parser/recovered-struct-variant.stderr b/src/test/ui/parser/recovered-struct-variant.stderr index 51aaf8bb3cfbe..78c67866fb0f1 100644 --- a/src/test/ui/parser/recovered-struct-variant.stderr +++ b/src/test/ui/parser/recovered-struct-variant.stderr @@ -2,7 +2,9 @@ error: expected `:`, found `,` --> $DIR/recovered-struct-variant.rs:2:10 | LL | A { a, b: usize } - | ^ expected `:` + | - ^ expected `:` + | | + | while parsing this struct error: aborting due to previous error diff --git a/src/test/ui/parser/removed-syntax-enum-newtype.stderr b/src/test/ui/parser/removed-syntax-enum-newtype.stderr index 2daa6249b4ce0..8f7ca356798e2 100644 --- a/src/test/ui/parser/removed-syntax-enum-newtype.stderr +++ b/src/test/ui/parser/removed-syntax-enum-newtype.stderr @@ -2,7 +2,9 @@ error: expected one of `<`, `where`, or `{`, found `=` --> $DIR/removed-syntax-enum-newtype.rs:1:8 | LL | enum e = isize; - | ^ expected one of `<`, `where`, or `{` + | - ^ expected one of `<`, `where`, or `{` + | | + | while parsing this enum error: aborting due to previous error diff --git a/src/test/ui/parser/removed-syntax-field-let.stderr b/src/test/ui/parser/removed-syntax-field-let.stderr index 10be2e045b2b8..ab3d2056581b6 100644 --- a/src/test/ui/parser/removed-syntax-field-let.stderr +++ b/src/test/ui/parser/removed-syntax-field-let.stderr @@ -1,6 +1,8 @@ error: expected identifier, found keyword `let` --> $DIR/removed-syntax-field-let.rs:2:5 | +LL | struct S { + | - while parsing this struct LL | let foo: (), | ^^^ expected identifier, found keyword diff --git a/src/test/ui/parser/removed-syntax-field-semicolon.stderr b/src/test/ui/parser/removed-syntax-field-semicolon.stderr index e4f75f672063c..532d4fb2b61f0 100644 --- a/src/test/ui/parser/removed-syntax-field-semicolon.stderr +++ b/src/test/ui/parser/removed-syntax-field-semicolon.stderr @@ -1,6 +1,8 @@ error: struct fields are separated by `,` --> $DIR/removed-syntax-field-semicolon.rs:2:12 | +LL | struct S { + | - while parsing this struct LL | bar: (); | ^ help: replace `;` with `,` diff --git a/src/test/ui/pattern/issue-66270-pat-struct-parser-recovery.stderr b/src/test/ui/pattern/issue-66270-pat-struct-parser-recovery.stderr index fef0f3c0e06ef..f40642f300cdf 100644 --- a/src/test/ui/pattern/issue-66270-pat-struct-parser-recovery.stderr +++ b/src/test/ui/pattern/issue-66270-pat-struct-parser-recovery.stderr @@ -1,6 +1,8 @@ error: expected type, found `0` --> $DIR/issue-66270-pat-struct-parser-recovery.rs:4:22 | +LL | struct Bug { + | --- while parsing this struct LL | incorrect_field: 0, | ^ expected type diff --git a/src/test/ui/proc-macro/derive-bad.stderr b/src/test/ui/proc-macro/derive-bad.stderr index ae48141fb3133..241f99b28c24b 100644 --- a/src/test/ui/proc-macro/derive-bad.stderr +++ b/src/test/ui/proc-macro/derive-bad.stderr @@ -2,7 +2,10 @@ error: expected `:`, found `}` --> $DIR/derive-bad.rs:6:10 | LL | #[derive(A)] - | ^ expected `:` + | ^ + | | + | expected `:` + | while parsing this struct | = note: this error originates in the derive macro `A` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/src/test/ui/pub/pub-restricted-error.stderr b/src/test/ui/pub/pub-restricted-error.stderr index 95bf498c7f746..b47328f34e6eb 100644 --- a/src/test/ui/pub/pub-restricted-error.stderr +++ b/src/test/ui/pub/pub-restricted-error.stderr @@ -1,6 +1,8 @@ error: expected identifier, found `(` --> $DIR/pub-restricted-error.rs:4:16 | +LL | struct Foo { + | --- while parsing this struct LL | pub(crate) () foo: usize, | ^ expected identifier diff --git a/src/test/ui/structs/struct-fn-in-definition.stderr b/src/test/ui/structs/struct-fn-in-definition.stderr index 1d7cd52729586..472365c6ed739 100644 --- a/src/test/ui/structs/struct-fn-in-definition.stderr +++ b/src/test/ui/structs/struct-fn-in-definition.stderr @@ -1,6 +1,9 @@ error: functions are not allowed in struct definitions --> $DIR/struct-fn-in-definition.rs:9:5 | +LL | struct S { + | - while parsing this struct +... LL | fn foo() {} | ^^^^^^^^^^^ | @@ -10,6 +13,9 @@ LL | fn foo() {} error: functions are not allowed in union definitions --> $DIR/struct-fn-in-definition.rs:18:5 | +LL | union U { + | - while parsing this union +... LL | fn foo() {} | ^^^^^^^^^^^ | @@ -19,6 +25,9 @@ LL | fn foo() {} error: functions are not allowed in enum definitions --> $DIR/struct-fn-in-definition.rs:27:5 | +LL | enum E { + | - while parsing this enum +... LL | fn foo() {} | ^^^^^^^^^^^ | diff --git a/src/test/ui/suggestions/struct-field-type-including-single-colon.stderr b/src/test/ui/suggestions/struct-field-type-including-single-colon.stderr index 189759d64fc4e..4dd514480da40 100644 --- a/src/test/ui/suggestions/struct-field-type-including-single-colon.stderr +++ b/src/test/ui/suggestions/struct-field-type-including-single-colon.stderr @@ -12,6 +12,8 @@ LL | a: foo::A, error: expected `,`, or `}`, found `:` --> $DIR/struct-field-type-including-single-colon.rs:9:11 | +LL | struct Foo { + | --- while parsing this struct LL | a: foo:A, | ^ @@ -29,6 +31,8 @@ LL | b: foo::bar::B, error: expected `,`, or `}`, found `:` --> $DIR/struct-field-type-including-single-colon.rs:15:16 | +LL | struct Bar { + | --- while parsing this struct LL | b: foo::bar:B, | ^ diff --git a/src/test/ui/typeck/issue-69378-ice-on-invalid-type-node-after-recovery.stderr b/src/test/ui/typeck/issue-69378-ice-on-invalid-type-node-after-recovery.stderr index 6bc9c8498c9d1..fc7c23a225240 100644 --- a/src/test/ui/typeck/issue-69378-ice-on-invalid-type-node-after-recovery.stderr +++ b/src/test/ui/typeck/issue-69378-ice-on-invalid-type-node-after-recovery.stderr @@ -2,7 +2,9 @@ error: expected identifier, found `0` --> $DIR/issue-69378-ice-on-invalid-type-node-after-recovery.rs:3:14 | LL | struct Foo { 0: u8 } - | ^ expected identifier + | --- ^ expected identifier + | | + | while parsing this struct error: aborting due to previous error From 5d05908c70dde1f39fa15f1b13fff67c13303c98 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Mon, 26 Sep 2022 23:43:31 +0900 Subject: [PATCH 3/3] fix a ui test --- src/test/ui/parser/issues/issue-101540.stderr | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/ui/parser/issues/issue-101540.stderr b/src/test/ui/parser/issues/issue-101540.stderr index 53c7c9590e6ca..8af887050023c 100644 --- a/src/test/ui/parser/issues/issue-101540.stderr +++ b/src/test/ui/parser/issues/issue-101540.stderr @@ -1,6 +1,8 @@ error: structs are not allowed in struct definitions --> $DIR/issue-101540.rs:2:5 | +LL | struct S1 { + | -- while parsing this struct LL | struct S2 { | ^^^^^^^^^ |