Auto merge of #102314 - TaKO8Ki:add-label-to-struct-enum-union-ident, r=estebank

Add a label to struct/enum/union ident name

Based on https://github.com/rust-lang/rust/pull/94996#discussion_r831694150
cc: `@estebank`
This commit is contained in:
bors 2022-09-27 07:30:11 +00:00
commit d9297d22ad
23 changed files with 96 additions and 21 deletions

View File

@ -1291,12 +1291,10 @@ fn recover_missing_const_type(&mut self, colon_present: bool, m: Option<Mutabili
/// 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,
@ -1320,7 +1318,8 @@ fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
(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
},
@ -1347,7 +1346,8 @@ fn parse_enum_variant(&mut self) -> PResult<'a, Option<Variant>> {
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)
@ -1401,8 +1401,11 @@ fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
VariantData::Unit(DUMMY_NODE_ID)
} else {
// If we see: `struct Foo<T> 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<T>;`
@ -1410,8 +1413,11 @@ fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
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) {
@ -1440,12 +1446,18 @@ fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> {
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);
@ -1461,6 +1473,7 @@ fn parse_item_union(&mut self) -> PResult<'a, ItemInfo> {
fn parse_record_struct_body(
&mut self,
adt_ty: &str,
ident_span: Span,
parsed_where: bool,
) -> PResult<'a, (Vec<FieldDef>, /* recovered */ bool)> {
let mut fields = Vec::new();
@ -1475,6 +1488,7 @@ fn parse_record_struct_body(
match field {
Ok(field) => fields.push(field),
Err(mut err) => {
err.span_label(ident_span, format!("while parsing this {adt_ty}"));
err.emit();
break;
}

View File

@ -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
|

View File

@ -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
| ^^^^^^^^^^^^
|

View File

@ -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 ()
| ^^
|

View File

@ -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 {
| ^^^^^^^^^
|

View File

@ -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

View File

@ -7,6 +7,8 @@ LL | pub bar: Vec<i32>ö
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<i32>ö
| - expected `:`
LL |

View File

@ -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
...

View File

@ -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<T> {
| --- while parsing this struct
...
LL | trait T {
| ^^^^^ expected identifier, found keyword

View File

@ -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<Mutex<usize>>,
| - expected one of `>`, a const expression, lifetime, or type
LL | }

View File

@ -1,6 +1,8 @@
error: expected type, found `{`
--> $DIR/recover-enum2.rs:6:18
|
LL | Var3 {
| ---- while parsing this struct
LL | abc: {},
| ^ expected type

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 `,`

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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() {}
| ^^^^^^^^^^^
|

View File

@ -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,
| ^

View File

@ -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