11use super :: diagnostics:: { dummy_arg, ConsumeClosingDelim , Error } ;
22use super :: ty:: { AllowPlus , RecoverQPath , RecoverReturnSign } ;
3- use super :: { FollowedByType , Parser , PathStyle } ;
3+ use super :: { FollowedByType , ForceCollect , Parser , PathStyle } ;
44
5- use crate :: maybe_whole;
5+ use crate :: { maybe_collect_tokens , maybe_whole} ;
66
77use rustc_ast:: ptr:: P ;
88use rustc_ast:: token:: { self , TokenKind } ;
@@ -69,7 +69,7 @@ impl<'a> Parser<'a> {
6969 unsafety : Unsafe ,
7070 ) -> PResult < ' a , Mod > {
7171 let mut items = vec ! [ ] ;
72- while let Some ( item) = self . parse_item ( ) ? {
72+ while let Some ( item) = self . parse_item ( ForceCollect :: No ) ? {
7373 items. push ( item) ;
7474 self . maybe_consume_incorrect_semicolon ( & items) ;
7575 }
@@ -93,13 +93,17 @@ impl<'a> Parser<'a> {
9393pub ( super ) type ItemInfo = ( Ident , ItemKind ) ;
9494
9595impl < ' a > Parser < ' a > {
96- pub fn parse_item ( & mut self ) -> PResult < ' a , Option < P < Item > > > {
97- self . parse_item_ ( |_| true ) . map ( |i| i. map ( P ) )
96+ pub fn parse_item ( & mut self , force_collect : ForceCollect ) -> PResult < ' a , Option < P < Item > > > {
97+ self . parse_item_ ( |_| true , force_collect ) . map ( |i| i. map ( P ) )
9898 }
9999
100- fn parse_item_ ( & mut self , req_name : ReqName ) -> PResult < ' a , Option < Item > > {
100+ fn parse_item_ (
101+ & mut self ,
102+ req_name : ReqName ,
103+ force_collect : ForceCollect ,
104+ ) -> PResult < ' a , Option < Item > > {
101105 let attrs = self . parse_outer_attributes ( ) ?;
102- self . parse_item_common ( attrs, true , false , req_name)
106+ self . parse_item_common ( attrs, true , false , req_name, force_collect )
103107 }
104108
105109 pub ( super ) fn parse_item_common (
@@ -108,6 +112,7 @@ impl<'a> Parser<'a> {
108112 mac_allowed : bool ,
109113 attrs_allowed : bool ,
110114 req_name : ReqName ,
115+ force_collect : ForceCollect ,
111116 ) -> PResult < ' a , Option < Item > > {
112117 maybe_whole ! ( self , NtItem , |item| {
113118 let mut item = item;
@@ -116,16 +121,12 @@ impl<'a> Parser<'a> {
116121 Some ( item. into_inner( ) )
117122 } ) ;
118123
119- let needs_tokens = super :: attr:: maybe_needs_tokens ( & attrs) ;
120-
121124 let mut unclosed_delims = vec ! [ ] ;
122- let parse_item = |this : & mut Self | {
125+ let item = maybe_collect_tokens ! ( self , force_collect , & attrs , |this: & mut Self | {
123126 let item = this. parse_item_common_( attrs, mac_allowed, attrs_allowed, req_name) ;
124127 unclosed_delims. append( & mut this. unclosed_delims) ;
125128 item
126- } ;
127-
128- let item = if needs_tokens { self . collect_tokens ( parse_item) } else { parse_item ( self ) } ?;
129+ } ) ?;
129130
130131 self . unclosed_delims . append ( & mut unclosed_delims) ;
131132 Ok ( item)
@@ -731,20 +732,22 @@ impl<'a> Parser<'a> {
731732
732733 /// Parses associated items.
733734 fn parse_assoc_item ( & mut self , req_name : ReqName ) -> PResult < ' a , Option < Option < P < AssocItem > > > > {
734- Ok ( self . parse_item_ ( req_name) ?. map ( |Item { attrs, id, span, vis, ident, kind, tokens } | {
735- let kind = match AssocItemKind :: try_from ( kind) {
736- Ok ( kind) => kind,
737- Err ( kind) => match kind {
738- ItemKind :: Static ( a, _, b) => {
739- self . struct_span_err ( span, "associated `static` items are not allowed" )
740- . emit ( ) ;
741- AssocItemKind :: Const ( Defaultness :: Final , a, b)
742- }
743- _ => return self . error_bad_item_kind ( span, & kind, "`trait`s or `impl`s" ) ,
744- } ,
745- } ;
746- Some ( P ( Item { attrs, id, span, vis, ident, kind, tokens } ) )
747- } ) )
735+ Ok ( self . parse_item_ ( req_name, ForceCollect :: No ) ?. map (
736+ |Item { attrs, id, span, vis, ident, kind, tokens } | {
737+ let kind = match AssocItemKind :: try_from ( kind) {
738+ Ok ( kind) => kind,
739+ Err ( kind) => match kind {
740+ ItemKind :: Static ( a, _, b) => {
741+ self . struct_span_err ( span, "associated `static` items are not allowed" )
742+ . emit ( ) ;
743+ AssocItemKind :: Const ( Defaultness :: Final , a, b)
744+ }
745+ _ => return self . error_bad_item_kind ( span, & kind, "`trait`s or `impl`s" ) ,
746+ } ,
747+ } ;
748+ Some ( P ( Item { attrs, id, span, vis, ident, kind, tokens } ) )
749+ } ,
750+ ) )
748751 }
749752
750753 /// Parses a `type` alias with the following grammar:
@@ -921,19 +924,21 @@ impl<'a> Parser<'a> {
921924
922925 /// Parses a foreign item (one in an `extern { ... }` block).
923926 pub fn parse_foreign_item ( & mut self ) -> PResult < ' a , Option < Option < P < ForeignItem > > > > {
924- Ok ( self . parse_item_ ( |_| true ) ?. map ( |Item { attrs, id, span, vis, ident, kind, tokens } | {
925- let kind = match ForeignItemKind :: try_from ( kind) {
926- Ok ( kind) => kind,
927- Err ( kind) => match kind {
928- ItemKind :: Const ( _, a, b) => {
929- self . error_on_foreign_const ( span, ident) ;
930- ForeignItemKind :: Static ( a, Mutability :: Not , b)
931- }
932- _ => return self . error_bad_item_kind ( span, & kind, "`extern` blocks" ) ,
933- } ,
934- } ;
935- Some ( P ( Item { attrs, id, span, vis, ident, kind, tokens } ) )
936- } ) )
927+ Ok ( self . parse_item_ ( |_| true , ForceCollect :: No ) ?. map (
928+ |Item { attrs, id, span, vis, ident, kind, tokens } | {
929+ let kind = match ForeignItemKind :: try_from ( kind) {
930+ Ok ( kind) => kind,
931+ Err ( kind) => match kind {
932+ ItemKind :: Const ( _, a, b) => {
933+ self . error_on_foreign_const ( span, ident) ;
934+ ForeignItemKind :: Static ( a, Mutability :: Not , b)
935+ }
936+ _ => return self . error_bad_item_kind ( span, & kind, "`extern` blocks" ) ,
937+ } ,
938+ } ;
939+ Some ( P ( Item { attrs, id, span, vis, ident, kind, tokens } ) )
940+ } ,
941+ ) )
937942 }
938943
939944 fn error_bad_item_kind < T > ( & self , span : Span , kind : & ItemKind , ctx : & str ) -> Option < T > {
@@ -1515,7 +1520,7 @@ impl<'a> Parser<'a> {
15151520 {
15161521 let kw_token = self . token . clone ( ) ;
15171522 let kw_str = pprust:: token_to_string ( & kw_token) ;
1518- let item = self . parse_item ( ) ?;
1523+ let item = self . parse_item ( ForceCollect :: No ) ?;
15191524
15201525 self . struct_span_err (
15211526 kw_token. span ,
0 commit comments