1
Fork 0

add a label to struct/enum/union ident name

This commit is contained in:
Takayuki Maeda 2022-09-26 23:13:38 +09:00
parent e123a61297
commit 4f44dee501
22 changed files with 91 additions and 16 deletions

View file

@ -1318,7 +1318,8 @@ impl<'a> Parser<'a> {
(vec![], false) (vec![], false)
} else { } else {
self.parse_delim_comma_seq(Delimiter::Brace, |p| p.parse_enum_variant()).map_err( 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(); self.recover_stmt();
e e
}, },
@ -1345,7 +1346,8 @@ impl<'a> Parser<'a> {
let struct_def = if this.check(&token::OpenDelim(Delimiter::Brace)) { let struct_def = if this.check(&token::OpenDelim(Delimiter::Brace)) {
// Parse a struct variant. // 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) VariantData::Struct(fields, recovered)
} else if this.check(&token::OpenDelim(Delimiter::Parenthesis)) { } else if this.check(&token::OpenDelim(Delimiter::Parenthesis)) {
VariantData::Tuple(this.parse_tuple_struct_body()?, DUMMY_NODE_ID) VariantData::Tuple(this.parse_tuple_struct_body()?, DUMMY_NODE_ID)
@ -1399,8 +1401,11 @@ impl<'a> Parser<'a> {
VariantData::Unit(DUMMY_NODE_ID) VariantData::Unit(DUMMY_NODE_ID)
} else { } else {
// If we see: `struct Foo<T> where T: Copy { ... }` // If we see: `struct Foo<T> where T: Copy { ... }`
let (fields, recovered) = let (fields, recovered) = self.parse_record_struct_body(
self.parse_record_struct_body("struct", generics.where_clause.has_where_token)?; "struct",
class_name.span,
generics.where_clause.has_where_token,
)?;
VariantData::Struct(fields, recovered) VariantData::Struct(fields, recovered)
} }
// No `where` so: `struct Foo<T>;` // No `where` so: `struct Foo<T>;`
@ -1408,8 +1413,11 @@ impl<'a> Parser<'a> {
VariantData::Unit(DUMMY_NODE_ID) VariantData::Unit(DUMMY_NODE_ID)
// Record-style struct definition // Record-style struct definition
} else if self.token == token::OpenDelim(Delimiter::Brace) { } else if self.token == token::OpenDelim(Delimiter::Brace) {
let (fields, recovered) = let (fields, recovered) = self.parse_record_struct_body(
self.parse_record_struct_body("struct", generics.where_clause.has_where_token)?; "struct",
class_name.span,
generics.where_clause.has_where_token,
)?;
VariantData::Struct(fields, recovered) VariantData::Struct(fields, recovered)
// Tuple-style struct definition with optional where-clause. // Tuple-style struct definition with optional where-clause.
} else if self.token == token::OpenDelim(Delimiter::Parenthesis) { } 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) { let vdata = if self.token.is_keyword(kw::Where) {
generics.where_clause = self.parse_where_clause()?; generics.where_clause = self.parse_where_clause()?;
let (fields, recovered) = let (fields, recovered) = self.parse_record_struct_body(
self.parse_record_struct_body("union", generics.where_clause.has_where_token)?; "union",
class_name.span,
generics.where_clause.has_where_token,
)?;
VariantData::Struct(fields, recovered) VariantData::Struct(fields, recovered)
} else if self.token == token::OpenDelim(Delimiter::Brace) { } else if self.token == token::OpenDelim(Delimiter::Brace) {
let (fields, recovered) = let (fields, recovered) = self.parse_record_struct_body(
self.parse_record_struct_body("union", generics.where_clause.has_where_token)?; "union",
class_name.span,
generics.where_clause.has_where_token,
)?;
VariantData::Struct(fields, recovered) VariantData::Struct(fields, recovered)
} else { } else {
let token_str = super::token_descr(&self.token); let token_str = super::token_descr(&self.token);
@ -1459,6 +1473,7 @@ impl<'a> Parser<'a> {
fn parse_record_struct_body( fn parse_record_struct_body(
&mut self, &mut self,
adt_ty: &str, adt_ty: &str,
ident_span: Span,
parsed_where: bool, parsed_where: bool,
) -> PResult<'a, (Vec<FieldDef>, /* recovered */ bool)> { ) -> PResult<'a, (Vec<FieldDef>, /* recovered */ bool)> {
let mut fields = Vec::new(); let mut fields = Vec::new();
@ -1473,6 +1488,7 @@ impl<'a> Parser<'a> {
match field { match field {
Ok(field) => fields.push(field), Ok(field) => fields.push(field),
Err(mut err) => { Err(mut err) => {
err.span_label(ident_span, format!("while parsing this {adt_ty}"));
err.emit(); err.emit();
break; break;
} }

View file

@ -46,7 +46,9 @@ error: expected identifier, found keyword `await`
--> $DIR/2018-edition-error-in-non-macro-position.rs:13:14 --> $DIR/2018-edition-error-in-non-macro-position.rs:13:14
| |
LL | struct Foo { await: () } 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 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 error[E0585]: found a documentation comment that doesn't document anything
--> $DIR/doc-before-struct-rbrace-1.rs:3:5 --> $DIR/doc-before-struct-rbrace-1.rs:3:5
| |
LL | struct X {
| - while parsing this struct
LL | a: u8,
LL | /// document LL | /// document
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
| |

View file

@ -7,6 +7,8 @@ LL | inner : dyn fn ()
error: functions are not allowed in struct definitions error: functions are not allowed in struct definitions
--> $DIR/fn-field-parse-error-ice.rs:4:17 --> $DIR/fn-field-parse-error-ice.rs:4:17
| |
LL | struct Baz {
| --- while parsing this struct
LL | inner : dyn fn () LL | inner : dyn fn ()
| ^^ | ^^
| |

View file

@ -1,6 +1,8 @@
error[E0585]: found a documentation comment that doesn't document anything error[E0585]: found a documentation comment that doesn't document anything
--> $DIR/issue-48636.rs:7:5 --> $DIR/issue-48636.rs:7:5
| |
LL | struct S {
| - while parsing this struct
LL | x: u8 LL | x: u8
| - help: missing comma here: `,` | - help: missing comma here: `,`
LL | /// The ID of the parent core LL | /// The ID of the parent core

View file

@ -7,6 +7,8 @@ LL | pub bar: Vec<i32>ö
error: expected `:`, found `}` error: expected `:`, found `}`
--> $DIR/issue-68000-unicode-ident-after-missing-comma.rs:4:1 --> $DIR/issue-68000-unicode-ident-after-missing-comma.rs:4:1
| |
LL | pub struct Foo {
| --- while parsing this struct
LL | pub bar: Vec<i32>ö LL | pub bar: Vec<i32>ö
| - expected `:` | - expected `:`
LL | LL |

View file

@ -1,6 +1,8 @@
error: expected identifier, found `String` error: expected identifier, found `String`
--> $DIR/issue-37113.rs:4:16 --> $DIR/issue-37113.rs:4:16
| |
LL | enum SomeEnum {
| -------- while parsing this enum
LL | $( $t, )* LL | $( $t, )*
| ^^ expected identifier | ^^ expected identifier
... ...

View file

@ -10,6 +10,9 @@ LL | fn main() {}
error: expected identifier, found keyword `trait` error: expected identifier, found keyword `trait`
--> $DIR/missing-close-brace-in-struct.rs:4:1 --> $DIR/missing-close-brace-in-struct.rs:4:1
| |
LL | pub(crate) struct Bar<T> {
| --- while parsing this struct
...
LL | trait T { LL | trait T {
| ^^^^^ expected identifier, found keyword | ^^^^^ expected identifier, found keyword

View file

@ -1,6 +1,9 @@
error: expected one of `>`, a const expression, lifetime, or type, found `}` error: expected one of `>`, a const expression, lifetime, or type, found `}`
--> $DIR/missing-closing-angle-bracket-struct-field-ty.rs:9:1 --> $DIR/missing-closing-angle-bracket-struct-field-ty.rs:9:1
| |
LL | pub struct Foo {
| --- while parsing this struct
...
LL | c: Arc<Mutex<usize>>, LL | c: Arc<Mutex<usize>>,
| - expected one of `>`, a const expression, lifetime, or type | - expected one of `>`, a const expression, lifetime, or type
LL | } LL | }

View file

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

View file

@ -1,12 +1,16 @@
error: struct fields are separated by `,` error: struct fields are separated by `,`
--> $DIR/recover-field-semi.rs:2:13 --> $DIR/recover-field-semi.rs:2:13
| |
LL | struct Foo {
| --- while parsing this struct
LL | foo: i32; LL | foo: i32;
| ^ help: replace `;` with `,` | ^ help: replace `;` with `,`
error: union fields are separated by `,` error: union fields are separated by `,`
--> $DIR/recover-field-semi.rs:7:13 --> $DIR/recover-field-semi.rs:7:13
| |
LL | union Bar {
| --- while parsing this union
LL | foo: i32; LL | foo: i32;
| ^ help: replace `;` with `,` | ^ help: replace `;` with `,`
@ -14,7 +18,9 @@ error: struct fields are separated by `,`
--> $DIR/recover-field-semi.rs:12:19 --> $DIR/recover-field-semi.rs:12:19
| |
LL | Qux { foo: i32; } LL | Qux { foo: i32; }
| ^ help: replace `;` with `,` | --- ^ help: replace `;` with `,`
| |
| while parsing this struct
error: unions cannot have zero fields error: unions cannot have zero fields
--> $DIR/recover-field-semi.rs:6:1 --> $DIR/recover-field-semi.rs:6:1

View file

@ -1,6 +1,8 @@
error: expected `:`, found `Bad` error: expected `:`, found `Bad`
--> $DIR/recover-struct.rs:4:9 --> $DIR/recover-struct.rs:4:9
| |
LL | struct Test {
| ---- while parsing this struct
LL | Very LL | Very
| - expected `:` | - expected `:`
LL | Bad LL | Bad

View file

@ -2,7 +2,9 @@ error: expected `:`, found `,`
--> $DIR/recovered-struct-variant.rs:2:10 --> $DIR/recovered-struct-variant.rs:2:10
| |
LL | A { a, b: usize } LL | A { a, b: usize }
| ^ expected `:` | - ^ expected `:`
| |
| while parsing this struct
error: aborting due to previous error 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 --> $DIR/removed-syntax-enum-newtype.rs:1:8
| |
LL | enum e = isize; LL | enum e = isize;
| ^ expected one of `<`, `where`, or `{` | - ^ expected one of `<`, `where`, or `{`
| |
| while parsing this enum
error: aborting due to previous error error: aborting due to previous error

View file

@ -1,6 +1,8 @@
error: expected identifier, found keyword `let` error: expected identifier, found keyword `let`
--> $DIR/removed-syntax-field-let.rs:2:5 --> $DIR/removed-syntax-field-let.rs:2:5
| |
LL | struct S {
| - while parsing this struct
LL | let foo: (), LL | let foo: (),
| ^^^ expected identifier, found keyword | ^^^ expected identifier, found keyword

View file

@ -1,6 +1,8 @@
error: struct fields are separated by `,` error: struct fields are separated by `,`
--> $DIR/removed-syntax-field-semicolon.rs:2:12 --> $DIR/removed-syntax-field-semicolon.rs:2:12
| |
LL | struct S {
| - while parsing this struct
LL | bar: (); LL | bar: ();
| ^ help: replace `;` with `,` | ^ help: replace `;` with `,`

View file

@ -1,6 +1,8 @@
error: expected type, found `0` error: expected type, found `0`
--> $DIR/issue-66270-pat-struct-parser-recovery.rs:4:22 --> $DIR/issue-66270-pat-struct-parser-recovery.rs:4:22
| |
LL | struct Bug {
| --- while parsing this struct
LL | incorrect_field: 0, LL | incorrect_field: 0,
| ^ expected type | ^ expected type

View file

@ -2,7 +2,10 @@ error: expected `:`, found `}`
--> $DIR/derive-bad.rs:6:10 --> $DIR/derive-bad.rs:6:10
| |
LL | #[derive(A)] 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) = 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 `(` error: expected identifier, found `(`
--> $DIR/pub-restricted-error.rs:4:16 --> $DIR/pub-restricted-error.rs:4:16
| |
LL | struct Foo {
| --- while parsing this struct
LL | pub(crate) () foo: usize, LL | pub(crate) () foo: usize,
| ^ expected identifier | ^ expected identifier

View file

@ -1,6 +1,9 @@
error: functions are not allowed in struct definitions error: functions are not allowed in struct definitions
--> $DIR/struct-fn-in-definition.rs:9:5 --> $DIR/struct-fn-in-definition.rs:9:5
| |
LL | struct S {
| - while parsing this struct
...
LL | fn foo() {} LL | fn foo() {}
| ^^^^^^^^^^^ | ^^^^^^^^^^^
| |
@ -10,6 +13,9 @@ LL | fn foo() {}
error: functions are not allowed in union definitions error: functions are not allowed in union definitions
--> $DIR/struct-fn-in-definition.rs:18:5 --> $DIR/struct-fn-in-definition.rs:18:5
| |
LL | union U {
| - while parsing this union
...
LL | fn foo() {} LL | fn foo() {}
| ^^^^^^^^^^^ | ^^^^^^^^^^^
| |
@ -19,6 +25,9 @@ LL | fn foo() {}
error: functions are not allowed in enum definitions error: functions are not allowed in enum definitions
--> $DIR/struct-fn-in-definition.rs:27:5 --> $DIR/struct-fn-in-definition.rs:27:5
| |
LL | enum E {
| - while parsing this enum
...
LL | fn foo() {} LL | fn foo() {}
| ^^^^^^^^^^^ | ^^^^^^^^^^^
| |

View file

@ -12,6 +12,8 @@ LL | a: foo::A,
error: expected `,`, or `}`, found `:` error: expected `,`, or `}`, found `:`
--> $DIR/struct-field-type-including-single-colon.rs:9:11 --> $DIR/struct-field-type-including-single-colon.rs:9:11
| |
LL | struct Foo {
| --- while parsing this struct
LL | a: foo:A, LL | a: foo:A,
| ^ | ^
@ -29,6 +31,8 @@ LL | b: foo::bar::B,
error: expected `,`, or `}`, found `:` error: expected `,`, or `}`, found `:`
--> $DIR/struct-field-type-including-single-colon.rs:15:16 --> $DIR/struct-field-type-including-single-colon.rs:15:16
| |
LL | struct Bar {
| --- while parsing this struct
LL | b: foo::bar:B, 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 --> $DIR/issue-69378-ice-on-invalid-type-node-after-recovery.rs:3:14
| |
LL | struct Foo { 0: u8 } LL | struct Foo { 0: u8 }
| ^ expected identifier | --- ^ expected identifier
| |
| while parsing this struct
error: aborting due to previous error error: aborting due to previous error