Skip to content

Commit 63e5c93

Browse files
committed
add a label to struct/enum/union ident name
1 parent a3e2314 commit 63e5c93

22 files changed

+91
-16
lines changed

compiler/rustc_parse/src/parser/item.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,8 @@ impl<'a> Parser<'a> {
13051305

13061306
let (variants, _) = self
13071307
.parse_delim_comma_seq(Delimiter::Brace, |p| p.parse_enum_variant())
1308-
.map_err(|e| {
1308+
.map_err(|mut e| {
1309+
e.span_label(id.span, "while parsing this enum");
13091310
self.recover_stmt();
13101311
e
13111312
})?;
@@ -1330,7 +1331,8 @@ impl<'a> Parser<'a> {
13301331

13311332
let struct_def = if this.check(&token::OpenDelim(Delimiter::Brace)) {
13321333
// Parse a struct variant.
1333-
let (fields, recovered) = this.parse_record_struct_body("struct", false)?;
1334+
let (fields, recovered) =
1335+
this.parse_record_struct_body("struct", ident.span, false)?;
13341336
VariantData::Struct(fields, recovered)
13351337
} else if this.check(&token::OpenDelim(Delimiter::Parenthesis)) {
13361338
VariantData::Tuple(this.parse_tuple_struct_body()?, DUMMY_NODE_ID)
@@ -1384,17 +1386,23 @@ impl<'a> Parser<'a> {
13841386
VariantData::Unit(DUMMY_NODE_ID)
13851387
} else {
13861388
// If we see: `struct Foo<T> where T: Copy { ... }`
1387-
let (fields, recovered) =
1388-
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+
)?;
13891394
VariantData::Struct(fields, recovered)
13901395
}
13911396
// No `where` so: `struct Foo<T>;`
13921397
} else if self.eat(&token::Semi) {
13931398
VariantData::Unit(DUMMY_NODE_ID)
13941399
// Record-style struct definition
13951400
} else if self.token == token::OpenDelim(Delimiter::Brace) {
1396-
let (fields, recovered) =
1397-
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+
)?;
13981406
VariantData::Struct(fields, recovered)
13991407
// Tuple-style struct definition with optional where-clause.
14001408
} else if self.token == token::OpenDelim(Delimiter::Parenthesis) {
@@ -1423,12 +1431,18 @@ impl<'a> Parser<'a> {
14231431

14241432
let vdata = if self.token.is_keyword(kw::Where) {
14251433
generics.where_clause = self.parse_where_clause()?;
1426-
let (fields, recovered) =
1427-
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+
)?;
14281439
VariantData::Struct(fields, recovered)
14291440
} else if self.token == token::OpenDelim(Delimiter::Brace) {
1430-
let (fields, recovered) =
1431-
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+
)?;
14321446
VariantData::Struct(fields, recovered)
14331447
} else {
14341448
let token_str = super::token_descr(&self.token);
@@ -1444,6 +1458,7 @@ impl<'a> Parser<'a> {
14441458
fn parse_record_struct_body(
14451459
&mut self,
14461460
adt_ty: &str,
1461+
ident_span: Span,
14471462
parsed_where: bool,
14481463
) -> PResult<'a, (Vec<FieldDef>, /* recovered */ bool)> {
14491464
let mut fields = Vec::new();
@@ -1458,6 +1473,7 @@ impl<'a> Parser<'a> {
14581473
match field {
14591474
Ok(field) => fields.push(field),
14601475
Err(mut err) => {
1476+
err.span_label(ident_span, format!("while parsing this {adt_ty}"));
14611477
err.emit();
14621478
break;
14631479
}

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

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

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

0 commit comments

Comments
 (0)