2020-03-08 21:32:25 +00:00
|
|
|
//! Conditional compilation stripping.
|
2019-11-20 02:35:11 +00:00
|
|
|
|
2020-11-18 22:43:23 +00:00
|
|
|
use crate::base::Annotatable;
|
|
|
|
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::mut_visit::*;
|
|
|
|
use rustc_ast::ptr::P;
|
2020-09-26 23:33:42 +00:00
|
|
|
use rustc_ast::token::{DelimToken, Token, TokenKind};
|
2020-10-30 21:40:41 +00:00
|
|
|
use rustc_ast::tokenstream::{DelimSpan, LazyTokenStream, Spacing, TokenStream, TokenTree};
|
2021-02-23 15:21:20 +00:00
|
|
|
use rustc_ast::{self as ast, AstLike, AttrItem, Attribute, MetaItem};
|
2020-01-11 12:15:20 +00:00
|
|
|
use rustc_attr as attr;
|
2020-01-02 11:33:56 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-04-18 04:01:54 +00:00
|
|
|
use rustc_data_structures::map_in_place::MapInPlace;
|
2020-01-02 11:33:56 +00:00
|
|
|
use rustc_errors::{error_code, struct_span_err, Applicability, Handler};
|
|
|
|
use rustc_feature::{Feature, Features, State as FeatureState};
|
|
|
|
use rustc_feature::{
|
|
|
|
ACCEPTED_FEATURES, ACTIVE_FEATURES, REMOVED_FEATURES, STABLE_REMOVED_FEATURES,
|
|
|
|
};
|
2020-03-08 21:32:25 +00:00
|
|
|
use rustc_parse::{parse_in, validate_attr};
|
2020-07-30 01:27:50 +00:00
|
|
|
use rustc_session::parse::feature_err;
|
|
|
|
use rustc_session::Session;
|
2020-01-02 11:33:56 +00:00
|
|
|
use rustc_span::edition::{Edition, ALL_EDITIONS};
|
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2019-02-06 17:33:01 +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 04:20:55 +00:00
|
|
|
use smallvec::SmallVec;
|
2014-11-04 22:59:42 +00: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> {
|
2020-07-30 01:27:50 +00:00
|
|
|
pub sess: &'a Session,
|
2016-06-11 01:37:24 +00:00
|
|
|
pub features: Option<&'a Features>,
|
2020-11-23 06:43:55 +00:00
|
|
|
pub modified: bool,
|
2013-02-19 07:40:42 +00:00
|
|
|
}
|
2011-06-30 05:32:08 +00:00
|
|
|
|
2020-01-02 11:33:56 +00:00
|
|
|
fn get_features(
|
2020-07-30 01:27:50 +00:00
|
|
|
sess: &Session,
|
2020-01-02 11:33:56 +00:00
|
|
|
span_handler: &Handler,
|
|
|
|
krate_attrs: &[ast::Attribute],
|
|
|
|
) -> Features {
|
|
|
|
fn feature_removed(span_handler: &Handler, span: Span, reason: Option<&str>) {
|
|
|
|
let mut err = struct_span_err!(span_handler, span, E0557, "feature has been removed");
|
|
|
|
err.span_label(span, "feature has been removed");
|
|
|
|
if let Some(reason) = reason {
|
|
|
|
err.note(reason);
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn active_features_up_to(edition: Edition) -> impl Iterator<Item = &'static Feature> {
|
|
|
|
ACTIVE_FEATURES.iter().filter(move |feature| {
|
|
|
|
if let Some(feature_edition) = feature.edition {
|
|
|
|
feature_edition <= edition
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut features = Features::default();
|
|
|
|
let mut edition_enabled_features = FxHashMap::default();
|
2020-07-30 01:27:50 +00:00
|
|
|
let crate_edition = sess.edition();
|
2020-01-02 11:33:56 +00:00
|
|
|
|
|
|
|
for &edition in ALL_EDITIONS {
|
|
|
|
if edition <= crate_edition {
|
|
|
|
// The `crate_edition` implies its respective umbrella feature-gate
|
|
|
|
// (i.e., `#![feature(rust_20XX_preview)]` isn't needed on edition 20XX).
|
|
|
|
edition_enabled_features.insert(edition.feature_name(), edition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for feature in active_features_up_to(crate_edition) {
|
|
|
|
feature.set(&mut features, DUMMY_SP);
|
|
|
|
edition_enabled_features.insert(feature.name, crate_edition);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process the edition umbrella feature-gates first, to ensure
|
|
|
|
// `edition_enabled_features` is completed before it's queried.
|
|
|
|
for attr in krate_attrs {
|
2020-07-30 01:27:50 +00:00
|
|
|
if !sess.check_name(attr, sym::feature) {
|
2020-01-02 11:33:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let list = match attr.meta_item_list() {
|
|
|
|
Some(list) => list,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
for mi in list {
|
|
|
|
if !mi.is_word() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let name = mi.name_or_empty();
|
|
|
|
|
|
|
|
let edition = ALL_EDITIONS.iter().find(|e| name == e.feature_name()).copied();
|
|
|
|
if let Some(edition) = edition {
|
|
|
|
if edition <= crate_edition {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for feature in active_features_up_to(edition) {
|
|
|
|
// FIXME(Manishearth) there is currently no way to set
|
|
|
|
// lib features by edition
|
|
|
|
feature.set(&mut features, DUMMY_SP);
|
|
|
|
edition_enabled_features.insert(feature.name, edition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for attr in krate_attrs {
|
2020-07-30 01:27:50 +00:00
|
|
|
if !sess.check_name(attr, sym::feature) {
|
2020-01-02 11:33:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let list = match attr.meta_item_list() {
|
|
|
|
Some(list) => list,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
let bad_input = |span| {
|
|
|
|
struct_span_err!(span_handler, span, E0556, "malformed `feature` attribute input")
|
|
|
|
};
|
|
|
|
|
|
|
|
for mi in list {
|
|
|
|
let name = match mi.ident() {
|
|
|
|
Some(ident) if mi.is_word() => ident.name,
|
|
|
|
Some(ident) => {
|
|
|
|
bad_input(mi.span())
|
|
|
|
.span_suggestion(
|
|
|
|
mi.span(),
|
|
|
|
"expected just one word",
|
|
|
|
format!("{}", ident.name),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
bad_input(mi.span()).span_label(mi.span(), "expected just one word").emit();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(edition) = edition_enabled_features.get(&name) {
|
|
|
|
let msg =
|
|
|
|
&format!("the feature `{}` is included in the Rust {} edition", name, edition);
|
|
|
|
span_handler.struct_span_warn_with_code(mi.span(), msg, error_code!(E0705)).emit();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ALL_EDITIONS.iter().any(|e| name == e.feature_name()) {
|
|
|
|
// Handled in the separate loop above.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let removed = REMOVED_FEATURES.iter().find(|f| name == f.name);
|
|
|
|
let stable_removed = STABLE_REMOVED_FEATURES.iter().find(|f| name == f.name);
|
|
|
|
if let Some(Feature { state, .. }) = removed.or(stable_removed) {
|
|
|
|
if let FeatureState::Removed { reason } | FeatureState::Stabilized { reason } =
|
|
|
|
state
|
|
|
|
{
|
|
|
|
feature_removed(span_handler, mi.span(), *reason);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(Feature { since, .. }) = ACCEPTED_FEATURES.iter().find(|f| name == f.name) {
|
|
|
|
let since = Some(Symbol::intern(since));
|
|
|
|
features.declared_lang_features.push((name, mi.span(), since));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-30 01:27:50 +00:00
|
|
|
if let Some(allowed) = sess.opts.debugging_opts.allow_features.as_ref() {
|
2020-01-02 11:33:56 +00:00
|
|
|
if allowed.iter().find(|&f| name.as_str() == *f).is_none() {
|
|
|
|
struct_span_err!(
|
|
|
|
span_handler,
|
|
|
|
mi.span(),
|
|
|
|
E0725,
|
|
|
|
"the feature `{}` is not in the list of allowed features",
|
|
|
|
name
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(f) = ACTIVE_FEATURES.iter().find(|f| name == f.name) {
|
|
|
|
f.set(&mut features, mi.span());
|
|
|
|
features.declared_lang_features.push((name, mi.span(), None));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
features.declared_lib_features.push((name, mi.span()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
features
|
|
|
|
}
|
|
|
|
|
2016-08-31 23:39:16 +00:00
|
|
|
// `cfg_attr`-process the crate's attributes and compute the crate's features.
|
2020-07-30 01:27:50 +00:00
|
|
|
pub fn features(sess: &Session, mut krate: ast::Crate) -> (ast::Crate, Features) {
|
2020-11-23 06:43:55 +00:00
|
|
|
let mut strip_unconfigured = StripUnconfigured { sess, features: None, modified: false };
|
2019-12-31 06:42:19 +00:00
|
|
|
|
|
|
|
let unconfigured_attrs = krate.attrs.clone();
|
2020-07-30 01:27:50 +00:00
|
|
|
let diag = &sess.parse_sess.span_diagnostic;
|
2019-12-31 06:42:19 +00:00
|
|
|
let err_count = diag.err_count();
|
2021-02-23 15:21:20 +00:00
|
|
|
let features = match strip_unconfigured.configure_krate_attrs(krate.attrs) {
|
2019-12-31 06:42:19 +00:00
|
|
|
None => {
|
|
|
|
// The entire crate is unconfigured.
|
2016-08-31 23:39:16 +00:00
|
|
|
krate.attrs = Vec::new();
|
2021-02-14 18:14:12 +00:00
|
|
|
krate.items = Vec::new();
|
2019-12-31 06:42:19 +00:00
|
|
|
Features::default()
|
2016-08-31 23:39:16 +00:00
|
|
|
}
|
2019-12-31 06:42:19 +00:00
|
|
|
Some(attrs) => {
|
|
|
|
krate.attrs = attrs;
|
2020-07-30 01:27:50 +00:00
|
|
|
let features = get_features(sess, diag, &krate.attrs);
|
2019-12-31 06:42:19 +00:00
|
|
|
if err_count == diag.err_count() {
|
|
|
|
// Avoid reconfiguring malformed `cfg_attr`s.
|
|
|
|
strip_unconfigured.features = Some(&features);
|
2021-02-23 15:21:20 +00:00
|
|
|
// Run configuration again, this time with features available
|
|
|
|
// so that we can perform feature-gating.
|
|
|
|
strip_unconfigured.configure_krate_attrs(unconfigured_attrs);
|
2019-12-31 06:42:19 +00:00
|
|
|
}
|
|
|
|
features
|
2016-08-31 23:39:16 +00:00
|
|
|
}
|
2019-12-31 06:42:19 +00:00
|
|
|
};
|
2016-08-31 23:39:16 +00:00
|
|
|
(krate, features)
|
|
|
|
}
|
|
|
|
|
2019-10-16 08:59:30 +00:00
|
|
|
#[macro_export]
|
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(),
|
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
};
|
2016-09-02 01:44:23 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 05:45:50 +00:00
|
|
|
const CFG_ATTR_GRAMMAR_HELP: &str = "#[cfg_attr(condition, attribute, other_attribute, ...)]";
|
|
|
|
const CFG_ATTR_NOTE_REF: &str = "for more information, visit \
|
|
|
|
<https://doc.rust-lang.org/reference/conditional-compilation.html\
|
|
|
|
#the-cfg_attr-attribute>";
|
|
|
|
|
2016-05-16 04:42:57 +00:00
|
|
|
impl<'a> StripUnconfigured<'a> {
|
2021-02-23 15:21:20 +00:00
|
|
|
pub fn configure<T: AstLike>(&mut self, mut node: T) -> Option<T> {
|
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 04:20:55 +00:00
|
|
|
self.process_cfg_attrs(&mut node);
|
2020-11-23 06:43:55 +00:00
|
|
|
if self.in_cfg(node.attrs()) {
|
|
|
|
Some(node)
|
|
|
|
} else {
|
|
|
|
self.modified = true;
|
|
|
|
None
|
|
|
|
}
|
2016-06-01 02:13:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 15:21:20 +00:00
|
|
|
fn configure_krate_attrs(
|
|
|
|
&mut self,
|
|
|
|
mut attrs: Vec<ast::Attribute>,
|
|
|
|
) -> Option<Vec<ast::Attribute>> {
|
|
|
|
attrs.flat_map_in_place(|attr| self.process_cfg_attr(attr));
|
|
|
|
if self.in_cfg(&attrs) {
|
|
|
|
Some(attrs)
|
|
|
|
} else {
|
|
|
|
self.modified = true;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 22:06:49 +00: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.
|
|
|
|
///
|
2020-03-06 11:13:55 +00:00
|
|
|
/// Gives compiler warnings if any `cfg_attr` does not contain any
|
2018-09-10 22:06:49 +00:00
|
|
|
/// attributes and is in the original source code. Gives compiler errors if
|
|
|
|
/// the syntax of any `cfg_attr` is incorrect.
|
2021-02-23 15:21:20 +00:00
|
|
|
pub fn process_cfg_attrs<T: AstLike>(&mut self, node: &mut T) {
|
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 04:20:55 +00:00
|
|
|
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 22:06:49 +00: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 16:21:55 +00:00
|
|
|
/// Gives a compiler warning when the `cfg_attr` contains no attributes and
|
2018-09-10 22:06:49 +00:00
|
|
|
/// is in the original source file. Gives a compiler error if the syntax of
|
2019-02-08 13:53:55 +00:00
|
|
|
/// the attribute is incorrect.
|
2019-12-05 05:45:50 +00:00
|
|
|
fn process_cfg_attr(&mut self, attr: Attribute) -> Vec<Attribute> {
|
2019-10-23 19:33:12 +00:00
|
|
|
if !attr.has_name(sym::cfg_attr) {
|
2018-09-10 22:06:49 +00:00
|
|
|
return vec![attr];
|
2016-05-16 04:42:57 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 06:43:55 +00:00
|
|
|
// A `#[cfg_attr]` either gets removed, or replaced with a new attribute
|
|
|
|
self.modified = true;
|
|
|
|
|
2019-12-05 05:45:50 +00:00
|
|
|
let (cfg_predicate, expanded_attrs) = match self.parse_cfg_attr(&attr) {
|
|
|
|
None => return vec![],
|
|
|
|
Some(r) => r,
|
2016-05-16 04:42:57 +00:00
|
|
|
};
|
2016-08-20 01:58:14 +00:00
|
|
|
|
2019-06-22 10:11:01 +00:00
|
|
|
// Lint on zero attributes in source.
|
|
|
|
if expanded_attrs.is_empty() {
|
|
|
|
return vec![attr];
|
2018-10-04 11:55:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-22 10:11:01 +00:00
|
|
|
// At this point we know the attribute is considered used.
|
2020-07-30 01:27:50 +00:00
|
|
|
self.sess.mark_attr_used(&attr);
|
2019-06-22 10:11:01 +00:00
|
|
|
|
2020-07-30 01:27:50 +00:00
|
|
|
if !attr::cfg_matches(&cfg_predicate, &self.sess.parse_sess, self.features) {
|
2019-12-05 05:45:50 +00:00
|
|
|
return vec![];
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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(|(item, span)| {
|
2020-11-05 17:27:48 +00:00
|
|
|
let orig_tokens = attr.tokens();
|
2020-09-26 23:33:42 +00:00
|
|
|
|
|
|
|
// We are taking an attribute of the form `#[cfg_attr(pred, attr)]`
|
|
|
|
// and producing an attribute of the form `#[attr]`. We
|
|
|
|
// have captured tokens for `attr` itself, but we need to
|
|
|
|
// synthesize tokens for the wrapper `#` and `[]`, which
|
|
|
|
// we do below.
|
|
|
|
|
|
|
|
// Use the `#` in `#[cfg_attr(pred, attr)]` as the `#` token
|
|
|
|
// for `attr` when we expand it to `#[attr]`
|
2020-11-05 17:27:48 +00:00
|
|
|
let pound_token = orig_tokens.trees().next().unwrap();
|
2020-09-26 23:33:42 +00:00
|
|
|
if !matches!(pound_token, TokenTree::Token(Token { kind: TokenKind::Pound, .. })) {
|
|
|
|
panic!("Bad tokens for attribute {:?}", attr);
|
|
|
|
}
|
|
|
|
// We don't really have a good span to use for the syntheized `[]`
|
|
|
|
// in `#[attr]`, so just use the span of the `#` token.
|
|
|
|
let bracket_group = TokenTree::Delimited(
|
|
|
|
DelimSpan::from_single(pound_token.span()),
|
|
|
|
DelimToken::Bracket,
|
|
|
|
item.tokens
|
2020-10-30 21:40:41 +00:00
|
|
|
.as_ref()
|
2020-09-26 23:33:42 +00:00
|
|
|
.unwrap_or_else(|| panic!("Missing tokens for {:?}", item))
|
2020-10-30 21:40:41 +00:00
|
|
|
.create_token_stream(),
|
2020-09-26 23:33:42 +00:00
|
|
|
);
|
2020-11-05 17:27:48 +00:00
|
|
|
let tokens = Some(LazyTokenStream::new(TokenStream::new(vec![
|
2020-09-26 23:33:42 +00:00
|
|
|
(pound_token, Spacing::Alone),
|
|
|
|
(bracket_group, Spacing::Alone),
|
2020-10-30 21:40:41 +00:00
|
|
|
])));
|
2020-11-05 17:27:48 +00:00
|
|
|
|
|
|
|
self.process_cfg_attr(attr::mk_attr_from_item(item, tokens, attr.style, span))
|
2019-12-05 05:45:50 +00:00
|
|
|
})
|
2018-09-10 22:06:49 +00:00
|
|
|
.collect()
|
2019-12-05 05:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_cfg_attr(&self, attr: &Attribute) -> Option<(MetaItem, Vec<(AttrItem, Span)>)> {
|
2019-12-05 13:19:00 +00:00
|
|
|
match attr.get_normal_item().args {
|
|
|
|
ast::MacArgs::Delimited(dspan, delim, ref tts) if !tts.is_empty() => {
|
|
|
|
let msg = "wrong `cfg_attr` delimiters";
|
2020-07-30 01:27:50 +00:00
|
|
|
validate_attr::check_meta_bad_delim(&self.sess.parse_sess, dspan, delim, msg);
|
|
|
|
match parse_in(&self.sess.parse_sess, tts.clone(), "`cfg_attr` input", |p| {
|
|
|
|
p.parse_cfg_attr()
|
|
|
|
}) {
|
2019-12-05 05:45:50 +00:00
|
|
|
Ok(r) => return Some(r),
|
2020-01-31 12:24:57 +00:00
|
|
|
Err(mut e) => {
|
|
|
|
e.help(&format!("the valid syntax is `{}`", CFG_ATTR_GRAMMAR_HELP))
|
2020-02-01 23:47:58 +00:00
|
|
|
.note(CFG_ATTR_NOTE_REF)
|
|
|
|
.emit();
|
|
|
|
}
|
2019-12-05 05:45:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => self.error_malformed_cfg_attr_missing(attr.span),
|
2016-05-16 02:42:24 +00:00
|
|
|
}
|
2019-12-05 05:45:50 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn error_malformed_cfg_attr_missing(&self, span: Span) {
|
|
|
|
self.sess
|
2020-07-30 01:27:50 +00:00
|
|
|
.parse_sess
|
2019-12-05 05:45:50 +00:00
|
|
|
.span_diagnostic
|
|
|
|
.struct_span_err(span, "malformed `cfg_attr` attribute input")
|
|
|
|
.span_suggestion(
|
|
|
|
span,
|
|
|
|
"missing condition and attribute",
|
|
|
|
CFG_ATTR_GRAMMAR_HELP.to_string(),
|
|
|
|
Applicability::HasPlaceholders,
|
|
|
|
)
|
|
|
|
.note(CFG_ATTR_NOTE_REF)
|
|
|
|
.emit();
|
2016-05-15 09:15:02 +00:00
|
|
|
}
|
2016-05-16 04:42:57 +00:00
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Determines if a node with the given attributes should be included in this configuration.
|
2019-12-05 05:45:50 +00:00
|
|
|
pub fn in_cfg(&self, attrs: &[Attribute]) -> bool {
|
2016-05-26 23:56:25 +00:00
|
|
|
attrs.iter().all(|attr| {
|
2020-07-30 01:27:50 +00:00
|
|
|
if !is_cfg(self.sess, attr) {
|
2017-03-08 23:13:35 +00:00
|
|
|
return true;
|
2018-09-01 21:13:22 +00:00
|
|
|
}
|
2020-07-30 01:27:50 +00:00
|
|
|
let meta_item = match validate_attr::parse_meta(&self.sess.parse_sess, attr) {
|
2019-12-31 06:15:43 +00:00
|
|
|
Ok(meta_item) => meta_item,
|
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2018-09-01 21:13:22 +00:00
|
|
|
let error = |span, msg, suggestion: &str| {
|
2020-07-30 01:27:50 +00:00
|
|
|
let mut err = self.sess.parse_sess.span_diagnostic.struct_span_err(span, msg);
|
2018-09-01 21:13:22 +00:00
|
|
|
if !suggestion.is_empty() {
|
2019-01-25 21:03:27 +00:00
|
|
|
err.span_suggestion(
|
2018-09-17 17:13:08 +00:00
|
|
|
span,
|
|
|
|
"expected syntax is",
|
|
|
|
suggestion.into(),
|
|
|
|
Applicability::MaybeIncorrect,
|
2018-09-17 09:52:08 +00:00
|
|
|
);
|
2018-09-01 21:13:22 +00:00
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
true
|
|
|
|
};
|
2019-12-31 06:15:43 +00:00
|
|
|
let span = meta_item.span;
|
|
|
|
match meta_item.meta_item_list() {
|
|
|
|
None => error(span, "`cfg` is not followed by parentheses", "cfg(/* predicate */)"),
|
|
|
|
Some([]) => error(span, "`cfg` predicate is not specified", ""),
|
|
|
|
Some([_, .., l]) => error(l.span(), "multiple `cfg` predicates are specified", ""),
|
|
|
|
Some([single]) => match single.meta_item() {
|
2020-07-30 01:27:50 +00:00
|
|
|
Some(meta_item) => {
|
|
|
|
attr::cfg_matches(meta_item, &self.sess.parse_sess, self.features)
|
|
|
|
}
|
2019-12-31 06:15:43 +00:00
|
|
|
None => error(single.span(), "`cfg` predicate key cannot be a literal", ""),
|
|
|
|
},
|
2016-08-20 01:58:14 +00:00
|
|
|
}
|
2016-05-26 23:56:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-10 22:06:49 +00:00
|
|
|
/// Visit attributes on expression and statements (but not attributes on items in blocks).
|
2019-12-05 05:45:50 +00:00
|
|
|
fn visit_expr_attrs(&mut self, attrs: &[Attribute]) {
|
2016-05-16 04:42:57 +00:00
|
|
|
// flag the offending attributes
|
|
|
|
for attr in attrs.iter() {
|
2018-03-16 06:20:56 +00:00
|
|
|
self.maybe_emit_expr_attr_err(attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If attributes are not allowed on expressions, emit an error for `attr`
|
2019-12-05 05:45:50 +00:00
|
|
|
pub fn maybe_emit_expr_attr_err(&self, attr: &Attribute) {
|
2021-01-11 19:45:33 +00:00
|
|
|
if !self.features.map_or(true, |features| features.stmt_expr_attributes) {
|
2019-12-22 22:42:04 +00:00
|
|
|
let mut err = feature_err(
|
2020-07-30 01:27:50 +00:00
|
|
|
&self.sess.parse_sess,
|
2019-12-22 22:42:04 +00:00
|
|
|
sym::stmt_expr_attributes,
|
|
|
|
attr.span,
|
|
|
|
"attributes on expressions are experimental",
|
|
|
|
);
|
2018-03-16 06:20:56 +00:00
|
|
|
|
2019-10-23 19:33:12 +00:00
|
|
|
if attr.is_doc_comment() {
|
2018-03-16 06:20:56 +00:00
|
|
|
err.help("`///` is for documentation comments. For a plain comment, use `//`.");
|
2016-06-11 01:37:24 +00:00
|
|
|
}
|
2018-03-16 06:20:56 +00: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 04:20:55 +00:00
|
|
|
pub fn configure_foreign_mod(&mut self, foreign_mod: &mut ast::ForeignMod) {
|
2020-08-23 10:42:19 +00:00
|
|
|
let ast::ForeignMod { unsafety: _, abi: _, items } = foreign_mod;
|
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 04:20:55 +00:00
|
|
|
items.flat_map_in_place(|item| self.configure(item));
|
2013-08-29 19:10:02 +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 04:20:55 +00:00
|
|
|
fn configure_variant_data(&mut self, vdata: &mut ast::VariantData) {
|
2016-09-02 01:44:23 +00:00
|
|
|
match vdata {
|
2019-12-22 22:42:04 +00:00
|
|
|
ast::VariantData::Struct(fields, ..) | ast::VariantData::Tuple(fields, _) => {
|
|
|
|
fields.flat_map_in_place(|field| self.configure(field))
|
|
|
|
}
|
2019-03-21 22:38:50 +00: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 04:20:55 +00:00
|
|
|
pub fn configure_item_kind(&mut self, item: &mut ast::ItemKind) {
|
2016-09-02 01:44:23 +00:00
|
|
|
match item {
|
2019-12-22 22:42:04 +00:00
|
|
|
ast::ItemKind::Struct(def, _generics) | ast::ItemKind::Union(def, _generics) => {
|
|
|
|
self.configure_variant_data(def)
|
|
|
|
}
|
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 04:20:55 +00:00
|
|
|
ast::ItemKind::Enum(ast::EnumDef { variants }, _generics) => {
|
|
|
|
variants.flat_map_in_place(|variant| self.configure(variant));
|
|
|
|
for variant in variants {
|
2019-08-14 00:40:21 +00:00
|
|
|
self.configure_variant_data(&mut variant.data);
|
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 04:20:55 +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 04:20:55 +00: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 04:20:55 +00:00
|
|
|
pub fn configure_expr_kind(&mut self, expr_kind: &mut ast::ExprKind) {
|
2017-01-04 03:13:01 +00: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 04:20:55 +00:00
|
|
|
ast::ExprKind::Match(_m, arms) => {
|
|
|
|
arms.flat_map_in_place(|arm| self.configure(arm));
|
2017-01-04 03:13:01 +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 04:20:55 +00:00
|
|
|
ast::ExprKind::Struct(_path, fields, _base) => {
|
|
|
|
fields.flat_map_in_place(|field| self.configure(field));
|
2017-01-04 03:13:01 +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 04:20:55 +00:00
|
|
|
_ => {}
|
2016-09-02 01:44:23 +00:00
|
|
|
}
|
2013-08-29 19:10:02 +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 04:20:55 +00:00
|
|
|
pub fn configure_expr(&mut self, expr: &mut P<ast::Expr>) {
|
2016-10-06 03:44:59 +00:00
|
|
|
self.visit_expr_attrs(expr.attrs());
|
2016-06-11 01:37:24 +00:00
|
|
|
|
2015-11-03 16:39:51 +00: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 04:20:55 +00: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
|
2020-07-30 01:27:50 +00:00
|
|
|
if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(self.sess, a)) {
|
2016-06-11 01:37:24 +00:00
|
|
|
let msg = "removing an expression is not supported in this position";
|
2020-07-30 01:27:50 +00:00
|
|
|
self.sess.parse_sess.span_diagnostic.span_err(attr.span, msg);
|
2016-06-11 01:37:24 +00:00
|
|
|
}
|
|
|
|
|
2016-09-02 01:44:23 +00:00
|
|
|
self.process_cfg_attrs(expr)
|
2014-04-23 04:54:48 +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 04:20:55 +00:00
|
|
|
pub fn configure_pat(&mut self, pat: &mut P<ast::Pat>) {
|
2019-09-26 15:18:31 +00:00
|
|
|
if let ast::PatKind::Struct(_path, fields, _etc) = &mut pat.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 04:20:55 +00:00
|
|
|
fields.flat_map_in_place(|field| self.configure(field));
|
|
|
|
}
|
2017-01-04 03:13:01 +00:00
|
|
|
}
|
2018-06-01 21:10:29 +00:00
|
|
|
|
2019-06-09 10:58:40 +00:00
|
|
|
pub fn configure_fn_decl(&mut self, fn_decl: &mut ast::FnDecl) {
|
|
|
|
fn_decl.inputs.flat_map_in_place(|arg| self.configure(arg));
|
|
|
|
}
|
2020-11-18 22:43:23 +00:00
|
|
|
|
|
|
|
pub fn fully_configure(&mut self, item: Annotatable) -> Annotatable {
|
|
|
|
// Since the item itself has already been configured by the InvocationCollector,
|
|
|
|
// we know that fold result vector will contain exactly one element
|
|
|
|
match item {
|
|
|
|
Annotatable::Item(item) => Annotatable::Item(self.flat_map_item(item).pop().unwrap()),
|
|
|
|
Annotatable::TraitItem(item) => {
|
|
|
|
Annotatable::TraitItem(self.flat_map_trait_item(item).pop().unwrap())
|
|
|
|
}
|
|
|
|
Annotatable::ImplItem(item) => {
|
|
|
|
Annotatable::ImplItem(self.flat_map_impl_item(item).pop().unwrap())
|
|
|
|
}
|
|
|
|
Annotatable::ForeignItem(item) => {
|
|
|
|
Annotatable::ForeignItem(self.flat_map_foreign_item(item).pop().unwrap())
|
|
|
|
}
|
|
|
|
Annotatable::Stmt(stmt) => {
|
|
|
|
Annotatable::Stmt(stmt.map(|stmt| self.flat_map_stmt(stmt).pop().unwrap()))
|
|
|
|
}
|
|
|
|
Annotatable::Expr(mut expr) => Annotatable::Expr({
|
|
|
|
self.visit_expr(&mut expr);
|
|
|
|
expr
|
|
|
|
}),
|
|
|
|
Annotatable::Arm(arm) => Annotatable::Arm(self.flat_map_arm(arm).pop().unwrap()),
|
|
|
|
Annotatable::Field(field) => {
|
|
|
|
Annotatable::Field(self.flat_map_field(field).pop().unwrap())
|
|
|
|
}
|
|
|
|
Annotatable::FieldPat(fp) => {
|
|
|
|
Annotatable::FieldPat(self.flat_map_field_pattern(fp).pop().unwrap())
|
|
|
|
}
|
|
|
|
Annotatable::GenericParam(param) => {
|
|
|
|
Annotatable::GenericParam(self.flat_map_generic_param(param).pop().unwrap())
|
|
|
|
}
|
|
|
|
Annotatable::Param(param) => {
|
|
|
|
Annotatable::Param(self.flat_map_param(param).pop().unwrap())
|
|
|
|
}
|
|
|
|
Annotatable::StructField(sf) => {
|
|
|
|
Annotatable::StructField(self.flat_map_struct_field(sf).pop().unwrap())
|
|
|
|
}
|
|
|
|
Annotatable::Variant(v) => {
|
|
|
|
Annotatable::Variant(self.flat_map_variant(v).pop().unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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 04:20:55 +00: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 16:39:51 +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 04:20:55 +00: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 16:39:51 +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 04:20:55 +00:00
|
|
|
fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
|
|
|
|
self.configure_expr(expr);
|
2019-09-26 13:39:48 +00:00
|
|
|
self.configure_expr_kind(&mut 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 04:20:55 +00:00
|
|
|
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 04:20:55 +00:00
|
|
|
fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
|
|
|
|
let mut expr = configure!(self, expr);
|
2019-09-26 13:39:48 +00:00
|
|
|
self.configure_expr_kind(&mut 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 04:20:55 +00:00
|
|
|
noop_visit_expr(&mut expr, self);
|
|
|
|
Some(expr)
|
2016-09-02 01:44:23 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 03:17:40 +00:00
|
|
|
fn flat_map_generic_param(
|
|
|
|
&mut self,
|
|
|
|
param: ast::GenericParam,
|
|
|
|
) -> SmallVec<[ast::GenericParam; 1]> {
|
|
|
|
noop_flat_map_generic_param(configure!(self, param), 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 04:20:55 +00: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 21:48:12 +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 04:20:55 +00: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
|
|
|
}
|
|
|
|
|
2019-12-12 05:41:18 +00:00
|
|
|
fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
|
2019-12-01 15:58:37 +00:00
|
|
|
noop_flat_map_assoc_item(configure!(self, item), self)
|
2016-06-02 07:34:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 05:41:18 +00:00
|
|
|
fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
|
2019-12-01 15:58:37 +00:00
|
|
|
noop_flat_map_assoc_item(configure!(self, item), self)
|
2014-11-04 22:59:42 +00: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 04:20:55 +00:00
|
|
|
fn visit_pat(&mut self, pat: &mut P<ast::Pat>) {
|
|
|
|
self.configure_pat(pat);
|
|
|
|
noop_visit_pat(pat, self)
|
2017-01-04 03:13:01 +00:00
|
|
|
}
|
2019-06-09 10:58:40 +00:00
|
|
|
|
|
|
|
fn visit_fn_decl(&mut self, mut fn_decl: &mut P<ast::FnDecl>) {
|
|
|
|
self.configure_fn_decl(&mut fn_decl);
|
|
|
|
noop_visit_fn_decl(fn_decl, self);
|
|
|
|
}
|
2013-08-29 19:10:02 +00:00
|
|
|
}
|
2011-06-30 05:32:08 +00:00
|
|
|
|
2020-07-30 01:27:50 +00:00
|
|
|
fn is_cfg(sess: &Session, attr: &Attribute) -> bool {
|
|
|
|
sess.check_name(attr, sym::cfg)
|
2015-11-03 16:39:51 +00:00
|
|
|
}
|