@@ -9,7 +9,7 @@ pub use StabilityLevel::*;
99pub use crate :: ast:: Attribute ;
1010
1111use crate :: ast;
12- use crate :: ast:: { AttrId , AttrStyle , Name , Ident , Path , PathSegment } ;
12+ use crate :: ast:: { AttrItem , AttrId , AttrStyle , Name , Ident , Path , PathSegment } ;
1313use crate :: ast:: { MetaItem , MetaItemKind , NestedMetaItem } ;
1414use crate :: ast:: { Lit , LitKind , Expr , Item , Local , Stmt , StmtKind , GenericParam } ;
1515use crate :: mut_visit:: visit_clobber;
@@ -254,9 +254,8 @@ impl MetaItem {
254254 }
255255}
256256
257- impl Attribute {
258- /// Extracts the MetaItem from inside this Attribute.
259- pub fn meta ( & self ) -> Option < MetaItem > {
257+ impl AttrItem {
258+ crate fn meta ( & self , span : Span ) -> Option < MetaItem > {
260259 let mut tokens = self . tokens . trees ( ) . peekable ( ) ;
261260 Some ( MetaItem {
262261 path : self . path . clone ( ) ,
@@ -268,9 +267,16 @@ impl Attribute {
268267 } else {
269268 return None ;
270269 } ,
271- span : self . span ,
270+ span,
272271 } )
273272 }
273+ }
274+
275+ impl Attribute {
276+ /// Extracts the MetaItem from inside this Attribute.
277+ pub fn meta ( & self ) -> Option < MetaItem > {
278+ self . item . meta ( self . span )
279+ }
274280
275281 pub fn parse < ' a , T , F > ( & self , sess : & ' a ParseSess , mut f : F ) -> PResult < ' a , T >
276282 where F : FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
@@ -332,10 +338,9 @@ impl Attribute {
332338 DUMMY_SP ,
333339 ) ;
334340 f ( & Attribute {
341+ item : AttrItem { path : meta. path , tokens : meta. node . tokens ( meta. span ) } ,
335342 id : self . id ,
336343 style : self . style ,
337- path : meta. path ,
338- tokens : meta. node . tokens ( meta. span ) ,
339344 is_sugared_doc : true ,
340345 span : self . span ,
341346 } )
@@ -384,10 +389,9 @@ crate fn mk_attr_id() -> AttrId {
384389/// Returns an inner attribute with the given value and span.
385390pub fn mk_attr_inner ( item : MetaItem ) -> Attribute {
386391 Attribute {
392+ item : AttrItem { path : item. path , tokens : item. node . tokens ( item. span ) } ,
387393 id : mk_attr_id ( ) ,
388394 style : ast:: AttrStyle :: Inner ,
389- path : item. path ,
390- tokens : item. node . tokens ( item. span ) ,
391395 is_sugared_doc : false ,
392396 span : item. span ,
393397 }
@@ -396,10 +400,9 @@ pub fn mk_attr_inner(item: MetaItem) -> Attribute {
396400/// Returns an outer attribute with the given value and span.
397401pub fn mk_attr_outer ( item : MetaItem ) -> Attribute {
398402 Attribute {
403+ item : AttrItem { path : item. path , tokens : item. node . tokens ( item. span ) } ,
399404 id : mk_attr_id ( ) ,
400405 style : ast:: AttrStyle :: Outer ,
401- path : item. path ,
402- tokens : item. node . tokens ( item. span ) ,
403406 is_sugared_doc : false ,
404407 span : item. span ,
405408 }
@@ -410,10 +413,12 @@ pub fn mk_sugared_doc_attr(text: Symbol, span: Span) -> Attribute {
410413 let lit_kind = LitKind :: Str ( text, ast:: StrStyle :: Cooked ) ;
411414 let lit = Lit :: from_lit_kind ( lit_kind, span) ;
412415 Attribute {
416+ item : AttrItem {
417+ path : Path :: from_ident ( Ident :: with_dummy_span ( sym:: doc) . with_span_pos ( span) ) ,
418+ tokens : MetaItemKind :: NameValue ( lit) . tokens ( span) ,
419+ } ,
413420 id : mk_attr_id ( ) ,
414421 style,
415- path : Path :: from_ident ( Ident :: with_dummy_span ( sym:: doc) . with_span_pos ( span) ) ,
416- tokens : MetaItemKind :: NameValue ( lit) . tokens ( span) ,
417422 is_sugared_doc : true ,
418423 span,
419424 }
@@ -502,7 +507,7 @@ impl MetaItem {
502507 }
503508 Some ( TokenTree :: Token ( Token { kind : token:: Interpolated ( nt) , .. } ) ) => match * nt {
504509 token:: Nonterminal :: NtIdent ( ident, _) => Path :: from_ident ( ident) ,
505- token:: Nonterminal :: NtMeta ( ref meta ) => return Some ( meta . clone ( ) ) ,
510+ token:: Nonterminal :: NtMeta ( ref item ) => return item . meta ( item . path . span ) ,
506511 token:: Nonterminal :: NtPath ( ref path) => path. clone ( ) ,
507512 _ => return None ,
508513 } ,
@@ -726,7 +731,7 @@ pub fn inject(mut krate: ast::Crate, parse_sess: &ParseSess, attrs: &[String]) -
726731 ) ;
727732
728733 let start_span = parser. token . span ;
729- let ( path , tokens ) = panictry ! ( parser. parse_meta_item_unrestricted ( ) ) ;
734+ let item = panictry ! ( parser. parse_attr_item ( ) ) ;
730735 let end_span = parser. token . span ;
731736 if parser. token != token:: Eof {
732737 parse_sess. span_diagnostic
@@ -735,10 +740,9 @@ pub fn inject(mut krate: ast::Crate, parse_sess: &ParseSess, attrs: &[String]) -
735740 }
736741
737742 krate. attrs . push ( Attribute {
743+ item,
738744 id : mk_attr_id ( ) ,
739745 style : AttrStyle :: Inner ,
740- path,
741- tokens,
742746 is_sugared_doc : false ,
743747 span : start_span. to ( end_span) ,
744748 } ) ;
0 commit comments