1
1
use super :: diagnostics:: { dummy_arg, ConsumeClosingDelim , Error } ;
2
2
use super :: ty:: { AllowPlus , RecoverQPath , RecoverReturnSign } ;
3
- use super :: { FollowedByType , Parser , PathStyle } ;
3
+ use super :: { FollowedByType , ForceCollect , Parser , PathStyle } ;
4
4
5
- use crate :: maybe_whole;
5
+ use crate :: { maybe_collect_tokens , maybe_whole} ;
6
6
7
7
use rustc_ast:: ptr:: P ;
8
8
use rustc_ast:: token:: { self , TokenKind } ;
@@ -69,7 +69,7 @@ impl<'a> Parser<'a> {
69
69
unsafety : Unsafe ,
70
70
) -> PResult < ' a , Mod > {
71
71
let mut items = vec ! [ ] ;
72
- while let Some ( item) = self . parse_item ( ) ? {
72
+ while let Some ( item) = self . parse_item ( ForceCollect :: No ) ? {
73
73
items. push ( item) ;
74
74
self . maybe_consume_incorrect_semicolon ( & items) ;
75
75
}
@@ -93,13 +93,17 @@ impl<'a> Parser<'a> {
93
93
pub ( super ) type ItemInfo = ( Ident , ItemKind ) ;
94
94
95
95
impl < ' 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 ) )
98
98
}
99
99
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 > > {
101
105
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 )
103
107
}
104
108
105
109
pub ( super ) fn parse_item_common (
@@ -108,6 +112,7 @@ impl<'a> Parser<'a> {
108
112
mac_allowed : bool ,
109
113
attrs_allowed : bool ,
110
114
req_name : ReqName ,
115
+ force_collect : ForceCollect ,
111
116
) -> PResult < ' a , Option < Item > > {
112
117
maybe_whole ! ( self , NtItem , |item| {
113
118
let mut item = item;
@@ -116,16 +121,12 @@ impl<'a> Parser<'a> {
116
121
Some ( item. into_inner( ) )
117
122
} ) ;
118
123
119
- let needs_tokens = super :: attr:: maybe_needs_tokens ( & attrs) ;
120
-
121
124
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 | {
123
126
let item = this. parse_item_common_( attrs, mac_allowed, attrs_allowed, req_name) ;
124
127
unclosed_delims. append( & mut this. unclosed_delims) ;
125
128
item
126
- } ;
127
-
128
- let item = if needs_tokens { self . collect_tokens ( parse_item) } else { parse_item ( self ) } ?;
129
+ } ) ?;
129
130
130
131
self . unclosed_delims . append ( & mut unclosed_delims) ;
131
132
Ok ( item)
@@ -731,20 +732,22 @@ impl<'a> Parser<'a> {
731
732
732
733
/// Parses associated items.
733
734
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
+ ) )
748
751
}
749
752
750
753
/// Parses a `type` alias with the following grammar:
@@ -921,19 +924,21 @@ impl<'a> Parser<'a> {
921
924
922
925
/// Parses a foreign item (one in an `extern { ... }` block).
923
926
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
+ ) )
937
942
}
938
943
939
944
fn error_bad_item_kind < T > ( & self , span : Span , kind : & ItemKind , ctx : & str ) -> Option < T > {
@@ -1515,7 +1520,7 @@ impl<'a> Parser<'a> {
1515
1520
{
1516
1521
let kw_token = self . token . clone ( ) ;
1517
1522
let kw_str = pprust:: token_to_string ( & kw_token) ;
1518
- let item = self . parse_item ( ) ?;
1523
+ let item = self . parse_item ( ForceCollect :: No ) ?;
1519
1524
1520
1525
self . struct_span_err (
1521
1526
kw_token. span ,
0 commit comments