1
1
use rustc_ast as ast;
2
2
use rustc_ast:: attr;
3
- use rustc_ast:: token:: { self , Delimiter } ;
3
+ use rustc_ast:: token:: { self , Delimiter , MetaVarKind } ;
4
4
use rustc_errors:: codes:: * ;
5
5
use rustc_errors:: { Diag , PResult } ;
6
6
use rustc_span:: symbol:: kw;
@@ -12,7 +12,7 @@ use super::{
12
12
AttrWrapper , Capturing , FnParseMode , ForceCollect , Parser , ParserRange , PathStyle , Trailing ,
13
13
UsePreAttrPos ,
14
14
} ;
15
- use crate :: { errors, fluent_generated as fluent, maybe_whole } ;
15
+ use crate :: { errors, fluent_generated as fluent} ;
16
16
17
17
// Public for rustfmt usage
18
18
#[ derive( Debug ) ]
@@ -272,7 +272,11 @@ impl<'a> Parser<'a> {
272
272
/// PATH `=` UNSUFFIXED_LIT
273
273
/// The delimiters or `=` are still put into the resulting token stream.
274
274
pub fn parse_attr_item ( & mut self , force_collect : ForceCollect ) -> PResult < ' a , ast:: AttrItem > {
275
- maybe_whole ! ( self , NtMeta , |attr| attr. into_inner( ) ) ;
275
+ if let Some ( item) =
276
+ self . eat_metavar_seq ( MetaVarKind :: Meta , |this| this. parse_attr_item ( force_collect) )
277
+ {
278
+ return Ok ( item) ;
279
+ }
276
280
277
281
// Attr items don't have attributes.
278
282
self . collect_tokens ( None , AttrWrapper :: empty ( ) , force_collect, |this, _empty_attrs| {
@@ -397,18 +401,18 @@ impl<'a> Parser<'a> {
397
401
& mut self ,
398
402
unsafe_allowed : AllowLeadingUnsafe ,
399
403
) -> PResult < ' a , ast:: MetaItem > {
400
- // We can't use `maybe_whole` here because it would bump in the `None`
401
- // case, which we don't want.
402
- if let token :: Interpolated ( nt ) = & self . token . kind
403
- && let token :: NtMeta ( attr_item ) = & * * nt
404
+ // Snapshot the parser so we can backtrack in the case where `attr_item.meta()` fails.
405
+ let mut snapshot = self . create_snapshot_for_diagnostic ( ) ;
406
+ if let Some ( attr_item ) = snapshot
407
+ . eat_metavar_seq ( MetaVarKind :: Meta , |this| this . parse_attr_item ( ForceCollect :: No ) )
404
408
{
405
- match attr_item. meta ( attr_item. path . span ) {
409
+ return match attr_item. meta ( attr_item. path . span ) {
406
410
Some ( meta) => {
407
- self . bump ( ) ;
408
- return Ok ( meta) ;
411
+ self . restore_snapshot ( snapshot ) ;
412
+ Ok ( meta)
409
413
}
410
- None => self . unexpected ( ) ? ,
411
- }
414
+ None => self . unexpected_any ( ) ,
415
+ } ;
412
416
}
413
417
414
418
let lo = self . token . span ;
@@ -465,7 +469,7 @@ impl<'a> Parser<'a> {
465
469
466
470
let mut err = errors:: InvalidMetaItem {
467
471
span : self . token . span ,
468
- token : self . token . clone ( ) ,
472
+ descr : super :: token_descr ( & self . token ) ,
469
473
quote_ident_sugg : None ,
470
474
} ;
471
475
0 commit comments