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| {
@@ -399,18 +403,18 @@ impl<'a> Parser<'a> {
399
403
& mut self ,
400
404
unsafe_allowed : AllowLeadingUnsafe ,
401
405
) -> PResult < ' a , ast:: MetaItem > {
402
- // We can't use `maybe_whole` here because it would bump in the `None`
403
- // case, which we don't want.
404
- if let token :: Interpolated ( nt ) = & self . token . kind
405
- && let token :: NtMeta ( attr_item ) = & * * nt
406
+ // Snapshot the parser so we can backtrack in the case where `attr_item.meta()` fails.
407
+ let mut snapshot = self . create_snapshot_for_diagnostic ( ) ;
408
+ if let Some ( attr_item ) = snapshot
409
+ . eat_metavar_seq ( MetaVarKind :: Meta , |this| this . parse_attr_item ( ForceCollect :: No ) )
406
410
{
407
- match attr_item. meta ( attr_item. path . span ) {
411
+ return match attr_item. meta ( attr_item. path . span ) {
408
412
Some ( meta) => {
409
- self . bump ( ) ;
410
- return Ok ( meta) ;
413
+ self . restore_snapshot ( snapshot ) ;
414
+ Ok ( meta)
411
415
}
412
- None => self . unexpected ( ) ? ,
413
- }
416
+ None => self . unexpected_any ( ) ,
417
+ } ;
414
418
}
415
419
416
420
let lo = self . token . span ;
@@ -467,7 +471,7 @@ impl<'a> Parser<'a> {
467
471
468
472
let mut err = errors:: InvalidMetaItem {
469
473
span : self . token . span ,
470
- token : self . token . clone ( ) ,
474
+ descr : super :: token_descr ( & self . token ) ,
471
475
quote_ident_sugg : None ,
472
476
} ;
473
477
0 commit comments