@@ -9,7 +9,7 @@ pub use StabilityLevel::*;
9
9
pub use crate :: ast:: Attribute ;
10
10
11
11
use crate :: ast;
12
- use crate :: ast:: { AttrId , AttrStyle , Name , Ident , Path , PathSegment } ;
12
+ use crate :: ast:: { AttrItem , AttrId , AttrStyle , Name , Ident , Path , PathSegment } ;
13
13
use crate :: ast:: { MetaItem , MetaItemKind , NestedMetaItem } ;
14
14
use crate :: ast:: { Lit , LitKind , Expr , Item , Local , Stmt , StmtKind , GenericParam } ;
15
15
use crate :: mut_visit:: visit_clobber;
@@ -254,9 +254,8 @@ impl MetaItem {
254
254
}
255
255
}
256
256
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 > {
260
259
let mut tokens = self . tokens . trees ( ) . peekable ( ) ;
261
260
Some ( MetaItem {
262
261
path : self . path . clone ( ) ,
@@ -268,9 +267,16 @@ impl Attribute {
268
267
} else {
269
268
return None ;
270
269
} ,
271
- span : self . span ,
270
+ span,
272
271
} )
273
272
}
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
+ }
274
280
275
281
pub fn parse < ' a , T , F > ( & self , sess : & ' a ParseSess , mut f : F ) -> PResult < ' a , T >
276
282
where F : FnMut ( & mut Parser < ' a > ) -> PResult < ' a , T > ,
@@ -332,10 +338,9 @@ impl Attribute {
332
338
DUMMY_SP ,
333
339
) ;
334
340
f ( & Attribute {
341
+ item : AttrItem { path : meta. path , tokens : meta. node . tokens ( meta. span ) } ,
335
342
id : self . id ,
336
343
style : self . style ,
337
- path : meta. path ,
338
- tokens : meta. node . tokens ( meta. span ) ,
339
344
is_sugared_doc : true ,
340
345
span : self . span ,
341
346
} )
@@ -384,10 +389,9 @@ crate fn mk_attr_id() -> AttrId {
384
389
/// Returns an inner attribute with the given value and span.
385
390
pub fn mk_attr_inner ( item : MetaItem ) -> Attribute {
386
391
Attribute {
392
+ item : AttrItem { path : item. path , tokens : item. node . tokens ( item. span ) } ,
387
393
id : mk_attr_id ( ) ,
388
394
style : ast:: AttrStyle :: Inner ,
389
- path : item. path ,
390
- tokens : item. node . tokens ( item. span ) ,
391
395
is_sugared_doc : false ,
392
396
span : item. span ,
393
397
}
@@ -396,10 +400,9 @@ pub fn mk_attr_inner(item: MetaItem) -> Attribute {
396
400
/// Returns an outer attribute with the given value and span.
397
401
pub fn mk_attr_outer ( item : MetaItem ) -> Attribute {
398
402
Attribute {
403
+ item : AttrItem { path : item. path , tokens : item. node . tokens ( item. span ) } ,
399
404
id : mk_attr_id ( ) ,
400
405
style : ast:: AttrStyle :: Outer ,
401
- path : item. path ,
402
- tokens : item. node . tokens ( item. span ) ,
403
406
is_sugared_doc : false ,
404
407
span : item. span ,
405
408
}
@@ -410,10 +413,12 @@ pub fn mk_sugared_doc_attr(text: Symbol, span: Span) -> Attribute {
410
413
let lit_kind = LitKind :: Str ( text, ast:: StrStyle :: Cooked ) ;
411
414
let lit = Lit :: from_lit_kind ( lit_kind, span) ;
412
415
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
+ } ,
413
420
id : mk_attr_id ( ) ,
414
421
style,
415
- path : Path :: from_ident ( Ident :: with_dummy_span ( sym:: doc) . with_span_pos ( span) ) ,
416
- tokens : MetaItemKind :: NameValue ( lit) . tokens ( span) ,
417
422
is_sugared_doc : true ,
418
423
span,
419
424
}
@@ -502,7 +507,7 @@ impl MetaItem {
502
507
}
503
508
Some ( TokenTree :: Token ( Token { kind : token:: Interpolated ( nt) , .. } ) ) => match * nt {
504
509
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 ) ,
506
511
token:: Nonterminal :: NtPath ( ref path) => path. clone ( ) ,
507
512
_ => return None ,
508
513
} ,
@@ -726,7 +731,7 @@ pub fn inject(mut krate: ast::Crate, parse_sess: &ParseSess, attrs: &[String]) -
726
731
) ;
727
732
728
733
let start_span = parser. token . span ;
729
- let ( path , tokens ) = panictry ! ( parser. parse_meta_item_unrestricted ( ) ) ;
734
+ let item = panictry ! ( parser. parse_attr_item ( ) ) ;
730
735
let end_span = parser. token . span ;
731
736
if parser. token != token:: Eof {
732
737
parse_sess. span_diagnostic
@@ -735,10 +740,9 @@ pub fn inject(mut krate: ast::Crate, parse_sess: &ParseSess, attrs: &[String]) -
735
740
}
736
741
737
742
krate. attrs . push ( Attribute {
743
+ item,
738
744
id : mk_attr_id ( ) ,
739
745
style : AttrStyle :: Inner ,
740
- path,
741
- tokens,
742
746
is_sugared_doc : false ,
743
747
span : start_span. to ( end_span) ,
744
748
} ) ;
0 commit comments