Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d9297d2

Browse files
committed
Auto 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 de0b511 + 5d05908 commit d9297d2

23 files changed

+96
-21
lines changed

compiler/rustc_parse/src/parser/item.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,12 +1291,10 @@ impl<'a> Parser<'a> {
12911291
/// Parses an enum declaration.
12921292
fn parse_item_enum(&mut self) -> PResult<'a, ItemInfo> {
12931293
if self.token.is_keyword(kw::Struct) {
1294-
let mut err = self.struct_span_err(
1295-
self.prev_token.span.to(self.token.span),
1296-
"`enum` and `struct` are mutually exclusive",
1297-
);
1294+
let span = self.prev_token.span.to(self.token.span);
1295+
let mut err = self.struct_span_err(span, "`enum` and `struct` are mutually exclusive");
12981296
err.span_suggestion(
1299-
self.prev_token.span.to(self.token.span),
1297+
span,
13001298
"replace `enum struct` with",
13011299
"enum",
13021300
Applicability::MachineApplicable,
@@ -1320,7 +1318,8 @@ impl<'a> Parser<'a> {
13201318
(vec![], false)
13211319
} else {
13221320
self.parse_delim_comma_seq(Delimiter::Brace, |p| p.parse_enum_variant()).map_err(
1323-
|e| {
1321+
|mut e| {
1322+
e.span_label(id.span, "while parsing this enum");
13241323
self.recover_stmt();
13251324
e
13261325
},
@@ -1347,7 +1346,8 @@ impl<'a> Parser<'a> {
13471346

13481347
let struct_def = if this.check(&token::OpenDelim(Delimiter::Brace)) {
13491348
// Parse a struct variant.
1350-
let (fields, recovered) = this.parse_record_struct_body("struct", false)?;
1349+
let (fields, recovered) =
1350+
this.parse_record_struct_body("struct", ident.span, false)?;
13511351
VariantData::Struct(fields, recovered)
13521352
} else if this.check(&token::OpenDelim(Delimiter::Parenthesis)) {
13531353
VariantData::Tuple(this.parse_tuple_struct_body()?, DUMMY_NODE_ID)
@@ -1401,17 +1401,23 @@ impl<'a> Parser<'a> {
14011401
VariantData::Unit(DUMMY_NODE_ID)
14021402
} else {
14031403
// If we see: `struct Foo<T> where T: Copy { ... }`
1404-
let (fields, recovered) =
1405-
self.parse_record_struct_body("struct", generics.where_clause.has_where_token)?;
1404+
let (fields, recovered) = self.parse_record_struct_body(
1405+
"struct",
1406+
class_name.span,
1407+
generics.where_clause.has_where_token,
1408+
)?;
14061409
VariantData::Struct(fields, recovered)
14071410
}
14081411
// No `where` so: `struct Foo<T>;`
14091412
} else if self.eat(&token::Semi) {
14101413
VariantData::Unit(DUMMY_NODE_ID)
14111414
// Record-style struct definition
14121415
} else if self.token == token::OpenDelim(Delimiter::Brace) {
1413-
let (fields, recovered) =
1414-
self.parse_record_struct_body("struct", generics.where_clause.has_where_token)?;
1416+
let (fields, recovered) = self.parse_record_struct_body(
1417+
"struct",
1418+
class_name.span,
1419+
generics.where_clause.has_where_token,
1420+
)?;
14151421
VariantData::Struct(fields, recovered)
14161422
// Tuple-style struct definition with optional where-clause.
14171423
} else if self.token == token::OpenDelim(Delimiter::Parenthesis) {
@@ -1440,12 +1446,18 @@ impl<'a> Parser<'a> {
14401446

14411447
let vdata = if self.token.is_keyword(kw::Where) {
14421448
generics.where_clause = self.parse_where_clause()?;
1443-
let (fields, recovered) =
1444-
self.parse_record_struct_body("union", generics.where_clause.has_where_token)?;
1449+
let (fields, recovered) = self.parse_record_struct_body(
1450+
"union",
1451+
class_name.span,
1452+
generics.where_clause.has_where_token,
1453+
)?;
14451454
VariantData::Struct(fields, recovered)
14461455
} else if self.token == token::OpenDelim(Delimiter::Brace) {
1447-
let (fields, recovered) =
1448-
self.parse_record_struct_body("union", generics.where_clause.has_where_token)?;
1456+
let (fields, recovered) = self.parse_record_struct_body(
1457+
"union",
1458+
class_name.span,
1459+
generics.where_clause.has_where_token,
1460+
)?;
14491461
VariantData::Struct(fields, recovered)
14501462
} else {
14511463
let token_str = super::token_descr(&self.token);
@@ -1461,6 +1473,7 @@ impl<'a> Parser<'a> {
14611473
fn parse_record_struct_body(
14621474
&mut self,
14631475
adt_ty: &str,
1476+
ident_span: Span,
14641477
parsed_where: bool,
14651478
) -> PResult<'a, (Vec<FieldDef>, /* recovered */ bool)> {
14661479
let mut fields = Vec::new();
@@ -1475,6 +1488,7 @@ impl<'a> Parser<'a> {
14751488
match field {
14761489
Ok(field) => fields.push(field),
14771490
Err(mut err) => {
1491+
err.span_label(ident_span, format!("while parsing this {adt_ty}"));
14781492
err.emit();
14791493
break;
14801494
}

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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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 | }

0 commit comments

Comments
 (0)