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;
|
2020-01-11 09:59:14 +01:00
|
|
|
use crate::ast::{AttrId, AttrItem, AttrKind, AttrStyle, AttrVec, Attribute};
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::ast::{Expr, GenericParam, Item, Lit, LitKind, Local, Stmt, StmtKind};
|
2019-12-01 17:07:38 +03:00
|
|
|
use crate::ast::{MacArgs, MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem};
|
2020-04-19 13:00:18 +02:00
|
|
|
use crate::ast::{Path, PathSegment};
|
2019-02-07 02:33:01 +09:00
|
|
|
use crate::mut_visit::visit_clobber;
|
|
|
|
use crate::ptr::P;
|
2020-07-21 22:16:19 +03:00
|
|
|
use crate::token::{self, CommentKind, Token};
|
2020-11-05 20:27:48 +03:00
|
|
|
use crate::tokenstream::{DelimSpan, LazyTokenStream, TokenStream, TokenTree, TreeAndSpacing};
|
2019-02-07 02:33:01 +09:00
|
|
|
|
2020-01-11 09:59:14 +01:00
|
|
|
use rustc_index::bit_set::GrowableBitSet;
|
2020-01-01 19:25:28 +01:00
|
|
|
use rustc_span::source_map::{BytePos, Spanned};
|
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;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2017-03-03 09:23:59 +00:00
|
|
|
use std::iter;
|
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 {
|
|
|
|
// We have no idea how many attributes there will be, so just
|
|
|
|
// initiate the vectors with 0 bits. We'll grow them as necessary.
|
|
|
|
pub fn new() -> Self {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-07-04 14:25:33 +02:00
|
|
|
pub fn is_known_lint_tool(m_item: Ident) -> bool {
|
2019-06-24 18:14:04 +02:00
|
|
|
[sym::clippy, sym::rustc].contains(&m_item.name)
|
2018-07-03 13:50:48 +02:00
|
|
|
}
|
|
|
|
|
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> {
|
2019-03-03 20:56:24 +03:00
|
|
|
match *self {
|
|
|
|
NestedMetaItem::MetaItem(ref item) => Some(item),
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => None,
|
2016-08-23 03:21:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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> {
|
2019-03-03 20:56:24 +03:00
|
|
|
match *self {
|
|
|
|
NestedMetaItem::Literal(ref lit) => Some(lit),
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => None,
|
2016-08-23 03:21:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-11-06 13:24:55 -08:00
|
|
|
self.ident().unwrap_or_else(Ident::invalid).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)> {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.meta_item().and_then(|meta_item| {
|
|
|
|
meta_item.meta_item_list().and_then(|meta_item_list| {
|
|
|
|
if meta_item_list.len() == 1 {
|
|
|
|
if let Some(ident) = meta_item.ident() {
|
|
|
|
if let Some(lit) = meta_item_list[0].literal() {
|
|
|
|
return Some((ident.name, lit));
|
2017-01-15 09:49:29 +11:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
|
|
|
None
|
|
|
|
})
|
|
|
|
})
|
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
|
|
|
}
|
|
|
|
|
2019-09-06 03:56:45 +01:00
|
|
|
/// Returns `true` if `self` is a `MetaItem` and the meta item is a `ValueString`.
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn is_value_str(&self) -> bool {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.value_str().is_some()
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:56:45 +01:00
|
|
|
/// Returns `true` if `self` is a `MetaItem` and the meta item is a list.
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn is_meta_item_list(&self) -> bool {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.meta_item_list().is_some()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
impl Attribute {
|
2019-10-24 06:33:12 +11:00
|
|
|
pub fn has_name(&self, name: Symbol) -> bool {
|
|
|
|
match self.kind {
|
2020-11-05 20:27:48 +03:00
|
|
|
AttrKind::Normal(ref item, _) => 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> {
|
2019-10-24 06:33:12 +11:00
|
|
|
match self.kind {
|
2020-11-05 20:27:48 +03:00
|
|
|
AttrKind::Normal(ref item, _) => {
|
2019-10-24 06:33:12 +11:00
|
|
|
if item.path.segments.len() == 1 {
|
|
|
|
Some(item.path.segments[0].ident)
|
|
|
|
} 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 {
|
2020-11-06 13:24:55 -08:00
|
|
|
self.ident().unwrap_or_else(Ident::invalid).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> {
|
2019-10-24 06:33:12 +11:00
|
|
|
match self.kind {
|
2020-11-05 20:27:48 +03:00
|
|
|
AttrKind::Normal(ref item, _) => item.meta(self.span).and_then(|meta| meta.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>> {
|
2019-10-24 06:33:12 +11:00
|
|
|
match self.kind {
|
2020-11-05 20:27:48 +03:00
|
|
|
AttrKind::Normal(ref item, _) => match item.meta(self.span) {
|
2019-12-22 17:42:04 -05:00
|
|
|
Some(MetaItem { kind: MetaItemKind::List(list), .. }) => Some(list),
|
|
|
|
_ => 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 {
|
2020-11-05 20:27:48 +03:00
|
|
|
if let AttrKind::Normal(item, _) = &self.kind {
|
2019-12-01 17:07:38 +03:00
|
|
|
matches!(item.args, MacArgs::Empty)
|
2019-10-24 06:33:12 +11:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
2016-08-23 03:54:53 +00:00
|
|
|
|
|
|
|
pub fn is_meta_item_list(&self) -> bool {
|
|
|
|
self.meta_item_list().is_some()
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:56:45 +01:00
|
|
|
/// Indicates if the attribute is a `ValueString`.
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn is_value_str(&self) -> bool {
|
|
|
|
self.value_str().is_some()
|
|
|
|
}
|
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-12-22 17:42:04 -05: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 {
|
2020-11-06 13:24:55 -08:00
|
|
|
self.ident().unwrap_or_else(Ident::invalid).name
|
2018-01-30 14:30:39 +09:00
|
|
|
}
|
|
|
|
|
2019-09-06 03:56:45 +01:00
|
|
|
// 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> {
|
2019-09-26 18:04:05 +01:00
|
|
|
match self.kind {
|
2019-12-22 17:42:04 -05:00
|
|
|
MetaItemKind::NameValue(ref v) => match v.kind {
|
|
|
|
LitKind::Str(ref s, _) => Some(*s),
|
|
|
|
_ => None,
|
2013-07-19 21:51:37 +10:00
|
|
|
},
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => None,
|
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]> {
|
2019-09-26 18:04:05 +01:00
|
|
|
match self.kind {
|
2016-11-15 07:37:10 +00:00
|
|
|
MetaItemKind::List(ref l) => Some(&l[..]),
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => None,
|
2013-07-19 21:51:37 +10:00
|
|
|
}
|
|
|
|
}
|
2016-07-15 13:13:17 -07:00
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn is_word(&self) -> bool {
|
2019-09-26 18:04:05 +01:00
|
|
|
match self.kind {
|
2016-11-15 07:37:10 +00:00
|
|
|
MetaItemKind::Word => true,
|
2016-07-15 13:13:17 -07:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn is_value_str(&self) -> bool {
|
|
|
|
self.value_str().is_some()
|
|
|
|
}
|
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(),
|
2019-12-01 19:16:44 +03:00
|
|
|
kind: MetaItemKind::from_mac_args(&self.args)?,
|
2019-08-18 01:10:56 +03:00
|
|
|
span,
|
2017-03-03 09:23:59 +00:00
|
|
|
})
|
|
|
|
}
|
2019-08-18 01:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Attribute {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 21:28:29 +03:00
|
|
|
pub fn doc_str(&self) -> Option<Symbol> {
|
|
|
|
match self.kind {
|
2020-07-21 22:16:19 +03:00
|
|
|
AttrKind::DocComment(.., data) => Some(data),
|
2020-11-05 20:27:48 +03:00
|
|
|
AttrKind::Normal(ref item, _) if item.path == sym::doc => {
|
2019-12-07 21:28:29 +03:00
|
|
|
item.meta(self.span).and_then(|meta| meta.value_str())
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 06:33:12 +11:00
|
|
|
pub fn get_normal_item(&self) -> &AttrItem {
|
|
|
|
match self.kind {
|
2020-11-05 20:27:48 +03:00
|
|
|
AttrKind::Normal(ref item, _) => 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 {
|
2020-11-05 20:27:48 +03:00
|
|
|
AttrKind::Normal(item, _) => 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> {
|
2019-10-24 06:33:12 +11:00
|
|
|
match self.kind {
|
2020-11-05 20:27:48 +03:00
|
|
|
AttrKind::Normal(ref item, _) => 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
|
|
|
|
|
|
|
pub fn tokens(&self) -> TokenStream {
|
|
|
|
match self.kind {
|
|
|
|
AttrKind::Normal(_, ref tokens) => tokens
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or_else(|| panic!("attribute is missing tokens: {:?}", self))
|
|
|
|
.create_token_stream(),
|
|
|
|
AttrKind::DocComment(comment_kind, data) => TokenStream::from(TokenTree::Token(
|
|
|
|
Token::new(token::DocComment(comment_kind, self.style, data), self.span),
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-08-04 19:35:29 -04:00
|
|
|
crate fn mk_attr_id() -> AttrId {
|
2020-03-21 02:16:27 +03:00
|
|
|
use std::sync::atomic::AtomicU32;
|
2017-12-03 14:03:28 +01:00
|
|
|
use std::sync::atomic::Ordering;
|
2016-07-15 13:13:17 -07:00
|
|
|
|
2020-03-21 02:16:27 +03:00
|
|
|
static NEXT_ATTR_ID: AtomicU32 = AtomicU32::new(0);
|
2016-07-15 13:13:17 -07:00
|
|
|
|
2017-12-03 14:03:28 +01:00
|
|
|
let id = NEXT_ATTR_ID.fetch_add(1, Ordering::SeqCst);
|
2020-03-21 02:16:27 +03:00
|
|
|
assert!(id != u32::MAX);
|
|
|
|
AttrId::from_u32(id)
|
2014-05-20 00:07:24 -07:00
|
|
|
}
|
|
|
|
|
2019-12-01 17:07:38 +03:00
|
|
|
pub fn mk_attr(style: AttrStyle, path: Path, args: MacArgs, span: Span) -> Attribute {
|
2020-11-05 20:27:48 +03:00
|
|
|
mk_attr_from_item(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(
|
|
|
|
item: AttrItem,
|
|
|
|
tokens: Option<LazyTokenStream>,
|
|
|
|
style: AttrStyle,
|
|
|
|
span: Span,
|
|
|
|
) -> Attribute {
|
|
|
|
Attribute { kind: AttrKind::Normal(item, tokens), id: mk_attr_id(), 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.
|
|
|
|
pub fn mk_attr_inner(item: MetaItem) -> Attribute {
|
2019-12-01 17:07:38 +03:00
|
|
|
mk_attr(AttrStyle::Inner, item.path, item.kind.mac_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.
|
2019-07-30 14:18:19 -04:00
|
|
|
pub fn mk_attr_outer(item: MetaItem) -> Attribute {
|
2019-12-01 17:07:38 +03:00
|
|
|
mk_attr(AttrStyle::Outer, item.path, item.kind.mac_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(
|
|
|
|
comment_kind: CommentKind,
|
|
|
|
style: AttrStyle,
|
|
|
|
data: Symbol,
|
|
|
|
span: Span,
|
|
|
|
) -> Attribute {
|
2020-11-05 20:27:48 +03:00
|
|
|
Attribute { kind: AttrKind::DocComment(comment_kind, data), id: 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 {
|
2020-09-03 17:21:53 +02:00
|
|
|
fn token_trees_and_spacings(&self) -> Vec<TreeAndSpacing> {
|
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 {
|
2019-12-22 17:42:04 -05:00
|
|
|
let mod_sep_span =
|
|
|
|
Span::new(last_pos, segment.ident.span.lo(), segment.ident.span.ctxt());
|
2019-06-05 13:25:26 +03:00
|
|
|
idents.push(TokenTree::token(token::ModSep, mod_sep_span).into());
|
2018-01-30 14:30:39 +09:00
|
|
|
}
|
2019-06-08 19:45:12 +03:00
|
|
|
idents.push(TokenTree::Token(Token::from_ast_ident(segment.ident)).into());
|
2018-04-17 15:33:39 +02:00
|
|
|
last_pos = segment.ident.span.hi();
|
2018-01-30 14:30:39 +09:00
|
|
|
}
|
2020-09-03 17:21:53 +02:00
|
|
|
idents.extend(self.kind.token_trees_and_spacings(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>
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
I: Iterator<Item = TokenTree>,
|
2017-03-03 09:23:59 +00:00
|
|
|
{
|
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) {
|
2020-04-16 17:38:52 -07: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 {
|
2019-12-22 17:42:04 -05:00
|
|
|
if let Some(TokenTree::Token(Token { kind: token::ModSep, .. })) = tokens.peek()
|
|
|
|
{
|
2019-01-02 02:21:05 +03:00
|
|
|
tokens.next();
|
2019-06-05 11:56:06 +03:00
|
|
|
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 {
|
2019-01-02 02:21:05 +03:00
|
|
|
vec![PathSegment::path_root(span)]
|
|
|
|
};
|
|
|
|
loop {
|
2019-12-22 17:42:04 -05: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-12-22 17:42:04 -05: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;
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
if let Some(TokenTree::Token(Token { kind: token::ModSep, .. })) = tokens.peek()
|
|
|
|
{
|
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
|
|
|
}
|
2020-07-01 13:16:49 +03:00
|
|
|
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. })) => match *nt {
|
2019-08-18 01:10:56 +03:00
|
|
|
token::Nonterminal::NtMeta(ref item) => return item.meta(item.path.span),
|
2018-01-30 14:30:39 +09:00
|
|
|
token::Nonterminal::NtPath(ref 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)?;
|
|
|
|
let hi = match kind {
|
2017-07-31 23:04:34 +03:00
|
|
|
MetaItemKind::NameValue(ref 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 {
|
2019-12-01 17:07:38 +03:00
|
|
|
pub fn mac_args(&self, span: Span) -> MacArgs {
|
|
|
|
match self {
|
|
|
|
MetaItemKind::Word => MacArgs::Empty,
|
|
|
|
MetaItemKind::NameValue(lit) => MacArgs::Eq(span, lit.token_tree().into()),
|
|
|
|
MetaItemKind::List(list) => {
|
|
|
|
let mut tts = Vec::new();
|
|
|
|
for (i, item) in list.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
tts.push(TokenTree::token(token::Comma, span).into());
|
|
|
|
}
|
2020-09-03 17:21:53 +02:00
|
|
|
tts.extend(item.token_trees_and_spacings())
|
2019-12-01 17:07:38 +03:00
|
|
|
}
|
|
|
|
MacArgs::Delimited(
|
2019-12-22 17:42:04 -05:00
|
|
|
DelimSpan::from_single(span),
|
|
|
|
MacDelimiter::Parenthesis,
|
|
|
|
TokenStream::new(tts),
|
2019-12-01 17:07:38 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 17:21:53 +02:00
|
|
|
fn token_trees_and_spacings(&self, span: Span) -> Vec<TreeAndSpacing> {
|
2017-03-03 09:23:59 +00:00
|
|
|
match *self {
|
2019-10-14 14:06:00 +11:00
|
|
|
MetaItemKind::Word => vec![],
|
2017-03-03 09:23:59 +00:00
|
|
|
MetaItemKind::NameValue(ref lit) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
vec![TokenTree::token(token::Eq, span).into(), lit.token_tree().into()]
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
MetaItemKind::List(ref list) => {
|
|
|
|
let mut tokens = Vec::new();
|
|
|
|
for (i, item) in list.iter().enumerate() {
|
|
|
|
if i > 0 {
|
2019-06-05 13:25:26 +03:00
|
|
|
tokens.push(TokenTree::token(token::Comma, span).into());
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
2020-09-03 17:21:53 +02:00
|
|
|
tokens.extend(item.token_trees_and_spacings())
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
2019-10-14 14:06:00 +11:00
|
|
|
vec![
|
|
|
|
TokenTree::Delimited(
|
|
|
|
DelimSpan::from_single(span),
|
|
|
|
token::Paren,
|
2020-02-25 18:10:34 +01:00
|
|
|
TokenStream::new(tokens),
|
2019-12-22 17:42:04 -05:00
|
|
|
)
|
|
|
|
.into(),
|
2019-10-14 14:06:00 +11:00
|
|
|
]
|
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();
|
|
|
|
while let Some(..) = tokens.peek() {
|
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() {
|
2019-06-04 20:42:43 +03: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() {
|
2020-06-20 20:59:04 -04:00
|
|
|
Some(TokenTree::Delimited(_, token::NoDelim, inner_tokens)) => {
|
|
|
|
MetaItemKind::name_value_from_tokens(&mut inner_tokens.trees())
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
Some(TokenTree::Token(token)) => {
|
|
|
|
Lit::from_token(&token).ok().map(MetaItemKind::NameValue)
|
|
|
|
}
|
2019-12-01 19:16:44 +03:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_mac_args(args: &MacArgs) -> Option<MetaItemKind> {
|
|
|
|
match args {
|
2019-12-22 17:42:04 -05:00
|
|
|
MacArgs::Delimited(_, MacDelimiter::Parenthesis, tokens) => {
|
|
|
|
MetaItemKind::list_from_tokens(tokens.clone())
|
|
|
|
}
|
2019-12-01 19:16:44 +03:00
|
|
|
MacArgs::Delimited(..) => None,
|
|
|
|
MacArgs::Eq(_, tokens) => {
|
|
|
|
assert!(tokens.len() == 1);
|
|
|
|
MetaItemKind::name_value_from_tokens(&mut tokens.trees())
|
|
|
|
}
|
|
|
|
MacArgs::Empty => Some(MetaItemKind::Word),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_tokens(
|
|
|
|
tokens: &mut iter::Peekable<impl Iterator<Item = TokenTree>>,
|
|
|
|
) -> Option<MetaItemKind> {
|
|
|
|
match tokens.peek() {
|
|
|
|
Some(TokenTree::Delimited(_, token::Paren, inner_tokens)) => {
|
|
|
|
let inner_tokens = inner_tokens.clone();
|
|
|
|
tokens.next();
|
|
|
|
MetaItemKind::list_from_tokens(inner_tokens)
|
|
|
|
}
|
|
|
|
Some(TokenTree::Delimited(..)) => None,
|
|
|
|
Some(TokenTree::Token(Token { kind: token::Eq, .. })) => {
|
|
|
|
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 {
|
2017-03-03 09:23:59 +00:00
|
|
|
match *self {
|
2019-03-03 20:56:24 +03:00
|
|
|
NestedMetaItem::MetaItem(ref item) => item.span,
|
|
|
|
NestedMetaItem::Literal(ref lit) => lit.span,
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 17:21:53 +02:00
|
|
|
fn token_trees_and_spacings(&self) -> Vec<TreeAndSpacing> {
|
2017-03-03 09:23:59 +00:00
|
|
|
match *self {
|
2020-09-03 17:21:53 +02:00
|
|
|
NestedMetaItem::MetaItem(ref item) => item.token_trees_and_spacings(),
|
2019-10-14 14:06:00 +11:00
|
|
|
NestedMetaItem::Literal(ref lit) => vec![lit.token_tree().into()],
|
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>
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
I: Iterator<Item = TokenTree>,
|
2017-03-03 09:23:59 +00:00
|
|
|
{
|
2020-06-20 20:59:04 -04:00
|
|
|
match tokens.peek() {
|
|
|
|
Some(TokenTree::Token(token)) => {
|
|
|
|
if let Ok(lit) = Lit::from_token(token) {
|
|
|
|
tokens.next();
|
|
|
|
return Some(NestedMetaItem::Literal(lit));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(TokenTree::Delimited(_, token::NoDelim, inner_tokens)) => {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 07:25:44 +00:00
|
|
|
pub trait HasAttrs: Sized {
|
2020-01-11 12:33:11 +01:00
|
|
|
fn attrs(&self) -> &[Attribute];
|
|
|
|
fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>));
|
2016-05-18 07:25:44 +00:00
|
|
|
}
|
|
|
|
|
2017-01-03 19:13:01 -08:00
|
|
|
impl<T: HasAttrs> HasAttrs for Spanned<T> {
|
2020-01-11 12:33:11 +01:00
|
|
|
fn attrs(&self) -> &[Attribute] {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.node.attrs()
|
|
|
|
}
|
2020-01-11 12:33:11 +01:00
|
|
|
fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>)) {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 15:20:55 +11:00
|
|
|
self.node.visit_attrs(f);
|
2017-01-03 19:13:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 07:25:44 +00:00
|
|
|
impl HasAttrs for Vec<Attribute> {
|
|
|
|
fn attrs(&self) -> &[Attribute] {
|
2017-05-12 20:05:39 +02:00
|
|
|
self
|
2016-05-18 07:25:44 +00:00
|
|
|
}
|
2020-01-11 12:33:11 +01:00
|
|
|
fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>)) {
|
2016-05-18 07:25:44 +00:00
|
|
|
f(self)
|
2015-11-03 17:39:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 16:38:34 +01:00
|
|
|
impl HasAttrs for AttrVec {
|
2016-05-18 07:25:44 +00:00
|
|
|
fn attrs(&self) -> &[Attribute] {
|
2017-05-12 20:05:39 +02:00
|
|
|
self
|
2016-05-18 07:25:44 +00:00
|
|
|
}
|
2020-01-11 12:33:11 +01:00
|
|
|
fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>)) {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 15:20:55 +11:00
|
|
|
visit_clobber(self, |this| {
|
|
|
|
let mut vec = this.into();
|
|
|
|
f(&mut vec);
|
|
|
|
vec.into()
|
|
|
|
});
|
2015-11-03 17:39:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 07:25:44 +00:00
|
|
|
impl<T: HasAttrs + 'static> HasAttrs for P<T> {
|
|
|
|
fn attrs(&self) -> &[Attribute] {
|
|
|
|
(**self).attrs()
|
|
|
|
}
|
2020-01-11 12:33:11 +01:00
|
|
|
fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>)) {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 15:20:55 +11:00
|
|
|
(**self).visit_attrs(f);
|
2015-11-03 17:39:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 07:25:44 +00:00
|
|
|
impl HasAttrs for StmtKind {
|
|
|
|
fn attrs(&self) -> &[Attribute] {
|
|
|
|
match *self {
|
2016-06-17 02:30:01 +00:00
|
|
|
StmtKind::Local(ref local) => local.attrs(),
|
|
|
|
StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => expr.attrs(),
|
Fix inconsistencies in handling of inert attributes on statements
When the 'early' and 'late' visitors visit an attribute target, they
activate any lint attributes (e.g. `#[allow]`) that apply to it.
This can affect warnings emitted on sibiling attributes. For example,
the following code does not produce an `unused_attributes` for
`#[inline]`, since the sibiling `#[allow(unused_attributes)]` suppressed
the warning.
```rust
trait Foo {
#[allow(unused_attributes)] #[inline] fn first();
#[inline] #[allow(unused_attributes)] fn second();
}
```
However, we do not do this for statements - instead, the lint attributes
only become active when we visit the struct nested inside `StmtKind`
(e.g. `Item`).
Currently, this is difficult to observe due to another issue - the
`HasAttrs` impl for `StmtKind` ignores attributes for `StmtKind::Item`.
As a result, the `unused_doc_comments` lint will never see attributes on
item statements.
This commit makes two interrelated fixes to the handling of inert
(non-proc-macro) attributes on statements:
* The `HasAttr` impl for `StmtKind` now returns attributes for
`StmtKind::Item`, treating it just like every other `StmtKind`
variant. The only place relying on the old behavior was macro
which has been updated to explicitly ignore attributes on item
statements. This allows the `unused_doc_comments` lint to fire for
item statements.
* The `early` and `late` lint visitors now activate lint attributes when
invoking the callback for `Stmt`. This ensures that a lint
attribute (e.g. `#[allow(unused_doc_comments)]`) can be applied to
sibiling attributes on an item statement.
For now, the `unused_doc_comments` lint is explicitly disabled on item
statements, which preserves the current behavior. The exact locatiosn
where this lint should fire are being discussed in PR #78306
2020-10-23 18:17:00 -04:00
|
|
|
StmtKind::Item(ref item) => item.attrs(),
|
|
|
|
StmtKind::Empty => &[],
|
2020-08-30 18:38:32 -04:00
|
|
|
StmtKind::MacCall(ref mac) => mac.attrs.attrs(),
|
2016-05-18 07:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 12:33:11 +01:00
|
|
|
fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>)) {
|
2016-05-18 07:25:44 +00:00
|
|
|
match self {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 15:20:55 +11:00
|
|
|
StmtKind::Local(local) => local.visit_attrs(f),
|
2020-02-27 04:10:42 +01:00
|
|
|
StmtKind::Expr(expr) | StmtKind::Semi(expr) => expr.visit_attrs(f),
|
Fix inconsistencies in handling of inert attributes on statements
When the 'early' and 'late' visitors visit an attribute target, they
activate any lint attributes (e.g. `#[allow]`) that apply to it.
This can affect warnings emitted on sibiling attributes. For example,
the following code does not produce an `unused_attributes` for
`#[inline]`, since the sibiling `#[allow(unused_attributes)]` suppressed
the warning.
```rust
trait Foo {
#[allow(unused_attributes)] #[inline] fn first();
#[inline] #[allow(unused_attributes)] fn second();
}
```
However, we do not do this for statements - instead, the lint attributes
only become active when we visit the struct nested inside `StmtKind`
(e.g. `Item`).
Currently, this is difficult to observe due to another issue - the
`HasAttrs` impl for `StmtKind` ignores attributes for `StmtKind::Item`.
As a result, the `unused_doc_comments` lint will never see attributes on
item statements.
This commit makes two interrelated fixes to the handling of inert
(non-proc-macro) attributes on statements:
* The `HasAttr` impl for `StmtKind` now returns attributes for
`StmtKind::Item`, treating it just like every other `StmtKind`
variant. The only place relying on the old behavior was macro
which has been updated to explicitly ignore attributes on item
statements. This allows the `unused_doc_comments` lint to fire for
item statements.
* The `early` and `late` lint visitors now activate lint attributes when
invoking the callback for `Stmt`. This ensures that a lint
attribute (e.g. `#[allow(unused_doc_comments)]`) can be applied to
sibiling attributes on an item statement.
For now, the `unused_doc_comments` lint is explicitly disabled on item
statements, which preserves the current behavior. The exact locatiosn
where this lint should fire are being discussed in PR #78306
2020-10-23 18:17:00 -04:00
|
|
|
StmtKind::Item(item) => item.visit_attrs(f),
|
|
|
|
StmtKind::Empty => {}
|
2020-02-29 19:32:20 +03:00
|
|
|
StmtKind::MacCall(mac) => {
|
2020-08-30 18:38:32 -04:00
|
|
|
mac.attrs.visit_attrs(f);
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 15:20:55 +11:00
|
|
|
}
|
2016-05-18 07:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:13:01 -08:00
|
|
|
impl HasAttrs for Stmt {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 15:20:55 +11:00
|
|
|
fn attrs(&self) -> &[ast::Attribute] {
|
2019-09-26 17:34:50 +01:00
|
|
|
self.kind.attrs()
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 15:20:55 +11:00
|
|
|
}
|
|
|
|
|
2020-01-11 12:33:11 +01:00
|
|
|
fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>)) {
|
2019-09-26 17:34:50 +01:00
|
|
|
self.kind.visit_attrs(f);
|
2017-01-03 19:13:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! derive_has_attrs {
|
|
|
|
($($ty:path),*) => { $(
|
2016-05-18 07:25:44 +00:00
|
|
|
impl HasAttrs for $ty {
|
|
|
|
fn attrs(&self) -> &[Attribute] {
|
2017-01-03 19:13:01 -08:00
|
|
|
&self.attrs
|
2016-05-18 07:25:44 +00:00
|
|
|
}
|
|
|
|
|
2020-01-11 12:33:11 +01:00
|
|
|
fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<Attribute>)) {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 15:20:55 +11:00
|
|
|
self.attrs.visit_attrs(f);
|
2016-05-18 07:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)* }
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:13:01 -08:00
|
|
|
derive_has_attrs! {
|
2020-02-29 15:51:08 +03:00
|
|
|
Item, Expr, Local, ast::AssocItem, ast::ForeignItem, ast::StructField, ast::Arm,
|
2020-01-11 12:33:11 +01:00
|
|
|
ast::Field, ast::FieldPat, ast::Variant, ast::Param, GenericParam
|
2016-05-18 07:25:44 +00:00
|
|
|
}
|