@@ -10,7 +10,7 @@ pub use crate::ast::Attribute;
1010
1111use crate :: ast;
1212use crate :: ast:: { AttrItem , AttrId , AttrKind , AttrStyle , Name , Ident , Path , PathSegment } ;
13- use crate :: ast:: { MetaItem , MetaItemKind , NestedMetaItem } ;
13+ use crate :: ast:: { MacArgs , MacDelimiter , MetaItem , MetaItemKind , NestedMetaItem } ;
1414use crate :: ast:: { Lit , LitKind , Expr , Item , Local , Stmt , StmtKind , GenericParam } ;
1515use crate :: mut_visit:: visit_clobber;
1616use crate :: source_map:: { BytePos , Spanned } ;
@@ -198,7 +198,7 @@ impl Attribute {
198198
199199 pub fn is_word ( & self ) -> bool {
200200 if let AttrKind :: Normal ( item) = & self . kind {
201- item. tokens . is_empty ( )
201+ matches ! ( item. args , MacArgs :: Empty )
202202 } else {
203203 false
204204 }
@@ -278,7 +278,7 @@ impl MetaItem {
278278
279279impl AttrItem {
280280 pub fn meta ( & self , span : Span ) -> Option < MetaItem > {
281- let mut tokens = self . tokens . trees ( ) . peekable ( ) ;
281+ let mut tokens = self . args . outer_tokens ( ) . trees ( ) . peekable ( ) ;
282282 Some ( MetaItem {
283283 path : self . path . clone ( ) ,
284284 kind : if let Some ( kind) = MetaItemKind :: from_tokens ( & mut tokens) {
@@ -362,8 +362,8 @@ crate fn mk_attr_id() -> AttrId {
362362 AttrId ( id)
363363}
364364
365- pub fn mk_attr ( style : AttrStyle , path : Path , tokens : TokenStream , span : Span ) -> Attribute {
366- mk_attr_from_item ( style, AttrItem { path, tokens } , span)
365+ pub fn mk_attr ( style : AttrStyle , path : Path , args : MacArgs , span : Span ) -> Attribute {
366+ mk_attr_from_item ( style, AttrItem { path, args } , span)
367367}
368368
369369pub fn mk_attr_from_item ( style : AttrStyle , item : AttrItem , span : Span ) -> Attribute {
@@ -377,12 +377,12 @@ pub fn mk_attr_from_item(style: AttrStyle, item: AttrItem, span: Span) -> Attrib
377377
378378/// Returns an inner attribute with the given value and span.
379379pub fn mk_attr_inner ( item : MetaItem ) -> Attribute {
380- mk_attr ( AttrStyle :: Inner , item. path , item. kind . tokens ( item. span ) , item. span )
380+ mk_attr ( AttrStyle :: Inner , item. path , item. kind . mac_args ( item. span ) , item. span )
381381}
382382
383383/// Returns an outer attribute with the given value and span.
384384pub fn mk_attr_outer ( item : MetaItem ) -> Attribute {
385- mk_attr ( AttrStyle :: Outer , item. path , item. kind . tokens ( item. span ) , item. span )
385+ mk_attr ( AttrStyle :: Outer , item. path , item. kind . mac_args ( item. span ) , item. span )
386386}
387387
388388pub fn mk_doc_comment ( style : AttrStyle , comment : Symbol , span : Span ) -> Attribute {
@@ -520,7 +520,26 @@ impl MetaItem {
520520}
521521
522522impl MetaItemKind {
523- pub fn token_trees_and_joints ( & self , span : Span ) -> Vec < TreeAndJoint > {
523+ pub fn mac_args ( & self , span : Span ) -> MacArgs {
524+ match self {
525+ MetaItemKind :: Word => MacArgs :: Empty ,
526+ MetaItemKind :: NameValue ( lit) => MacArgs :: Eq ( span, lit. token_tree ( ) . into ( ) ) ,
527+ MetaItemKind :: List ( list) => {
528+ let mut tts = Vec :: new ( ) ;
529+ for ( i, item) in list. iter ( ) . enumerate ( ) {
530+ if i > 0 {
531+ tts. push ( TokenTree :: token ( token:: Comma , span) . into ( ) ) ;
532+ }
533+ tts. extend ( item. token_trees_and_joints ( ) )
534+ }
535+ MacArgs :: Delimited (
536+ DelimSpan :: from_single ( span) , MacDelimiter :: Parenthesis , TokenStream :: new ( tts)
537+ )
538+ }
539+ }
540+ }
541+
542+ fn token_trees_and_joints ( & self , span : Span ) -> Vec < TreeAndJoint > {
524543 match * self {
525544 MetaItemKind :: Word => vec ! [ ] ,
526545 MetaItemKind :: NameValue ( ref lit) => {
@@ -548,13 +567,6 @@ impl MetaItemKind {
548567 }
549568 }
550569
551- // Premature conversions of `TokenTree`s to `TokenStream`s can hurt
552- // performance. Do not use this function if `token_trees_and_joints()` can
553- // be used instead.
554- pub fn tokens ( & self , span : Span ) -> TokenStream {
555- TokenStream :: new ( self . token_trees_and_joints ( span) )
556- }
557-
558570 fn from_tokens < I > ( tokens : & mut iter:: Peekable < I > ) -> Option < MetaItemKind >
559571 where I : Iterator < Item = TokenTree > ,
560572 {
0 commit comments