@@ -1283,12 +1283,10 @@ impl<'a> Parser<'a> {
1283
1283
/// Parses an enum declaration.
1284
1284
fn parse_item_enum ( & mut self ) -> PResult < ' a , ItemInfo > {
1285
1285
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" ) ;
1290
1288
err. span_suggestion (
1291
- self . prev_token . span . to ( self . token . span ) ,
1289
+ span,
1292
1290
"replace `enum struct` with" ,
1293
1291
"enum" ,
1294
1292
Applicability :: MachineApplicable ,
@@ -1307,7 +1305,8 @@ impl<'a> Parser<'a> {
1307
1305
1308
1306
let ( variants, _) = self
1309
1307
. 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" ) ;
1311
1310
self . recover_stmt ( ) ;
1312
1311
e
1313
1312
} ) ?;
@@ -1332,7 +1331,8 @@ impl<'a> Parser<'a> {
1332
1331
1333
1332
let struct_def = if this. check ( & token:: OpenDelim ( Delimiter :: Brace ) ) {
1334
1333
// 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 ) ?;
1336
1336
VariantData :: Struct ( fields, recovered)
1337
1337
} else if this. check ( & token:: OpenDelim ( Delimiter :: Parenthesis ) ) {
1338
1338
VariantData :: Tuple ( this. parse_tuple_struct_body ( ) ?, DUMMY_NODE_ID )
@@ -1386,17 +1386,23 @@ impl<'a> Parser<'a> {
1386
1386
VariantData :: Unit ( DUMMY_NODE_ID )
1387
1387
} else {
1388
1388
// 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
+ ) ?;
1391
1394
VariantData :: Struct ( fields, recovered)
1392
1395
}
1393
1396
// No `where` so: `struct Foo<T>;`
1394
1397
} else if self . eat ( & token:: Semi ) {
1395
1398
VariantData :: Unit ( DUMMY_NODE_ID )
1396
1399
// Record-style struct definition
1397
1400
} 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
+ ) ?;
1400
1406
VariantData :: Struct ( fields, recovered)
1401
1407
// Tuple-style struct definition with optional where-clause.
1402
1408
} else if self . token == token:: OpenDelim ( Delimiter :: Parenthesis ) {
@@ -1425,12 +1431,18 @@ impl<'a> Parser<'a> {
1425
1431
1426
1432
let vdata = if self . token . is_keyword ( kw:: Where ) {
1427
1433
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
+ ) ?;
1430
1439
VariantData :: Struct ( fields, recovered)
1431
1440
} 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
+ ) ?;
1434
1446
VariantData :: Struct ( fields, recovered)
1435
1447
} else {
1436
1448
let token_str = super :: token_descr ( & self . token ) ;
@@ -1446,6 +1458,7 @@ impl<'a> Parser<'a> {
1446
1458
fn parse_record_struct_body (
1447
1459
& mut self ,
1448
1460
adt_ty : & str ,
1461
+ ident_span : Span ,
1449
1462
parsed_where : bool ,
1450
1463
) -> PResult < ' a , ( Vec < FieldDef > , /* recovered */ bool ) > {
1451
1464
let mut fields = Vec :: new ( ) ;
@@ -1460,6 +1473,7 @@ impl<'a> Parser<'a> {
1460
1473
match field {
1461
1474
Ok ( field) => fields. push ( field) ,
1462
1475
Err ( mut err) => {
1476
+ err. span_label ( ident_span, format ! ( "while parsing this {adt_ty}" ) ) ;
1463
1477
err. emit ( ) ;
1464
1478
break ;
1465
1479
}
0 commit comments