@@ -7,6 +7,7 @@ use rustc_ast::visit::Visitor;
77use rustc_ast:: NodeId ;
88use rustc_ast:: { mut_visit, visit} ;
99use rustc_ast:: { Attribute , HasAttrs , HasTokens } ;
10+ use rustc_errors:: PResult ;
1011use rustc_expand:: base:: { Annotatable , ExtCtxt } ;
1112use rustc_expand:: config:: StripUnconfigured ;
1213use rustc_expand:: configure;
@@ -144,33 +145,34 @@ impl CfgEval<'_, '_> {
144145 // the location of `#[cfg]` and `#[cfg_attr]` in the token stream. The tokenization
145146 // process is lossless, so this process is invisible to proc-macros.
146147
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+ } ;
174176
175177 // 'Flatten' all nonterminals (i.e. `TokenKind::Interpolated`)
176178 // to `None`-delimited groups containing the corresponding tokens. This
@@ -193,7 +195,13 @@ impl CfgEval<'_, '_> {
193195 let mut parser =
194196 rustc_parse:: stream_to_parser ( & self . cfg . sess . parse_sess , orig_tokens, None ) ;
195197 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+ }
197205
198206 // Now that we have our re-parsed `AttrTokenStream`, recursively configuring
199207 // our attribute target will correctly the tokens as well.
0 commit comments