2019-09-06 03:56:45 +01:00
|
|
|
//! Functions dealing with attributes and meta items.
|
2011-06-28 15:25:20 -07:00
|
|
|
|
2019-02-07 02:33:01 +09:00
|
|
|
use crate::ast;
|
2022-11-18 11:24:21 +11:00
|
|
|
use crate::ast::{AttrArgs, AttrArgsEq, AttrId, AttrItem, AttrKind, AttrStyle, Attribute};
|
|
|
|
use crate::ast::{DelimArgs, Lit, LitKind};
|
|
|
|
use crate::ast::{MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem};
|
2020-04-19 13:00:18 +02:00
|
|
|
use crate::ast::{Path, PathSegment};
|
Overhaul `MacArgs::Eq`.
The value in `MacArgs::Eq` is currently represented as a `Token`.
Because of `TokenKind::Interpolated`, `Token` can be either a token or
an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a
literal or macro call AST fragment, and then is later lowered to a
literal token. But this is very non-obvious. `Token` is a much more
general type than what is needed.
This commit restricts things, by introducing a new type `MacArgsEqKind`
that is either an AST expression (pre-lowering) or an AST literal
(post-lowering). The downside is that the code is a bit more verbose in
a few places. The benefit is that makes it much clearer what the
possibilities are (though also shorter in some other places). Also, it
removes one use of `TokenKind::Interpolated`, taking us a step closer to
removing that variant, which will let us make `Token` impl `Copy` and
remove many "handle Interpolated" code paths in the parser.
Things to note:
- Error messages have improved. Messages like this:
```
unexpected token: `"bug" + "found"`
```
now say "unexpected expression", which makes more sense. Although
arbitrary expressions can exist within tokens thanks to
`TokenKind::Interpolated`, that's not obvious to anyone who doesn't
know compiler internals.
- In `parse_mac_args_common`, we no longer need to collect tokens for
the value expression.
2022-04-29 06:52:01 +10:00
|
|
|
use crate::ptr::P;
|
2022-04-26 15:40:14 +03:00
|
|
|
use crate::token::{self, CommentKind, Delimiter, Token};
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
use crate::tokenstream::{DelimSpan, Spacing, TokenTree};
|
2022-09-09 17:15:53 +10:00
|
|
|
use crate::tokenstream::{LazyAttrTokenStream, TokenStream};
|
2022-04-16 23:49:37 +03:00
|
|
|
use crate::util::comments;
|
2022-09-02 16:29:40 +08:00
|
|
|
use rustc_data_structures::sync::WorkerLocal;
|
2020-01-11 09:59:14 +01:00
|
|
|
use rustc_index::bit_set::GrowableBitSet;
|
2021-02-23 10:21:20 -05:00
|
|
|
use rustc_span::source_map::BytePos;
|
2020-04-19 13:00:18 +02:00
|
|
|
use rustc_span::symbol::{sym, Ident, Symbol};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::Span;
|
2022-09-02 16:29:40 +08:00
|
|
|
use std::cell::Cell;
|
2017-03-03 09:23:59 +00:00
|
|
|
use std::iter;
|
2022-09-13 15:35:44 +08:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
use std::ops::BitXor;
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
use std::sync::atomic::{AtomicU32, Ordering};
|
2022-09-08 17:22:52 +10:00
|
|
|
use thin_vec::thin_vec;
|
2012-03-02 13:14:10 -08:00
|
|
|
|
2020-07-30 11:27:50 +10:00
|
|
|
pub struct MarkedAttrs(GrowableBitSet<AttrId>);
|
2020-01-11 09:59:14 +01:00
|
|
|
|
2020-07-30 11:27:50 +10:00
|
|
|
impl MarkedAttrs {
|
|
|
|
pub fn new() -> Self {
|
2022-11-27 11:15:06 +00:00
|
|
|
// We have no idea how many attributes there will be, so just
|
|
|
|
// initiate the vectors with 0 bits. We'll grow them as necessary.
|
2020-07-30 11:27:50 +10:00
|
|
|
MarkedAttrs(GrowableBitSet::new_empty())
|
2020-01-11 09:59:14 +01:00
|
|
|
}
|
|
|
|
|
2020-07-30 11:27:50 +10:00
|
|
|
pub fn mark(&mut self, attr: &Attribute) {
|
|
|
|
self.0.insert(attr.id);
|
|
|
|
}
|
2016-11-08 08:30:26 +10:30
|
|
|
|
2020-07-30 11:27:50 +10:00
|
|
|
pub fn is_marked(&self, attr: &Attribute) -> bool {
|
|
|
|
self.0.contains(attr.id)
|
|
|
|
}
|
2016-11-08 08:30:26 +10:30
|
|
|
}
|
|
|
|
|
2016-08-23 03:21:17 +00:00
|
|
|
impl NestedMetaItem {
|
2019-09-06 03:56:45 +01:00
|
|
|
/// Returns the `MetaItem` if `self` is a `NestedMetaItem::MetaItem`.
|
2016-11-15 10:17:24 +00:00
|
|
|
pub fn meta_item(&self) -> Option<&MetaItem> {
|
2022-11-16 19:26:38 +00:00
|
|
|
match self {
|
|
|
|
NestedMetaItem::MetaItem(item) => Some(item),
|
2016-08-23 03:21:17 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:56:45 +01:00
|
|
|
/// Returns the `Lit` if `self` is a `NestedMetaItem::Literal`s.
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn literal(&self) -> Option<&Lit> {
|
2022-11-16 19:26:38 +00:00
|
|
|
match self {
|
|
|
|
NestedMetaItem::Literal(lit) => Some(lit),
|
2016-08-23 03:21:17 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `true` if this list item is a MetaItem with a name of `name`.
|
2020-08-02 13:17:20 +03:00
|
|
|
pub fn has_name(&self, name: Symbol) -> bool {
|
|
|
|
self.meta_item().map_or(false, |meta_item| meta_item.has_name(name))
|
2016-08-19 18:58:14 -07:00
|
|
|
}
|
|
|
|
|
2019-09-06 03:56:45 +01:00
|
|
|
/// For a single-segment meta item, returns its name; otherwise, returns `None`.
|
2019-02-28 09:17:24 +03:00
|
|
|
pub fn ident(&self) -> Option<Ident> {
|
|
|
|
self.meta_item().and_then(|meta_item| meta_item.ident())
|
|
|
|
}
|
2019-05-08 14:33:06 +10:00
|
|
|
pub fn name_or_empty(&self) -> Symbol {
|
2021-10-17 23:20:30 +03:00
|
|
|
self.ident().unwrap_or_else(Ident::empty).name
|
2016-08-19 18:58:14 -07:00
|
|
|
}
|
|
|
|
|
2019-09-06 03:56:45 +01:00
|
|
|
/// Gets the string value if `self` is a `MetaItem` and the `MetaItem` is a
|
|
|
|
/// `MetaItemKind::NameValue` variant containing a string, otherwise `None`.
|
2016-11-16 10:52:37 +00:00
|
|
|
pub fn value_str(&self) -> Option<Symbol> {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.meta_item().and_then(|meta_item| meta_item.value_str())
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:56:45 +01:00
|
|
|
/// Returns a name and single literal value tuple of the `MetaItem`.
|
2020-04-19 13:00:18 +02:00
|
|
|
pub fn name_value_literal(&self) -> Option<(Symbol, &Lit)> {
|
2017-01-15 09:49:29 +11:00
|
|
|
self.meta_item().and_then(|meta_item| {
|
|
|
|
meta_item.meta_item_list().and_then(|meta_item_list| {
|
2022-02-26 13:45:36 -03:00
|
|
|
if meta_item_list.len() == 1
|
|
|
|
&& let Some(ident) = meta_item.ident()
|
|
|
|
&& let Some(lit) = meta_item_list[0].literal()
|
|
|
|
{
|
|
|
|
return Some((ident.name, lit));
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-02-28 09:17:24 +03:00
|
|
|
None
|
|
|
|
})
|
2019-12-22 17:42:04 -05:00
|
|
|
})
|
2016-08-19 18:58:14 -07:00
|
|
|
}
|
|
|
|
|
2019-09-06 03:56:45 +01:00
|
|
|
/// Gets a list of inner meta items from a list `MetaItem` type.
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.meta_item().and_then(|meta_item| meta_item.meta_item_list())
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:56:45 +01:00
|
|
|
/// Returns `true` if the variant is `MetaItem`.
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn is_meta_item(&self) -> bool {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.meta_item().is_some()
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:56:45 +01:00
|
|
|
/// Returns `true` if `self` is a `MetaItem` and the meta item is a word.
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn is_word(&self) -> bool {
|
2019-02-28 09:17:24 +03:00
|
|
|
self.meta_item().map_or(false, |meta_item| meta_item.is_word())
|
2016-08-19 18:58:14 -07:00
|
|
|
}
|
|
|
|
|
2021-03-16 01:50:34 -04:00
|
|
|
/// See [`MetaItem::name_value_literal_span`].
|
2020-11-28 16:11:25 +01:00
|
|
|
pub fn name_value_literal_span(&self) -> Option<Span> {
|
|
|
|
self.meta_item()?.name_value_literal_span()
|
|
|
|
}
|
2016-08-19 18:58:14 -07:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
impl Attribute {
|
2021-03-11 00:00:00 +00:00
|
|
|
#[inline]
|
2019-10-24 06:33:12 +11:00
|
|
|
pub fn has_name(&self, name: Symbol) -> bool {
|
2022-11-16 19:26:38 +00:00
|
|
|
match &self.kind {
|
|
|
|
AttrKind::Normal(normal) => normal.item.path == name,
|
2020-07-21 22:16:19 +03:00
|
|
|
AttrKind::DocComment(..) => false,
|
2019-10-24 06:33:12 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:56:45 +01:00
|
|
|
/// For a single-segment attribute, returns its name; otherwise, returns `None`.
|
2019-02-28 09:17:24 +03:00
|
|
|
pub fn ident(&self) -> Option<Ident> {
|
2022-11-16 19:26:38 +00:00
|
|
|
match &self.kind {
|
|
|
|
AttrKind::Normal(normal) => {
|
|
|
|
if let [ident] = &*normal.item.path.segments {
|
|
|
|
Some(ident.ident)
|
2019-10-24 06:33:12 +11:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2020-07-21 22:16:19 +03:00
|
|
|
AttrKind::DocComment(..) => None,
|
2019-02-28 09:17:24 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-08 14:33:06 +10:00
|
|
|
pub fn name_or_empty(&self) -> Symbol {
|
2021-10-17 23:20:30 +03:00
|
|
|
self.ident().unwrap_or_else(Ident::empty).name
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
2016-08-19 18:58:14 -07:00
|
|
|
|
2016-11-16 10:52:37 +00:00
|
|
|
pub fn value_str(&self) -> Option<Symbol> {
|
2022-11-16 19:26:38 +00:00
|
|
|
match &self.kind {
|
|
|
|
AttrKind::Normal(normal) => normal.item.meta_kind().and_then(|kind| kind.value_str()),
|
2019-12-07 21:28:29 +03:00
|
|
|
AttrKind::DocComment(..) => None,
|
2019-10-24 06:33:12 +11:00
|
|
|
}
|
2014-01-10 14:02:36 -08:00
|
|
|
}
|
2016-08-19 18:58:14 -07:00
|
|
|
|
2017-03-03 09:23:59 +00:00
|
|
|
pub fn meta_item_list(&self) -> Option<Vec<NestedMetaItem>> {
|
2022-11-16 19:26:38 +00:00
|
|
|
match &self.kind {
|
|
|
|
AttrKind::Normal(normal) => match normal.item.meta_kind() {
|
2021-12-26 16:47:08 +01:00
|
|
|
Some(MetaItemKind::List(list)) => Some(list),
|
2019-10-24 06:33:12 +11:00
|
|
|
_ => None,
|
|
|
|
},
|
2020-07-21 22:16:19 +03:00
|
|
|
AttrKind::DocComment(..) => None,
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
2013-07-19 21:51:37 +10:00
|
|
|
}
|
2016-07-15 13:13:17 -07:00
|
|
|
|
2017-03-03 09:23:59 +00:00
|
|
|
pub fn is_word(&self) -> bool {
|
2022-08-11 21:06:11 +10:00
|
|
|
if let AttrKind::Normal(normal) = &self.kind {
|
2022-11-18 11:24:21 +11:00
|
|
|
matches!(normal.item.args, AttrArgs::Empty)
|
2019-10-24 06:33:12 +11:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
2011-06-28 11:24:24 -07:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
impl MetaItem {
|
2019-09-06 03:56:45 +01:00
|
|
|
/// For a single-segment meta item, returns its name; otherwise, returns `None`.
|
2019-02-28 09:17:24 +03:00
|
|
|
pub fn ident(&self) -> Option<Ident> {
|
2019-03-02 19:15:26 +03:00
|
|
|
if self.path.segments.len() == 1 { Some(self.path.segments[0].ident) } else { None }
|
2019-02-28 09:17:24 +03:00
|
|
|
}
|
2019-05-08 14:33:06 +10:00
|
|
|
pub fn name_or_empty(&self) -> Symbol {
|
2021-10-17 23:20:30 +03:00
|
|
|
self.ident().unwrap_or_else(Ident::empty).name
|
2018-01-30 14:30:39 +09:00
|
|
|
}
|
|
|
|
|
2022-11-27 11:15:06 +00:00
|
|
|
/// ```text
|
|
|
|
/// Example:
|
|
|
|
/// #[attribute(name = "value")]
|
|
|
|
/// ^^^^^^^^^^^^^^
|
|
|
|
/// ```
|
2018-10-09 22:54:47 +08:00
|
|
|
pub fn name_value_literal(&self) -> Option<&Lit> {
|
2019-09-26 18:04:05 +01:00
|
|
|
match &self.kind {
|
2018-10-09 22:54:47 +08:00
|
|
|
MetaItemKind::NameValue(v) => Some(v),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 10:52:37 +00:00
|
|
|
pub fn value_str(&self) -> Option<Symbol> {
|
2022-08-01 16:32:42 +10:00
|
|
|
self.kind.value_str()
|
2013-07-19 21:51:37 +10:00
|
|
|
}
|
2012-04-15 01:07:47 -07:00
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
|
2022-11-16 19:26:38 +00:00
|
|
|
match &self.kind {
|
|
|
|
MetaItemKind::List(l) => Some(&**l),
|
2013-07-19 21:51:37 +10:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 13:13:17 -07:00
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn is_word(&self) -> bool {
|
2020-12-24 02:55:21 +01:00
|
|
|
matches!(self.kind, MetaItemKind::Word)
|
2016-07-15 13:13:17 -07:00
|
|
|
}
|
|
|
|
|
2020-08-02 13:17:20 +03:00
|
|
|
pub fn has_name(&self, name: Symbol) -> bool {
|
2019-04-03 02:43:49 +02:00
|
|
|
self.path == name
|
|
|
|
}
|
|
|
|
|
2020-12-01 17:32:14 +01:00
|
|
|
/// This is used in case you want the value span instead of the whole attribute. Example:
|
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// #[doc(alias = "foo")]
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// In here, it'll return a span for `"foo"`.
|
2020-11-28 16:11:25 +01:00
|
|
|
pub fn name_value_literal_span(&self) -> Option<Span> {
|
|
|
|
Some(self.name_value_literal()?.span)
|
|
|
|
}
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
2012-04-15 01:07:47 -07:00
|
|
|
|
2019-08-18 01:10:56 +03:00
|
|
|
impl AttrItem {
|
2020-03-05 00:34:57 +03:00
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
self.args.span().map_or(self.path.span, |args_span| self.path.span.to(args_span))
|
|
|
|
}
|
|
|
|
|
2019-10-15 22:48:13 +02:00
|
|
|
pub fn meta(&self, span: Span) -> Option<MetaItem> {
|
2017-03-03 09:23:59 +00:00
|
|
|
Some(MetaItem {
|
2019-03-02 19:15:26 +03:00
|
|
|
path: self.path.clone(),
|
2022-11-18 11:24:21 +11:00
|
|
|
kind: MetaItemKind::from_attr_args(&self.args)?,
|
2019-08-18 01:10:56 +03:00
|
|
|
span,
|
2017-03-03 09:23:59 +00:00
|
|
|
})
|
|
|
|
}
|
2021-12-26 16:47:08 +01:00
|
|
|
|
|
|
|
pub fn meta_kind(&self) -> Option<MetaItemKind> {
|
2022-11-18 11:24:21 +11:00
|
|
|
MetaItemKind::from_attr_args(&self.args)
|
2021-12-26 16:47:08 +01:00
|
|
|
}
|
2019-08-18 01:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Attribute {
|
2022-09-07 15:34:16 +02:00
|
|
|
/// Returns `true` if it is a sugared doc comment (`///` or `//!` for example).
|
2022-09-12 21:18:59 +02:00
|
|
|
/// So `#[doc = "doc"]` (which is a doc comment) and `#[doc(...)]` (which is not
|
|
|
|
/// a doc comment) will return `false`.
|
2019-10-24 06:33:12 +11:00
|
|
|
pub fn is_doc_comment(&self) -> bool {
|
|
|
|
match self.kind {
|
2020-11-05 20:27:48 +03:00
|
|
|
AttrKind::Normal(..) => false,
|
2020-07-21 22:16:19 +03:00
|
|
|
AttrKind::DocComment(..) => true,
|
2019-10-24 06:33:12 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-12 21:18:59 +02:00
|
|
|
/// Returns the documentation and its kind if this is a doc comment or a sugared doc comment.
|
|
|
|
/// * `///doc` returns `Some(("doc", CommentKind::Line))`.
|
|
|
|
/// * `/** doc */` returns `Some(("doc", CommentKind::Block))`.
|
|
|
|
/// * `#[doc = "doc"]` returns `Some(("doc", CommentKind::Line))`.
|
|
|
|
/// * `#[doc(...)]` returns `None`.
|
2022-01-18 16:56:16 +01:00
|
|
|
pub fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {
|
|
|
|
match self.kind {
|
|
|
|
AttrKind::DocComment(kind, data) => Some((data, kind)),
|
2022-08-11 21:06:11 +10:00
|
|
|
AttrKind::Normal(ref normal) if normal.item.path == sym::doc => normal
|
|
|
|
.item
|
2022-01-18 16:56:16 +01:00
|
|
|
.meta_kind()
|
|
|
|
.and_then(|kind| kind.value_str())
|
|
|
|
.map(|data| (data, CommentKind::Line)),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-12 21:18:59 +02:00
|
|
|
/// Returns the documentation if this is a doc comment or a sugared doc comment.
|
|
|
|
/// * `///doc` returns `Some("doc")`.
|
|
|
|
/// * `#[doc = "doc"]` returns `Some("doc")`.
|
|
|
|
/// * `#[doc(...)]` returns `None`.
|
2019-12-07 21:28:29 +03:00
|
|
|
pub fn doc_str(&self) -> Option<Symbol> {
|
2022-11-16 19:26:38 +00:00
|
|
|
match &self.kind {
|
|
|
|
AttrKind::DocComment(.., data) => Some(*data),
|
|
|
|
AttrKind::Normal(normal) if normal.item.path == sym::doc => {
|
2022-08-11 21:06:11 +10:00
|
|
|
normal.item.meta_kind().and_then(|kind| kind.value_str())
|
2019-12-07 21:28:29 +03:00
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-16 23:49:37 +03:00
|
|
|
pub fn may_have_doc_links(&self) -> bool {
|
|
|
|
self.doc_str().map_or(false, |s| comments::may_have_doc_links(s.as_str()))
|
|
|
|
}
|
|
|
|
|
2019-10-24 06:33:12 +11:00
|
|
|
pub fn get_normal_item(&self) -> &AttrItem {
|
2022-11-16 19:26:38 +00:00
|
|
|
match &self.kind {
|
|
|
|
AttrKind::Normal(normal) => &normal.item,
|
2020-07-21 22:16:19 +03:00
|
|
|
AttrKind::DocComment(..) => panic!("unexpected doc comment"),
|
2019-10-24 06:33:12 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unwrap_normal_item(self) -> AttrItem {
|
|
|
|
match self.kind {
|
2022-08-11 21:06:11 +10:00
|
|
|
AttrKind::Normal(normal) => normal.into_inner().item,
|
2020-07-21 22:16:19 +03:00
|
|
|
AttrKind::DocComment(..) => panic!("unexpected doc comment"),
|
2019-10-24 06:33:12 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 01:10:56 +03:00
|
|
|
/// Extracts the MetaItem from inside this Attribute.
|
|
|
|
pub fn meta(&self) -> Option<MetaItem> {
|
2022-11-16 19:26:38 +00:00
|
|
|
match &self.kind {
|
|
|
|
AttrKind::Normal(normal) => normal.item.meta(self.span),
|
2019-12-07 21:28:29 +03:00
|
|
|
AttrKind::DocComment(..) => None,
|
2019-10-24 06:33:12 +11:00
|
|
|
}
|
2019-08-18 01:10:56 +03:00
|
|
|
}
|
2020-11-05 20:27:48 +03:00
|
|
|
|
2021-12-26 16:47:08 +01:00
|
|
|
pub fn meta_kind(&self) -> Option<MetaItemKind> {
|
2022-11-16 19:26:38 +00:00
|
|
|
match &self.kind {
|
|
|
|
AttrKind::Normal(normal) => normal.item.meta_kind(),
|
2021-12-26 16:47:08 +01:00
|
|
|
AttrKind::DocComment(..) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 16:23:39 +10:00
|
|
|
pub fn tokens(&self) -> TokenStream {
|
2022-11-16 19:26:38 +00:00
|
|
|
match &self.kind {
|
|
|
|
AttrKind::Normal(normal) => normal
|
2022-08-11 21:06:11 +10:00
|
|
|
.tokens
|
2020-11-05 20:27:48 +03:00
|
|
|
.as_ref()
|
|
|
|
.unwrap_or_else(|| panic!("attribute is missing tokens: {:?}", self))
|
2022-09-09 17:15:53 +10:00
|
|
|
.to_attr_token_stream()
|
2022-09-09 16:23:39 +10:00
|
|
|
.to_tokenstream(),
|
2022-11-16 19:26:38 +00:00
|
|
|
&AttrKind::DocComment(comment_kind, data) => TokenStream::new(vec![TokenTree::Token(
|
2022-09-09 16:23:39 +10:00
|
|
|
Token::new(token::DocComment(comment_kind, self.style, data), self.span),
|
|
|
|
Spacing::Alone,
|
|
|
|
)]),
|
2020-11-05 20:27:48 +03:00
|
|
|
}
|
|
|
|
}
|
2011-07-05 17:01:23 -07:00
|
|
|
}
|
|
|
|
|
2013-07-19 21:51:37 +10:00
|
|
|
/* Constructors */
|
2011-10-29 17:35:45 -07:00
|
|
|
|
2019-08-15 01:56:44 +03:00
|
|
|
pub fn mk_name_value_item_str(ident: Ident, str: Symbol, str_span: Span) -> MetaItem {
|
|
|
|
let lit_kind = LitKind::Str(str, ast::StrStyle::Cooked);
|
|
|
|
mk_name_value_item(ident, lit_kind, str_span)
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
2011-06-28 12:53:59 -07:00
|
|
|
|
2019-08-04 18:03:34 -04:00
|
|
|
pub fn mk_name_value_item(ident: Ident, lit_kind: LitKind, lit_span: Span) -> MetaItem {
|
2019-05-10 03:00:51 +03:00
|
|
|
let lit = Lit::from_lit_kind(lit_kind, lit_span);
|
2019-08-04 18:03:34 -04:00
|
|
|
let span = ident.span.to(lit_span);
|
2019-09-26 18:04:05 +01:00
|
|
|
MetaItem { path: Path::from_ident(ident), span, kind: MetaItemKind::NameValue(lit) }
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
|
|
|
|
2019-08-04 17:59:06 -04:00
|
|
|
pub fn mk_list_item(ident: Ident, items: Vec<NestedMetaItem>) -> MetaItem {
|
2019-09-26 18:04:05 +01:00
|
|
|
MetaItem { path: Path::from_ident(ident), span: ident.span, kind: MetaItemKind::List(items) }
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
|
|
|
|
2018-03-24 21:17:27 +03:00
|
|
|
pub fn mk_word_item(ident: Ident) -> MetaItem {
|
2019-09-26 18:04:05 +01:00
|
|
|
MetaItem { path: Path::from_ident(ident), span: ident.span, kind: MetaItemKind::Word }
|
2016-08-19 18:58:14 -07:00
|
|
|
}
|
2018-01-30 14:30:39 +09:00
|
|
|
|
2018-03-24 21:17:27 +03:00
|
|
|
pub fn mk_nested_word_item(ident: Ident) -> NestedMetaItem {
|
2019-03-03 20:56:24 +03:00
|
|
|
NestedMetaItem::MetaItem(mk_word_item(ident))
|
2016-07-15 13:13:17 -07:00
|
|
|
}
|
|
|
|
|
2022-09-02 16:29:40 +08:00
|
|
|
pub struct AttrIdGenerator(WorkerLocal<Cell<u32>>);
|
2016-07-15 13:13:17 -07:00
|
|
|
|
2022-09-13 15:35:44 +08:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
static MAX_ATTR_ID: AtomicU32 = AtomicU32::new(u32::MAX);
|
|
|
|
|
2022-09-02 16:29:40 +08:00
|
|
|
impl AttrIdGenerator {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
// We use `(index as u32).reverse_bits()` to initialize the
|
|
|
|
// starting value of AttrId in each worker thread.
|
|
|
|
// The `index` is the index of the worker thread.
|
|
|
|
// This ensures that the AttrId generated in each thread is unique.
|
2022-09-13 15:35:44 +08:00
|
|
|
AttrIdGenerator(WorkerLocal::new(|index| {
|
|
|
|
let index: u32 = index.try_into().unwrap();
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
let max_id = ((index + 1).next_power_of_two() - 1).bitxor(u32::MAX).reverse_bits();
|
|
|
|
MAX_ATTR_ID.fetch_min(max_id, Ordering::Release);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cell::new(index.reverse_bits())
|
|
|
|
}))
|
2022-09-02 16:29:40 +08:00
|
|
|
}
|
2016-07-15 13:13:17 -07:00
|
|
|
|
2022-09-02 16:29:40 +08:00
|
|
|
pub fn mk_attr_id(&self) -> AttrId {
|
|
|
|
let id = self.0.get();
|
2022-09-13 15:35:44 +08:00
|
|
|
|
|
|
|
// Ensure the assigned attr_id does not overlap the bits
|
|
|
|
// representing the number of threads.
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
assert!(id <= MAX_ATTR_ID.load(Ordering::Acquire));
|
|
|
|
|
2022-09-02 16:29:40 +08:00
|
|
|
self.0.set(id + 1);
|
|
|
|
AttrId::from_u32(id)
|
|
|
|
}
|
2014-05-20 00:07:24 -07:00
|
|
|
}
|
|
|
|
|
2022-09-02 16:29:40 +08:00
|
|
|
pub fn mk_attr(
|
|
|
|
g: &AttrIdGenerator,
|
|
|
|
style: AttrStyle,
|
|
|
|
path: Path,
|
2022-11-18 11:24:21 +11:00
|
|
|
args: AttrArgs,
|
2022-09-02 16:29:40 +08:00
|
|
|
span: Span,
|
|
|
|
) -> Attribute {
|
|
|
|
mk_attr_from_item(g, AttrItem { path, args, tokens: None }, None, style, span)
|
2019-10-10 10:26:10 +02:00
|
|
|
}
|
|
|
|
|
2020-11-05 20:27:48 +03:00
|
|
|
pub fn mk_attr_from_item(
|
2022-09-02 16:29:40 +08:00
|
|
|
g: &AttrIdGenerator,
|
2020-11-05 20:27:48 +03:00
|
|
|
item: AttrItem,
|
2022-09-09 17:15:53 +10:00
|
|
|
tokens: Option<LazyAttrTokenStream>,
|
2020-11-05 20:27:48 +03:00
|
|
|
style: AttrStyle,
|
|
|
|
span: Span,
|
|
|
|
) -> Attribute {
|
2022-08-11 21:06:11 +10:00
|
|
|
Attribute {
|
|
|
|
kind: AttrKind::Normal(P(ast::NormalAttr { item, tokens })),
|
2022-09-02 16:29:40 +08:00
|
|
|
id: g.mk_attr_id(),
|
2022-08-11 21:06:11 +10:00
|
|
|
style,
|
|
|
|
span,
|
|
|
|
}
|
2016-07-15 13:13:17 -07:00
|
|
|
}
|
|
|
|
|
2019-09-06 23:41:54 +03:00
|
|
|
/// Returns an inner attribute with the given value and span.
|
2022-09-02 16:29:40 +08:00
|
|
|
pub fn mk_attr_inner(g: &AttrIdGenerator, item: MetaItem) -> Attribute {
|
2022-11-18 11:24:21 +11:00
|
|
|
mk_attr(g, AttrStyle::Inner, item.path, item.kind.attr_args(item.span), item.span)
|
2019-09-06 23:41:54 +03:00
|
|
|
}
|
|
|
|
|
2016-07-15 13:13:17 -07:00
|
|
|
/// Returns an outer attribute with the given value and span.
|
2022-09-02 16:29:40 +08:00
|
|
|
pub fn mk_attr_outer(g: &AttrIdGenerator, item: MetaItem) -> Attribute {
|
2022-11-18 11:24:21 +11:00
|
|
|
mk_attr(g, AttrStyle::Outer, item.path, item.kind.attr_args(item.span), item.span)
|
2016-07-15 13:13:17 -07:00
|
|
|
}
|
|
|
|
|
2020-07-21 22:16:19 +03:00
|
|
|
pub fn mk_doc_comment(
|
2022-09-02 16:29:40 +08:00
|
|
|
g: &AttrIdGenerator,
|
2020-07-21 22:16:19 +03:00
|
|
|
comment_kind: CommentKind,
|
|
|
|
style: AttrStyle,
|
|
|
|
data: Symbol,
|
|
|
|
span: Span,
|
|
|
|
) -> Attribute {
|
2022-09-02 16:29:40 +08:00
|
|
|
Attribute { kind: AttrKind::DocComment(comment_kind, data), id: g.mk_attr_id(), style, span }
|
2011-07-13 18:13:19 -07:00
|
|
|
}
|
|
|
|
|
2019-05-08 13:21:18 +10:00
|
|
|
pub fn list_contains_name(items: &[NestedMetaItem], name: Symbol) -> bool {
|
2020-08-02 13:17:20 +03:00
|
|
|
items.iter().any(|item| item.has_name(name))
|
2016-08-19 18:58:14 -07:00
|
|
|
}
|
|
|
|
|
2017-03-03 09:23:59 +00:00
|
|
|
impl MetaItem {
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
fn token_trees(&self) -> Vec<TokenTree> {
|
2018-01-30 14:30:39 +09:00
|
|
|
let mut idents = vec![];
|
2020-11-04 13:48:50 +01:00
|
|
|
let mut last_pos = BytePos(0_u32);
|
2019-03-02 19:15:26 +03:00
|
|
|
for (i, segment) in self.path.segments.iter().enumerate() {
|
2018-01-30 14:30:39 +09:00
|
|
|
let is_first = i == 0;
|
|
|
|
if !is_first {
|
2018-04-17 15:33:39 +02:00
|
|
|
let mod_sep_span =
|
2021-04-18 14:27:04 +02:00
|
|
|
Span::new(last_pos, segment.ident.span.lo(), segment.ident.span.ctxt(), None);
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
idents.push(TokenTree::token_alone(token::ModSep, mod_sep_span));
|
2018-01-30 14:30:39 +09:00
|
|
|
}
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
idents.push(TokenTree::Token(Token::from_ast_ident(segment.ident), Spacing::Alone));
|
2018-04-17 15:33:39 +02:00
|
|
|
last_pos = segment.ident.span.hi();
|
2018-01-30 14:30:39 +09:00
|
|
|
}
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
idents.extend(self.kind.token_trees(self.span));
|
2019-10-14 14:06:00 +11:00
|
|
|
idents
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<MetaItem>
|
|
|
|
where
|
|
|
|
I: Iterator<Item = TokenTree>,
|
|
|
|
{
|
2018-04-24 16:57:41 +02:00
|
|
|
// FIXME: Share code with `parse_path`.
|
2020-03-07 15:58:27 +03:00
|
|
|
let path = match tokens.next().map(TokenTree::uninterpolate) {
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
Some(TokenTree::Token(
|
|
|
|
Token { kind: kind @ (token::Ident(..) | token::ModSep), span },
|
|
|
|
_,
|
|
|
|
)) => 'arm: {
|
2019-06-05 11:56:06 +03:00
|
|
|
let mut segments = if let token::Ident(name, _) = kind {
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
if let Some(TokenTree::Token(Token { kind: token::ModSep, .. }, _)) =
|
|
|
|
tokens.peek()
|
2019-06-05 14:17:56 +03:00
|
|
|
{
|
2019-01-02 02:21:05 +03:00
|
|
|
tokens.next();
|
2022-09-08 17:22:52 +10:00
|
|
|
thin_vec![PathSegment::from_ident(Ident::new(name, span))]
|
2019-01-02 02:21:05 +03:00
|
|
|
} else {
|
2019-06-05 11:56:06 +03:00
|
|
|
break 'arm Path::from_ident(Ident::new(name, span));
|
2018-01-30 14:30:39 +09:00
|
|
|
}
|
|
|
|
} else {
|
2022-09-08 17:22:52 +10:00
|
|
|
thin_vec![PathSegment::path_root(span)]
|
2019-01-02 02:21:05 +03:00
|
|
|
};
|
|
|
|
loop {
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
if let Some(TokenTree::Token(Token { kind: token::Ident(name, _), span }, _)) =
|
2020-03-07 15:58:27 +03:00
|
|
|
tokens.next().map(TokenTree::uninterpolate)
|
2019-06-05 14:17:56 +03:00
|
|
|
{
|
2019-06-05 11:56:06 +03:00
|
|
|
segments.push(PathSegment::from_ident(Ident::new(name, span)));
|
2019-01-02 02:21:05 +03:00
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
if let Some(TokenTree::Token(Token { kind: token::ModSep, .. }, _)) =
|
|
|
|
tokens.peek()
|
2019-06-05 14:17:56 +03:00
|
|
|
{
|
2019-01-02 02:21:05 +03:00
|
|
|
tokens.next();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2018-01-30 14:30:39 +09:00
|
|
|
}
|
2019-01-02 02:21:05 +03:00
|
|
|
let span = span.with_hi(segments.last().unwrap().ident.span.hi());
|
2020-08-21 18:51:23 -04:00
|
|
|
Path { span, segments, tokens: None }
|
2018-01-30 14:30:39 +09:00
|
|
|
}
|
2022-11-16 19:26:38 +00:00
|
|
|
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &*nt {
|
|
|
|
token::Nonterminal::NtMeta(item) => return item.meta(item.path.span),
|
|
|
|
token::Nonterminal::NtPath(path) => (**path).clone(),
|
2017-03-29 07:17:18 +00:00
|
|
|
_ => return None,
|
2017-03-08 23:13:35 +00:00
|
|
|
},
|
2017-03-03 09:23:59 +00:00
|
|
|
_ => return None,
|
|
|
|
};
|
2017-07-31 23:04:34 +03:00
|
|
|
let list_closing_paren_pos = tokens.peek().map(|tt| tt.span().hi());
|
2019-09-26 18:04:05 +01:00
|
|
|
let kind = MetaItemKind::from_tokens(tokens)?;
|
2022-11-16 19:26:38 +00:00
|
|
|
let hi = match &kind {
|
|
|
|
MetaItemKind::NameValue(lit) => lit.span.hi(),
|
2019-03-02 19:15:26 +03:00
|
|
|
MetaItemKind::List(..) => list_closing_paren_pos.unwrap_or(path.span.hi()),
|
|
|
|
_ => path.span.hi(),
|
2017-08-17 21:58:01 +09:00
|
|
|
};
|
2019-03-02 19:15:26 +03:00
|
|
|
let span = path.span.with_hi(hi);
|
2019-09-26 18:04:05 +01:00
|
|
|
Some(MetaItem { path, kind, span })
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MetaItemKind {
|
2021-12-26 16:47:08 +01:00
|
|
|
pub fn value_str(&self) -> Option<Symbol> {
|
|
|
|
match self {
|
2022-11-16 19:26:38 +00:00
|
|
|
MetaItemKind::NameValue(v) => match v.kind {
|
|
|
|
LitKind::Str(s, _) => Some(s),
|
2021-12-26 16:47:08 +01:00
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 11:24:21 +11:00
|
|
|
pub fn attr_args(&self, span: Span) -> AttrArgs {
|
2019-12-01 17:07:38 +03:00
|
|
|
match self {
|
2022-11-18 11:24:21 +11:00
|
|
|
MetaItemKind::Word => AttrArgs::Empty,
|
Overhaul `MacArgs::Eq`.
The value in `MacArgs::Eq` is currently represented as a `Token`.
Because of `TokenKind::Interpolated`, `Token` can be either a token or
an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a
literal or macro call AST fragment, and then is later lowered to a
literal token. But this is very non-obvious. `Token` is a much more
general type than what is needed.
This commit restricts things, by introducing a new type `MacArgsEqKind`
that is either an AST expression (pre-lowering) or an AST literal
(post-lowering). The downside is that the code is a bit more verbose in
a few places. The benefit is that makes it much clearer what the
possibilities are (though also shorter in some other places). Also, it
removes one use of `TokenKind::Interpolated`, taking us a step closer to
removing that variant, which will let us make `Token` impl `Copy` and
remove many "handle Interpolated" code paths in the parser.
Things to note:
- Error messages have improved. Messages like this:
```
unexpected token: `"bug" + "found"`
```
now say "unexpected expression", which makes more sense. Although
arbitrary expressions can exist within tokens thanks to
`TokenKind::Interpolated`, that's not obvious to anyone who doesn't
know compiler internals.
- In `parse_mac_args_common`, we no longer need to collect tokens for
the value expression.
2022-04-29 06:52:01 +10:00
|
|
|
MetaItemKind::NameValue(lit) => {
|
|
|
|
let expr = P(ast::Expr {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2022-10-10 13:40:56 +11:00
|
|
|
kind: ast::ExprKind::Lit(lit.token_lit.clone()),
|
Overhaul `MacArgs::Eq`.
The value in `MacArgs::Eq` is currently represented as a `Token`.
Because of `TokenKind::Interpolated`, `Token` can be either a token or
an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a
literal or macro call AST fragment, and then is later lowered to a
literal token. But this is very non-obvious. `Token` is a much more
general type than what is needed.
This commit restricts things, by introducing a new type `MacArgsEqKind`
that is either an AST expression (pre-lowering) or an AST literal
(post-lowering). The downside is that the code is a bit more verbose in
a few places. The benefit is that makes it much clearer what the
possibilities are (though also shorter in some other places). Also, it
removes one use of `TokenKind::Interpolated`, taking us a step closer to
removing that variant, which will let us make `Token` impl `Copy` and
remove many "handle Interpolated" code paths in the parser.
Things to note:
- Error messages have improved. Messages like this:
```
unexpected token: `"bug" + "found"`
```
now say "unexpected expression", which makes more sense. Although
arbitrary expressions can exist within tokens thanks to
`TokenKind::Interpolated`, that's not obvious to anyone who doesn't
know compiler internals.
- In `parse_mac_args_common`, we no longer need to collect tokens for
the value expression.
2022-04-29 06:52:01 +10:00
|
|
|
span: lit.span,
|
2022-08-17 12:34:33 +10:00
|
|
|
attrs: ast::AttrVec::new(),
|
Overhaul `MacArgs::Eq`.
The value in `MacArgs::Eq` is currently represented as a `Token`.
Because of `TokenKind::Interpolated`, `Token` can be either a token or
an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a
literal or macro call AST fragment, and then is later lowered to a
literal token. But this is very non-obvious. `Token` is a much more
general type than what is needed.
This commit restricts things, by introducing a new type `MacArgsEqKind`
that is either an AST expression (pre-lowering) or an AST literal
(post-lowering). The downside is that the code is a bit more verbose in
a few places. The benefit is that makes it much clearer what the
possibilities are (though also shorter in some other places). Also, it
removes one use of `TokenKind::Interpolated`, taking us a step closer to
removing that variant, which will let us make `Token` impl `Copy` and
remove many "handle Interpolated" code paths in the parser.
Things to note:
- Error messages have improved. Messages like this:
```
unexpected token: `"bug" + "found"`
```
now say "unexpected expression", which makes more sense. Although
arbitrary expressions can exist within tokens thanks to
`TokenKind::Interpolated`, that's not obvious to anyone who doesn't
know compiler internals.
- In `parse_mac_args_common`, we no longer need to collect tokens for
the value expression.
2022-04-29 06:52:01 +10:00
|
|
|
tokens: None,
|
|
|
|
});
|
2022-11-18 11:24:21 +11:00
|
|
|
AttrArgs::Eq(span, AttrArgsEq::Ast(expr))
|
Overhaul `MacArgs::Eq`.
The value in `MacArgs::Eq` is currently represented as a `Token`.
Because of `TokenKind::Interpolated`, `Token` can be either a token or
an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a
literal or macro call AST fragment, and then is later lowered to a
literal token. But this is very non-obvious. `Token` is a much more
general type than what is needed.
This commit restricts things, by introducing a new type `MacArgsEqKind`
that is either an AST expression (pre-lowering) or an AST literal
(post-lowering). The downside is that the code is a bit more verbose in
a few places. The benefit is that makes it much clearer what the
possibilities are (though also shorter in some other places). Also, it
removes one use of `TokenKind::Interpolated`, taking us a step closer to
removing that variant, which will let us make `Token` impl `Copy` and
remove many "handle Interpolated" code paths in the parser.
Things to note:
- Error messages have improved. Messages like this:
```
unexpected token: `"bug" + "found"`
```
now say "unexpected expression", which makes more sense. Although
arbitrary expressions can exist within tokens thanks to
`TokenKind::Interpolated`, that's not obvious to anyone who doesn't
know compiler internals.
- In `parse_mac_args_common`, we no longer need to collect tokens for
the value expression.
2022-04-29 06:52:01 +10:00
|
|
|
}
|
2019-12-01 17:07:38 +03:00
|
|
|
MetaItemKind::List(list) => {
|
|
|
|
let mut tts = Vec::new();
|
|
|
|
for (i, item) in list.iter().enumerate() {
|
|
|
|
if i > 0 {
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
tts.push(TokenTree::token_alone(token::Comma, span));
|
2019-12-01 17:07:38 +03:00
|
|
|
}
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
tts.extend(item.token_trees())
|
2019-12-01 17:07:38 +03:00
|
|
|
}
|
2022-11-18 11:24:21 +11:00
|
|
|
AttrArgs::Delimited(DelimArgs {
|
|
|
|
dspan: DelimSpan::from_single(span),
|
|
|
|
delim: MacDelimiter::Parenthesis,
|
|
|
|
tokens: TokenStream::new(tts),
|
|
|
|
})
|
2019-12-01 17:07:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
fn token_trees(&self, span: Span) -> Vec<TokenTree> {
|
2022-11-16 19:26:38 +00:00
|
|
|
match self {
|
2019-10-14 14:06:00 +11:00
|
|
|
MetaItemKind::Word => vec![],
|
2022-11-16 19:26:38 +00:00
|
|
|
MetaItemKind::NameValue(lit) => {
|
2020-12-19 23:38:22 +03:00
|
|
|
vec![
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
TokenTree::token_alone(token::Eq, span),
|
|
|
|
TokenTree::Token(lit.to_token(), Spacing::Alone),
|
2020-12-19 23:38:22 +03:00
|
|
|
]
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
2022-11-16 19:26:38 +00:00
|
|
|
MetaItemKind::List(list) => {
|
2017-03-03 09:23:59 +00:00
|
|
|
let mut tokens = Vec::new();
|
|
|
|
for (i, item) in list.iter().enumerate() {
|
|
|
|
if i > 0 {
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
tokens.push(TokenTree::token_alone(token::Comma, span));
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
tokens.extend(item.token_trees())
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
vec![TokenTree::Delimited(
|
|
|
|
DelimSpan::from_single(span),
|
|
|
|
Delimiter::Parenthesis,
|
|
|
|
TokenStream::new(tokens),
|
|
|
|
)]
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-01 19:16:44 +03:00
|
|
|
fn list_from_tokens(tokens: TokenStream) -> Option<MetaItemKind> {
|
|
|
|
let mut tokens = tokens.into_trees().peekable();
|
2017-03-03 09:23:59 +00:00
|
|
|
let mut result = Vec::new();
|
2021-01-17 19:06:12 -05:00
|
|
|
while tokens.peek().is_some() {
|
2019-03-03 20:56:24 +03:00
|
|
|
let item = NestedMetaItem::from_tokens(&mut tokens)?;
|
|
|
|
result.push(item);
|
2017-03-03 09:23:59 +00:00
|
|
|
match tokens.next() {
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
None | Some(TokenTree::Token(Token { kind: token::Comma, .. }, _)) => {}
|
2017-03-03 09:23:59 +00:00
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(MetaItemKind::List(result))
|
|
|
|
}
|
2019-12-01 19:16:44 +03:00
|
|
|
|
|
|
|
fn name_value_from_tokens(
|
|
|
|
tokens: &mut impl Iterator<Item = TokenTree>,
|
|
|
|
) -> Option<MetaItemKind> {
|
|
|
|
match tokens.next() {
|
2022-04-26 15:40:14 +03:00
|
|
|
Some(TokenTree::Delimited(_, Delimiter::Invisible, inner_tokens)) => {
|
2022-05-16 18:58:15 +03:00
|
|
|
MetaItemKind::name_value_from_tokens(&mut inner_tokens.into_trees())
|
2020-06-20 20:59:04 -04:00
|
|
|
}
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
Some(TokenTree::Token(token, _)) => {
|
2022-10-10 13:40:56 +11:00
|
|
|
Lit::from_token(&token).map(MetaItemKind::NameValue)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-12-01 19:16:44 +03:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 11:24:21 +11:00
|
|
|
fn from_attr_args(args: &AttrArgs) -> Option<MetaItemKind> {
|
2019-12-01 19:16:44 +03:00
|
|
|
match args {
|
2022-11-18 11:24:21 +11:00
|
|
|
AttrArgs::Empty => Some(MetaItemKind::Word),
|
|
|
|
AttrArgs::Delimited(DelimArgs {
|
|
|
|
dspan: _,
|
|
|
|
delim: MacDelimiter::Parenthesis,
|
|
|
|
tokens,
|
|
|
|
}) => MetaItemKind::list_from_tokens(tokens.clone()),
|
|
|
|
AttrArgs::Delimited(..) => None,
|
|
|
|
AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => match expr.kind {
|
2022-11-22 16:48:42 +11:00
|
|
|
ast::ExprKind::Lit(token_lit) => {
|
|
|
|
// Turn failures to `None`, we'll get parse errors elsewhere.
|
|
|
|
Lit::from_token_lit(token_lit, expr.span)
|
|
|
|
.ok()
|
|
|
|
.map(|lit| MetaItemKind::NameValue(lit))
|
|
|
|
}
|
Overhaul `MacArgs::Eq`.
The value in `MacArgs::Eq` is currently represented as a `Token`.
Because of `TokenKind::Interpolated`, `Token` can be either a token or
an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a
literal or macro call AST fragment, and then is later lowered to a
literal token. But this is very non-obvious. `Token` is a much more
general type than what is needed.
This commit restricts things, by introducing a new type `MacArgsEqKind`
that is either an AST expression (pre-lowering) or an AST literal
(post-lowering). The downside is that the code is a bit more verbose in
a few places. The benefit is that makes it much clearer what the
possibilities are (though also shorter in some other places). Also, it
removes one use of `TokenKind::Interpolated`, taking us a step closer to
removing that variant, which will let us make `Token` impl `Copy` and
remove many "handle Interpolated" code paths in the parser.
Things to note:
- Error messages have improved. Messages like this:
```
unexpected token: `"bug" + "found"`
```
now say "unexpected expression", which makes more sense. Although
arbitrary expressions can exist within tokens thanks to
`TokenKind::Interpolated`, that's not obvious to anyone who doesn't
know compiler internals.
- In `parse_mac_args_common`, we no longer need to collect tokens for
the value expression.
2022-04-29 06:52:01 +10:00
|
|
|
_ => None,
|
|
|
|
},
|
2022-11-18 11:24:21 +11:00
|
|
|
AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => Some(MetaItemKind::NameValue(lit.clone())),
|
2019-12-01 19:16:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_tokens(
|
|
|
|
tokens: &mut iter::Peekable<impl Iterator<Item = TokenTree>>,
|
|
|
|
) -> Option<MetaItemKind> {
|
|
|
|
match tokens.peek() {
|
2022-04-26 15:40:14 +03:00
|
|
|
Some(TokenTree::Delimited(_, Delimiter::Parenthesis, inner_tokens)) => {
|
2019-12-01 19:16:44 +03:00
|
|
|
let inner_tokens = inner_tokens.clone();
|
|
|
|
tokens.next();
|
|
|
|
MetaItemKind::list_from_tokens(inner_tokens)
|
|
|
|
}
|
|
|
|
Some(TokenTree::Delimited(..)) => None,
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
Some(TokenTree::Token(Token { kind: token::Eq, .. }, _)) => {
|
2019-12-01 19:16:44 +03:00
|
|
|
tokens.next();
|
|
|
|
MetaItemKind::name_value_from_tokens(tokens)
|
|
|
|
}
|
|
|
|
_ => Some(MetaItemKind::Word),
|
|
|
|
}
|
|
|
|
}
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:56:24 +03:00
|
|
|
impl NestedMetaItem {
|
|
|
|
pub fn span(&self) -> Span {
|
2022-11-16 19:26:38 +00:00
|
|
|
match self {
|
|
|
|
NestedMetaItem::MetaItem(item) => item.span,
|
|
|
|
NestedMetaItem::Literal(lit) => lit.span,
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
fn token_trees(&self) -> Vec<TokenTree> {
|
2022-11-16 19:26:38 +00:00
|
|
|
match self {
|
|
|
|
NestedMetaItem::MetaItem(item) => item.token_trees(),
|
|
|
|
NestedMetaItem::Literal(lit) => {
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
vec![TokenTree::Token(lit.to_token(), Spacing::Alone)]
|
|
|
|
}
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 20:56:24 +03:00
|
|
|
fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<NestedMetaItem>
|
2017-03-03 09:23:59 +00:00
|
|
|
where
|
|
|
|
I: Iterator<Item = TokenTree>,
|
|
|
|
{
|
2020-06-20 20:59:04 -04:00
|
|
|
match tokens.peek() {
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
Some(TokenTree::Token(token, _))
|
2022-10-10 13:40:56 +11:00
|
|
|
if let Some(lit) = Lit::from_token(token) =>
|
2021-08-16 17:29:49 +02:00
|
|
|
{
|
|
|
|
tokens.next();
|
|
|
|
return Some(NestedMetaItem::Literal(lit));
|
2020-06-20 20:59:04 -04:00
|
|
|
}
|
2022-04-26 15:40:14 +03:00
|
|
|
Some(TokenTree::Delimited(_, Delimiter::Invisible, inner_tokens)) => {
|
2020-06-20 20:59:04 -04:00
|
|
|
let inner_tokens = inner_tokens.clone();
|
2017-03-03 09:23:59 +00:00
|
|
|
tokens.next();
|
2020-06-20 20:59:04 -04:00
|
|
|
return NestedMetaItem::from_tokens(&mut inner_tokens.into_trees().peekable());
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
2020-06-20 20:59:04 -04:00
|
|
|
_ => {}
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
2019-03-03 20:56:24 +03:00
|
|
|
MetaItem::from_tokens(tokens).map(NestedMetaItem::MetaItem)
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
}
|