]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast/src/attr/mod.rs
c948faeb358356b263587e00d448c54d2d9d512f
[rust.git] / compiler / rustc_ast / src / attr / mod.rs
1 //! Functions dealing with attributes and meta items.
2
3 use crate::ast;
4 use crate::ast::{AttrArgs, AttrArgsEq, AttrId, AttrItem, AttrKind, AttrStyle, Attribute};
5 use crate::ast::{DelimArgs, Lit, LitKind};
6 use crate::ast::{MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem};
7 use crate::ast::{Path, PathSegment};
8 use crate::ptr::P;
9 use crate::token::{self, CommentKind, Delimiter, Token};
10 use crate::tokenstream::{DelimSpan, Spacing, TokenTree};
11 use crate::tokenstream::{LazyAttrTokenStream, TokenStream};
12 use crate::util::comments;
13 use rustc_data_structures::sync::WorkerLocal;
14 use rustc_index::bit_set::GrowableBitSet;
15 use rustc_span::source_map::BytePos;
16 use rustc_span::symbol::{sym, Ident, Symbol};
17 use rustc_span::Span;
18 use std::cell::Cell;
19 use std::iter;
20 #[cfg(debug_assertions)]
21 use std::ops::BitXor;
22 #[cfg(debug_assertions)]
23 use std::sync::atomic::{AtomicU32, Ordering};
24 use thin_vec::thin_vec;
25
26 pub struct MarkedAttrs(GrowableBitSet<AttrId>);
27
28 impl MarkedAttrs {
29     pub fn new() -> Self {
30         // We have no idea how many attributes there will be, so just
31         // initiate the vectors with 0 bits. We'll grow them as necessary.
32         MarkedAttrs(GrowableBitSet::new_empty())
33     }
34
35     pub fn mark(&mut self, attr: &Attribute) {
36         self.0.insert(attr.id);
37     }
38
39     pub fn is_marked(&self, attr: &Attribute) -> bool {
40         self.0.contains(attr.id)
41     }
42 }
43
44 impl NestedMetaItem {
45     /// Returns the `MetaItem` if `self` is a `NestedMetaItem::MetaItem`.
46     pub fn meta_item(&self) -> Option<&MetaItem> {
47         match self {
48             NestedMetaItem::MetaItem(item) => Some(item),
49             _ => None,
50         }
51     }
52
53     /// Returns the `Lit` if `self` is a `NestedMetaItem::Literal`s.
54     pub fn literal(&self) -> Option<&Lit> {
55         match self {
56             NestedMetaItem::Literal(lit) => Some(lit),
57             _ => None,
58         }
59     }
60
61     /// Returns `true` if this list item is a MetaItem with a name of `name`.
62     pub fn has_name(&self, name: Symbol) -> bool {
63         self.meta_item().map_or(false, |meta_item| meta_item.has_name(name))
64     }
65
66     /// For a single-segment meta item, returns its name; otherwise, returns `None`.
67     pub fn ident(&self) -> Option<Ident> {
68         self.meta_item().and_then(|meta_item| meta_item.ident())
69     }
70     pub fn name_or_empty(&self) -> Symbol {
71         self.ident().unwrap_or_else(Ident::empty).name
72     }
73
74     /// Gets the string value if `self` is a `MetaItem` and the `MetaItem` is a
75     /// `MetaItemKind::NameValue` variant containing a string, otherwise `None`.
76     pub fn value_str(&self) -> Option<Symbol> {
77         self.meta_item().and_then(|meta_item| meta_item.value_str())
78     }
79
80     /// Returns a name and single literal value tuple of the `MetaItem`.
81     pub fn name_value_literal(&self) -> Option<(Symbol, &Lit)> {
82         self.meta_item().and_then(|meta_item| {
83             meta_item.meta_item_list().and_then(|meta_item_list| {
84                 if meta_item_list.len() == 1
85                     && let Some(ident) = meta_item.ident()
86                     && let Some(lit) = meta_item_list[0].literal()
87                 {
88                     return Some((ident.name, lit));
89                 }
90                 None
91             })
92         })
93     }
94
95     /// Gets a list of inner meta items from a list `MetaItem` type.
96     pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
97         self.meta_item().and_then(|meta_item| meta_item.meta_item_list())
98     }
99
100     /// Returns `true` if the variant is `MetaItem`.
101     pub fn is_meta_item(&self) -> bool {
102         self.meta_item().is_some()
103     }
104
105     /// Returns `true` if `self` is a `MetaItem` and the meta item is a word.
106     pub fn is_word(&self) -> bool {
107         self.meta_item().map_or(false, |meta_item| meta_item.is_word())
108     }
109
110     /// See [`MetaItem::name_value_literal_span`].
111     pub fn name_value_literal_span(&self) -> Option<Span> {
112         self.meta_item()?.name_value_literal_span()
113     }
114 }
115
116 impl Attribute {
117     #[inline]
118     pub fn has_name(&self, name: Symbol) -> bool {
119         match &self.kind {
120             AttrKind::Normal(normal) => normal.item.path == name,
121             AttrKind::DocComment(..) => false,
122         }
123     }
124
125     /// For a single-segment attribute, returns its name; otherwise, returns `None`.
126     pub fn ident(&self) -> Option<Ident> {
127         match &self.kind {
128             AttrKind::Normal(normal) => {
129                 if let [ident] = &*normal.item.path.segments {
130                     Some(ident.ident)
131                 } else {
132                     None
133                 }
134             }
135             AttrKind::DocComment(..) => None,
136         }
137     }
138     pub fn name_or_empty(&self) -> Symbol {
139         self.ident().unwrap_or_else(Ident::empty).name
140     }
141
142     pub fn value_str(&self) -> Option<Symbol> {
143         match &self.kind {
144             AttrKind::Normal(normal) => normal.item.meta_kind().and_then(|kind| kind.value_str()),
145             AttrKind::DocComment(..) => None,
146         }
147     }
148
149     pub fn meta_item_list(&self) -> Option<Vec<NestedMetaItem>> {
150         match &self.kind {
151             AttrKind::Normal(normal) => match normal.item.meta_kind() {
152                 Some(MetaItemKind::List(list)) => Some(list),
153                 _ => None,
154             },
155             AttrKind::DocComment(..) => None,
156         }
157     }
158
159     pub fn is_word(&self) -> bool {
160         if let AttrKind::Normal(normal) = &self.kind {
161             matches!(normal.item.args, AttrArgs::Empty)
162         } else {
163             false
164         }
165     }
166 }
167
168 impl MetaItem {
169     /// For a single-segment meta item, returns its name; otherwise, returns `None`.
170     pub fn ident(&self) -> Option<Ident> {
171         if self.path.segments.len() == 1 { Some(self.path.segments[0].ident) } else { None }
172     }
173     pub fn name_or_empty(&self) -> Symbol {
174         self.ident().unwrap_or_else(Ident::empty).name
175     }
176
177     /// ```text
178     /// Example:
179     ///     #[attribute(name = "value")]
180     ///                 ^^^^^^^^^^^^^^
181     /// ```
182     pub fn name_value_literal(&self) -> Option<&Lit> {
183         match &self.kind {
184             MetaItemKind::NameValue(v) => Some(v),
185             _ => None,
186         }
187     }
188
189     pub fn value_str(&self) -> Option<Symbol> {
190         self.kind.value_str()
191     }
192
193     pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
194         match &self.kind {
195             MetaItemKind::List(l) => Some(&**l),
196             _ => None,
197         }
198     }
199
200     pub fn is_word(&self) -> bool {
201         matches!(self.kind, MetaItemKind::Word)
202     }
203
204     pub fn has_name(&self, name: Symbol) -> bool {
205         self.path == name
206     }
207
208     /// This is used in case you want the value span instead of the whole attribute. Example:
209     ///
210     /// ```text
211     /// #[doc(alias = "foo")]
212     /// ```
213     ///
214     /// In here, it'll return a span for `"foo"`.
215     pub fn name_value_literal_span(&self) -> Option<Span> {
216         Some(self.name_value_literal()?.span)
217     }
218 }
219
220 impl AttrItem {
221     pub fn span(&self) -> Span {
222         self.args.span().map_or(self.path.span, |args_span| self.path.span.to(args_span))
223     }
224
225     pub fn meta(&self, span: Span) -> Option<MetaItem> {
226         Some(MetaItem {
227             path: self.path.clone(),
228             kind: MetaItemKind::from_attr_args(&self.args)?,
229             span,
230         })
231     }
232
233     pub fn meta_kind(&self) -> Option<MetaItemKind> {
234         MetaItemKind::from_attr_args(&self.args)
235     }
236 }
237
238 impl Attribute {
239     /// Returns `true` if it is a sugared doc comment (`///` or `//!` for example).
240     /// So `#[doc = "doc"]` (which is a doc comment) and `#[doc(...)]` (which is not
241     /// a doc comment) will return `false`.
242     pub fn is_doc_comment(&self) -> bool {
243         match self.kind {
244             AttrKind::Normal(..) => false,
245             AttrKind::DocComment(..) => true,
246         }
247     }
248
249     /// Returns the documentation and its kind if this is a doc comment or a sugared doc comment.
250     /// * `///doc` returns `Some(("doc", CommentKind::Line))`.
251     /// * `/** doc */` returns `Some(("doc", CommentKind::Block))`.
252     /// * `#[doc = "doc"]` returns `Some(("doc", CommentKind::Line))`.
253     /// * `#[doc(...)]` returns `None`.
254     pub fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {
255         match self.kind {
256             AttrKind::DocComment(kind, data) => Some((data, kind)),
257             AttrKind::Normal(ref normal) if normal.item.path == sym::doc => normal
258                 .item
259                 .meta_kind()
260                 .and_then(|kind| kind.value_str())
261                 .map(|data| (data, CommentKind::Line)),
262             _ => None,
263         }
264     }
265
266     /// Returns the documentation if this is a doc comment or a sugared doc comment.
267     /// * `///doc` returns `Some("doc")`.
268     /// * `#[doc = "doc"]` returns `Some("doc")`.
269     /// * `#[doc(...)]` returns `None`.
270     pub fn doc_str(&self) -> Option<Symbol> {
271         match &self.kind {
272             AttrKind::DocComment(.., data) => Some(*data),
273             AttrKind::Normal(normal) if normal.item.path == sym::doc => {
274                 normal.item.meta_kind().and_then(|kind| kind.value_str())
275             }
276             _ => None,
277         }
278     }
279
280     pub fn may_have_doc_links(&self) -> bool {
281         self.doc_str().map_or(false, |s| comments::may_have_doc_links(s.as_str()))
282     }
283
284     pub fn get_normal_item(&self) -> &AttrItem {
285         match &self.kind {
286             AttrKind::Normal(normal) => &normal.item,
287             AttrKind::DocComment(..) => panic!("unexpected doc comment"),
288         }
289     }
290
291     pub fn unwrap_normal_item(self) -> AttrItem {
292         match self.kind {
293             AttrKind::Normal(normal) => normal.into_inner().item,
294             AttrKind::DocComment(..) => panic!("unexpected doc comment"),
295         }
296     }
297
298     /// Extracts the MetaItem from inside this Attribute.
299     pub fn meta(&self) -> Option<MetaItem> {
300         match &self.kind {
301             AttrKind::Normal(normal) => normal.item.meta(self.span),
302             AttrKind::DocComment(..) => None,
303         }
304     }
305
306     pub fn meta_kind(&self) -> Option<MetaItemKind> {
307         match &self.kind {
308             AttrKind::Normal(normal) => normal.item.meta_kind(),
309             AttrKind::DocComment(..) => None,
310         }
311     }
312
313     pub fn tokens(&self) -> TokenStream {
314         match &self.kind {
315             AttrKind::Normal(normal) => normal
316                 .tokens
317                 .as_ref()
318                 .unwrap_or_else(|| panic!("attribute is missing tokens: {:?}", self))
319                 .to_attr_token_stream()
320                 .to_tokenstream(),
321             &AttrKind::DocComment(comment_kind, data) => TokenStream::new(vec![TokenTree::Token(
322                 Token::new(token::DocComment(comment_kind, self.style, data), self.span),
323                 Spacing::Alone,
324             )]),
325         }
326     }
327 }
328
329 /* Constructors */
330
331 pub fn mk_name_value_item_str(ident: Ident, str: Symbol, str_span: Span) -> MetaItem {
332     let lit_kind = LitKind::Str(str, ast::StrStyle::Cooked);
333     mk_name_value_item(ident, lit_kind, str_span)
334 }
335
336 pub fn mk_name_value_item(ident: Ident, lit_kind: LitKind, lit_span: Span) -> MetaItem {
337     let lit = Lit::from_lit_kind(lit_kind, lit_span);
338     let span = ident.span.to(lit_span);
339     MetaItem { path: Path::from_ident(ident), span, kind: MetaItemKind::NameValue(lit) }
340 }
341
342 pub fn mk_list_item(ident: Ident, items: Vec<NestedMetaItem>) -> MetaItem {
343     MetaItem { path: Path::from_ident(ident), span: ident.span, kind: MetaItemKind::List(items) }
344 }
345
346 pub fn mk_word_item(ident: Ident) -> MetaItem {
347     MetaItem { path: Path::from_ident(ident), span: ident.span, kind: MetaItemKind::Word }
348 }
349
350 pub fn mk_nested_word_item(ident: Ident) -> NestedMetaItem {
351     NestedMetaItem::MetaItem(mk_word_item(ident))
352 }
353
354 pub struct AttrIdGenerator(WorkerLocal<Cell<u32>>);
355
356 #[cfg(debug_assertions)]
357 static MAX_ATTR_ID: AtomicU32 = AtomicU32::new(u32::MAX);
358
359 impl AttrIdGenerator {
360     pub fn new() -> Self {
361         // We use `(index as u32).reverse_bits()` to initialize the
362         // starting value of AttrId in each worker thread.
363         // The `index` is the index of the worker thread.
364         // This ensures that the AttrId generated in each thread is unique.
365         AttrIdGenerator(WorkerLocal::new(|index| {
366             let index: u32 = index.try_into().unwrap();
367
368             #[cfg(debug_assertions)]
369             {
370                 let max_id = ((index + 1).next_power_of_two() - 1).bitxor(u32::MAX).reverse_bits();
371                 MAX_ATTR_ID.fetch_min(max_id, Ordering::Release);
372             }
373
374             Cell::new(index.reverse_bits())
375         }))
376     }
377
378     pub fn mk_attr_id(&self) -> AttrId {
379         let id = self.0.get();
380
381         // Ensure the assigned attr_id does not overlap the bits
382         // representing the number of threads.
383         #[cfg(debug_assertions)]
384         assert!(id <= MAX_ATTR_ID.load(Ordering::Acquire));
385
386         self.0.set(id + 1);
387         AttrId::from_u32(id)
388     }
389 }
390
391 pub fn mk_attr(
392     g: &AttrIdGenerator,
393     style: AttrStyle,
394     path: Path,
395     args: AttrArgs,
396     span: Span,
397 ) -> Attribute {
398     mk_attr_from_item(g, AttrItem { path, args, tokens: None }, None, style, span)
399 }
400
401 pub fn mk_attr_from_item(
402     g: &AttrIdGenerator,
403     item: AttrItem,
404     tokens: Option<LazyAttrTokenStream>,
405     style: AttrStyle,
406     span: Span,
407 ) -> Attribute {
408     Attribute {
409         kind: AttrKind::Normal(P(ast::NormalAttr { item, tokens })),
410         id: g.mk_attr_id(),
411         style,
412         span,
413     }
414 }
415
416 /// Returns an inner attribute with the given value and span.
417 pub fn mk_attr_inner(g: &AttrIdGenerator, item: MetaItem) -> Attribute {
418     mk_attr(g, AttrStyle::Inner, item.path, item.kind.attr_args(item.span), item.span)
419 }
420
421 /// Returns an outer attribute with the given value and span.
422 pub fn mk_attr_outer(g: &AttrIdGenerator, item: MetaItem) -> Attribute {
423     mk_attr(g, AttrStyle::Outer, item.path, item.kind.attr_args(item.span), item.span)
424 }
425
426 pub fn mk_doc_comment(
427     g: &AttrIdGenerator,
428     comment_kind: CommentKind,
429     style: AttrStyle,
430     data: Symbol,
431     span: Span,
432 ) -> Attribute {
433     Attribute { kind: AttrKind::DocComment(comment_kind, data), id: g.mk_attr_id(), style, span }
434 }
435
436 pub fn list_contains_name(items: &[NestedMetaItem], name: Symbol) -> bool {
437     items.iter().any(|item| item.has_name(name))
438 }
439
440 impl MetaItem {
441     fn token_trees(&self) -> Vec<TokenTree> {
442         let mut idents = vec![];
443         let mut last_pos = BytePos(0_u32);
444         for (i, segment) in self.path.segments.iter().enumerate() {
445             let is_first = i == 0;
446             if !is_first {
447                 let mod_sep_span =
448                     Span::new(last_pos, segment.ident.span.lo(), segment.ident.span.ctxt(), None);
449                 idents.push(TokenTree::token_alone(token::ModSep, mod_sep_span));
450             }
451             idents.push(TokenTree::Token(Token::from_ast_ident(segment.ident), Spacing::Alone));
452             last_pos = segment.ident.span.hi();
453         }
454         idents.extend(self.kind.token_trees(self.span));
455         idents
456     }
457
458     fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<MetaItem>
459     where
460         I: Iterator<Item = TokenTree>,
461     {
462         // FIXME: Share code with `parse_path`.
463         let path = match tokens.next().map(TokenTree::uninterpolate) {
464             Some(TokenTree::Token(
465                 Token { kind: kind @ (token::Ident(..) | token::ModSep), span },
466                 _,
467             )) => 'arm: {
468                 let mut segments = if let token::Ident(name, _) = kind {
469                     if let Some(TokenTree::Token(Token { kind: token::ModSep, .. }, _)) =
470                         tokens.peek()
471                     {
472                         tokens.next();
473                         thin_vec![PathSegment::from_ident(Ident::new(name, span))]
474                     } else {
475                         break 'arm Path::from_ident(Ident::new(name, span));
476                     }
477                 } else {
478                     thin_vec![PathSegment::path_root(span)]
479                 };
480                 loop {
481                     if let Some(TokenTree::Token(Token { kind: token::Ident(name, _), span }, _)) =
482                         tokens.next().map(TokenTree::uninterpolate)
483                     {
484                         segments.push(PathSegment::from_ident(Ident::new(name, span)));
485                     } else {
486                         return None;
487                     }
488                     if let Some(TokenTree::Token(Token { kind: token::ModSep, .. }, _)) =
489                         tokens.peek()
490                     {
491                         tokens.next();
492                     } else {
493                         break;
494                     }
495                 }
496                 let span = span.with_hi(segments.last().unwrap().ident.span.hi());
497                 Path { span, segments, tokens: None }
498             }
499             Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &*nt {
500                 token::Nonterminal::NtMeta(item) => return item.meta(item.path.span),
501                 token::Nonterminal::NtPath(path) => (**path).clone(),
502                 _ => return None,
503             },
504             _ => return None,
505         };
506         let list_closing_paren_pos = tokens.peek().map(|tt| tt.span().hi());
507         let kind = MetaItemKind::from_tokens(tokens)?;
508         let hi = match &kind {
509             MetaItemKind::NameValue(lit) => lit.span.hi(),
510             MetaItemKind::List(..) => list_closing_paren_pos.unwrap_or(path.span.hi()),
511             _ => path.span.hi(),
512         };
513         let span = path.span.with_hi(hi);
514         Some(MetaItem { path, kind, span })
515     }
516 }
517
518 impl MetaItemKind {
519     pub fn value_str(&self) -> Option<Symbol> {
520         match self {
521             MetaItemKind::NameValue(v) => match v.kind {
522                 LitKind::Str(s, _) => Some(s),
523                 _ => None,
524             },
525             _ => None,
526         }
527     }
528
529     pub fn attr_args(&self, span: Span) -> AttrArgs {
530         match self {
531             MetaItemKind::Word => AttrArgs::Empty,
532             MetaItemKind::NameValue(lit) => {
533                 let expr = P(ast::Expr {
534                     id: ast::DUMMY_NODE_ID,
535                     kind: ast::ExprKind::Lit(lit.token_lit.clone()),
536                     span: lit.span,
537                     attrs: ast::AttrVec::new(),
538                     tokens: None,
539                 });
540                 AttrArgs::Eq(span, AttrArgsEq::Ast(expr))
541             }
542             MetaItemKind::List(list) => {
543                 let mut tts = Vec::new();
544                 for (i, item) in list.iter().enumerate() {
545                     if i > 0 {
546                         tts.push(TokenTree::token_alone(token::Comma, span));
547                     }
548                     tts.extend(item.token_trees())
549                 }
550                 AttrArgs::Delimited(DelimArgs {
551                     dspan: DelimSpan::from_single(span),
552                     delim: MacDelimiter::Parenthesis,
553                     tokens: TokenStream::new(tts),
554                 })
555             }
556         }
557     }
558
559     fn token_trees(&self, span: Span) -> Vec<TokenTree> {
560         match self {
561             MetaItemKind::Word => vec![],
562             MetaItemKind::NameValue(lit) => {
563                 vec![
564                     TokenTree::token_alone(token::Eq, span),
565                     TokenTree::Token(lit.to_token(), Spacing::Alone),
566                 ]
567             }
568             MetaItemKind::List(list) => {
569                 let mut tokens = Vec::new();
570                 for (i, item) in list.iter().enumerate() {
571                     if i > 0 {
572                         tokens.push(TokenTree::token_alone(token::Comma, span));
573                     }
574                     tokens.extend(item.token_trees())
575                 }
576                 vec![TokenTree::Delimited(
577                     DelimSpan::from_single(span),
578                     Delimiter::Parenthesis,
579                     TokenStream::new(tokens),
580                 )]
581             }
582         }
583     }
584
585     fn list_from_tokens(tokens: TokenStream) -> Option<MetaItemKind> {
586         let mut tokens = tokens.into_trees().peekable();
587         let mut result = Vec::new();
588         while tokens.peek().is_some() {
589             let item = NestedMetaItem::from_tokens(&mut tokens)?;
590             result.push(item);
591             match tokens.next() {
592                 None | Some(TokenTree::Token(Token { kind: token::Comma, .. }, _)) => {}
593                 _ => return None,
594             }
595         }
596         Some(MetaItemKind::List(result))
597     }
598
599     fn name_value_from_tokens(
600         tokens: &mut impl Iterator<Item = TokenTree>,
601     ) -> Option<MetaItemKind> {
602         match tokens.next() {
603             Some(TokenTree::Delimited(_, Delimiter::Invisible, inner_tokens)) => {
604                 MetaItemKind::name_value_from_tokens(&mut inner_tokens.into_trees())
605             }
606             Some(TokenTree::Token(token, _)) => {
607                 Lit::from_token(&token).map(MetaItemKind::NameValue)
608             }
609             _ => None,
610         }
611     }
612
613     fn from_attr_args(args: &AttrArgs) -> Option<MetaItemKind> {
614         match args {
615             AttrArgs::Empty => Some(MetaItemKind::Word),
616             AttrArgs::Delimited(DelimArgs {
617                 dspan: _,
618                 delim: MacDelimiter::Parenthesis,
619                 tokens,
620             }) => MetaItemKind::list_from_tokens(tokens.clone()),
621             AttrArgs::Delimited(..) => None,
622             AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => match expr.kind {
623                 ast::ExprKind::Lit(token_lit) => {
624                     // Turn failures to `None`, we'll get parse errors elsewhere.
625                     Lit::from_token_lit(token_lit, expr.span)
626                         .ok()
627                         .map(|lit| MetaItemKind::NameValue(lit))
628                 }
629                 _ => None,
630             },
631             AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => Some(MetaItemKind::NameValue(lit.clone())),
632         }
633     }
634
635     fn from_tokens(
636         tokens: &mut iter::Peekable<impl Iterator<Item = TokenTree>>,
637     ) -> Option<MetaItemKind> {
638         match tokens.peek() {
639             Some(TokenTree::Delimited(_, Delimiter::Parenthesis, inner_tokens)) => {
640                 let inner_tokens = inner_tokens.clone();
641                 tokens.next();
642                 MetaItemKind::list_from_tokens(inner_tokens)
643             }
644             Some(TokenTree::Delimited(..)) => None,
645             Some(TokenTree::Token(Token { kind: token::Eq, .. }, _)) => {
646                 tokens.next();
647                 MetaItemKind::name_value_from_tokens(tokens)
648             }
649             _ => Some(MetaItemKind::Word),
650         }
651     }
652 }
653
654 impl NestedMetaItem {
655     pub fn span(&self) -> Span {
656         match self {
657             NestedMetaItem::MetaItem(item) => item.span,
658             NestedMetaItem::Literal(lit) => lit.span,
659         }
660     }
661
662     fn token_trees(&self) -> Vec<TokenTree> {
663         match self {
664             NestedMetaItem::MetaItem(item) => item.token_trees(),
665             NestedMetaItem::Literal(lit) => {
666                 vec![TokenTree::Token(lit.to_token(), Spacing::Alone)]
667             }
668         }
669     }
670
671     fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<NestedMetaItem>
672     where
673         I: Iterator<Item = TokenTree>,
674     {
675         match tokens.peek() {
676             Some(TokenTree::Token(token, _))
677                 if let Some(lit) = Lit::from_token(token) =>
678             {
679                 tokens.next();
680                 return Some(NestedMetaItem::Literal(lit));
681             }
682             Some(TokenTree::Delimited(_, Delimiter::Invisible, inner_tokens)) => {
683                 let inner_tokens = inner_tokens.clone();
684                 tokens.next();
685                 return NestedMetaItem::from_tokens(&mut inner_tokens.into_trees().peekable());
686             }
687             _ => {}
688         }
689         MetaItem::from_tokens(tokens).map(NestedMetaItem::MetaItem)
690     }
691 }