Skip to content

Commit c455dac

Browse files
authored
Rollup merge of rust-lang#102314 - TaKO8Ki:add-label-to-struct-enum-union-ident, r=estebank
Add a label to struct/enum/union ident name Based on rust-lang#94996 (comment) cc: ``@estebank``
2 parents e096298 + 9d49d64 commit c455dac

23 files changed

+96
-21
lines changed

compiler/rustc_parse/src/parser/item.rs

+29-15
Original file line numberDiff line numberDiff line change
@@ -1283,12 +1283,10 @@ impl<'a> Parser<'a> {
12831283
/// Parses an enum declaration.
12841284
fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
12851285
if self.token.is_keyword(kw::Struct) {
1286-
let mut err = self.struct_span_err(
1287-
self.prev_token.span.to(self.token.span),
1288-
"`enum` and `struct` are mutually exclusive",
1289-
);
1286+
let span = self.prev_token.span.to(self.token.span);
1287+
let mut err = self.struct_span_err(span, "`enum` and `struct` are mutually exclusive");
12901288
err.span_suggestion(
1291-
self.prev_token.span.to(self.token.span),
1289+
span,
12921290
"replace `enum struct` with",
12931291
"enum",
12941292
Applicability::MachineApplicable,
@@ -1307,7 +1305,8 @@ impl<'a> Parser<'a> {
13071305

13081306
let (variants, _) = self
13091307
.parse_delim_comma_seq(Delimiter::Brace, |p| p.parse_enum_variant())
1310-
.map_err(|e| {
1308+
.map_err(|mut e| {
1309+
e.span_label(id.span, "while parsing this enum");
13111310
self.recover_stmt();
13121311
e
13131312
})?;
@@ -1332,7 +1331,8 @@ impl<'a> Parser<'a> {
13321331

13331332
let struct_def = if this.check(&token::OpenDelim(Delimiter::Brace)) {
13341333
// Parse a struct variant.
1335-
let (fields, recovered) = this.parse_record_struct_body("struct", false)?;
1334+
let (fields, recovered) =
1335+
this.parse_record_struct_body("struct", ident.span, false)?;
13361336
VariantData::Struct(fields, recovered)
13371337
} else if this.check(&token::OpenDelim(Delimiter::Parenthesis)) {
13381338
VariantData::Tuple(this.parse_tuple_struct_body()?, DUMMY_NODE_ID)
@@ -1386,17 +1386,23 @@ impl<'a> Parser<'a> {
13861386
VariantData::Unit(DUMMY_NODE_ID)
13871387
} else {
13881388
// If we see: `struct Foo<T> where T: Copy { ... }`
1389-
let (fields, recovered) =
1390-
self.parse_record_struct_body("struct", generics.where_clause.has_where_token)?;
1389+
let (fields, recovered) = self.parse_record_struct_body(
1390+
"struct",
1391+
class_name.span,
1392+
generics.where_clause.has_where_token,
1393+
)?;
13911394
VariantData::Struct(fields, recovered)
13921395
}
13931396
// No `where` so: `struct Foo<T>;`
13941397
} else if self.eat(&token::Semi) {
13951398
VariantData::Unit(DUMMY_NODE_ID)
13961399
// Record-style struct definition
13971400
} else if self.token == token::OpenDelim(Delimiter::Brace) {
1398-
let (fields, recovered) =
1399-
self.parse_record_struct_body("struct", generics.where_clause.has_where_token)?;
1401+
let (fields, recovered) = self.parse_record_struct_body(
1402+
"struct",
1403+
class_name.span,
1404+
generics.where_clause.has_where_token,
1405+
)?;
14001406
VariantData::Struct(fields, recovered)
14011407
// Tuple-style struct definition with optional where-clause.
14021408
} else if self.token == token::OpenDelim(Delimiter::Parenthesis) {
@@ -1425,12 +1431,18 @@ impl<'a> Parser<'a> {
14251431

14261432
let vdata = if self.token.is_keyword(kw::Where) {
14271433
generics.where_clause = self.parse_where_clause()?;
1428-
let (fields, recovered) =
1429-
self.parse_record_struct_body("union", generics.where_clause.has_where_token)?;
1434+
let (fields, recovered) = self.parse_record_struct_body(
1435+
"union",
1436+
class_name.span,
1437+
generics.where_clause.has_where_token,
1438+
)?;
14301439
VariantData::Struct(fields, recovered)
14311440
} else if self.token == token::OpenDelim(Delimiter::Brace) {
1432-
let (fields, recovered) =
1433-
self.parse_record_struct_body("union", generics.where_clause.has_where_token)?;
1441+
let (fields, recovered) = self.parse_record_struct_body(
1442+
"union",
1443+
class_name.span,
1444+
generics.where_clause.has_where_token,
1445+
)?;
14341446
VariantData::Struct(fields, recovered)
14351447
} else {
14361448
let token_str = super::token_descr(&self.token);
@@ -1446,6 +1458,7 @@ impl<'a> Parser<'a> {
14461458
fn parse_record_struct_body(
14471459
&mut self,
14481460
adt_ty: &str,
1461+
ident_span: Span,
14491462
parsed_where: bool,
14501463
) -> PResult<'a, (Vec<FieldDef>, /* recovered */ bool)> {
14511464
let mut fields = Vec::new();
@@ -1460,6 +1473,7 @@ impl<'a> Parser<'a> {
14601473
match field {
14611474
Ok(field) => fields.push(field),
14621475
Err(mut err) => {
1476+
err.span_label(ident_span, format!("while parsing this {adt_ty}"));
14631477
err.emit();
14641478
break;
14651479
}

src/test/ui/async-await/await-keyword/2018-edition-error-in-non-macro-position.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ error: expected identifier, found keyword `await`
4646
--> $DIR/2018-edition-error-in-non-macro-position.rs:13:14
4747
|
4848
LL | struct Foo { await: () }
49-
| ^^^^^ expected identifier, found keyword
49+
| --- ^^^^^ expected identifier, found keyword
50+
| |
51+
| while parsing this struct
5052
|
5153
help: escape `await` to use it as an identifier
5254
|

src/test/ui/parser/doc-before-struct-rbrace-1.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0585]: found a documentation comment that doesn't document anything
22
--> $DIR/doc-before-struct-rbrace-1.rs:3:5
33
|
4+
LL | struct X {
5+
| - while parsing this struct
6+
LL | a: u8,
47
LL | /// document
58
| ^^^^^^^^^^^^
69
|

src/test/ui/parser/fn-field-parse-error-ice.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ LL | inner : dyn fn ()
77
error: functions are not allowed in struct definitions
88
--> $DIR/fn-field-parse-error-ice.rs:4:17
99
|
10+
LL | struct Baz {
11+
| --- while parsing this struct
1012
LL | inner : dyn fn ()
1113
| ^^
1214
|

src/test/ui/parser/issues/issue-101540.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error: structs are not allowed in struct definitions
22
--> $DIR/issue-101540.rs:2:5
33
|
4+
LL | struct S1 {
5+
| -- while parsing this struct
46
LL | struct S2 {
57
| ^^^^^^^^^
68
|

src/test/ui/parser/issues/issue-48636.stderr

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

src/test/ui/parser/issues/issue-68000-unicode-ident-after-missing-comma.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ LL | pub bar: Vec<i32>ö
77
error: expected `:`, found `}`
88
--> $DIR/issue-68000-unicode-ident-after-missing-comma.rs:4:1
99
|
10+
LL | pub struct Foo {
11+
| --- while parsing this struct
1012
LL | pub bar: Vec<i32>ö
1113
| - expected `:`
1214
LL |

src/test/ui/parser/macro/issue-37113.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error: expected identifier, found `String`
22
--> $DIR/issue-37113.rs:4:16
33
|
4+
LL | enum SomeEnum {
5+
| -------- while parsing this enum
46
LL | $( $t, )*
57
| ^^ expected identifier
68
...

src/test/ui/parser/mismatched-braces/missing-close-brace-in-struct.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ LL | fn main() {}
1010
error: expected identifier, found keyword `trait`
1111
--> $DIR/missing-close-brace-in-struct.rs:4:1
1212
|
13+
LL | pub(crate) struct Bar<T> {
14+
| --- while parsing this struct
15+
...
1316
LL | trait T {
1417
| ^^^^^ expected identifier, found keyword
1518

src/test/ui/parser/missing-closing-angle-bracket-struct-field-ty.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error: expected one of `>`, a const expression, lifetime, or type, found `}`
22
--> $DIR/missing-closing-angle-bracket-struct-field-ty.rs:9:1
33
|
4+
LL | pub struct Foo {
5+
| --- while parsing this struct
6+
...
47
LL | c: Arc<Mutex<usize>>,
58
| - expected one of `>`, a const expression, lifetime, or type
69
LL | }

src/test/ui/parser/recover-enum2.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error: expected type, found `{`
22
--> $DIR/recover-enum2.rs:6:18
33
|
4+
LL | Var3 {
5+
| ---- while parsing this struct
46
LL | abc: {},
57
| ^ expected type
68

src/test/ui/parser/recover-field-semi.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
error: struct fields are separated by `,`
22
--> $DIR/recover-field-semi.rs:2:13
33
|
4+
LL | struct Foo {
5+
| --- while parsing this struct
46
LL | foo: i32;
57
| ^ help: replace `;` with `,`
68

79
error: union fields are separated by `,`
810
--> $DIR/recover-field-semi.rs:7:13
911
|
12+
LL | union Bar {
13+
| --- while parsing this union
1014
LL | foo: i32;
1115
| ^ help: replace `;` with `,`
1216

1317
error: struct fields are separated by `,`
1418
--> $DIR/recover-field-semi.rs:12:19
1519
|
1620
LL | Qux { foo: i32; }
17-
| ^ help: replace `;` with `,`
21+
| --- ^ help: replace `;` with `,`
22+
| |
23+
| while parsing this struct
1824

1925
error: unions cannot have zero fields
2026
--> $DIR/recover-field-semi.rs:6:1

src/test/ui/parser/recover-struct.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error: expected `:`, found `Bad`
22
--> $DIR/recover-struct.rs:4:9
33
|
4+
LL | struct Test {
5+
| ---- while parsing this struct
46
LL | Very
57
| - expected `:`
68
LL | Bad

src/test/ui/parser/recovered-struct-variant.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error: expected `:`, found `,`
22
--> $DIR/recovered-struct-variant.rs:2:10
33
|
44
LL | A { a, b: usize }
5-
| ^ expected `:`
5+
| - ^ expected `:`
6+
| |
7+
| while parsing this struct
68

79
error: aborting due to previous error
810

src/test/ui/parser/removed-syntax-enum-newtype.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error: expected one of `<`, `where`, or `{`, found `=`
22
--> $DIR/removed-syntax-enum-newtype.rs:1:8
33
|
44
LL | enum e = isize;
5-
| ^ expected one of `<`, `where`, or `{`
5+
| - ^ expected one of `<`, `where`, or `{`
6+
| |
7+
| while parsing this enum
68

79
error: aborting due to previous error
810

src/test/ui/parser/removed-syntax-field-let.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error: expected identifier, found keyword `let`
22
--> $DIR/removed-syntax-field-let.rs:2:5
33
|
4+
LL | struct S {
5+
| - while parsing this struct
46
LL | let foo: (),
57
| ^^^ expected identifier, found keyword
68

src/test/ui/parser/removed-syntax-field-semicolon.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error: struct fields are separated by `,`
22
--> $DIR/removed-syntax-field-semicolon.rs:2:12
33
|
4+
LL | struct S {
5+
| - while parsing this struct
46
LL | bar: ();
57
| ^ help: replace `;` with `,`
68

src/test/ui/pattern/issue-66270-pat-struct-parser-recovery.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error: expected type, found `0`
22
--> $DIR/issue-66270-pat-struct-parser-recovery.rs:4:22
33
|
4+
LL | struct Bug {
5+
| --- while parsing this struct
46
LL | incorrect_field: 0,
57
| ^ expected type
68

src/test/ui/proc-macro/derive-bad.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error: expected `:`, found `}`
22
--> $DIR/derive-bad.rs:6:10
33
|
44
LL | #[derive(A)]
5-
| ^ expected `:`
5+
| ^
6+
| |
7+
| expected `:`
8+
| while parsing this struct
69
|
710
= note: this error originates in the derive macro `A` (in Nightly builds, run with -Z macro-backtrace for more info)
811

src/test/ui/pub/pub-restricted-error.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error: expected identifier, found `(`
22
--> $DIR/pub-restricted-error.rs:4:16
33
|
4+
LL | struct Foo {
5+
| --- while parsing this struct
46
LL | pub(crate) () foo: usize,
57
| ^ expected identifier
68

src/test/ui/structs/struct-fn-in-definition.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error: functions are not allowed in struct definitions
22
--> $DIR/struct-fn-in-definition.rs:9:5
33
|
4+
LL | struct S {
5+
| - while parsing this struct
6+
...
47
LL | fn foo() {}
58
| ^^^^^^^^^^^
69
|
@@ -10,6 +13,9 @@ LL | fn foo() {}
1013
error: functions are not allowed in union definitions
1114
--> $DIR/struct-fn-in-definition.rs:18:5
1215
|
16+
LL | union U {
17+
| - while parsing this union
18+
...
1319
LL | fn foo() {}
1420
| ^^^^^^^^^^^
1521
|
@@ -19,6 +25,9 @@ LL | fn foo() {}
1925
error: functions are not allowed in enum definitions
2026
--> $DIR/struct-fn-in-definition.rs:27:5
2127
|
28+
LL | enum E {
29+
| - while parsing this enum
30+
...
2231
LL | fn foo() {}
2332
| ^^^^^^^^^^^
2433
|

src/test/ui/suggestions/struct-field-type-including-single-colon.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ LL | a: foo::A,
1212
error: expected `,`, or `}`, found `:`
1313
--> $DIR/struct-field-type-including-single-colon.rs:9:11
1414
|
15+
LL | struct Foo {
16+
| --- while parsing this struct
1517
LL | a: foo:A,
1618
| ^
1719

@@ -29,6 +31,8 @@ LL | b: foo::bar::B,
2931
error: expected `,`, or `}`, found `:`
3032
--> $DIR/struct-field-type-including-single-colon.rs:15:16
3133
|
34+
LL | struct Bar {
35+
| --- while parsing this struct
3236
LL | b: foo::bar:B,
3337
| ^
3438

src/test/ui/typeck/issue-69378-ice-on-invalid-type-node-after-recovery.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error: expected identifier, found `0`
22
--> $DIR/issue-69378-ice-on-invalid-type-node-after-recovery.rs:3:14
33
|
44
LL | struct Foo { 0: u8 }
5-
| ^ expected identifier
5+
| --- ^ expected identifier
6+
| |
7+
| while parsing this struct
68

79
error: aborting due to previous error
810

0 commit comments

Comments
 (0)