@@ -22,7 +22,7 @@ use syntax::tokenstream::{DelimSpan, TokenStream, TokenTree};
22
22
use log:: debug;
23
23
use std:: mem;
24
24
25
- pub ( super ) type ItemInfo = ( Ident , ItemKind , Option < Vec < Attribute > > ) ;
25
+ pub ( super ) type ItemInfo = ( Ident , ItemKind ) ;
26
26
27
27
impl < ' a > Parser < ' a > {
28
28
pub fn parse_item ( & mut self ) -> PResult < ' a , Option < P < Item > > > {
@@ -85,8 +85,8 @@ impl<'a> Parser<'a> {
85
85
let lo = self . token . span ;
86
86
let vis = self . parse_visibility ( FollowedByType :: No ) ?;
87
87
88
- if let Some ( info ) = self . parse_item_kind ( & mut attrs, macros_allowed, lo, & vis) ? {
89
- return Ok ( Some ( self . mk_item_with_info ( attrs , lo , vis, info ) ) ) ;
88
+ if let Some ( ( ident , kind ) ) = self . parse_item_kind ( & mut attrs, macros_allowed, lo, & vis) ? {
89
+ return Ok ( Some ( self . mk_item ( lo . to ( self . prev_span ) , ident , kind , vis, attrs ) ) ) ;
90
90
}
91
91
92
92
// FAILURE TO PARSE ITEM
@@ -118,18 +118,18 @@ impl<'a> Parser<'a> {
118
118
// USE ITEM
119
119
let tree = self . parse_use_tree ( ) ?;
120
120
self . expect_semi ( ) ?;
121
- ( Ident :: invalid ( ) , ItemKind :: Use ( P ( tree) ) , None )
121
+ ( Ident :: invalid ( ) , ItemKind :: Use ( P ( tree) ) )
122
122
} else if self . check_fn_front_matter ( ) {
123
123
// FUNCTION ITEM
124
124
let ( ident, sig, generics, body) = self . parse_fn ( & mut false , attrs, |_| true ) ?;
125
- ( ident, ItemKind :: Fn ( sig, generics, body) , None )
125
+ ( ident, ItemKind :: Fn ( sig, generics, body) )
126
126
} else if self . eat_keyword ( kw:: Extern ) {
127
127
if self . eat_keyword ( kw:: Crate ) {
128
128
// EXTERN CRATE
129
129
self . parse_item_extern_crate ( ) ?
130
130
} else {
131
131
// EXTERN BLOCK
132
- self . parse_item_foreign_mod ( ) ?
132
+ self . parse_item_foreign_mod ( attrs ) ?
133
133
}
134
134
} else if self . is_static_global ( ) {
135
135
// STATIC ITEM
@@ -156,7 +156,7 @@ impl<'a> Parser<'a> {
156
156
{
157
157
// UNSAFE TRAIT ITEM
158
158
let unsafety = self . parse_unsafety ( ) ;
159
- self . parse_item_trait ( lo, unsafety) ?
159
+ self . parse_item_trait ( attrs , lo, unsafety) ?
160
160
} else if self . check_keyword ( kw:: Impl )
161
161
|| self . check_keyword ( kw:: Unsafe ) && self . is_keyword_ahead ( 1 , & [ kw:: Impl ] )
162
162
|| self . check_keyword ( kw:: Default ) && self . is_keyword_ahead ( 1 , & [ kw:: Impl , kw:: Unsafe ] )
@@ -165,22 +165,22 @@ impl<'a> Parser<'a> {
165
165
let defaultness = self . parse_defaultness ( ) ;
166
166
let unsafety = self . parse_unsafety ( ) ;
167
167
self . expect_keyword ( kw:: Impl ) ?;
168
- self . parse_item_impl ( unsafety, defaultness) ?
168
+ self . parse_item_impl ( attrs , unsafety, defaultness) ?
169
169
} else if self . eat_keyword ( kw:: Mod ) {
170
170
// MODULE ITEM
171
171
self . parse_item_mod ( attrs) ?
172
172
} else if self . eat_keyword ( kw:: Type ) {
173
173
// TYPE ITEM
174
174
let ( ident, ty, generics) = self . parse_type_alias ( ) ?;
175
- ( ident, ItemKind :: TyAlias ( ty, generics) , None )
175
+ ( ident, ItemKind :: TyAlias ( ty, generics) )
176
176
} else if self . eat_keyword ( kw:: Enum ) {
177
177
// ENUM ITEM
178
178
self . parse_item_enum ( ) ?
179
179
} else if self . check_keyword ( kw:: Trait )
180
180
|| ( self . check_keyword ( kw:: Auto ) && self . is_keyword_ahead ( 1 , & [ kw:: Trait ] ) )
181
181
{
182
182
// TRAIT ITEM
183
- self . parse_item_trait ( lo, Unsafe :: No ) ?
183
+ self . parse_item_trait ( attrs , lo, Unsafe :: No ) ?
184
184
} else if self . eat_keyword ( kw:: Struct ) {
185
185
// STRUCT ITEM
186
186
self . parse_item_struct ( ) ?
@@ -306,26 +306,6 @@ impl<'a> Parser<'a> {
306
306
}
307
307
}
308
308
309
- pub ( super ) fn mk_item_with_info (
310
- & self ,
311
- attrs : Vec < Attribute > ,
312
- lo : Span ,
313
- vis : Visibility ,
314
- info : ItemInfo ,
315
- ) -> P < Item > {
316
- let ( ident, item, extra_attrs) = info;
317
- let span = lo. to ( self . prev_span ) ;
318
- let attrs = Self :: maybe_append ( attrs, extra_attrs) ;
319
- self . mk_item ( span, ident, item, vis, attrs)
320
- }
321
-
322
- fn maybe_append < T > ( mut lhs : Vec < T > , mut rhs : Option < Vec < T > > ) -> Vec < T > {
323
- if let Some ( ref mut rhs) = rhs {
324
- lhs. append ( rhs) ;
325
- }
326
- lhs
327
- }
328
-
329
309
/// Parses an item macro, e.g., `item!();`.
330
310
fn parse_item_macro ( & mut self , vis : & Visibility ) -> PResult < ' a , ItemInfo > {
331
311
self . complain_if_pub_macro ( & vis. node , vis. span ) ;
@@ -339,7 +319,7 @@ impl<'a> Parser<'a> {
339
319
}
340
320
341
321
let mac = Mac { path, args, prior_type_ascription : self . last_type_ascription } ;
342
- Ok ( ( Ident :: invalid ( ) , ItemKind :: Mac ( mac) , None ) )
322
+ Ok ( ( Ident :: invalid ( ) , ItemKind :: Mac ( mac) ) )
343
323
}
344
324
345
325
/// Emits an expected-item-after-attributes error.
@@ -428,16 +408,21 @@ impl<'a> Parser<'a> {
428
408
429
409
/// Parses an implementation item, `impl` keyword is already parsed.
430
410
///
431
- /// impl<'a, T> TYPE { /* impl items */ }
432
- /// impl<'a, T> TRAIT for TYPE { /* impl items */ }
433
- /// impl<'a, T> !TRAIT for TYPE { /* impl items */ }
434
- /// impl<'a, T> const TRAIT for TYPE { /* impl items */ }
411
+ /// ```
412
+ /// impl<'a, T> TYPE { /* impl items */ }
413
+ /// impl<'a, T> TRAIT for TYPE { /* impl items */ }
414
+ /// impl<'a, T> !TRAIT for TYPE { /* impl items */ }
415
+ /// impl<'a, T> const TRAIT for TYPE { /* impl items */ }
416
+ /// ```
435
417
///
436
418
/// We actually parse slightly more relaxed grammar for better error reporting and recovery.
437
- /// `impl` GENERICS `const`? `!`? TYPE `for`? (TYPE | `..`) (`where` PREDICATES)? `{` BODY `}`
438
- /// `impl` GENERICS `const`? `!`? TYPE (`where` PREDICATES)? `{` BODY `}`
419
+ /// ```
420
+ /// "impl" GENERICS "const"? "!"? TYPE "for"? (TYPE | "..") ("where" PREDICATES)? "{" BODY "}"
421
+ /// "impl" GENERICS "const"? "!"? TYPE ("where" PREDICATES)? "{" BODY "}"
422
+ /// ```
439
423
fn parse_item_impl (
440
424
& mut self ,
425
+ attrs : & mut Vec < Attribute > ,
441
426
unsafety : Unsafe ,
442
427
defaultness : Defaultness ,
443
428
) -> PResult < ' a , ItemInfo > {
@@ -492,7 +477,7 @@ impl<'a> Parser<'a> {
492
477
493
478
generics. where_clause = self . parse_where_clause ( ) ?;
494
479
495
- let ( impl_items, attrs ) = self . parse_item_list ( |p, at_end| p. parse_impl_item ( at_end) ) ?;
480
+ let impl_items = self . parse_item_list ( attrs , |p, at_end| p. parse_impl_item ( at_end) ) ?;
496
481
497
482
let item_kind = match ty_second {
498
483
Some ( ty_second) => {
@@ -545,15 +530,16 @@ impl<'a> Parser<'a> {
545
530
}
546
531
} ;
547
532
548
- Ok ( ( Ident :: invalid ( ) , item_kind, Some ( attrs ) ) )
533
+ Ok ( ( Ident :: invalid ( ) , item_kind) )
549
534
}
550
535
551
536
fn parse_item_list < T > (
552
537
& mut self ,
538
+ attrs : & mut Vec < Attribute > ,
553
539
mut parse_item : impl FnMut ( & mut Parser < ' a > , & mut bool ) -> PResult < ' a , T > ,
554
- ) -> PResult < ' a , ( Vec < T > , Vec < Attribute > ) > {
540
+ ) -> PResult < ' a , Vec < T > > {
555
541
self . expect ( & token:: OpenDelim ( token:: Brace ) ) ?;
556
- let attrs = self . parse_inner_attributes ( ) ?;
542
+ attrs. append ( & mut self . parse_inner_attributes ( ) ?) ;
557
543
558
544
let mut items = Vec :: new ( ) ;
559
545
while !self . eat ( & token:: CloseDelim ( token:: Brace ) ) {
@@ -572,7 +558,7 @@ impl<'a> Parser<'a> {
572
558
}
573
559
}
574
560
}
575
- Ok ( ( items, attrs ) )
561
+ Ok ( items)
576
562
}
577
563
578
564
/// Recover on a doc comment before `}`.
@@ -624,7 +610,12 @@ impl<'a> Parser<'a> {
624
610
}
625
611
626
612
/// Parses `auto? trait Foo { ... }` or `trait Foo = Bar;`.
627
- fn parse_item_trait ( & mut self , lo : Span , unsafety : Unsafe ) -> PResult < ' a , ItemInfo > {
613
+ fn parse_item_trait (
614
+ & mut self ,
615
+ attrs : & mut Vec < Attribute > ,
616
+ lo : Span ,
617
+ unsafety : Unsafe ,
618
+ ) -> PResult < ' a , ItemInfo > {
628
619
// Parse optional `auto` prefix.
629
620
let is_auto = if self . eat_keyword ( kw:: Auto ) { IsAuto :: Yes } else { IsAuto :: No } ;
630
621
@@ -662,12 +653,12 @@ impl<'a> Parser<'a> {
662
653
663
654
self . sess . gated_spans . gate ( sym:: trait_alias, whole_span) ;
664
655
665
- Ok ( ( ident, ItemKind :: TraitAlias ( tps, bounds) , None ) )
656
+ Ok ( ( ident, ItemKind :: TraitAlias ( tps, bounds) ) )
666
657
} else {
667
658
// It's a normal trait.
668
659
tps. where_clause = self . parse_where_clause ( ) ?;
669
- let ( items, attrs ) = self . parse_item_list ( |p, at_end| p. parse_trait_item ( at_end) ) ?;
670
- Ok ( ( ident, ItemKind :: Trait ( is_auto, unsafety, tps, bounds, items) , Some ( attrs ) ) )
660
+ let items = self . parse_item_list ( attrs , |p, at_end| p. parse_trait_item ( at_end) ) ?;
661
+ Ok ( ( ident, ItemKind :: Trait ( is_auto, unsafety, tps, bounds, items) ) )
671
662
}
672
663
}
673
664
@@ -854,7 +845,7 @@ impl<'a> Parser<'a> {
854
845
( orig_name, None )
855
846
} ;
856
847
self . expect_semi ( ) ?;
857
- Ok ( ( item_name, ItemKind :: ExternCrate ( orig_name) , None ) )
848
+ Ok ( ( item_name, ItemKind :: ExternCrate ( orig_name) ) )
858
849
}
859
850
860
851
fn parse_crate_name_with_dashes ( & mut self ) -> PResult < ' a , ast:: Ident > {
@@ -905,11 +896,11 @@ impl<'a> Parser<'a> {
905
896
/// extern "C" {}
906
897
/// extern {}
907
898
/// ```
908
- fn parse_item_foreign_mod ( & mut self ) -> PResult < ' a , ItemInfo > {
899
+ fn parse_item_foreign_mod ( & mut self , attrs : & mut Vec < Attribute > ) -> PResult < ' a , ItemInfo > {
909
900
let abi = self . parse_abi ( ) ; // ABI?
910
- let ( items, attrs ) = self . parse_item_list ( |p, at_end| p. parse_foreign_item ( at_end) ) ?;
901
+ let items = self . parse_item_list ( attrs , |p, at_end| p. parse_foreign_item ( at_end) ) ?;
911
902
let module = ast:: ForeignMod { abi, items } ;
912
- Ok ( ( Ident :: invalid ( ) , ItemKind :: ForeignMod ( module) , Some ( attrs ) ) )
903
+ Ok ( ( Ident :: invalid ( ) , ItemKind :: ForeignMod ( module) ) )
913
904
}
914
905
915
906
/// Parses a foreign item (one in an `extern { ... }` block).
@@ -1016,7 +1007,7 @@ impl<'a> Parser<'a> {
1016
1007
Some ( m) => ItemKind :: Static ( ty, m, e) ,
1017
1008
None => ItemKind :: Const ( ty, e) ,
1018
1009
} ;
1019
- Ok ( ( id, item, None ) )
1010
+ Ok ( ( id, item) )
1020
1011
}
1021
1012
1022
1013
/// We were supposed to parse `:` but instead, we're already at `=`.
@@ -1069,7 +1060,7 @@ impl<'a> Parser<'a> {
1069
1060
1070
1061
let enum_definition =
1071
1062
EnumDef { variants : variants. into_iter ( ) . filter_map ( |v| v) . collect ( ) } ;
1072
- Ok ( ( id, ItemKind :: Enum ( enum_definition, generics) , None ) )
1063
+ Ok ( ( id, ItemKind :: Enum ( enum_definition, generics) ) )
1073
1064
}
1074
1065
1075
1066
fn parse_enum_variant ( & mut self ) -> PResult < ' a , Option < Variant > > {
@@ -1163,7 +1154,7 @@ impl<'a> Parser<'a> {
1163
1154
return Err ( err) ;
1164
1155
} ;
1165
1156
1166
- Ok ( ( class_name, ItemKind :: Struct ( vdata, generics) , None ) )
1157
+ Ok ( ( class_name, ItemKind :: Struct ( vdata, generics) ) )
1167
1158
}
1168
1159
1169
1160
/// Parses `union Foo { ... }`.
@@ -1187,7 +1178,7 @@ impl<'a> Parser<'a> {
1187
1178
return Err ( err) ;
1188
1179
} ;
1189
1180
1190
- Ok ( ( class_name, ItemKind :: Union ( vdata, generics) , None ) )
1181
+ Ok ( ( class_name, ItemKind :: Union ( vdata, generics) ) )
1191
1182
}
1192
1183
1193
1184
pub ( super ) fn is_union_item ( & self ) -> bool {
@@ -1369,7 +1360,7 @@ impl<'a> Parser<'a> {
1369
1360
} ;
1370
1361
1371
1362
self . sess . gated_spans . gate ( sym:: decl_macro, lo. to ( self . prev_span ) ) ;
1372
- Ok ( ( ident, ItemKind :: MacroDef ( ast:: MacroDef { body, legacy : false } ) , None ) )
1363
+ Ok ( ( ident, ItemKind :: MacroDef ( ast:: MacroDef { body, legacy : false } ) ) )
1373
1364
}
1374
1365
1375
1366
/// Is this unambiguously the start of a `macro_rules! foo` item defnition?
@@ -1391,7 +1382,7 @@ impl<'a> Parser<'a> {
1391
1382
self . report_invalid_macro_expansion_item ( ) ;
1392
1383
}
1393
1384
1394
- Ok ( ( ident, ItemKind :: MacroDef ( ast:: MacroDef { body, legacy : true } ) , None ) )
1385
+ Ok ( ( ident, ItemKind :: MacroDef ( ast:: MacroDef { body, legacy : true } ) ) )
1395
1386
}
1396
1387
1397
1388
pub ( super ) fn eat_macro_def (
@@ -1400,14 +1391,14 @@ impl<'a> Parser<'a> {
1400
1391
vis : & Visibility ,
1401
1392
lo : Span ,
1402
1393
) -> PResult < ' a , Option < P < Item > > > {
1403
- let info = if self . eat_keyword ( kw:: Macro ) {
1394
+ let ( ident , kind ) = if self . eat_keyword ( kw:: Macro ) {
1404
1395
self . parse_item_decl_macro ( lo) ?
1405
1396
} else if self . is_macro_rules_item ( ) {
1406
1397
self . parse_item_macro_rules ( vis) ?
1407
1398
} else {
1408
1399
return Ok ( None ) ;
1409
1400
} ;
1410
- Ok ( Some ( self . mk_item_with_info ( attrs . to_vec ( ) , lo , vis. clone ( ) , info ) ) )
1401
+ Ok ( Some ( self . mk_item ( lo . to ( self . prev_span ) , ident , kind , vis. clone ( ) , attrs . to_vec ( ) ) ) )
1411
1402
}
1412
1403
1413
1404
fn complain_if_pub_macro ( & self , vis : & VisibilityKind , sp : Span ) {
0 commit comments