11use rustc_ast as ast;
22use rustc_ast:: attr;
3- use rustc_ast:: token:: { self , Delimiter } ;
3+ use rustc_ast:: token:: { self , Delimiter , MetaVarKind } ;
44use rustc_errors:: codes:: * ;
55use rustc_errors:: { Diag , PResult } ;
66use rustc_span:: symbol:: kw;
@@ -12,7 +12,7 @@ use super::{
1212 AttrWrapper , Capturing , FnParseMode , ForceCollect , Parser , ParserRange , PathStyle , Trailing ,
1313 UsePreAttrPos ,
1414} ;
15- use crate :: { errors, fluent_generated as fluent, maybe_whole } ;
15+ use crate :: { errors, fluent_generated as fluent} ;
1616
1717// Public for rustfmt usage
1818#[ derive( Debug ) ]
@@ -272,7 +272,11 @@ impl<'a> Parser<'a> {
272272 /// PATH `=` UNSUFFIXED_LIT
273273 /// The delimiters or `=` are still put into the resulting token stream.
274274 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+ }
276280
277281 // Attr items don't have attributes.
278282 self . collect_tokens ( None , AttrWrapper :: empty ( ) , force_collect, |this, _empty_attrs| {
@@ -399,18 +403,18 @@ impl<'a> Parser<'a> {
399403 & mut self ,
400404 unsafe_allowed : AllowLeadingUnsafe ,
401405 ) -> 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 ) )
406410 {
407- match attr_item. meta ( attr_item. path . span ) {
411+ return match attr_item. meta ( attr_item. path . span ) {
408412 Some ( meta) => {
409- self . bump ( ) ;
410- return Ok ( meta) ;
413+ self . restore_snapshot ( snapshot ) ;
414+ Ok ( meta)
411415 }
412- None => self . unexpected ( ) ? ,
413- }
416+ None => self . unexpected_any ( ) ,
417+ } ;
414418 }
415419
416420 let lo = self . token . span ;
@@ -467,7 +471,7 @@ impl<'a> Parser<'a> {
467471
468472 let mut err = errors:: InvalidMetaItem {
469473 span : self . token . span ,
470- token : self . token . clone ( ) ,
474+ descr : super :: token_descr ( & self . token ) ,
471475 quote_ident_sugg : None ,
472476 } ;
473477
0 commit comments