@@ -31,13 +31,33 @@ impl<'a> Parser<'a> {
31
31
32
32
pub ( super ) fn parse_item_ (
33
33
& mut self ,
34
- attrs : Vec < Attribute > ,
34
+ mut attrs : Vec < Attribute > ,
35
35
macros_allowed : bool ,
36
36
attributes_allowed : bool ,
37
37
) -> PResult < ' a , Option < P < Item > > > {
38
+ maybe_whole ! ( self , NtItem , |item| {
39
+ let mut item = item;
40
+ mem:: swap( & mut item. attrs, & mut attrs) ;
41
+ item. attrs. extend( attrs) ;
42
+ Some ( item)
43
+ } ) ;
44
+ let item = self . parse_item_common ( attrs, macros_allowed, attributes_allowed, |_| true ) ?;
45
+ if let Some ( ref item) = item {
46
+ self . error_on_illegal_default ( item. defaultness ) ;
47
+ }
48
+ Ok ( item. map ( P ) )
49
+ }
50
+
51
+ fn parse_item_common (
52
+ & mut self ,
53
+ attrs : Vec < Attribute > ,
54
+ mac_allowed : bool ,
55
+ attrs_allowed : bool ,
56
+ req_name : ReqName ,
57
+ ) -> PResult < ' a , Option < Item > > {
38
58
let mut unclosed_delims = vec ! [ ] ;
39
- let ( ret , tokens) = self . collect_tokens ( |this| {
40
- let item = this. parse_item_implementation ( attrs, macros_allowed , attributes_allowed ) ;
59
+ let ( mut item , tokens) = self . collect_tokens ( |this| {
60
+ let item = this. parse_item_common_ ( attrs, mac_allowed , attrs_allowed , req_name ) ;
41
61
unclosed_delims. append ( & mut this. unclosed_delims ) ;
42
62
item
43
63
} ) ?;
@@ -57,38 +77,15 @@ impl<'a> Parser<'a> {
57
77
// it (bad!). To work around this case for now we just avoid recording
58
78
// `tokens` if we detect any inner attributes. This should help keep
59
79
// expansion correct, but we should fix this bug one day!
60
- Ok ( ret. map ( |item| {
61
- item. map ( |mut i| {
62
- if !i. attrs . iter ( ) . any ( |attr| attr. style == AttrStyle :: Inner ) {
63
- i. tokens = Some ( tokens) ;
64
- }
65
- i
66
- } )
67
- } ) )
68
- }
69
-
70
- /// Parses one of the items allowed by the flags.
71
- fn parse_item_implementation (
72
- & mut self ,
73
- mut attrs : Vec < Attribute > ,
74
- macros_allowed : bool ,
75
- attributes_allowed : bool ,
76
- ) -> PResult < ' a , Option < P < Item > > > {
77
- maybe_whole ! ( self , NtItem , |item| {
78
- let mut item = item;
79
- mem:: swap( & mut item. attrs, & mut attrs) ;
80
- item. attrs. extend( attrs) ;
81
- Some ( item)
82
- } ) ;
83
-
84
- let item = self . parse_item_common ( attrs, macros_allowed, attributes_allowed, |_| true ) ?;
85
- if let Some ( ref item) = item {
86
- self . error_on_illegal_default ( item. defaultness ) ;
80
+ if let Some ( item) = & mut item {
81
+ if !item. attrs . iter ( ) . any ( |attr| attr. style == AttrStyle :: Inner ) {
82
+ item. tokens = Some ( tokens) ;
83
+ }
87
84
}
88
- Ok ( item. map ( P ) )
85
+ Ok ( item)
89
86
}
90
87
91
- fn parse_item_common (
88
+ fn parse_item_common_ (
92
89
& mut self ,
93
90
mut attrs : Vec < Attribute > ,
94
91
mac_allowed : bool ,
@@ -652,27 +649,6 @@ impl<'a> Parser<'a> {
652
649
/// Parses associated items.
653
650
fn parse_assoc_item ( & mut self , req_name : ReqName ) -> PResult < ' a , Option < Option < P < AssocItem > > > > {
654
651
let attrs = self . parse_outer_attributes ( ) ?;
655
- let mut unclosed_delims = vec ! [ ] ;
656
- let ( mut item, tokens) = self . collect_tokens ( |this| {
657
- let item = this. parse_assoc_item_ ( attrs, req_name) ;
658
- unclosed_delims. append ( & mut this. unclosed_delims ) ;
659
- item
660
- } ) ?;
661
- self . unclosed_delims . append ( & mut unclosed_delims) ;
662
- // See `parse_item` for why this clause is here.
663
- if let Some ( Some ( item) ) = & mut item {
664
- if !item. attrs . iter ( ) . any ( |attr| attr. style == AttrStyle :: Inner ) {
665
- item. tokens = Some ( tokens) ;
666
- }
667
- }
668
- Ok ( item)
669
- }
670
-
671
- fn parse_assoc_item_ (
672
- & mut self ,
673
- attrs : Vec < Attribute > ,
674
- req_name : ReqName ,
675
- ) -> PResult < ' a , Option < Option < P < AssocItem > > > > {
676
652
let it = self . parse_item_common ( attrs, true , false , req_name) ?;
677
653
Ok ( it. map ( |Item { attrs, id, span, vis, ident, defaultness, kind, tokens } | {
678
654
let kind = match kind {
@@ -869,8 +845,8 @@ impl<'a> Parser<'a> {
869
845
maybe_whole ! ( self , NtForeignItem , |item| Some ( Some ( item) ) ) ;
870
846
871
847
let attrs = self . parse_outer_attributes ( ) ?;
872
- let it = self . parse_item_common ( attrs, true , false , |_| true ) ?;
873
- Ok ( it . map ( |Item { attrs, id, span, vis, ident, defaultness, kind, tokens } | {
848
+ let item = self . parse_item_common ( attrs, true , false , |_| true ) ?;
849
+ Ok ( item . map ( |Item { attrs, id, span, vis, ident, defaultness, kind, tokens } | {
874
850
self . error_on_illegal_default ( defaultness) ;
875
851
let kind = match kind {
876
852
ItemKind :: Mac ( a) => ForeignItemKind :: Macro ( a) ,
0 commit comments