Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a label to struct/enum/union ident name #102314

Merged
merged 3 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -1320,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
},
Expand All @@ -1347,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)
Expand Down Expand Up @@ -1401,17 +1401,23 @@ impl<'a> Parser<'a> {
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>;`
} else if self.eat(&token::Semi) {
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) {
Expand Down Expand Up @@ -1440,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);
Expand All @@ -1461,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<FieldDef>, /* recovered */ bool)> {
let mut fields = Vec::new();
Expand All @@ -1475,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/parser/doc-before-struct-rbrace-1.stderr
Original file line number Diff line number Diff line change
@@ -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
| ^^^^^^^^^^^^
|
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/parser/fn-field-parse-error-ice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
| ^^
|
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/parser/issues/issue-101540.stderr
Original file line number Diff line number Diff line change
@@ -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 {
| ^^^^^^^^^
|
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/parser/issues/issue-48636.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/parser/macro/issue-37113.stderr
Original file line number Diff line number Diff line change
@@ -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
...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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 | }
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/parser/recover-enum2.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error: expected type, found `{`
--> $DIR/recover-enum2.rs:6:18
|
LL | Var3 {
| ---- while parsing this struct
LL | abc: {},
| ^ expected type

Expand Down
8 changes: 7 additions & 1 deletion src/test/ui/parser/recover-field-semi.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
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 `,`

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
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/parser/recover-struct.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/parser/recovered-struct-variant.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

4 changes: 3 additions & 1 deletion src/test/ui/parser/removed-syntax-enum-newtype.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 2 additions & 0 deletions src/test/ui/parser/removed-syntax-field-let.stderr
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/parser/removed-syntax-field-semicolon.stderr
Original file line number Diff line number Diff line change
@@ -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 `,`

Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 4 additions & 1 deletion src/test/ui/proc-macro/derive-bad.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/pub/pub-restricted-error.stderr
Original file line number Diff line number Diff line change
@@ -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

Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/structs/struct-fn-in-definition.stderr
Original file line number Diff line number Diff line change
@@ -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() {}
| ^^^^^^^^^^^
|
Expand All @@ -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() {}
| ^^^^^^^^^^^
|
Expand All @@ -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() {}
| ^^^^^^^^^^^
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
| ^

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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