2018-06-30 12:19:18 -04:00
|
|
|
//! Functions dealing with attributes and meta items
|
2011-06-28 15:25:20 -07:00
|
|
|
|
2018-06-30 12:19:18 -04:00
|
|
|
mod builtin;
|
|
|
|
|
2019-02-07 02:33:01 +09:00
|
|
|
pub use builtin::{
|
2018-06-30 12:19:18 -04:00
|
|
|
cfg_matches, contains_feature_attr, eval_condition, find_crate_name, find_deprecation,
|
2018-10-27 15:29:06 +03:00
|
|
|
find_repr_attrs, find_stability, find_unwind_attr, Deprecation, InlineAttr, OptimizeAttr,
|
|
|
|
IntType, ReprAttr, RustcDeprecation, Stability, StabilityLevel, UnwindAttr,
|
2018-06-30 12:19:18 -04:00
|
|
|
};
|
2019-02-07 02:33:01 +09:00
|
|
|
pub use IntType::*;
|
|
|
|
pub use ReprAttr::*;
|
|
|
|
pub use StabilityLevel::*;
|
2014-11-06 00:05:53 -08:00
|
|
|
|
2019-02-07 02:33:01 +09:00
|
|
|
use crate::ast;
|
|
|
|
use crate::ast::{AttrId, Attribute, AttrStyle, Name, Ident, Path, PathSegment};
|
|
|
|
use crate::ast::{MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
|
|
|
|
use crate::ast::{Lit, LitKind, Expr, ExprKind, Item, Local, Stmt, StmtKind, GenericParam};
|
|
|
|
use crate::mut_visit::visit_clobber;
|
|
|
|
use crate::source_map::{BytePos, Spanned, respan, dummy_spanned};
|
|
|
|
use crate::parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
|
|
|
|
use crate::parse::parser::Parser;
|
|
|
|
use crate::parse::{self, ParseSess, PResult};
|
|
|
|
use crate::parse::token::{self, Token};
|
|
|
|
use crate::ptr::P;
|
|
|
|
use crate::symbol::Symbol;
|
|
|
|
use crate::ThinVec;
|
|
|
|
use crate::tokenstream::{TokenStream, TokenTree, DelimSpan};
|
|
|
|
use crate::GLOBALS;
|
|
|
|
|
|
|
|
use log::debug;
|
2018-07-18 20:34:08 +02:00
|
|
|
use syntax_pos::{FileName, Span};
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2017-03-03 09:23:59 +00:00
|
|
|
use std::iter;
|
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
|
|
|
use std::ops::DerefMut;
|
2012-03-02 13:14:10 -08:00
|
|
|
|
2014-05-20 00:07:24 -07:00
|
|
|
pub fn mark_used(attr: &Attribute) {
|
2016-08-19 18:58:14 -07:00
|
|
|
debug!("Marking {:?} as used.", attr);
|
2018-03-07 02:44:10 +01:00
|
|
|
GLOBALS.with(|globals| {
|
2018-07-30 08:39:03 -06:00
|
|
|
globals.used_attrs.lock().insert(attr.id);
|
2015-08-11 17:27:05 -07:00
|
|
|
});
|
2014-05-20 00:07:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_used(attr: &Attribute) -> bool {
|
2018-03-07 02:44:10 +01:00
|
|
|
GLOBALS.with(|globals| {
|
2018-07-30 08:39:03 -06:00
|
|
|
globals.used_attrs.lock().contains(attr.id)
|
2015-08-11 17:27:05 -07:00
|
|
|
})
|
2014-05-20 00:07:24 -07:00
|
|
|
}
|
|
|
|
|
2016-11-08 08:30:26 +10:30
|
|
|
pub fn mark_known(attr: &Attribute) {
|
|
|
|
debug!("Marking {:?} as known.", attr);
|
2018-03-07 02:44:10 +01:00
|
|
|
GLOBALS.with(|globals| {
|
2018-07-30 08:39:03 -06:00
|
|
|
globals.known_attrs.lock().insert(attr.id);
|
2016-11-08 08:30:26 +10:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_known(attr: &Attribute) -> bool {
|
2018-03-07 02:44:10 +01:00
|
|
|
GLOBALS.with(|globals| {
|
2018-07-30 08:39:03 -06:00
|
|
|
globals.known_attrs.lock().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 {
|
2018-07-23 02:52:51 +03:00
|
|
|
["clippy"].contains(&m_item.as_str().as_ref())
|
2018-07-03 13:50:48 +02:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:21:17 +00:00
|
|
|
impl NestedMetaItem {
|
|
|
|
/// Returns the MetaItem if self is a NestedMetaItemKind::MetaItem.
|
2016-11-15 10:17:24 +00:00
|
|
|
pub fn meta_item(&self) -> Option<&MetaItem> {
|
2016-08-23 03:21:17 +00:00
|
|
|
match self.node {
|
2017-05-12 20:05:39 +02:00
|
|
|
NestedMetaItemKind::MetaItem(ref item) => Some(item),
|
2016-08-23 03:21:17 +00:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the Lit if self is a NestedMetaItemKind::Literal.
|
|
|
|
pub fn literal(&self) -> Option<&Lit> {
|
|
|
|
match self.node {
|
2017-05-12 20:05:39 +02:00
|
|
|
NestedMetaItemKind::Literal(ref lit) => Some(lit),
|
2016-08-23 03:21:17 +00:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the Span for `self`.
|
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
self.span
|
|
|
|
}
|
|
|
|
|
2016-08-19 18:58:14 -07:00
|
|
|
/// Returns true if this list item is a MetaItem with a name of `name`.
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn check_name(&self, name: &str) -> bool {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.meta_item().map_or(false, |meta_item| meta_item.check_name(name))
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
/// Returns the name of the meta item, e.g., `foo` in `#[foo]`,
|
2016-08-19 18:58:14 -07:00
|
|
|
/// `#[foo="bar"]` and `#[foo(bar)]`, if self is a MetaItem
|
2016-11-15 04:34:52 +00:00
|
|
|
pub fn name(&self) -> Option<Name> {
|
2018-01-30 14:30:39 +09:00
|
|
|
self.meta_item().and_then(|meta_item| Some(meta_item.name()))
|
2016-08-19 18:58:14 -07: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())
|
|
|
|
}
|
|
|
|
|
2017-01-15 09:49:29 +11:00
|
|
|
/// Returns a name and single literal value tuple of the MetaItem.
|
|
|
|
pub fn name_value_literal(&self) -> Option<(Name, &Lit)> {
|
|
|
|
self.meta_item().and_then(
|
|
|
|
|meta_item| meta_item.meta_item_list().and_then(
|
|
|
|
|meta_item_list| {
|
|
|
|
if meta_item_list.len() == 1 {
|
|
|
|
let nested_item = &meta_item_list[0];
|
|
|
|
if nested_item.is_literal() {
|
2018-01-30 14:30:39 +09:00
|
|
|
Some((meta_item.name(), nested_item.literal().unwrap()))
|
2017-01-15 09:49:29 +11:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
None
|
|
|
|
}}))
|
|
|
|
}
|
|
|
|
|
2016-08-19 18:58:14 -07:00
|
|
|
/// Returns a MetaItem if self is a MetaItem with Kind Word.
|
2016-11-15 10:17:24 +00:00
|
|
|
pub fn word(&self) -> Option<&MetaItem> {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.meta_item().and_then(|meta_item| if meta_item.is_word() {
|
|
|
|
Some(meta_item)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the variant is Literal.
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn is_literal(&self) -> bool {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.literal().is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 {
|
2016-08-19 18:58:14 -07:00
|
|
|
self.word().is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 16:57:41 +02:00
|
|
|
fn name_from_path(path: &Path) -> Name {
|
2018-04-17 15:33:39 +02:00
|
|
|
path.segments.last().expect("empty path in attribute").ident.name
|
2018-01-30 14:30:39 +09:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
impl Attribute {
|
|
|
|
pub fn check_name(&self, name: &str) -> bool {
|
2017-03-03 09:23:59 +00:00
|
|
|
let matches = self.path == name;
|
2014-05-23 08:39:26 -07:00
|
|
|
if matches {
|
2014-05-20 22:07:42 -07:00
|
|
|
mark_used(self);
|
|
|
|
}
|
2014-05-23 08:39:26 -07:00
|
|
|
matches
|
2014-05-20 22:07:42 -07:00
|
|
|
}
|
2016-08-19 18:58:14 -07:00
|
|
|
|
2018-02-04 15:36:26 +09:00
|
|
|
/// Returns the **last** segment of the name of this attribute.
|
2018-11-27 02:59:49 +00:00
|
|
|
/// e.g., `foo` for `#[foo]`, `skip` for `#[rustfmt::skip]`.
|
2018-01-30 14:53:01 +09:00
|
|
|
pub fn name(&self) -> Name {
|
|
|
|
name_from_path(&self.path)
|
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> {
|
2017-03-03 09:23:59 +00:00
|
|
|
self.meta().and_then(|meta| meta.value_str())
|
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>> {
|
|
|
|
match self.meta() {
|
|
|
|
Some(MetaItem { node: MetaItemKind::List(list), .. }) => Some(list),
|
|
|
|
_ => None
|
|
|
|
}
|
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 {
|
|
|
|
self.path.segments.len() == 1 && self.tokens.is_empty()
|
|
|
|
}
|
2016-08-23 03:54:53 +00:00
|
|
|
|
2017-03-03 09:23:59 +00:00
|
|
|
pub fn span(&self) -> Span {
|
|
|
|
self.span
|
|
|
|
}
|
2016-07-15 13:13:17 -07:00
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn is_meta_item_list(&self) -> bool {
|
|
|
|
self.meta_item_list().is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Indicates if the attribute is a Value String.
|
|
|
|
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 {
|
2018-01-30 14:30:39 +09:00
|
|
|
pub fn name(&self) -> Name {
|
2018-04-17 15:33:39 +02:00
|
|
|
name_from_path(&self.ident)
|
2018-01-30 14:30:39 +09:00
|
|
|
}
|
|
|
|
|
2018-10-09 22:54:47 +08:00
|
|
|
// #[attribute(name = "value")]
|
|
|
|
// ^^^^^^^^^^^^^^
|
|
|
|
pub fn name_value_literal(&self) -> Option<&Lit> {
|
|
|
|
match &self.node {
|
|
|
|
MetaItemKind::NameValue(v) => Some(v),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 10:52:37 +00:00
|
|
|
pub fn value_str(&self) -> Option<Symbol> {
|
2013-07-19 21:51:37 +10:00
|
|
|
match self.node {
|
2016-11-15 07:37:10 +00:00
|
|
|
MetaItemKind::NameValue(ref v) => {
|
2013-07-19 21:51:37 +10:00
|
|
|
match v.node {
|
2017-05-12 20:05:39 +02:00
|
|
|
LitKind::Str(ref s, _) => Some(*s),
|
2013-07-19 21:51:37 +10:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
2012-04-15 01:07:47 -07:00
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
|
2013-07-19 21:51:37 +10:00
|
|
|
match self.node {
|
2016-11-15 07:37:10 +00:00
|
|
|
MetaItemKind::List(ref 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 {
|
2016-07-15 13:13:17 -07:00
|
|
|
match self.node {
|
2016-11-15 07:37:10 +00:00
|
|
|
MetaItemKind::Word => true,
|
2016-07-15 13:13:17 -07:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn span(&self) -> Span { self.span }
|
|
|
|
|
|
|
|
pub fn check_name(&self, name: &str) -> bool {
|
2018-01-30 14:30:39 +09:00
|
|
|
self.name() == name
|
2016-08-23 03:54:53 +00:00
|
|
|
}
|
2012-01-26 16:23:34 -08:00
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn is_value_str(&self) -> bool {
|
|
|
|
self.value_str().is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_meta_item_list(&self) -> bool {
|
|
|
|
self.meta_item_list().is_some()
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
2018-07-03 13:50:48 +02:00
|
|
|
|
2018-07-04 14:25:33 +02:00
|
|
|
pub fn is_scoped(&self) -> Option<Ident> {
|
|
|
|
if self.ident.segments.len() > 1 {
|
|
|
|
Some(self.ident.segments[0].ident)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2018-07-03 13:50:48 +02:00
|
|
|
}
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
2012-04-15 01:07:47 -07:00
|
|
|
|
2016-08-23 03:39:04 +00:00
|
|
|
impl Attribute {
|
2013-07-19 21:51:37 +10:00
|
|
|
/// Extract the MetaItem from inside this Attribute.
|
2017-03-03 09:23:59 +00:00
|
|
|
pub fn meta(&self) -> Option<MetaItem> {
|
|
|
|
let mut tokens = self.tokens.trees().peekable();
|
|
|
|
Some(MetaItem {
|
2018-04-17 15:33:39 +02:00
|
|
|
ident: self.path.clone(),
|
2017-03-03 09:23:59 +00:00
|
|
|
node: if let Some(node) = MetaItemKind::from_tokens(&mut tokens) {
|
|
|
|
if tokens.peek().is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
node
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
},
|
|
|
|
span: self.span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-08 23:13:35 +00:00
|
|
|
pub fn parse<'a, T, F>(&self, sess: &'a ParseSess, mut f: F) -> PResult<'a, T>
|
|
|
|
where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
|
|
|
{
|
2017-05-18 10:37:24 +12:00
|
|
|
let mut parser = Parser::new(sess, self.tokens.clone(), None, false, false);
|
2017-03-08 23:13:35 +00:00
|
|
|
let result = f(&mut parser)?;
|
|
|
|
if parser.token != token::Eof {
|
|
|
|
parser.unexpected()?;
|
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_list<'a, T, F>(&self, sess: &'a ParseSess, mut f: F) -> PResult<'a, Vec<T>>
|
|
|
|
where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
|
|
|
|
{
|
|
|
|
if self.tokens.is_empty() {
|
|
|
|
return Ok(Vec::new());
|
|
|
|
}
|
|
|
|
self.parse(sess, |parser| {
|
|
|
|
parser.expect(&token::OpenDelim(token::Paren))?;
|
|
|
|
let mut list = Vec::new();
|
|
|
|
while !parser.eat(&token::CloseDelim(token::Paren)) {
|
|
|
|
list.push(f(parser)?);
|
|
|
|
if !parser.eat(&token::Comma) {
|
|
|
|
parser.expect(&token::CloseDelim(token::Paren))?;
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(list)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-03 09:23:59 +00:00
|
|
|
pub fn parse_meta<'a>(&self, sess: &'a ParseSess) -> PResult<'a, MetaItem> {
|
|
|
|
Ok(MetaItem {
|
2018-04-17 15:33:39 +02:00
|
|
|
ident: self.path.clone(),
|
2017-03-08 23:13:35 +00:00
|
|
|
node: self.parse(sess, |parser| parser.parse_meta_item_kind())?,
|
2017-03-03 09:23:59 +00:00
|
|
|
span: self.span,
|
|
|
|
})
|
2011-06-28 11:24:24 -07:00
|
|
|
}
|
|
|
|
|
2013-07-19 21:51:37 +10:00
|
|
|
/// Convert self to a normal #[doc="foo"] comment, if it is a
|
|
|
|
/// comment like `///` or `/** */`. (Returns self unchanged for
|
|
|
|
/// non-sugared doc attributes.)
|
2016-08-23 03:39:04 +00:00
|
|
|
pub fn with_desugared_doc<T, F>(&self, f: F) -> T where
|
2014-12-08 13:28:32 -05:00
|
|
|
F: FnOnce(&Attribute) -> T,
|
|
|
|
{
|
2016-11-14 12:00:25 +00:00
|
|
|
if self.is_sugared_doc {
|
2013-08-04 01:59:24 +02:00
|
|
|
let comment = self.value_str().unwrap();
|
2014-01-10 14:02:36 -08:00
|
|
|
let meta = mk_name_value_item_str(
|
2018-03-24 21:17:27 +03:00
|
|
|
Ident::from_str("doc"),
|
|
|
|
dummy_spanned(Symbol::intern(&strip_doc_comment_decoration(&comment.as_str()))));
|
2017-09-23 11:58:06 -05:00
|
|
|
let mut attr = if self.style == ast::AttrStyle::Outer {
|
|
|
|
mk_attr_outer(self.span, self.id, meta)
|
2014-05-20 21:25:42 -07:00
|
|
|
} else {
|
2017-09-23 11:58:06 -05:00
|
|
|
mk_attr_inner(self.span, self.id, meta)
|
|
|
|
};
|
|
|
|
attr.is_sugared_doc = true;
|
|
|
|
f(&attr)
|
2013-07-19 21:51:37 +10:00
|
|
|
} else {
|
2014-09-13 19:06:01 +03:00
|
|
|
f(self)
|
2013-07-19 21:51:37 +10: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
|
|
|
|
2018-03-24 21:17:27 +03:00
|
|
|
pub fn mk_name_value_item_str(ident: Ident, value: Spanned<Symbol>) -> MetaItem {
|
|
|
|
let value = respan(value.span, LitKind::Str(value.node, ast::StrStyle::Cooked));
|
|
|
|
mk_name_value_item(ident.span.to(value.span), ident, value)
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
2011-06-28 12:53:59 -07:00
|
|
|
|
2018-03-24 21:17:27 +03:00
|
|
|
pub fn mk_name_value_item(span: Span, ident: Ident, value: ast::Lit) -> MetaItem {
|
2018-04-24 16:57:41 +02:00
|
|
|
MetaItem { ident: Path::from_ident(ident), span, node: MetaItemKind::NameValue(value) }
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
|
|
|
|
2018-03-24 21:17:27 +03:00
|
|
|
pub fn mk_list_item(span: Span, ident: Ident, items: Vec<NestedMetaItem>) -> MetaItem {
|
2018-04-24 16:57:41 +02:00
|
|
|
MetaItem { ident: Path::from_ident(ident), span, node: 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 {
|
2018-04-24 16:57:41 +02:00
|
|
|
MetaItem { ident: Path::from_ident(ident), span: ident.span, node: 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 {
|
|
|
|
respan(ident.span, NestedMetaItemKind::MetaItem(mk_word_item(ident)))
|
2016-07-15 13:13:17 -07:00
|
|
|
}
|
|
|
|
|
2017-12-03 14:03:28 +01:00
|
|
|
pub fn mk_attr_id() -> AttrId {
|
|
|
|
use std::sync::atomic::AtomicUsize;
|
|
|
|
use std::sync::atomic::Ordering;
|
2016-07-15 13:13:17 -07:00
|
|
|
|
2017-12-03 14:03:28 +01:00
|
|
|
static NEXT_ATTR_ID: AtomicUsize = AtomicUsize::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);
|
|
|
|
assert!(id != ::std::usize::MAX);
|
2014-05-20 00:07:24 -07:00
|
|
|
AttrId(id)
|
|
|
|
}
|
|
|
|
|
2014-05-20 21:25:42 -07:00
|
|
|
/// Returns an inner attribute with the given value.
|
2017-01-17 23:54:51 +01:00
|
|
|
pub fn mk_attr_inner(span: Span, id: AttrId, item: MetaItem) -> Attribute {
|
|
|
|
mk_spanned_attr_inner(span, id, item)
|
2011-06-28 12:53:59 -07:00
|
|
|
}
|
|
|
|
|
2017-08-11 00:16:18 +02:00
|
|
|
/// Returns an inner attribute with the given value and span.
|
2016-11-15 10:17:24 +00:00
|
|
|
pub fn mk_spanned_attr_inner(sp: Span, id: AttrId, item: MetaItem) -> Attribute {
|
2016-11-14 12:00:25 +00:00
|
|
|
Attribute {
|
2017-08-06 22:54:09 -07:00
|
|
|
id,
|
2016-11-14 12:00:25 +00:00
|
|
|
style: ast::AttrStyle::Inner,
|
2018-04-17 15:33:39 +02:00
|
|
|
path: item.ident,
|
2017-03-03 09:23:59 +00:00
|
|
|
tokens: item.node.tokens(item.span),
|
2016-11-14 12:00:25 +00:00
|
|
|
is_sugared_doc: false,
|
|
|
|
span: sp,
|
|
|
|
}
|
2016-07-15 13:13:17 -07:00
|
|
|
}
|
|
|
|
|
2014-05-20 21:25:42 -07:00
|
|
|
/// Returns an outer attribute with the given value.
|
2017-01-17 23:54:51 +01:00
|
|
|
pub fn mk_attr_outer(span: Span, id: AttrId, item: MetaItem) -> Attribute {
|
|
|
|
mk_spanned_attr_outer(span, id, item)
|
2016-07-15 13:13:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an outer attribute with the given value and span.
|
2016-11-15 10:17:24 +00:00
|
|
|
pub fn mk_spanned_attr_outer(sp: Span, id: AttrId, item: MetaItem) -> Attribute {
|
2016-11-14 12:00:25 +00:00
|
|
|
Attribute {
|
2017-08-06 22:54:09 -07:00
|
|
|
id,
|
2016-11-14 12:00:25 +00:00
|
|
|
style: ast::AttrStyle::Outer,
|
2018-04-17 15:33:39 +02:00
|
|
|
path: item.ident,
|
2017-03-03 09:23:59 +00:00
|
|
|
tokens: item.node.tokens(item.span),
|
2016-11-14 12:00:25 +00:00
|
|
|
is_sugared_doc: false,
|
|
|
|
span: sp,
|
|
|
|
}
|
2016-07-15 13:13:17 -07:00
|
|
|
}
|
|
|
|
|
2017-03-15 00:22:48 +00:00
|
|
|
pub fn mk_sugared_doc_attr(id: AttrId, text: Symbol, span: Span) -> Attribute {
|
2016-11-16 10:52:37 +00:00
|
|
|
let style = doc_comment_style(&text.as_str());
|
2017-03-15 00:22:48 +00:00
|
|
|
let lit = respan(span, LitKind::Str(text, ast::StrStyle::Cooked));
|
2016-11-14 12:00:25 +00:00
|
|
|
Attribute {
|
2017-08-06 22:54:09 -07:00
|
|
|
id,
|
|
|
|
style,
|
2018-04-24 16:57:41 +02:00
|
|
|
path: Path::from_ident(Ident::from_str("doc").with_span_pos(span)),
|
2017-03-15 00:22:48 +00:00
|
|
|
tokens: MetaItemKind::NameValue(lit).tokens(span),
|
2016-11-14 12:00:25 +00:00
|
|
|
is_sugared_doc: true,
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2016-11-14 12:00:25 +00:00
|
|
|
}
|
2011-07-13 18:13:19 -07:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:21:17 +00:00
|
|
|
pub fn list_contains_name(items: &[NestedMetaItem], name: &str) -> bool {
|
2016-08-19 18:58:14 -07:00
|
|
|
items.iter().any(|item| {
|
|
|
|
item.check_name(name)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:54:53 +00:00
|
|
|
pub fn contains_name(attrs: &[Attribute], name: &str) -> bool {
|
|
|
|
attrs.iter().any(|item| {
|
2014-05-21 00:05:45 -07:00
|
|
|
item.check_name(name)
|
2013-11-20 16:23:04 -08:00
|
|
|
})
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
|
|
|
|
2017-08-26 18:00:33 -07:00
|
|
|
pub fn find_by_name<'a>(attrs: &'a [Attribute], name: &str) -> Option<&'a Attribute> {
|
|
|
|
attrs.iter().find(|attr| attr.check_name(name))
|
|
|
|
}
|
|
|
|
|
2018-10-31 02:54:56 +11:00
|
|
|
pub fn filter_by_name<'a>(attrs: &'a [Attribute], name: &'a str)
|
|
|
|
-> impl Iterator<Item = &'a Attribute> {
|
|
|
|
attrs.iter().filter(move |attr| attr.check_name(name))
|
|
|
|
}
|
|
|
|
|
2016-11-16 10:52:37 +00:00
|
|
|
pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: &str) -> Option<Symbol> {
|
2013-07-19 21:51:37 +10:00
|
|
|
attrs.iter()
|
2014-05-21 00:05:45 -07:00
|
|
|
.find(|at| at.check_name(name))
|
2013-09-11 12:52:17 -07:00
|
|
|
.and_then(|at| at.value_str())
|
2012-04-15 01:07:47 -07:00
|
|
|
}
|
|
|
|
|
2017-03-03 09:23:59 +00:00
|
|
|
impl MetaItem {
|
|
|
|
fn tokens(&self) -> TokenStream {
|
2018-01-30 14:30:39 +09:00
|
|
|
let mut idents = vec![];
|
|
|
|
let mut last_pos = BytePos(0 as u32);
|
2018-04-17 15:33:39 +02:00
|
|
|
for (i, segment) in self.ident.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 = Span::new(last_pos,
|
|
|
|
segment.ident.span.lo(),
|
|
|
|
segment.ident.span.ctxt());
|
2018-01-30 14:30:39 +09:00
|
|
|
idents.push(TokenTree::Token(mod_sep_span, Token::ModSep).into());
|
|
|
|
}
|
2018-04-17 15:33:39 +02:00
|
|
|
idents.push(TokenTree::Token(segment.ident.span,
|
|
|
|
Token::from_ast_ident(segment.ident)).into());
|
|
|
|
last_pos = segment.ident.span.hi();
|
2018-01-30 14:30:39 +09:00
|
|
|
}
|
2018-12-19 14:53:52 +11:00
|
|
|
self.node.tokens(self.span).append_to_tree_and_joint_vec(&mut idents);
|
2018-12-12 10:01:08 +11:00
|
|
|
TokenStream::new(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`.
|
2018-04-17 15:33:39 +02:00
|
|
|
let ident = match tokens.next() {
|
2019-01-02 02:21:05 +03:00
|
|
|
Some(TokenTree::Token(span, token @ Token::Ident(..))) |
|
|
|
|
Some(TokenTree::Token(span, token @ Token::ModSep)) => 'arm: {
|
|
|
|
let mut segments = if let Token::Ident(ident, _) = token {
|
|
|
|
if let Some(TokenTree::Token(_, Token::ModSep)) = tokens.peek() {
|
|
|
|
tokens.next();
|
|
|
|
vec![PathSegment::from_ident(ident.with_span_pos(span))]
|
|
|
|
} else {
|
|
|
|
break 'arm Path::from_ident(ident.with_span_pos(span));
|
2018-01-30 14:30:39 +09:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-02 02:21:05 +03:00
|
|
|
vec![PathSegment::path_root(span)]
|
|
|
|
};
|
|
|
|
loop {
|
|
|
|
if let Some(TokenTree::Token(span,
|
|
|
|
Token::Ident(ident, _))) = tokens.next() {
|
|
|
|
segments.push(PathSegment::from_ident(ident.with_span_pos(span)));
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
if let Some(TokenTree::Token(_, Token::ModSep)) = tokens.peek() {
|
|
|
|
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());
|
|
|
|
Path { span, segments }
|
2018-01-30 14:30:39 +09:00
|
|
|
}
|
2017-03-29 01:55:01 +00:00
|
|
|
Some(TokenTree::Token(_, Token::Interpolated(ref nt))) => match nt.0 {
|
2018-04-24 16:57:41 +02:00
|
|
|
token::Nonterminal::NtIdent(ident, _) => Path::from_ident(ident),
|
2017-03-29 07:17:18 +00:00
|
|
|
token::Nonterminal::NtMeta(ref meta) => return Some(meta.clone()),
|
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());
|
2017-12-08 17:32:04 -08:00
|
|
|
let node = MetaItemKind::from_tokens(tokens)?;
|
2017-07-31 23:04:34 +03:00
|
|
|
let hi = match node {
|
|
|
|
MetaItemKind::NameValue(ref lit) => lit.span.hi(),
|
2018-04-17 15:33:39 +02:00
|
|
|
MetaItemKind::List(..) => list_closing_paren_pos.unwrap_or(ident.span.hi()),
|
|
|
|
_ => ident.span.hi(),
|
2017-08-17 21:58:01 +09:00
|
|
|
};
|
2018-04-17 15:33:39 +02:00
|
|
|
let span = ident.span.with_hi(hi);
|
|
|
|
Some(MetaItem { ident, node, span })
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MetaItemKind {
|
|
|
|
pub fn tokens(&self, span: Span) -> TokenStream {
|
|
|
|
match *self {
|
|
|
|
MetaItemKind::Word => TokenStream::empty(),
|
|
|
|
MetaItemKind::NameValue(ref lit) => {
|
2018-12-19 14:53:52 +11:00
|
|
|
let mut vec = vec![TokenTree::Token(span, Token::Eq).into()];
|
|
|
|
lit.tokens().append_to_tree_and_joint_vec(&mut vec);
|
|
|
|
TokenStream::new(vec)
|
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 {
|
|
|
|
tokens.push(TokenTree::Token(span, Token::Comma).into());
|
|
|
|
}
|
2018-12-19 14:53:52 +11:00
|
|
|
item.node.tokens().append_to_tree_and_joint_vec(&mut tokens);
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
2018-11-30 10:02:04 +11:00
|
|
|
TokenTree::Delimited(
|
|
|
|
DelimSpan::from_single(span),
|
|
|
|
token::Paren,
|
2018-12-12 10:01:08 +11:00
|
|
|
TokenStream::new(tokens).into(),
|
2018-11-30 10:02:04 +11:00
|
|
|
).into()
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<MetaItemKind>
|
|
|
|
where I: Iterator<Item = TokenTree>,
|
|
|
|
{
|
|
|
|
let delimited = match tokens.peek().cloned() {
|
|
|
|
Some(TokenTree::Token(_, token::Eq)) => {
|
|
|
|
tokens.next();
|
|
|
|
return if let Some(TokenTree::Token(span, token)) = tokens.next() {
|
|
|
|
LitKind::from_token(token)
|
|
|
|
.map(|lit| MetaItemKind::NameValue(Spanned { node: lit, span: span }))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
}
|
2018-11-30 10:02:04 +11:00
|
|
|
Some(TokenTree::Delimited(_, delim, ref tts)) if delim == token::Paren => {
|
2017-03-03 09:23:59 +00:00
|
|
|
tokens.next();
|
2019-01-09 16:53:14 +11:00
|
|
|
tts.clone()
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
_ => return Some(MetaItemKind::Word),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut tokens = delimited.into_trees().peekable();
|
|
|
|
let mut result = Vec::new();
|
|
|
|
while let Some(..) = tokens.peek() {
|
2017-12-08 17:32:04 -08:00
|
|
|
let item = NestedMetaItemKind::from_tokens(&mut tokens)?;
|
|
|
|
result.push(respan(item.span(), item));
|
2017-03-03 09:23:59 +00:00
|
|
|
match tokens.next() {
|
|
|
|
None | Some(TokenTree::Token(_, Token::Comma)) => {}
|
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(MetaItemKind::List(result))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NestedMetaItemKind {
|
|
|
|
fn span(&self) -> Span {
|
|
|
|
match *self {
|
|
|
|
NestedMetaItemKind::MetaItem(ref item) => item.span,
|
|
|
|
NestedMetaItemKind::Literal(ref lit) => lit.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tokens(&self) -> TokenStream {
|
|
|
|
match *self {
|
|
|
|
NestedMetaItemKind::MetaItem(ref item) => item.tokens(),
|
|
|
|
NestedMetaItemKind::Literal(ref lit) => lit.tokens(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<NestedMetaItemKind>
|
|
|
|
where I: Iterator<Item = TokenTree>,
|
|
|
|
{
|
|
|
|
if let Some(TokenTree::Token(span, token)) = tokens.peek().cloned() {
|
|
|
|
if let Some(node) = LitKind::from_token(token) {
|
|
|
|
tokens.next();
|
2017-08-17 21:24:08 +09:00
|
|
|
return Some(NestedMetaItemKind::Literal(respan(span, node)));
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MetaItem::from_tokens(tokens).map(NestedMetaItemKind::MetaItem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Lit {
|
2018-08-12 20:15:59 +03:00
|
|
|
crate fn tokens(&self) -> TokenStream {
|
2017-03-03 09:23:59 +00:00
|
|
|
TokenTree::Token(self.span, self.node.token()).into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LitKind {
|
|
|
|
fn token(&self) -> Token {
|
|
|
|
use std::ascii;
|
|
|
|
|
|
|
|
match *self {
|
|
|
|
LitKind::Str(string, ast::StrStyle::Cooked) => {
|
2018-05-02 11:50:21 +10:00
|
|
|
let escaped = string.as_str().escape_default();
|
2017-03-03 09:23:59 +00:00
|
|
|
Token::Literal(token::Lit::Str_(Symbol::intern(&escaped)), None)
|
|
|
|
}
|
|
|
|
LitKind::Str(string, ast::StrStyle::Raw(n)) => {
|
|
|
|
Token::Literal(token::Lit::StrRaw(string, n), None)
|
|
|
|
}
|
|
|
|
LitKind::ByteStr(ref bytes) => {
|
|
|
|
let string = bytes.iter().cloned().flat_map(ascii::escape_default)
|
|
|
|
.map(Into::<char>::into).collect::<String>();
|
|
|
|
Token::Literal(token::Lit::ByteStr(Symbol::intern(&string)), None)
|
|
|
|
}
|
|
|
|
LitKind::Byte(byte) => {
|
|
|
|
let string: String = ascii::escape_default(byte).map(Into::<char>::into).collect();
|
|
|
|
Token::Literal(token::Lit::Byte(Symbol::intern(&string)), None)
|
|
|
|
}
|
|
|
|
LitKind::Char(ch) => {
|
|
|
|
let string: String = ch.escape_default().map(Into::<char>::into).collect();
|
|
|
|
Token::Literal(token::Lit::Char(Symbol::intern(&string)), None)
|
|
|
|
}
|
|
|
|
LitKind::Int(n, ty) => {
|
|
|
|
let suffix = match ty {
|
|
|
|
ast::LitIntType::Unsigned(ty) => Some(Symbol::intern(ty.ty_to_string())),
|
|
|
|
ast::LitIntType::Signed(ty) => Some(Symbol::intern(ty.ty_to_string())),
|
|
|
|
ast::LitIntType::Unsuffixed => None,
|
|
|
|
};
|
|
|
|
Token::Literal(token::Lit::Integer(Symbol::intern(&n.to_string())), suffix)
|
|
|
|
}
|
|
|
|
LitKind::Float(symbol, ty) => {
|
|
|
|
Token::Literal(token::Lit::Float(symbol), Some(Symbol::intern(ty.ty_to_string())))
|
|
|
|
}
|
|
|
|
LitKind::FloatUnsuffixed(symbol) => Token::Literal(token::Lit::Float(symbol), None),
|
2017-05-12 20:05:39 +02:00
|
|
|
LitKind::Bool(value) => Token::Ident(Ident::with_empty_ctxt(Symbol::intern(if value {
|
|
|
|
"true"
|
|
|
|
} else {
|
|
|
|
"false"
|
2018-03-09 23:56:40 -06:00
|
|
|
})), false),
|
2019-01-20 14:51:54 +09:00
|
|
|
LitKind::Err(val) => Token::Literal(token::Lit::Err(val), None),
|
2017-03-03 09:23:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_token(token: Token) -> Option<LitKind> {
|
|
|
|
match token {
|
2018-03-09 23:56:40 -06:00
|
|
|
Token::Ident(ident, false) if ident.name == "true" => Some(LitKind::Bool(true)),
|
|
|
|
Token::Ident(ident, false) if ident.name == "false" => Some(LitKind::Bool(false)),
|
2017-03-29 01:55:01 +00:00
|
|
|
Token::Interpolated(ref nt) => match nt.0 {
|
2018-04-10 02:08:47 +03:00
|
|
|
token::NtExpr(ref v) | token::NtLiteral(ref v) => match v.node {
|
2017-03-08 23:13:35 +00:00
|
|
|
ExprKind::Lit(ref lit) => Some(lit.node.clone()),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
},
|
2017-03-03 09:23:59 +00:00
|
|
|
Token::Literal(lit, suf) => {
|
|
|
|
let (suffix_illegal, result) = parse::lit_token(lit, suf, None);
|
|
|
|
if suffix_illegal && suf.is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 07:25:44 +00:00
|
|
|
pub trait HasAttrs: Sized {
|
|
|
|
fn attrs(&self) -> &[ast::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
|
|
|
fn visit_attrs<F: FnOnce(&mut Vec<ast::Attribute>)>(&mut self, f: F);
|
2016-05-18 07:25:44 +00:00
|
|
|
}
|
|
|
|
|
2017-01-03 19:13:01 -08:00
|
|
|
impl<T: HasAttrs> HasAttrs for Spanned<T> {
|
|
|
|
fn attrs(&self) -> &[ast::Attribute] { self.node.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
|
|
|
fn visit_attrs<F: FnOnce(&mut Vec<ast::Attribute>)>(&mut self, f: F) {
|
|
|
|
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
|
|
|
}
|
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 visit_attrs<F: FnOnce(&mut Vec<Attribute>)>(&mut self, f: F) {
|
2016-05-18 07:25:44 +00:00
|
|
|
f(self)
|
2015-11-03 17:39:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-18 04:01:57 +00:00
|
|
|
impl HasAttrs for ThinVec<Attribute> {
|
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
|
|
|
}
|
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 visit_attrs<F: FnOnce(&mut Vec<Attribute>)>(&mut self, f: F) {
|
|
|
|
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()
|
|
|
|
}
|
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 visit_attrs<F: FnOnce(&mut Vec<Attribute>)>(&mut self, f: F) {
|
|
|
|
(**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(),
|
2016-06-14 06:48:24 +00:00
|
|
|
StmtKind::Item(..) => &[],
|
2016-06-17 02:30:01 +00:00
|
|
|
StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => expr.attrs(),
|
|
|
|
StmtKind::Mac(ref mac) => {
|
|
|
|
let (_, _, ref attrs) = **mac;
|
|
|
|
attrs.attrs()
|
|
|
|
}
|
2016-05-18 07:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 visit_attrs<F: FnOnce(&mut Vec<Attribute>)>(&mut self, f: F) {
|
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),
|
|
|
|
StmtKind::Item(..) => {}
|
|
|
|
StmtKind::Expr(expr) => expr.visit_attrs(f),
|
|
|
|
StmtKind::Semi(expr) => expr.visit_attrs(f),
|
|
|
|
StmtKind::Mac(mac) => {
|
|
|
|
let (_mac, _style, attrs) = mac.deref_mut();
|
|
|
|
attrs.visit_attrs(f);
|
|
|
|
}
|
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] {
|
|
|
|
self.node.attrs()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_attrs<F: FnOnce(&mut Vec<ast::Attribute>)>(&mut self, f: F) {
|
|
|
|
self.node.visit_attrs(f);
|
2017-01-03 19:13:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-02 05:10:29 +08:00
|
|
|
impl HasAttrs for GenericParam {
|
|
|
|
fn attrs(&self) -> &[ast::Attribute] {
|
2018-06-13 13:29:40 +01:00
|
|
|
&self.attrs
|
2018-06-02 05:10:29 +08:00
|
|
|
}
|
|
|
|
|
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 visit_attrs<F: FnOnce(&mut Vec<Attribute>)>(&mut self, f: F) {
|
|
|
|
self.attrs.visit_attrs(f);
|
2018-06-02 05:10:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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 visit_attrs<F: FnOnce(&mut Vec<Attribute>)>(&mut self, f: F) {
|
|
|
|
self.attrs.visit_attrs(f);
|
2016-05-18 07:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)* }
|
|
|
|
}
|
|
|
|
|
2017-01-03 19:13:01 -08:00
|
|
|
derive_has_attrs! {
|
|
|
|
Item, Expr, Local, ast::ForeignItem, ast::StructField, ast::ImplItem, ast::TraitItem, ast::Arm,
|
2018-06-13 13:29:40 +01:00
|
|
|
ast::Field, ast::FieldPat, ast::Variant_
|
2016-05-18 07:25:44 +00:00
|
|
|
}
|
2018-07-18 20:34:08 +02:00
|
|
|
|
|
|
|
pub fn inject(mut krate: ast::Crate, parse_sess: &ParseSess, attrs: &[String]) -> ast::Crate {
|
|
|
|
for raw_attr in attrs {
|
|
|
|
let mut parser = parse::new_parser_from_source_str(
|
|
|
|
parse_sess,
|
2018-10-30 10:11:24 -04:00
|
|
|
FileName::cli_crate_attr_source_code(&raw_attr),
|
2018-07-18 20:34:08 +02:00
|
|
|
raw_attr.clone(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let start_span = parser.span;
|
2018-08-12 20:15:59 +03:00
|
|
|
let (path, tokens) = panictry!(parser.parse_meta_item_unrestricted());
|
2018-07-18 20:34:08 +02:00
|
|
|
let end_span = parser.span;
|
|
|
|
if parser.token != token::Eof {
|
|
|
|
parse_sess.span_diagnostic
|
|
|
|
.span_err(start_span.to(end_span), "invalid crate attribute");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
krate.attrs.push(Attribute {
|
|
|
|
id: mk_attr_id(),
|
|
|
|
style: AttrStyle::Inner,
|
|
|
|
path,
|
|
|
|
tokens,
|
|
|
|
is_sugared_doc: false,
|
|
|
|
span: start_span.to(end_span),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
krate
|
|
|
|
}
|