@@ -31,13 +31,33 @@ impl<'a> Parser<'a> {
3131
3232 pub ( super ) fn parse_item_ (
3333 & mut self ,
34- attrs : Vec < Attribute > ,
34+ mut attrs : Vec < Attribute > ,
3535 macros_allowed : bool ,
3636 attributes_allowed : bool ,
3737 ) -> 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 > > {
3858 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 ) ;
4161 unclosed_delims. append ( & mut this. unclosed_delims ) ;
4262 item
4363 } ) ?;
@@ -57,38 +77,15 @@ impl<'a> Parser<'a> {
5777 // it (bad!). To work around this case for now we just avoid recording
5878 // `tokens` if we detect any inner attributes. This should help keep
5979 // 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+ }
8784 }
88- Ok ( item. map ( P ) )
85+ Ok ( item)
8986 }
9087
91- fn parse_item_common (
88+ fn parse_item_common_ (
9289 & mut self ,
9390 mut attrs : Vec < Attribute > ,
9491 mac_allowed : bool ,
@@ -652,27 +649,6 @@ impl<'a> Parser<'a> {
652649 /// Parses associated items.
653650 fn parse_assoc_item ( & mut self , req_name : ReqName ) -> PResult < ' a , Option < Option < P < AssocItem > > > > {
654651 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 > > > > {
676652 let it = self . parse_item_common ( attrs, true , false , req_name) ?;
677653 Ok ( it. map ( |Item { attrs, id, span, vis, ident, defaultness, kind, tokens } | {
678654 let kind = match kind {
@@ -869,8 +845,8 @@ impl<'a> Parser<'a> {
869845 maybe_whole ! ( self , NtForeignItem , |item| Some ( Some ( item) ) ) ;
870846
871847 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 } | {
874850 self . error_on_illegal_default ( defaultness) ;
875851 let kind = match kind {
876852 ItemKind :: Mac ( a) => ForeignItemKind :: Macro ( a) ,
0 commit comments