1
1
use super :: diagnostics:: { dummy_arg, ConsumeClosingDelim , Error } ;
2
2
use super :: ty:: { AllowPlus , RecoverQPath , RecoverReturnSign } ;
3
- use super :: { AttrWrapper , FollowedByType , ForceCollect , Parser , PathStyle , TrailingToken } ;
3
+ use super :: { AttrWrapper , FollowedByType , ForceCollect , Parser , PathStyle , SeqSep , TrailingToken } ;
4
4
5
5
use rustc_ast:: ast:: * ;
6
6
use rustc_ast:: ptr:: P ;
@@ -272,14 +272,14 @@ impl<'a> Parser<'a> {
272
272
self . parse_type_alias ( def ( ) ) ?
273
273
} else if self . eat_keyword ( kw:: Enum ) {
274
274
// ENUM ITEM
275
- self . parse_item_enum ( ) ?
275
+ self . parse_item_enum ( attrs ) ?
276
276
} else if self . eat_keyword ( kw:: Struct ) {
277
277
// STRUCT ITEM
278
- self . parse_item_struct ( ) ?
278
+ self . parse_item_struct ( attrs ) ?
279
279
} else if self . is_kw_followed_by_ident ( kw:: Union ) {
280
280
// UNION ITEM
281
281
self . bump ( ) ; // `union`
282
- self . parse_item_union ( ) ?
282
+ self . parse_item_union ( attrs ) ?
283
283
} else if self . eat_keyword ( kw:: Macro ) {
284
284
// MACROS 2.0 ITEM
285
285
self . parse_item_decl_macro ( lo) ?
@@ -1190,13 +1190,20 @@ impl<'a> Parser<'a> {
1190
1190
}
1191
1191
1192
1192
/// Parses an enum declaration.
1193
- fn parse_item_enum ( & mut self ) -> PResult < ' a , ItemInfo > {
1193
+ fn parse_item_enum ( & mut self , attrs : & mut Vec < Attribute > ) -> PResult < ' a , ItemInfo > {
1194
1194
let id = self . parse_ident ( ) ?;
1195
1195
let mut generics = self . parse_generics ( ) ?;
1196
1196
generics. where_clause = self . parse_where_clause ( ) ?;
1197
1197
1198
- let ( variants, _) =
1199
- self . parse_delim_comma_seq ( token:: Brace , |p| p. parse_enum_variant ( ) ) . map_err ( |e| {
1198
+ self . expect ( & token:: OpenDelim ( token:: Brace ) ) ?;
1199
+ attrs. append ( & mut self . parse_inner_attributes ( ) ?) ;
1200
+ let ( variants, _) = self
1201
+ . parse_seq_to_end (
1202
+ & token:: CloseDelim ( token:: Brace ) ,
1203
+ SeqSep :: trailing_allowed ( token:: Comma ) ,
1204
+ |p| p. parse_enum_variant ( ) ,
1205
+ )
1206
+ . map_err ( |e| {
1200
1207
self . recover_stmt ( ) ;
1201
1208
e
1202
1209
} ) ?;
@@ -1210,7 +1217,7 @@ impl<'a> Parser<'a> {
1210
1217
self . collect_tokens_trailing_token (
1211
1218
variant_attrs,
1212
1219
ForceCollect :: No ,
1213
- |this, variant_attrs| {
1220
+ |this, mut variant_attrs| {
1214
1221
let vlo = this. token . span ;
1215
1222
1216
1223
let vis = this. parse_visibility ( FollowedByType :: No ) ?;
@@ -1221,7 +1228,8 @@ impl<'a> Parser<'a> {
1221
1228
1222
1229
let struct_def = if this. check ( & token:: OpenDelim ( token:: Brace ) ) {
1223
1230
// Parse a struct variant.
1224
- let ( fields, recovered) = this. parse_record_struct_body ( "struct" , false ) ?;
1231
+ let ( fields, recovered) =
1232
+ this. parse_record_struct_body ( "struct" , false , & mut variant_attrs) ?;
1225
1233
VariantData :: Struct ( fields, recovered)
1226
1234
} else if this. check ( & token:: OpenDelim ( token:: Paren ) ) {
1227
1235
VariantData :: Tuple ( this. parse_tuple_struct_body ( ) ?, DUMMY_NODE_ID )
@@ -1249,7 +1257,7 @@ impl<'a> Parser<'a> {
1249
1257
}
1250
1258
1251
1259
/// Parses `struct Foo { ... }`.
1252
- fn parse_item_struct ( & mut self ) -> PResult < ' a , ItemInfo > {
1260
+ fn parse_item_struct ( & mut self , attrs : & mut Vec < Attribute > ) -> PResult < ' a , ItemInfo > {
1253
1261
let class_name = self . parse_ident ( ) ?;
1254
1262
1255
1263
let mut generics = self . parse_generics ( ) ?;
@@ -1275,17 +1283,23 @@ impl<'a> Parser<'a> {
1275
1283
VariantData :: Unit ( DUMMY_NODE_ID )
1276
1284
} else {
1277
1285
// If we see: `struct Foo<T> where T: Copy { ... }`
1278
- let ( fields, recovered) =
1279
- self . parse_record_struct_body ( "struct" , generics. where_clause . has_where_token ) ?;
1286
+ let ( fields, recovered) = self . parse_record_struct_body (
1287
+ "struct" ,
1288
+ generics. where_clause . has_where_token ,
1289
+ attrs,
1290
+ ) ?;
1280
1291
VariantData :: Struct ( fields, recovered)
1281
1292
}
1282
1293
// No `where` so: `struct Foo<T>;`
1283
1294
} else if self . eat ( & token:: Semi ) {
1284
1295
VariantData :: Unit ( DUMMY_NODE_ID )
1285
1296
// Record-style struct definition
1286
1297
} else if self . token == token:: OpenDelim ( token:: Brace ) {
1287
- let ( fields, recovered) =
1288
- self . parse_record_struct_body ( "struct" , generics. where_clause . has_where_token ) ?;
1298
+ let ( fields, recovered) = self . parse_record_struct_body (
1299
+ "struct" ,
1300
+ generics. where_clause . has_where_token ,
1301
+ attrs,
1302
+ ) ?;
1289
1303
VariantData :: Struct ( fields, recovered)
1290
1304
// Tuple-style struct definition with optional where-clause.
1291
1305
} else if self . token == token:: OpenDelim ( token:: Paren ) {
@@ -1308,19 +1322,25 @@ impl<'a> Parser<'a> {
1308
1322
}
1309
1323
1310
1324
/// Parses `union Foo { ... }`.
1311
- fn parse_item_union ( & mut self ) -> PResult < ' a , ItemInfo > {
1325
+ fn parse_item_union ( & mut self , attrs : & mut Vec < Attribute > ) -> PResult < ' a , ItemInfo > {
1312
1326
let class_name = self . parse_ident ( ) ?;
1313
1327
1314
1328
let mut generics = self . parse_generics ( ) ?;
1315
1329
1316
1330
let vdata = if self . token . is_keyword ( kw:: Where ) {
1317
1331
generics. where_clause = self . parse_where_clause ( ) ?;
1318
- let ( fields, recovered) =
1319
- self . parse_record_struct_body ( "union" , generics. where_clause . has_where_token ) ?;
1332
+ let ( fields, recovered) = self . parse_record_struct_body (
1333
+ "union" ,
1334
+ generics. where_clause . has_where_token ,
1335
+ attrs,
1336
+ ) ?;
1320
1337
VariantData :: Struct ( fields, recovered)
1321
1338
} else if self . token == token:: OpenDelim ( token:: Brace ) {
1322
- let ( fields, recovered) =
1323
- self . parse_record_struct_body ( "union" , generics. where_clause . has_where_token ) ?;
1339
+ let ( fields, recovered) = self . parse_record_struct_body (
1340
+ "union" ,
1341
+ generics. where_clause . has_where_token ,
1342
+ attrs,
1343
+ ) ?;
1324
1344
VariantData :: Struct ( fields, recovered)
1325
1345
} else {
1326
1346
let token_str = super :: token_descr ( & self . token ) ;
@@ -1337,10 +1357,12 @@ impl<'a> Parser<'a> {
1337
1357
& mut self ,
1338
1358
adt_ty : & str ,
1339
1359
parsed_where : bool ,
1360
+ attrs : & mut Vec < Attribute > ,
1340
1361
) -> PResult < ' a , ( Vec < FieldDef > , /* recovered */ bool ) > {
1341
1362
let mut fields = Vec :: new ( ) ;
1342
1363
let mut recovered = false ;
1343
1364
if self . eat ( & token:: OpenDelim ( token:: Brace ) ) {
1365
+ attrs. append ( & mut self . parse_inner_attributes ( ) ?) ;
1344
1366
while self . token != token:: CloseDelim ( token:: Brace ) {
1345
1367
let field = self . parse_field_def ( adt_ty) . map_err ( |e| {
1346
1368
self . consume_block ( token:: Brace , ConsumeClosingDelim :: No ) ;
0 commit comments