@@ -7,6 +7,7 @@ use rustc_ast::visit::Visitor;
7
7
use rustc_ast:: NodeId ;
8
8
use rustc_ast:: { mut_visit, visit} ;
9
9
use rustc_ast:: { Attribute , HasAttrs , HasTokens } ;
10
+ use rustc_errors:: PResult ;
10
11
use rustc_expand:: base:: { Annotatable , ExtCtxt } ;
11
12
use rustc_expand:: config:: StripUnconfigured ;
12
13
use rustc_expand:: configure;
@@ -144,33 +145,34 @@ impl CfgEval<'_, '_> {
144
145
// the location of `#[cfg]` and `#[cfg_attr]` in the token stream. The tokenization
145
146
// process is lossless, so this process is invisible to proc-macros.
146
147
147
- let parse_annotatable_with: fn ( & mut Parser < ' _ > ) -> _ = match annotatable {
148
- Annotatable :: Item ( _) => {
149
- |parser| Annotatable :: Item ( parser. parse_item ( ForceCollect :: Yes ) . unwrap ( ) . unwrap ( ) )
150
- }
151
- Annotatable :: TraitItem ( _) => |parser| {
152
- Annotatable :: TraitItem (
153
- parser. parse_trait_item ( ForceCollect :: Yes ) . unwrap ( ) . unwrap ( ) . unwrap ( ) ,
154
- )
155
- } ,
156
- Annotatable :: ImplItem ( _) => |parser| {
157
- Annotatable :: ImplItem (
158
- parser. parse_impl_item ( ForceCollect :: Yes ) . unwrap ( ) . unwrap ( ) . unwrap ( ) ,
159
- )
160
- } ,
161
- Annotatable :: ForeignItem ( _) => |parser| {
162
- Annotatable :: ForeignItem (
163
- parser. parse_foreign_item ( ForceCollect :: Yes ) . unwrap ( ) . unwrap ( ) . unwrap ( ) ,
164
- )
165
- } ,
166
- Annotatable :: Stmt ( _) => |parser| {
167
- Annotatable :: Stmt ( P ( parser. parse_stmt ( ForceCollect :: Yes ) . unwrap ( ) . unwrap ( ) ) )
168
- } ,
169
- Annotatable :: Expr ( _) => {
170
- |parser| Annotatable :: Expr ( parser. parse_expr_force_collect ( ) . unwrap ( ) )
171
- }
172
- _ => unreachable ! ( ) ,
173
- } ;
148
+ let parse_annotatable_with: for <' a > fn ( & mut Parser < ' a > ) -> PResult < ' a , _ > =
149
+ match annotatable {
150
+ Annotatable :: Item ( _) => {
151
+ |parser| Ok ( Annotatable :: Item ( parser. parse_item ( ForceCollect :: Yes ) ?. unwrap ( ) ) )
152
+ }
153
+ Annotatable :: TraitItem ( _) => |parser| {
154
+ Ok ( Annotatable :: TraitItem (
155
+ parser. parse_trait_item ( ForceCollect :: Yes ) ?. unwrap ( ) . unwrap ( ) ,
156
+ ) )
157
+ } ,
158
+ Annotatable :: ImplItem ( _) => |parser| {
159
+ Ok ( Annotatable :: ImplItem (
160
+ parser. parse_impl_item ( ForceCollect :: Yes ) ?. unwrap ( ) . unwrap ( ) ,
161
+ ) )
162
+ } ,
163
+ Annotatable :: ForeignItem ( _) => |parser| {
164
+ Ok ( Annotatable :: ForeignItem (
165
+ parser. parse_foreign_item ( ForceCollect :: Yes ) ?. unwrap ( ) . unwrap ( ) ,
166
+ ) )
167
+ } ,
168
+ Annotatable :: Stmt ( _) => |parser| {
169
+ Ok ( Annotatable :: Stmt ( P ( parser. parse_stmt ( ForceCollect :: Yes ) ?. unwrap ( ) ) ) )
170
+ } ,
171
+ Annotatable :: Expr ( _) => {
172
+ |parser| Ok ( Annotatable :: Expr ( parser. parse_expr_force_collect ( ) ?) )
173
+ }
174
+ _ => unreachable ! ( ) ,
175
+ } ;
174
176
175
177
// 'Flatten' all nonterminals (i.e. `TokenKind::Interpolated`)
176
178
// to `None`-delimited groups containing the corresponding tokens. This
@@ -193,7 +195,13 @@ impl CfgEval<'_, '_> {
193
195
let mut parser =
194
196
rustc_parse:: stream_to_parser ( & self . cfg . sess . parse_sess , orig_tokens, None ) ;
195
197
parser. capture_cfg = true ;
196
- annotatable = parse_annotatable_with ( & mut parser) ;
198
+ match parse_annotatable_with ( & mut parser) {
199
+ Ok ( a) => annotatable = a,
200
+ Err ( mut err) => {
201
+ err. emit ( ) ;
202
+ return Some ( annotatable) ;
203
+ }
204
+ }
197
205
198
206
// Now that we have our re-parsed `AttrTokenStream`, recursively configuring
199
207
// our attribute target will correctly the tokens as well.
0 commit comments