@@ -83,7 +83,7 @@ impl CfgEval<'_> {
83
83
self . 0 . configure ( node)
84
84
}
85
85
86
- fn configure_annotatable ( mut self , mut annotatable : Annotatable ) -> Annotatable {
86
+ fn configure_annotatable ( mut self , annotatable : Annotatable ) -> Annotatable {
87
87
// Tokenizing and re-parsing the `Annotatable` can have a significant
88
88
// performance impact, so try to avoid it if possible
89
89
if !has_cfg_or_cfg_attr ( & annotatable) {
@@ -100,39 +100,6 @@ impl CfgEval<'_> {
100
100
// the location of `#[cfg]` and `#[cfg_attr]` in the token stream. The tokenization
101
101
// process is lossless, so this process is invisible to proc-macros.
102
102
103
- let parse_annotatable_with: for <' a > fn ( & mut Parser < ' a > ) -> PResult < ' a , _ > =
104
- match annotatable {
105
- Annotatable :: Item ( _) => {
106
- |parser| Ok ( Annotatable :: Item ( parser. parse_item ( ForceCollect :: Yes ) ?. unwrap ( ) ) )
107
- }
108
- Annotatable :: AssocItem ( _, AssocCtxt :: Trait ) => |parser| {
109
- Ok ( Annotatable :: AssocItem (
110
- parser. parse_trait_item ( ForceCollect :: Yes ) ?. unwrap ( ) . unwrap ( ) ,
111
- AssocCtxt :: Trait ,
112
- ) )
113
- } ,
114
- Annotatable :: AssocItem ( _, AssocCtxt :: Impl ) => |parser| {
115
- Ok ( Annotatable :: AssocItem (
116
- parser. parse_impl_item ( ForceCollect :: Yes ) ?. unwrap ( ) . unwrap ( ) ,
117
- AssocCtxt :: Impl ,
118
- ) )
119
- } ,
120
- Annotatable :: ForeignItem ( _) => |parser| {
121
- Ok ( Annotatable :: ForeignItem (
122
- parser. parse_foreign_item ( ForceCollect :: Yes ) ?. unwrap ( ) . unwrap ( ) ,
123
- ) )
124
- } ,
125
- Annotatable :: Stmt ( _) => |parser| {
126
- Ok ( Annotatable :: Stmt ( P ( parser
127
- . parse_stmt_without_recovery ( false , ForceCollect :: Yes ) ?
128
- . unwrap ( ) ) ) )
129
- } ,
130
- Annotatable :: Expr ( _) => {
131
- |parser| Ok ( Annotatable :: Expr ( parser. parse_expr_force_collect ( ) ?) )
132
- }
133
- _ => unreachable ! ( ) ,
134
- } ;
135
-
136
103
// 'Flatten' all nonterminals (i.e. `TokenKind::Interpolated`)
137
104
// to `None`-delimited groups containing the corresponding tokens. This
138
105
// is normally delayed until the proc-macro server actually needs to
@@ -151,34 +118,55 @@ impl CfgEval<'_> {
151
118
// Re-parse the tokens, setting the `capture_cfg` flag to save extra information
152
119
// to the captured `AttrTokenStream` (specifically, we capture
153
120
// `AttrTokenTree::AttrsTarget` for all occurrences of `#[cfg]` and `#[cfg_attr]`)
121
+ //
122
+ // After that we have our re-parsed `AttrTokenStream`, recursively configuring
123
+ // our attribute target will correctly configure the tokens as well.
154
124
let mut parser = Parser :: new ( & self . 0 . sess . psess , orig_tokens, None ) ;
155
125
parser. capture_cfg = true ;
156
- match parse_annotatable_with ( & mut parser) {
157
- Ok ( a) => annotatable = a,
158
- Err ( err) => {
159
- err. emit ( ) ;
160
- return annotatable;
126
+ let res: PResult < ' _ , Annotatable > = try {
127
+ match annotatable {
128
+ Annotatable :: Item ( _) => {
129
+ let item = parser. parse_item ( ForceCollect :: Yes ) ?. unwrap ( ) ;
130
+ Annotatable :: Item ( self . flat_map_item ( item) . pop ( ) . unwrap ( ) )
131
+ }
132
+ Annotatable :: AssocItem ( _, AssocCtxt :: Trait ) => {
133
+ let item = parser. parse_trait_item ( ForceCollect :: Yes ) ?. unwrap ( ) . unwrap ( ) ;
134
+ Annotatable :: AssocItem (
135
+ self . flat_map_assoc_item ( item, AssocCtxt :: Trait ) . pop ( ) . unwrap ( ) ,
136
+ AssocCtxt :: Trait ,
137
+ )
138
+ }
139
+ Annotatable :: AssocItem ( _, AssocCtxt :: Impl ) => {
140
+ let item = parser. parse_impl_item ( ForceCollect :: Yes ) ?. unwrap ( ) . unwrap ( ) ;
141
+ Annotatable :: AssocItem (
142
+ self . flat_map_assoc_item ( item, AssocCtxt :: Impl ) . pop ( ) . unwrap ( ) ,
143
+ AssocCtxt :: Impl ,
144
+ )
145
+ }
146
+ Annotatable :: ForeignItem ( _) => {
147
+ let item = parser. parse_foreign_item ( ForceCollect :: Yes ) ?. unwrap ( ) . unwrap ( ) ;
148
+ Annotatable :: ForeignItem ( self . flat_map_foreign_item ( item) . pop ( ) . unwrap ( ) )
149
+ }
150
+ Annotatable :: Stmt ( _) => {
151
+ let stmt =
152
+ parser. parse_stmt_without_recovery ( false , ForceCollect :: Yes ) ?. unwrap ( ) ;
153
+ Annotatable :: Stmt ( P ( self . flat_map_stmt ( stmt) . pop ( ) . unwrap ( ) ) )
154
+ }
155
+ Annotatable :: Expr ( _) => {
156
+ let mut expr = parser. parse_expr_force_collect ( ) ?;
157
+ self . visit_expr ( & mut expr) ;
158
+ Annotatable :: Expr ( expr)
159
+ }
160
+ _ => unreachable ! ( ) ,
161
161
}
162
- }
162
+ } ;
163
163
164
- // Now that we have our re-parsed `AttrTokenStream`, recursively configuring
165
- // our attribute target will correctly configure the tokens as well.
166
- match annotatable {
167
- Annotatable :: Item ( item) => Annotatable :: Item ( self . flat_map_item ( item) . pop ( ) . unwrap ( ) ) ,
168
- Annotatable :: AssocItem ( item, ctxt) => {
169
- Annotatable :: AssocItem ( self . flat_map_assoc_item ( item, ctxt) . pop ( ) . unwrap ( ) , ctxt)
170
- }
171
- Annotatable :: ForeignItem ( item) => {
172
- Annotatable :: ForeignItem ( self . flat_map_foreign_item ( item) . pop ( ) . unwrap ( ) )
173
- }
174
- Annotatable :: Stmt ( stmt) => {
175
- Annotatable :: Stmt ( P ( self . flat_map_stmt ( stmt. into_inner ( ) ) . pop ( ) . unwrap ( ) ) )
176
- }
177
- Annotatable :: Expr ( mut expr) => {
178
- self . visit_expr ( & mut expr) ;
179
- Annotatable :: Expr ( expr)
164
+ match res {
165
+ Ok ( ann) => ann,
166
+ Err ( err) => {
167
+ err. emit ( ) ;
168
+ annotatable
180
169
}
181
- _ => unreachable ! ( ) ,
182
170
}
183
171
}
184
172
}
0 commit comments