2019-02-07 02:33:01 +09:00
|
|
|
use crate::attr::HasAttrs;
|
|
|
|
use crate::feature_gate::{
|
2018-10-04 04:55:47 -07:00
|
|
|
feature_err,
|
|
|
|
EXPLAIN_STMT_ATTR_SYNTAX,
|
|
|
|
Features,
|
|
|
|
get_features,
|
|
|
|
GateIssue,
|
|
|
|
};
|
2019-02-07 02:33:01 +09:00
|
|
|
use crate::attr;
|
|
|
|
use crate::ast;
|
|
|
|
use crate::edition::Edition;
|
|
|
|
use crate::mut_visit::*;
|
|
|
|
use crate::parse::{token, ParseSess};
|
|
|
|
use crate::ptr::P;
|
|
|
|
use crate::util::map_in_place::MapInPlace;
|
|
|
|
|
2019-02-09 11:24:02 +09:00
|
|
|
use errors::Applicability;
|
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 smallvec::SmallVec;
|
2014-11-04 14:59:42 -08:00
|
|
|
|
2016-06-01 02:13:45 +00:00
|
|
|
/// A folder that strips out items that do not belong in the current configuration.
|
2016-05-15 09:22:58 +00:00
|
|
|
pub struct StripUnconfigured<'a> {
|
2016-06-11 01:37:24 +00:00
|
|
|
pub sess: &'a ParseSess,
|
|
|
|
pub features: Option<&'a Features>,
|
2013-02-19 02:40:42 -05:00
|
|
|
}
|
2011-06-29 22:32:08 -07:00
|
|
|
|
2016-08-31 23:39:16 +00:00
|
|
|
// `cfg_attr`-process the crate's attributes and compute the crate's features.
|
2019-03-13 16:29:24 -07:00
|
|
|
pub fn features(mut krate: ast::Crate, sess: &ParseSess, edition: Edition,
|
|
|
|
allow_features: &Option<Vec<String>>) -> (ast::Crate, Features) {
|
2016-08-31 23:39:16 +00:00
|
|
|
let features;
|
|
|
|
{
|
|
|
|
let mut strip_unconfigured = StripUnconfigured {
|
2017-08-06 22:54:09 -07:00
|
|
|
sess,
|
2016-08-31 23:39:16 +00:00
|
|
|
features: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let unconfigured_attrs = krate.attrs.clone();
|
|
|
|
let err_count = sess.span_diagnostic.err_count();
|
|
|
|
if let Some(attrs) = strip_unconfigured.configure(krate.attrs) {
|
|
|
|
krate.attrs = attrs;
|
|
|
|
} else { // the entire crate is unconfigured
|
|
|
|
krate.attrs = Vec::new();
|
|
|
|
krate.module.items = Vec::new();
|
|
|
|
return (krate, Features::new());
|
|
|
|
}
|
|
|
|
|
2019-03-13 16:29:24 -07:00
|
|
|
features = get_features(&sess.span_diagnostic, &krate.attrs, edition, allow_features);
|
2016-08-31 23:39:16 +00:00
|
|
|
|
|
|
|
// Avoid reconfiguring malformed `cfg_attr`s
|
|
|
|
if err_count == sess.span_diagnostic.err_count() {
|
|
|
|
strip_unconfigured.features = Some(&features);
|
|
|
|
strip_unconfigured.configure(unconfigured_attrs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(krate, features)
|
|
|
|
}
|
|
|
|
|
2016-09-02 01:44:23 +00:00
|
|
|
macro_rules! configure {
|
|
|
|
($this:ident, $node:ident) => {
|
|
|
|
match $this.configure($node) {
|
|
|
|
Some(node) => node,
|
|
|
|
None => return Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 04:42:57 +00:00
|
|
|
impl<'a> StripUnconfigured<'a> {
|
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
|
|
|
pub fn configure<T: HasAttrs>(&mut self, mut node: T) -> Option<T> {
|
|
|
|
self.process_cfg_attrs(&mut node);
|
2016-06-01 02:13:45 +00:00
|
|
|
if self.in_cfg(node.attrs()) { Some(node) } else { None }
|
|
|
|
}
|
|
|
|
|
2018-09-10 15:06:49 -07:00
|
|
|
/// Parse and expand all `cfg_attr` attributes into a list of attributes
|
|
|
|
/// that are within each `cfg_attr` that has a true configuration predicate.
|
|
|
|
///
|
|
|
|
/// Gives compiler warnigns if any `cfg_attr` does not contain any
|
|
|
|
/// attributes and is in the original source code. Gives compiler errors if
|
|
|
|
/// the syntax of any `cfg_attr` is incorrect.
|
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
|
|
|
pub fn process_cfg_attrs<T: HasAttrs>(&mut self, node: &mut T) {
|
|
|
|
node.visit_attrs(|attrs| {
|
|
|
|
attrs.flat_map_in_place(|attr| self.process_cfg_attr(attr));
|
|
|
|
});
|
2016-06-01 02:13:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 15:06:49 -07:00
|
|
|
/// Parse and expand a single `cfg_attr` attribute into a list of attributes
|
|
|
|
/// when the configuration predicate is true, or otherwise expand into an
|
|
|
|
/// empty list of attributes.
|
|
|
|
///
|
2018-10-22 18:21:55 +02:00
|
|
|
/// Gives a compiler warning when the `cfg_attr` contains no attributes and
|
2018-09-10 15:06:49 -07:00
|
|
|
/// is in the original source file. Gives a compiler error if the syntax of
|
2019-02-08 14:53:55 +01:00
|
|
|
/// the attribute is incorrect.
|
2018-09-10 15:06:49 -07:00
|
|
|
fn process_cfg_attr(&mut self, attr: ast::Attribute) -> Vec<ast::Attribute> {
|
2016-05-16 04:42:57 +00:00
|
|
|
if !attr.check_name("cfg_attr") {
|
2018-09-10 15:06:49 -07:00
|
|
|
return vec![attr];
|
2016-05-16 04:42:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 15:06:49 -07:00
|
|
|
let (cfg_predicate, expanded_attrs) = match attr.parse(self.sess, |parser| {
|
2017-03-08 23:13:35 +00:00
|
|
|
parser.expect(&token::OpenDelim(token::Paren))?;
|
2018-09-10 15:06:49 -07:00
|
|
|
|
|
|
|
let cfg_predicate = parser.parse_meta_item()?;
|
2017-03-08 23:13:35 +00:00
|
|
|
parser.expect(&token::Comma)?;
|
2018-09-10 15:06:49 -07:00
|
|
|
|
|
|
|
// Presumably, the majority of the time there will only be one attr.
|
|
|
|
let mut expanded_attrs = Vec::with_capacity(1);
|
|
|
|
|
|
|
|
while !parser.check(&token::CloseDelim(token::Paren)) {
|
|
|
|
let lo = parser.span.lo();
|
|
|
|
let (path, tokens) = parser.parse_meta_item_unrestricted()?;
|
|
|
|
expanded_attrs.push((path, tokens, parser.prev_span.with_lo(lo)));
|
|
|
|
parser.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Paren)])?;
|
|
|
|
}
|
|
|
|
|
2017-03-08 23:13:35 +00:00
|
|
|
parser.expect(&token::CloseDelim(token::Paren))?;
|
2018-09-10 15:06:49 -07:00
|
|
|
Ok((cfg_predicate, expanded_attrs))
|
2017-03-08 23:13:35 +00:00
|
|
|
}) {
|
|
|
|
Ok(result) => result,
|
|
|
|
Err(mut e) => {
|
|
|
|
e.emit();
|
2018-09-10 15:06:49 -07:00
|
|
|
return Vec::new();
|
2016-05-16 04:42:57 +00:00
|
|
|
}
|
|
|
|
};
|
2016-08-19 18:58:14 -07:00
|
|
|
|
2018-10-04 04:55:47 -07:00
|
|
|
// Check feature gate and lint on zero attributes in source. Even if the feature is gated,
|
2018-10-22 18:21:55 +02:00
|
|
|
// we still compute as if it wasn't, since the emitted error will stop compilation further
|
2018-10-04 04:55:47 -07:00
|
|
|
// along the compilation.
|
2019-01-04 12:37:27 +05:30
|
|
|
if expanded_attrs.len() == 0 {
|
|
|
|
// FIXME: Emit unused attribute lint here.
|
2018-10-04 04:55:47 -07:00
|
|
|
}
|
|
|
|
|
2018-09-10 15:06:49 -07:00
|
|
|
if attr::cfg_matches(&cfg_predicate, self.sess, self.features) {
|
|
|
|
// We call `process_cfg_attr` recursively in case there's a
|
|
|
|
// `cfg_attr` inside of another `cfg_attr`. E.g.
|
|
|
|
// `#[cfg_attr(false, cfg_attr(true, some_attr))]`.
|
|
|
|
expanded_attrs.into_iter()
|
|
|
|
.flat_map(|(path, tokens, span)| self.process_cfg_attr(ast::Attribute {
|
2017-03-08 23:13:35 +00:00
|
|
|
id: attr::mk_attr_id(),
|
|
|
|
style: attr.style,
|
2017-08-06 22:54:09 -07:00
|
|
|
path,
|
|
|
|
tokens,
|
2017-03-08 23:13:35 +00:00
|
|
|
is_sugared_doc: false,
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2018-09-10 15:06:49 -07:00
|
|
|
}))
|
|
|
|
.collect()
|
2017-03-08 23:13:35 +00:00
|
|
|
} else {
|
2018-09-10 15:06:49 -07:00
|
|
|
Vec::new()
|
2016-05-16 02:42:24 +00:00
|
|
|
}
|
2016-05-15 09:15:02 +00:00
|
|
|
}
|
2016-05-16 04:42:57 +00:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Determines if a node with the given attributes should be included in this configuration.
|
2016-09-14 22:36:42 +00:00
|
|
|
pub fn in_cfg(&mut self, attrs: &[ast::Attribute]) -> bool {
|
2016-05-26 23:56:25 +00:00
|
|
|
attrs.iter().all(|attr| {
|
2018-09-02 00:13:22 +03:00
|
|
|
if !is_cfg(attr) {
|
2017-03-08 23:13:35 +00:00
|
|
|
return true;
|
2018-09-02 00:13:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let error = |span, msg, suggestion: &str| {
|
|
|
|
let mut err = self.sess.span_diagnostic.struct_span_err(span, msg);
|
|
|
|
if !suggestion.is_empty() {
|
2019-01-25 16:03:27 -05:00
|
|
|
err.span_suggestion(
|
2018-09-17 20:13:08 +03:00
|
|
|
span,
|
|
|
|
"expected syntax is",
|
|
|
|
suggestion.into(),
|
|
|
|
Applicability::MaybeIncorrect,
|
2018-09-17 12:52:08 +03:00
|
|
|
);
|
2018-09-02 00:13:22 +03:00
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
true
|
|
|
|
};
|
|
|
|
|
2019-01-02 02:21:05 +03:00
|
|
|
let meta_item = match attr.parse_meta(self.sess) {
|
|
|
|
Ok(meta_item) => meta_item,
|
|
|
|
Err(mut err) => { err.emit(); return true; }
|
2018-09-02 00:13:22 +03:00
|
|
|
};
|
|
|
|
let nested_meta_items = if let Some(nested_meta_items) = meta_item.meta_item_list() {
|
|
|
|
nested_meta_items
|
|
|
|
} else {
|
|
|
|
return error(meta_item.span, "`cfg` is not followed by parentheses",
|
|
|
|
"cfg(/* predicate */)");
|
2016-05-26 23:56:25 +00:00
|
|
|
};
|
|
|
|
|
2018-09-02 00:13:22 +03:00
|
|
|
if nested_meta_items.is_empty() {
|
|
|
|
return error(meta_item.span, "`cfg` predicate is not specified", "");
|
|
|
|
} else if nested_meta_items.len() > 1 {
|
2019-03-03 20:56:24 +03:00
|
|
|
return error(nested_meta_items.last().unwrap().span(),
|
2018-09-02 00:13:22 +03:00
|
|
|
"multiple `cfg` predicates are specified", "");
|
2016-05-26 23:56:25 +00:00
|
|
|
}
|
|
|
|
|
2018-09-02 00:13:22 +03:00
|
|
|
match nested_meta_items[0].meta_item() {
|
|
|
|
Some(meta_item) => attr::cfg_matches(meta_item, self.sess, self.features),
|
2019-03-03 20:56:24 +03:00
|
|
|
None => error(nested_meta_items[0].span(),
|
2018-09-02 00:13:22 +03:00
|
|
|
"`cfg` predicate key cannot be a literal", ""),
|
2016-08-19 18:58:14 -07:00
|
|
|
}
|
2016-05-26 23:56:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-10 15:06:49 -07:00
|
|
|
/// Visit attributes on expression and statements (but not attributes on items in blocks).
|
2016-10-06 16:44:59 +13:00
|
|
|
fn visit_expr_attrs(&mut self, attrs: &[ast::Attribute]) {
|
2016-05-16 04:42:57 +00:00
|
|
|
// flag the offending attributes
|
|
|
|
for attr in attrs.iter() {
|
2018-03-15 23:20:56 -07:00
|
|
|
self.maybe_emit_expr_attr_err(attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If attributes are not allowed on expressions, emit an error for `attr`
|
|
|
|
pub fn maybe_emit_expr_attr_err(&self, attr: &ast::Attribute) {
|
|
|
|
if !self.features.map(|features| features.stmt_expr_attributes).unwrap_or(true) {
|
|
|
|
let mut err = feature_err(self.sess,
|
|
|
|
"stmt_expr_attributes",
|
|
|
|
attr.span,
|
|
|
|
GateIssue::Language,
|
|
|
|
EXPLAIN_STMT_ATTR_SYNTAX);
|
|
|
|
|
|
|
|
if attr.is_sugared_doc {
|
|
|
|
err.help("`///` is for documentation comments. For a plain comment, use `//`.");
|
2016-06-11 01:37:24 +00:00
|
|
|
}
|
2018-03-15 23:20:56 -07:00
|
|
|
|
|
|
|
err.emit();
|
2016-05-15 09:15:02 +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
|
|
|
pub fn configure_foreign_mod(&mut self, foreign_mod: &mut ast::ForeignMod) {
|
|
|
|
let ast::ForeignMod { abi: _, items } = foreign_mod;
|
|
|
|
items.flat_map_in_place(|item| self.configure(item));
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
2016-05-15 02:34:32 +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 configure_variant_data(&mut self, vdata: &mut ast::VariantData) {
|
2016-09-02 01:44:23 +00:00
|
|
|
match vdata {
|
2019-03-21 23:38:50 +01:00
|
|
|
ast::VariantData::Struct(fields, ..) | ast::VariantData::Tuple(fields, _) =>
|
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
|
|
|
fields.flat_map_in_place(|field| self.configure(field)),
|
2019-03-21 23:38:50 +01:00
|
|
|
ast::VariantData::Unit(_) => {}
|
2016-09-02 01:44:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-15 02:34:32 +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
|
|
|
pub fn configure_item_kind(&mut self, item: &mut ast::ItemKind) {
|
2016-09-02 01:44:23 +00:00
|
|
|
match item {
|
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
|
|
|
ast::ItemKind::Struct(def, _generics) |
|
|
|
|
ast::ItemKind::Union(def, _generics) => self.configure_variant_data(def),
|
|
|
|
ast::ItemKind::Enum(ast::EnumDef { variants }, _generics) => {
|
|
|
|
variants.flat_map_in_place(|variant| self.configure(variant));
|
|
|
|
for variant in variants {
|
|
|
|
self.configure_variant_data(&mut variant.node.data);
|
|
|
|
}
|
2016-05-15 02:34:32 +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
|
|
|
_ => {}
|
2016-09-02 01:44:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-15 02:34:32 +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
|
|
|
pub fn configure_expr_kind(&mut self, expr_kind: &mut ast::ExprKind) {
|
2017-01-03 19:13:01 -08:00
|
|
|
match expr_kind {
|
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
|
|
|
ast::ExprKind::Match(_m, arms) => {
|
|
|
|
arms.flat_map_in_place(|arm| self.configure(arm));
|
2017-01-03 19:13:01 -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
|
|
|
ast::ExprKind::Struct(_path, fields, _base) => {
|
|
|
|
fields.flat_map_in_place(|field| self.configure(field));
|
2017-01-03 19:13:01 -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
|
|
|
_ => {}
|
2016-09-02 01:44:23 +00:00
|
|
|
}
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
2016-05-15 02:34:32 +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
|
|
|
pub fn configure_expr(&mut self, expr: &mut P<ast::Expr>) {
|
2016-10-06 16:44:59 +13:00
|
|
|
self.visit_expr_attrs(expr.attrs());
|
2016-06-11 01:37:24 +00:00
|
|
|
|
2015-11-03 17:39:51 +01:00
|
|
|
// If an expr is valid to cfg away it will have been removed by the
|
|
|
|
// outer stmt or expression folder before descending in here.
|
|
|
|
// Anything else is always required, and thus has to error out
|
|
|
|
// in case of a cfg attr.
|
|
|
|
//
|
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
|
|
|
// N.B., this is intentionally not part of the visit_expr() function
|
|
|
|
// in order for filter_map_expr() to be able to avoid this check
|
2018-09-02 09:03:24 -07:00
|
|
|
if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a)) {
|
2016-06-11 01:37:24 +00:00
|
|
|
let msg = "removing an expression is not supported in this position";
|
|
|
|
self.sess.span_diagnostic.span_err(attr.span, msg);
|
|
|
|
}
|
|
|
|
|
2016-09-02 01:44:23 +00:00
|
|
|
self.process_cfg_attrs(expr)
|
2014-04-22 21:54:48 -07:00
|
|
|
}
|
2016-05-15 02:34:32 +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
|
|
|
pub fn configure_pat(&mut self, pat: &mut P<ast::Pat>) {
|
|
|
|
if let ast::PatKind::Struct(_path, fields, _etc) = &mut pat.node {
|
|
|
|
fields.flat_map_in_place(|field| self.configure(field));
|
|
|
|
}
|
2017-01-03 19:13:01 -08:00
|
|
|
}
|
2018-06-02 05:10:29 +08:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Denies `#[cfg]` on generic parameters until we decide what to do with it.
|
|
|
|
/// See issue #51279.
|
2018-06-02 05:10:29 +08:00
|
|
|
pub fn disallow_cfg_on_generic_param(&mut self, param: &ast::GenericParam) {
|
|
|
|
for attr in param.attrs() {
|
|
|
|
let offending_attr = if attr.check_name("cfg") {
|
|
|
|
"cfg"
|
|
|
|
} else if attr.check_name("cfg_attr") {
|
|
|
|
"cfg_attr"
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
let msg = format!("#[{}] cannot be applied on a generic parameter", offending_attr);
|
|
|
|
self.sess.span_diagnostic.span_err(attr.span, &msg);
|
|
|
|
}
|
|
|
|
}
|
2016-09-02 01:44:23 +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
|
|
|
impl<'a> MutVisitor for StripUnconfigured<'a> {
|
|
|
|
fn visit_foreign_mod(&mut self, foreign_mod: &mut ast::ForeignMod) {
|
|
|
|
self.configure_foreign_mod(foreign_mod);
|
|
|
|
noop_visit_foreign_mod(foreign_mod, self);
|
2015-11-03 17:39:51 +01:00
|
|
|
}
|
2016-05-15 02:34:32 +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_item_kind(&mut self, item: &mut ast::ItemKind) {
|
|
|
|
self.configure_item_kind(item);
|
|
|
|
noop_visit_item_kind(item, self);
|
2015-11-03 17:39:51 +01:00
|
|
|
}
|
2016-05-15 02:34:32 +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_expr(&mut self, expr: &mut P<ast::Expr>) {
|
|
|
|
self.configure_expr(expr);
|
|
|
|
self.configure_expr_kind(&mut expr.node);
|
|
|
|
noop_visit_expr(expr, self);
|
2016-09-02 01:44:23 +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 filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
|
|
|
|
let mut expr = configure!(self, expr);
|
|
|
|
self.configure_expr_kind(&mut expr.node);
|
|
|
|
noop_visit_expr(&mut expr, self);
|
|
|
|
Some(expr)
|
2016-09-02 01:44:23 +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 flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
|
|
|
|
noop_flat_map_stmt(configure!(self, stmt), self)
|
2014-07-09 14:48:12 -07:00
|
|
|
}
|
2016-05-15 02:34:32 +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 flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
|
|
|
|
noop_flat_map_item(configure!(self, item), self)
|
2016-06-02 07:34:19 +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 flat_map_impl_item(&mut self, item: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> {
|
|
|
|
noop_flat_map_impl_item(configure!(self, item), self)
|
2016-06-02 07:34:19 +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 flat_map_trait_item(&mut self, item: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> {
|
|
|
|
noop_flat_map_trait_item(configure!(self, item), self)
|
2014-11-04 14:59:42 -08:00
|
|
|
}
|
2016-06-09 00:26:35 +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_mac(&mut self, _mac: &mut ast::Mac) {
|
2018-11-27 02:59:49 +00:00
|
|
|
// Don't configure interpolated AST (cf. issue #34171).
|
2016-06-09 00:26:35 +00:00
|
|
|
// Interpolated AST will get configured once the surrounding tokens are parsed.
|
|
|
|
}
|
2017-01-03 19:13:01 -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_pat(&mut self, pat: &mut P<ast::Pat>) {
|
|
|
|
self.configure_pat(pat);
|
|
|
|
noop_visit_pat(pat, self)
|
2017-01-03 19:13:01 -08:00
|
|
|
}
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
2011-06-29 22:32:08 -07:00
|
|
|
|
2015-11-03 17:39:51 +01:00
|
|
|
fn is_cfg(attr: &ast::Attribute) -> bool {
|
|
|
|
attr.check_name("cfg")
|
|
|
|
}
|