@@ -8,7 +8,7 @@ use crate::ast::{Path, PathSegment};
8
8
use crate :: mut_visit:: visit_clobber;
9
9
use crate :: ptr:: P ;
10
10
use crate :: token:: { self , CommentKind , Token } ;
11
- use crate :: tokenstream:: { DelimSpan , TokenStream , TokenTree , TreeAndSpacing } ;
11
+ use crate :: tokenstream:: { DelimSpan , LazyTokenStream , TokenStream , TokenTree , TreeAndSpacing } ;
12
12
13
13
use rustc_index:: bit_set:: GrowableBitSet ;
14
14
use rustc_span:: source_map:: { BytePos , Spanned } ;
@@ -120,15 +120,15 @@ impl NestedMetaItem {
120
120
impl Attribute {
121
121
pub fn has_name ( & self , name : Symbol ) -> bool {
122
122
match self . kind {
123
- AttrKind :: Normal ( ref item) => item. path == name,
123
+ AttrKind :: Normal ( ref item, _ ) => item. path == name,
124
124
AttrKind :: DocComment ( ..) => false ,
125
125
}
126
126
}
127
127
128
128
/// For a single-segment attribute, returns its name; otherwise, returns `None`.
129
129
pub fn ident ( & self ) -> Option < Ident > {
130
130
match self . kind {
131
- AttrKind :: Normal ( ref item) => {
131
+ AttrKind :: Normal ( ref item, _ ) => {
132
132
if item. path . segments . len ( ) == 1 {
133
133
Some ( item. path . segments [ 0 ] . ident )
134
134
} else {
@@ -144,14 +144,14 @@ impl Attribute {
144
144
145
145
pub fn value_str ( & self ) -> Option < Symbol > {
146
146
match self . kind {
147
- AttrKind :: Normal ( ref item) => item. meta ( self . span ) . and_then ( |meta| meta. value_str ( ) ) ,
147
+ AttrKind :: Normal ( ref item, _ ) => item. meta ( self . span ) . and_then ( |meta| meta. value_str ( ) ) ,
148
148
AttrKind :: DocComment ( ..) => None ,
149
149
}
150
150
}
151
151
152
152
pub fn meta_item_list ( & self ) -> Option < Vec < NestedMetaItem > > {
153
153
match self . kind {
154
- AttrKind :: Normal ( ref item) => match item. meta ( self . span ) {
154
+ AttrKind :: Normal ( ref item, _ ) => match item. meta ( self . span ) {
155
155
Some ( MetaItem { kind : MetaItemKind :: List ( list) , .. } ) => Some ( list) ,
156
156
_ => None ,
157
157
} ,
@@ -160,7 +160,7 @@ impl Attribute {
160
160
}
161
161
162
162
pub fn is_word ( & self ) -> bool {
163
- if let AttrKind :: Normal ( item) = & self . kind {
163
+ if let AttrKind :: Normal ( item, _ ) = & self . kind {
164
164
matches ! ( item. args, MacArgs :: Empty )
165
165
} else {
166
166
false
@@ -246,15 +246,15 @@ impl AttrItem {
246
246
impl Attribute {
247
247
pub fn is_doc_comment ( & self ) -> bool {
248
248
match self . kind {
249
- AttrKind :: Normal ( _ ) => false ,
249
+ AttrKind :: Normal ( .. ) => false ,
250
250
AttrKind :: DocComment ( ..) => true ,
251
251
}
252
252
}
253
253
254
254
pub fn doc_str ( & self ) -> Option < Symbol > {
255
255
match self . kind {
256
256
AttrKind :: DocComment ( .., data) => Some ( data) ,
257
- AttrKind :: Normal ( ref item) if item. path == sym:: doc => {
257
+ AttrKind :: Normal ( ref item, _ ) if item. path == sym:: doc => {
258
258
item. meta ( self . span ) . and_then ( |meta| meta. value_str ( ) )
259
259
}
260
260
_ => None ,
@@ -263,25 +263,37 @@ impl Attribute {
263
263
264
264
pub fn get_normal_item ( & self ) -> & AttrItem {
265
265
match self . kind {
266
- AttrKind :: Normal ( ref item) => item,
266
+ AttrKind :: Normal ( ref item, _ ) => item,
267
267
AttrKind :: DocComment ( ..) => panic ! ( "unexpected doc comment" ) ,
268
268
}
269
269
}
270
270
271
271
pub fn unwrap_normal_item ( self ) -> AttrItem {
272
272
match self . kind {
273
- AttrKind :: Normal ( item) => item,
273
+ AttrKind :: Normal ( item, _ ) => item,
274
274
AttrKind :: DocComment ( ..) => panic ! ( "unexpected doc comment" ) ,
275
275
}
276
276
}
277
277
278
278
/// Extracts the MetaItem from inside this Attribute.
279
279
pub fn meta ( & self ) -> Option < MetaItem > {
280
280
match self . kind {
281
- AttrKind :: Normal ( ref item) => item. meta ( self . span ) ,
281
+ AttrKind :: Normal ( ref item, _ ) => item. meta ( self . span ) ,
282
282
AttrKind :: DocComment ( ..) => None ,
283
283
}
284
284
}
285
+
286
+ pub fn tokens ( & self ) -> TokenStream {
287
+ match self . kind {
288
+ AttrKind :: Normal ( _, ref tokens) => tokens
289
+ . as_ref ( )
290
+ . unwrap_or_else ( || panic ! ( "attribute is missing tokens: {:?}" , self ) )
291
+ . create_token_stream ( ) ,
292
+ AttrKind :: DocComment ( comment_kind, data) => TokenStream :: from ( TokenTree :: Token (
293
+ Token :: new ( token:: DocComment ( comment_kind, self . style , data) , self . span ) ,
294
+ ) ) ,
295
+ }
296
+ }
285
297
}
286
298
287
299
/* Constructors */
@@ -321,11 +333,16 @@ crate fn mk_attr_id() -> AttrId {
321
333
}
322
334
323
335
pub fn mk_attr ( style : AttrStyle , path : Path , args : MacArgs , span : Span ) -> Attribute {
324
- mk_attr_from_item ( style , AttrItem { path, args, tokens : None } , span)
336
+ mk_attr_from_item ( AttrItem { path, args, tokens : None } , None , style , span)
325
337
}
326
338
327
- pub fn mk_attr_from_item ( style : AttrStyle , item : AttrItem , span : Span ) -> Attribute {
328
- Attribute { kind : AttrKind :: Normal ( item) , id : mk_attr_id ( ) , style, span, tokens : None }
339
+ pub fn mk_attr_from_item (
340
+ item : AttrItem ,
341
+ tokens : Option < LazyTokenStream > ,
342
+ style : AttrStyle ,
343
+ span : Span ,
344
+ ) -> Attribute {
345
+ Attribute { kind : AttrKind :: Normal ( item, tokens) , id : mk_attr_id ( ) , style, span }
329
346
}
330
347
331
348
/// Returns an inner attribute with the given value and span.
@@ -344,13 +361,7 @@ pub fn mk_doc_comment(
344
361
data : Symbol ,
345
362
span : Span ,
346
363
) -> Attribute {
347
- Attribute {
348
- kind : AttrKind :: DocComment ( comment_kind, data) ,
349
- id : mk_attr_id ( ) ,
350
- style,
351
- span,
352
- tokens : None ,
353
- }
364
+ Attribute { kind : AttrKind :: DocComment ( comment_kind, data) , id : mk_attr_id ( ) , style, span }
354
365
}
355
366
356
367
pub fn list_contains_name ( items : & [ NestedMetaItem ] , name : Symbol ) -> bool {
0 commit comments