2019-02-07 02:33:01 +09:00
|
|
|
use crate::ast::{self, NodeId};
|
|
|
|
use crate::source_map::{DUMMY_SP, dummy_spanned};
|
|
|
|
use crate::ext::base::ExtCtxt;
|
|
|
|
use crate::ext::expand::{AstFragment, AstFragmentKind};
|
|
|
|
use crate::tokenstream::TokenStream;
|
|
|
|
use crate::mut_visit::*;
|
|
|
|
use crate::ptr::P;
|
|
|
|
use crate::ThinVec;
|
|
|
|
|
|
|
|
use smallvec::{smallvec, SmallVec};
|
2016-08-29 05:32:41 +00:00
|
|
|
|
2018-08-18 13:55:43 +03:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2016-08-29 05:32:41 +00:00
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
pub fn placeholder(kind: AstFragmentKind, id: ast::NodeId) -> AstFragment {
|
2016-08-29 05:32:41 +00:00
|
|
|
fn mac_placeholder() -> ast::Mac {
|
2019-08-15 02:13:53 +03:00
|
|
|
ast::Mac {
|
2016-12-05 03:51:11 +00:00
|
|
|
path: ast::Path { span: DUMMY_SP, segments: Vec::new() },
|
2017-02-21 05:05:59 +00:00
|
|
|
tts: TokenStream::empty().into(),
|
2018-05-22 08:01:21 -07:00
|
|
|
delim: ast::MacDelimiter::Brace,
|
2019-08-15 02:13:53 +03:00
|
|
|
span: DUMMY_SP,
|
2019-07-18 18:36:19 -07:00
|
|
|
prior_type_ascription: None,
|
2019-08-15 02:13:53 +03:00
|
|
|
}
|
2016-08-29 05:32:41 +00:00
|
|
|
}
|
|
|
|
|
2019-05-11 19:08:09 +03:00
|
|
|
let ident = ast::Ident::invalid();
|
2016-08-29 05:32:41 +00:00
|
|
|
let attrs = Vec::new();
|
2017-09-21 22:18:47 -04:00
|
|
|
let generics = ast::Generics::default();
|
2018-01-29 14:12:09 +09:00
|
|
|
let vis = dummy_spanned(ast::VisibilityKind::Inherited);
|
2016-08-29 05:32:41 +00:00
|
|
|
let span = DUMMY_SP;
|
|
|
|
let expr_placeholder = || P(ast::Expr {
|
2017-08-06 22:54:09 -07:00
|
|
|
id, span,
|
2018-08-05 12:04:56 +02:00
|
|
|
attrs: ThinVec::new(),
|
2019-09-26 14:39:48 +01:00
|
|
|
kind: ast::ExprKind::Mac(mac_placeholder()),
|
2016-08-29 05:32:41 +00:00
|
|
|
});
|
2019-09-09 09:26:25 -03:00
|
|
|
let ty = P(ast::Ty {
|
|
|
|
id,
|
2019-09-26 17:25:31 +01:00
|
|
|
kind: ast::TyKind::Mac(mac_placeholder()),
|
2019-09-09 09:26:25 -03:00
|
|
|
span,
|
|
|
|
});
|
|
|
|
let pat = P(ast::Pat {
|
|
|
|
id,
|
2019-09-26 16:18:31 +01:00
|
|
|
kind: ast::PatKind::Mac(mac_placeholder()),
|
2019-09-09 09:26:25 -03:00
|
|
|
span,
|
|
|
|
});
|
2016-08-29 05:32:41 +00:00
|
|
|
|
|
|
|
match kind {
|
2018-06-20 02:08:08 +03:00
|
|
|
AstFragmentKind::Expr => AstFragment::Expr(expr_placeholder()),
|
|
|
|
AstFragmentKind::OptExpr => AstFragment::OptExpr(Some(expr_placeholder())),
|
2018-08-13 22:15:16 +03:00
|
|
|
AstFragmentKind::Items => AstFragment::Items(smallvec![P(ast::Item {
|
2017-08-06 22:54:09 -07:00
|
|
|
id, span, ident, vis, attrs,
|
2016-08-29 05:32:41 +00:00
|
|
|
node: ast::ItemKind::Mac(mac_placeholder()),
|
2017-07-10 17:44:46 -07:00
|
|
|
tokens: None,
|
2018-08-13 22:15:16 +03:00
|
|
|
})]),
|
|
|
|
AstFragmentKind::TraitItems => AstFragment::TraitItems(smallvec![ast::TraitItem {
|
2017-09-21 22:18:47 -04:00
|
|
|
id, span, ident, attrs, generics,
|
2019-09-26 17:07:54 +01:00
|
|
|
kind: ast::TraitItemKind::Macro(mac_placeholder()),
|
2017-07-12 09:50:05 -07:00
|
|
|
tokens: None,
|
2018-08-13 22:15:16 +03:00
|
|
|
}]),
|
|
|
|
AstFragmentKind::ImplItems => AstFragment::ImplItems(smallvec![ast::ImplItem {
|
2017-09-21 22:18:47 -04:00
|
|
|
id, span, ident, vis, attrs, generics,
|
2019-09-26 16:38:13 +01:00
|
|
|
kind: ast::ImplItemKind::Macro(mac_placeholder()),
|
2016-08-29 05:32:41 +00:00
|
|
|
defaultness: ast::Defaultness::Final,
|
2017-07-12 09:50:05 -07:00
|
|
|
tokens: None,
|
2018-08-13 22:15:16 +03:00
|
|
|
}]),
|
2018-06-20 02:08:08 +03:00
|
|
|
AstFragmentKind::ForeignItems =>
|
2018-08-13 22:15:16 +03:00
|
|
|
AstFragment::ForeignItems(smallvec![ast::ForeignItem {
|
2018-06-20 02:08:08 +03:00
|
|
|
id, span, ident, vis, attrs,
|
|
|
|
node: ast::ForeignItemKind::Macro(mac_placeholder()),
|
2018-08-13 22:15:16 +03:00
|
|
|
}]),
|
2018-06-20 02:08:08 +03:00
|
|
|
AstFragmentKind::Pat => AstFragment::Pat(P(ast::Pat {
|
2019-09-26 16:18:31 +01:00
|
|
|
id, span, kind: ast::PatKind::Mac(mac_placeholder()),
|
2016-08-29 05:32:41 +00:00
|
|
|
})),
|
2018-06-20 02:08:08 +03:00
|
|
|
AstFragmentKind::Ty => AstFragment::Ty(P(ast::Ty {
|
2019-09-26 17:25:31 +01:00
|
|
|
id, span, kind: ast::TyKind::Mac(mac_placeholder()),
|
2016-08-29 05:32:41 +00:00
|
|
|
})),
|
2018-08-13 22:15:16 +03:00
|
|
|
AstFragmentKind::Stmts => AstFragment::Stmts(smallvec![{
|
2018-08-05 12:04:56 +02:00
|
|
|
let mac = P((mac_placeholder(), ast::MacStmtStyle::Braces, ThinVec::new()));
|
2017-08-06 22:54:09 -07:00
|
|
|
ast::Stmt { id, span, node: ast::StmtKind::Mac(mac) }
|
2018-08-13 22:15:16 +03:00
|
|
|
}]),
|
2019-09-09 09:26:25 -03:00
|
|
|
AstFragmentKind::Arms => AstFragment::Arms(smallvec![
|
|
|
|
ast::Arm {
|
|
|
|
attrs: Default::default(),
|
|
|
|
body: expr_placeholder(),
|
|
|
|
guard: None,
|
|
|
|
id,
|
|
|
|
pat,
|
|
|
|
span,
|
|
|
|
is_placeholder: true,
|
|
|
|
}
|
|
|
|
]),
|
|
|
|
AstFragmentKind::Fields => AstFragment::Fields(smallvec![
|
|
|
|
ast::Field {
|
|
|
|
attrs: Default::default(),
|
|
|
|
expr: expr_placeholder(),
|
|
|
|
id,
|
|
|
|
ident,
|
|
|
|
is_shorthand: false,
|
|
|
|
span,
|
|
|
|
is_placeholder: true,
|
|
|
|
}
|
|
|
|
]),
|
|
|
|
AstFragmentKind::FieldPats => AstFragment::FieldPats(smallvec![
|
|
|
|
ast::FieldPat {
|
|
|
|
attrs: Default::default(),
|
|
|
|
id,
|
|
|
|
ident,
|
|
|
|
is_shorthand: false,
|
|
|
|
pat,
|
|
|
|
span,
|
|
|
|
is_placeholder: true,
|
|
|
|
}
|
|
|
|
]),
|
|
|
|
AstFragmentKind::GenericParams => AstFragment::GenericParams(smallvec![{
|
|
|
|
ast::GenericParam {
|
|
|
|
attrs: Default::default(),
|
|
|
|
bounds: Default::default(),
|
|
|
|
id,
|
|
|
|
ident,
|
|
|
|
is_placeholder: true,
|
|
|
|
kind: ast::GenericParamKind::Lifetime,
|
|
|
|
}
|
|
|
|
}]),
|
|
|
|
AstFragmentKind::Params => AstFragment::Params(smallvec![
|
|
|
|
ast::Param {
|
|
|
|
attrs: Default::default(),
|
|
|
|
id,
|
|
|
|
pat,
|
|
|
|
span,
|
|
|
|
ty,
|
|
|
|
is_placeholder: true,
|
|
|
|
}
|
|
|
|
]),
|
|
|
|
AstFragmentKind::StructFields => AstFragment::StructFields(smallvec![
|
|
|
|
ast::StructField {
|
|
|
|
attrs: Default::default(),
|
|
|
|
id,
|
|
|
|
ident: None,
|
|
|
|
span,
|
|
|
|
ty,
|
|
|
|
vis,
|
|
|
|
is_placeholder: true,
|
|
|
|
}
|
|
|
|
]),
|
|
|
|
AstFragmentKind::Variants => AstFragment::Variants(smallvec![
|
|
|
|
ast::Variant {
|
|
|
|
attrs: Default::default(),
|
|
|
|
data: ast::VariantData::Struct(Default::default(), false),
|
|
|
|
disr_expr: None,
|
|
|
|
id,
|
|
|
|
ident,
|
|
|
|
span,
|
|
|
|
is_placeholder: true,
|
|
|
|
}
|
|
|
|
])
|
2016-08-29 05:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
pub struct PlaceholderExpander<'a, 'b> {
|
2018-08-18 13:55:43 +03:00
|
|
|
expanded_fragments: FxHashMap<ast::NodeId, AstFragment>,
|
2016-09-05 00:10:27 +00:00
|
|
|
cx: &'a mut ExtCtxt<'b>,
|
2016-09-06 05:42:45 +00:00
|
|
|
monotonic: bool,
|
2016-08-29 05:32:41 +00:00
|
|
|
}
|
|
|
|
|
2016-09-05 00:10:27 +00:00
|
|
|
impl<'a, 'b> PlaceholderExpander<'a, 'b> {
|
2016-09-06 05:42:45 +00:00
|
|
|
pub fn new(cx: &'a mut ExtCtxt<'b>, monotonic: bool) -> Self {
|
2016-08-29 05:32:41 +00:00
|
|
|
PlaceholderExpander {
|
2017-08-06 22:54:09 -07:00
|
|
|
cx,
|
2018-08-18 13:55:43 +03:00
|
|
|
expanded_fragments: FxHashMap::default(),
|
2017-08-06 22:54:09 -07:00
|
|
|
monotonic,
|
2016-08-29 05:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 20:49:00 +03:00
|
|
|
pub fn add(&mut self, id: ast::NodeId, mut fragment: AstFragment, placeholders: Vec<NodeId>) {
|
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
|
|
|
fragment.mut_visit_with(self);
|
2018-06-20 02:08:08 +03:00
|
|
|
if let AstFragment::Items(mut items) = fragment {
|
2019-08-17 20:49:00 +03:00
|
|
|
for placeholder in placeholders {
|
|
|
|
match self.remove(placeholder) {
|
2018-06-20 02:08:08 +03:00
|
|
|
AstFragment::Items(derived_items) => items.extend(derived_items),
|
2017-02-02 07:01:15 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2018-06-20 02:08:08 +03:00
|
|
|
fragment = AstFragment::Items(items);
|
2017-02-02 07:01:15 +00:00
|
|
|
}
|
2018-06-20 02:08:08 +03:00
|
|
|
self.expanded_fragments.insert(id, fragment);
|
2016-09-02 03:35:59 +00:00
|
|
|
}
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
fn remove(&mut self, id: ast::NodeId) -> AstFragment {
|
|
|
|
self.expanded_fragments.remove(&id).unwrap()
|
2016-08-29 05:32:41 +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, 'b> MutVisitor for PlaceholderExpander<'a, 'b> {
|
2019-09-09 09:26:25 -03:00
|
|
|
fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
|
|
|
|
if arm.is_placeholder {
|
|
|
|
self.remove(arm.id).make_arms()
|
|
|
|
} else {
|
|
|
|
noop_flat_map_arm(arm, self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flat_map_field(&mut self, field: ast::Field) -> SmallVec<[ast::Field; 1]> {
|
|
|
|
if field.is_placeholder {
|
|
|
|
self.remove(field.id).make_fields()
|
|
|
|
} else {
|
|
|
|
noop_flat_map_field(field, self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flat_map_field_pattern(&mut self, fp: ast::FieldPat) -> SmallVec<[ast::FieldPat; 1]> {
|
|
|
|
if fp.is_placeholder {
|
|
|
|
self.remove(fp.id).make_field_patterns()
|
|
|
|
} else {
|
|
|
|
noop_flat_map_field_pattern(fp, self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flat_map_generic_param(
|
|
|
|
&mut self,
|
|
|
|
param: ast::GenericParam
|
|
|
|
) -> SmallVec<[ast::GenericParam; 1]>
|
|
|
|
{
|
|
|
|
if param.is_placeholder {
|
|
|
|
self.remove(param.id).make_generic_params()
|
|
|
|
} else {
|
|
|
|
noop_flat_map_generic_param(param, self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
|
|
|
|
if p.is_placeholder {
|
|
|
|
self.remove(p.id).make_params()
|
|
|
|
} else {
|
|
|
|
noop_flat_map_param(p, self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flat_map_struct_field(&mut self, sf: ast::StructField) -> SmallVec<[ast::StructField; 1]> {
|
|
|
|
if sf.is_placeholder {
|
|
|
|
self.remove(sf.id).make_struct_fields()
|
|
|
|
} else {
|
|
|
|
noop_flat_map_struct_field(sf, self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
|
|
|
|
if variant.is_placeholder {
|
|
|
|
self.remove(variant.id).make_variants()
|
|
|
|
} else {
|
|
|
|
noop_flat_map_variant(variant, self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 15:20:55 +11:00
|
|
|
fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
|
2016-08-29 05:32:41 +00:00
|
|
|
match item.node {
|
2016-12-01 11:20:04 +00:00
|
|
|
ast::ItemKind::Mac(_) => return self.remove(item.id).make_items(),
|
2018-08-13 22:15:16 +03:00
|
|
|
ast::ItemKind::MacroDef(_) => return smallvec![item],
|
2016-12-01 11:20:04 +00:00
|
|
|
_ => {}
|
2016-08-29 05:32:41 +00:00
|
|
|
}
|
2016-12-01 11:20:04 +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
|
|
|
noop_flat_map_item(item, self)
|
2016-08-29 05:32:41 +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]> {
|
2019-09-26 17:07:54 +01:00
|
|
|
match item.kind {
|
2016-08-29 05:32:41 +00:00
|
|
|
ast::TraitItemKind::Macro(_) => self.remove(item.id).make_trait_items(),
|
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
|
|
|
_ => noop_flat_map_trait_item(item, self),
|
2016-08-29 05:32:41 +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]> {
|
2019-09-26 16:38:13 +01:00
|
|
|
match item.kind {
|
2016-08-29 05:32:41 +00:00
|
|
|
ast::ImplItemKind::Macro(_) => self.remove(item.id).make_impl_items(),
|
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
|
|
|
_ => noop_flat_map_impl_item(item, self),
|
2016-08-29 05:32:41 +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_foreign_item(&mut self, item: ast::ForeignItem) -> SmallVec<[ast::ForeignItem; 1]> {
|
2018-03-10 18:16:26 -08:00
|
|
|
match item.node {
|
|
|
|
ast::ForeignItemKind::Macro(_) => self.remove(item.id).make_foreign_items(),
|
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
|
|
|
_ => noop_flat_map_foreign_item(item, self),
|
2018-03-10 18:16:26 -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_expr(&mut self, expr: &mut P<ast::Expr>) {
|
2019-09-26 14:39:48 +01: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::Mac(_) => *expr = self.remove(expr.id).make_expr(),
|
|
|
|
_ => noop_visit_expr(expr, self),
|
2016-08-29 05:32:41 +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>> {
|
2019-09-26 14:39:48 +01:00
|
|
|
match expr.kind {
|
2016-08-29 05:32:41 +00:00
|
|
|
ast::ExprKind::Mac(_) => self.remove(expr.id).make_opt_expr(),
|
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
|
|
|
_ => noop_filter_map_expr(expr, self),
|
2016-08-29 05:32:41 +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]> {
|
2018-06-20 02:08:08 +03:00
|
|
|
let (style, mut stmts) = match stmt.node {
|
2016-08-29 05:32:41 +00:00
|
|
|
ast::StmtKind::Mac(mac) => (mac.1, self.remove(stmt.id).make_stmts()),
|
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
|
|
|
_ => return noop_flat_map_stmt(stmt, self),
|
2016-08-29 05:32:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if style == ast::MacStmtStyle::Semicolon {
|
2018-06-20 02:08:08 +03:00
|
|
|
if let Some(stmt) = stmts.pop() {
|
|
|
|
stmts.push(stmt.add_trailing_semicolon());
|
2016-08-29 05:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 02:08:08 +03:00
|
|
|
stmts
|
2016-08-29 05:32:41 +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_pat(&mut self, pat: &mut P<ast::Pat>) {
|
2019-09-26 16:18:31 +01:00
|
|
|
match 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 15:20:55 +11:00
|
|
|
ast::PatKind::Mac(_) => *pat = self.remove(pat.id).make_pat(),
|
|
|
|
_ => noop_visit_pat(pat, self),
|
2016-08-29 05:32:41 +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_ty(&mut self, ty: &mut P<ast::Ty>) {
|
2019-09-26 17:25:31 +01:00
|
|
|
match ty.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::TyKind::Mac(_) => *ty = self.remove(ty.id).make_ty(),
|
|
|
|
_ => noop_visit_ty(ty, self),
|
2016-08-29 05:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-05 00:10:27 +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_block(&mut self, block: &mut P<ast::Block>) {
|
|
|
|
noop_visit_block(block, self);
|
2016-09-05 00:10:27 +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
|
|
|
for stmt in block.stmts.iter_mut() {
|
2019-05-29 04:10:49 +03:00
|
|
|
if self.monotonic {
|
|
|
|
assert_eq!(stmt.id, ast::DUMMY_NODE_ID);
|
|
|
|
stmt.id = self.cx.resolver.next_node_id();
|
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-05 00:10:27 +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_mod(&mut self, module: &mut ast::Mod) {
|
|
|
|
noop_visit_mod(module, self);
|
2019-02-05 15:18:29 +11:00
|
|
|
module.items.retain(|item| match item.node {
|
|
|
|
ast::ItemKind::Mac(_) if !self.cx.ecfg.keep_macs => false, // remove macro definitions
|
|
|
|
_ => true,
|
2016-09-05 00:10:27 +00:00
|
|
|
});
|
|
|
|
}
|
2016-08-29 05:32:41 +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) {
|
|
|
|
// Do nothing.
|
2016-12-01 11:20:04 +00:00
|
|
|
}
|
2016-08-29 05:32:41 +00:00
|
|
|
}
|