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| {
@@ -397,18 +401,18 @@ impl<'a> Parser<'a> {
397401 & mut self ,
398402 unsafe_allowed : AllowLeadingUnsafe ,
399403 ) -> 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 ) )
404408 {
405- match attr_item. meta ( attr_item. path . span ) {
409+ return match attr_item. meta ( attr_item. path . span ) {
406410 Some ( meta) => {
407- self . bump ( ) ;
408- return Ok ( meta) ;
411+ self . restore_snapshot ( snapshot ) ;
412+ Ok ( meta)
409413 }
410- None => self . unexpected ( ) ? ,
411- }
414+ None => self . unexpected_any ( ) ,
415+ } ;
412416 }
413417
414418 let lo = self . token . span ;
@@ -465,7 +469,7 @@ impl<'a> Parser<'a> {
465469
466470 let mut err = errors:: InvalidMetaItem {
467471 span : self . token . span ,
468- token : self . token . clone ( ) ,
472+ descr : super :: token_descr ( & self . token ) ,
469473 quote_ident_sugg : None ,
470474 } ;
471475
0 commit comments