2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::mut_visit::*;
|
|
|
|
use rustc_ast::ptr::P;
|
2023-08-01 23:56:26 +00:00
|
|
|
use rustc_ast::token::Delimiter;
|
2024-07-08 10:40:37 +00:00
|
|
|
use rustc_ast::visit::AssocCtxt;
|
2024-10-26 23:35:33 +00:00
|
|
|
use rustc_ast::{self as ast, Safety};
|
2022-09-08 07:22:52 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2024-12-12 23:29:23 +00:00
|
|
|
use rustc_span::{DUMMY_SP, Ident};
|
2019-02-06 17:33:01 +00:00
|
|
|
use smallvec::{SmallVec, smallvec};
|
2022-09-08 07:22:52 +00:00
|
|
|
use thin_vec::ThinVec;
|
2016-08-29 05:32:41 +00:00
|
|
|
|
2019-10-16 08:59:30 +00:00
|
|
|
use crate::expand::{AstFragment, AstFragmentKind};
|
2024-07-28 22:13:50 +00:00
|
|
|
|
2024-04-30 23:39:07 +00:00
|
|
|
pub(crate) fn placeholder(
|
2019-11-23 13:54:24 +00:00
|
|
|
kind: AstFragmentKind,
|
|
|
|
id: ast::NodeId,
|
|
|
|
vis: Option<ast::Visibility>,
|
|
|
|
) -> AstFragment {
|
2022-08-12 02:20:10 +00:00
|
|
|
fn mac_placeholder() -> P<ast::MacCall> {
|
|
|
|
P(ast::MacCall {
|
2022-09-08 07:22:52 +00:00
|
|
|
path: ast::Path { span: DUMMY_SP, segments: ThinVec::new(), tokens: None },
|
2022-11-18 00:24:21 +00:00
|
|
|
args: P(ast::DelimArgs {
|
|
|
|
dspan: ast::tokenstream::DelimSpan::dummy(),
|
2023-08-01 23:56:26 +00:00
|
|
|
delim: Delimiter::Parenthesis,
|
2022-11-18 00:24:21 +00:00
|
|
|
tokens: ast::tokenstream::TokenStream::new(Vec::new()),
|
|
|
|
}),
|
2022-08-12 02:20:10 +00:00
|
|
|
})
|
2016-08-29 05:32:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-17 20:20:30 +00:00
|
|
|
let ident = Ident::empty();
|
2022-08-17 02:34:33 +00:00
|
|
|
let attrs = ast::AttrVec::new();
|
2020-08-21 23:11:00 +00:00
|
|
|
let vis = vis.unwrap_or(ast::Visibility {
|
|
|
|
span: DUMMY_SP,
|
|
|
|
kind: ast::VisibilityKind::Inherited,
|
|
|
|
tokens: None,
|
|
|
|
});
|
2016-08-29 05:32:41 +00:00
|
|
|
let span = DUMMY_SP;
|
|
|
|
let expr_placeholder = || {
|
|
|
|
P(ast::Expr {
|
2017-08-07 05:54:09 +00:00
|
|
|
id,
|
|
|
|
span,
|
2019-12-03 15:38:34 +00:00
|
|
|
attrs: ast::AttrVec::new(),
|
2020-02-29 16:32:20 +00:00
|
|
|
kind: ast::ExprKind::MacCall(mac_placeholder()),
|
2020-05-19 20:56:20 +00:00
|
|
|
tokens: None,
|
2016-08-29 05:32:41 +00:00
|
|
|
})
|
|
|
|
};
|
2020-08-21 22:18:04 +00:00
|
|
|
let ty =
|
|
|
|
|| P(ast::Ty { id, kind: ast::TyKind::MacCall(mac_placeholder()), span, tokens: None });
|
2020-07-27 22:02:29 +00:00
|
|
|
let pat =
|
|
|
|
|| P(ast::Pat { id, kind: ast::PatKind::MacCall(mac_placeholder()), span, tokens: None });
|
2016-08-29 05:32:41 +00:00
|
|
|
|
|
|
|
match kind {
|
2021-10-17 16:32:34 +00:00
|
|
|
AstFragmentKind::Crate => AstFragment::Crate(ast::Crate {
|
|
|
|
attrs: Default::default(),
|
|
|
|
items: Default::default(),
|
2022-03-03 23:45:25 +00:00
|
|
|
spans: ast::ModSpans { inner_span: span, ..Default::default() },
|
2022-01-05 08:09:55 +00:00
|
|
|
id,
|
|
|
|
is_placeholder: true,
|
2021-10-17 16:32:34 +00:00
|
|
|
}),
|
2018-06-19 23:08:08 +00:00
|
|
|
AstFragmentKind::Expr => AstFragment::Expr(expr_placeholder()),
|
|
|
|
AstFragmentKind::OptExpr => AstFragment::OptExpr(Some(expr_placeholder())),
|
2022-10-23 09:22:19 +00:00
|
|
|
AstFragmentKind::MethodReceiverExpr => AstFragment::MethodReceiverExpr(expr_placeholder()),
|
2018-08-13 19:15:16 +00:00
|
|
|
AstFragmentKind::Items => AstFragment::Items(smallvec![P(ast::Item {
|
2017-08-07 05:54:09 +00:00
|
|
|
id,
|
|
|
|
span,
|
|
|
|
ident,
|
|
|
|
vis,
|
|
|
|
attrs,
|
2020-02-29 16:32:20 +00:00
|
|
|
kind: ast::ItemKind::MacCall(mac_placeholder()),
|
2017-07-11 00:44:46 +00:00
|
|
|
tokens: None,
|
2018-08-13 19:15:16 +00:00
|
|
|
})]),
|
2019-12-12 05:41:18 +00:00
|
|
|
AstFragmentKind::TraitItems => AstFragment::TraitItems(smallvec![P(ast::AssocItem {
|
2019-11-07 10:26:36 +00:00
|
|
|
id,
|
|
|
|
span,
|
|
|
|
ident,
|
|
|
|
vis,
|
|
|
|
attrs,
|
2020-02-29 16:32:20 +00:00
|
|
|
kind: ast::AssocItemKind::MacCall(mac_placeholder()),
|
2017-07-12 16:50:05 +00:00
|
|
|
tokens: None,
|
2019-12-12 05:41:18 +00:00
|
|
|
})]),
|
|
|
|
AstFragmentKind::ImplItems => AstFragment::ImplItems(smallvec![P(ast::AssocItem {
|
2017-09-22 02:18:47 +00:00
|
|
|
id,
|
|
|
|
span,
|
|
|
|
ident,
|
|
|
|
vis,
|
|
|
|
attrs,
|
2020-02-29 16:32:20 +00:00
|
|
|
kind: ast::AssocItemKind::MacCall(mac_placeholder()),
|
2017-07-12 16:50:05 +00:00
|
|
|
tokens: None,
|
2019-12-12 05:41:18 +00:00
|
|
|
})]),
|
|
|
|
AstFragmentKind::ForeignItems => {
|
|
|
|
AstFragment::ForeignItems(smallvec![P(ast::ForeignItem {
|
|
|
|
id,
|
|
|
|
span,
|
|
|
|
ident,
|
|
|
|
vis,
|
|
|
|
attrs,
|
2020-02-29 16:32:20 +00:00
|
|
|
kind: ast::ForeignItemKind::MacCall(mac_placeholder()),
|
2019-12-12 05:41:18 +00:00
|
|
|
tokens: None,
|
|
|
|
})])
|
|
|
|
}
|
2020-02-29 16:32:20 +00:00
|
|
|
AstFragmentKind::Pat => AstFragment::Pat(P(ast::Pat {
|
|
|
|
id,
|
|
|
|
span,
|
|
|
|
kind: ast::PatKind::MacCall(mac_placeholder()),
|
2020-07-27 22:02:29 +00:00
|
|
|
tokens: None,
|
2020-02-29 16:32:20 +00:00
|
|
|
})),
|
2020-08-21 22:18:04 +00:00
|
|
|
AstFragmentKind::Ty => AstFragment::Ty(P(ast::Ty {
|
|
|
|
id,
|
|
|
|
span,
|
|
|
|
kind: ast::TyKind::MacCall(mac_placeholder()),
|
|
|
|
tokens: None,
|
|
|
|
})),
|
2018-08-13 19:15:16 +00:00
|
|
|
AstFragmentKind::Stmts => AstFragment::Stmts(smallvec![{
|
2020-08-30 22:38:32 +00:00
|
|
|
let mac = P(ast::MacCallStmt {
|
|
|
|
mac: mac_placeholder(),
|
|
|
|
style: ast::MacStmtStyle::Braces,
|
|
|
|
attrs: ast::AttrVec::new(),
|
2020-11-17 19:27:44 +00:00
|
|
|
tokens: None,
|
2020-08-30 22:38:32 +00:00
|
|
|
});
|
2020-11-17 19:27:44 +00:00
|
|
|
ast::Stmt { id, span, kind: ast::StmtKind::MacCall(mac) }
|
2018-08-13 19:15:16 +00:00
|
|
|
}]),
|
2019-09-09 12:26:25 +00:00
|
|
|
AstFragmentKind::Arms => AstFragment::Arms(smallvec![ast::Arm {
|
|
|
|
attrs: Default::default(),
|
2023-11-27 02:15:56 +00:00
|
|
|
body: Some(expr_placeholder()),
|
2019-09-09 12:26:25 +00:00
|
|
|
guard: None,
|
|
|
|
id,
|
2019-10-10 01:55:39 +00:00
|
|
|
pat: pat(),
|
2019-09-09 12:26:25 +00:00
|
|
|
span,
|
|
|
|
is_placeholder: true,
|
|
|
|
}]),
|
2022-01-05 05:56:45 +00:00
|
|
|
AstFragmentKind::ExprFields => AstFragment::ExprFields(smallvec![ast::ExprField {
|
2019-09-09 12:26:25 +00:00
|
|
|
attrs: Default::default(),
|
|
|
|
expr: expr_placeholder(),
|
|
|
|
id,
|
|
|
|
ident,
|
|
|
|
is_shorthand: false,
|
|
|
|
span,
|
|
|
|
is_placeholder: true,
|
|
|
|
}]),
|
2022-01-05 05:56:45 +00:00
|
|
|
AstFragmentKind::PatFields => AstFragment::PatFields(smallvec![ast::PatField {
|
2019-09-09 12:26:25 +00:00
|
|
|
attrs: Default::default(),
|
|
|
|
id,
|
|
|
|
ident,
|
|
|
|
is_shorthand: false,
|
2019-10-10 01:55:39 +00:00
|
|
|
pat: pat(),
|
2019-09-09 12:26:25 +00:00
|
|
|
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,
|
2022-04-28 19:59:41 +00:00
|
|
|
colon_span: None,
|
2019-09-09 12:26:25 +00:00
|
|
|
}
|
|
|
|
}]),
|
|
|
|
AstFragmentKind::Params => AstFragment::Params(smallvec![ast::Param {
|
|
|
|
attrs: Default::default(),
|
|
|
|
id,
|
2019-10-10 01:55:39 +00:00
|
|
|
pat: pat(),
|
2019-09-09 12:26:25 +00:00
|
|
|
span,
|
2019-10-10 01:55:39 +00:00
|
|
|
ty: ty(),
|
2019-09-09 12:26:25 +00:00
|
|
|
is_placeholder: true,
|
|
|
|
}]),
|
2022-01-05 05:56:45 +00:00
|
|
|
AstFragmentKind::FieldDefs => AstFragment::FieldDefs(smallvec![ast::FieldDef {
|
2019-09-09 12:26:25 +00:00
|
|
|
attrs: Default::default(),
|
|
|
|
id,
|
|
|
|
ident: None,
|
|
|
|
span,
|
2019-10-10 01:55:39 +00:00
|
|
|
ty: ty(),
|
2019-09-09 12:26:25 +00:00
|
|
|
vis,
|
|
|
|
is_placeholder: true,
|
2024-10-26 23:35:33 +00:00
|
|
|
safety: Safety::Default,
|
2024-08-24 17:22:48 +00:00
|
|
|
default: None,
|
2019-09-09 12:26:25 +00:00
|
|
|
}]),
|
|
|
|
AstFragmentKind::Variants => AstFragment::Variants(smallvec![ast::Variant {
|
|
|
|
attrs: Default::default(),
|
2024-05-09 08:44:40 +00:00
|
|
|
data: ast::VariantData::Struct {
|
|
|
|
fields: Default::default(),
|
|
|
|
recovered: ast::Recovered::No
|
|
|
|
},
|
2019-09-09 12:26:25 +00:00
|
|
|
disr_expr: None,
|
|
|
|
id,
|
|
|
|
ident,
|
|
|
|
span,
|
2019-11-07 10:26:36 +00:00
|
|
|
vis,
|
2019-09-09 12:26:25 +00:00
|
|
|
is_placeholder: true,
|
|
|
|
}]),
|
2016-08-29 05:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 14:18:03 +00:00
|
|
|
#[derive(Default)]
|
2024-08-27 02:40:38 +00:00
|
|
|
pub(crate) struct PlaceholderExpander {
|
2018-08-18 10:55:43 +00:00
|
|
|
expanded_fragments: FxHashMap<ast::NodeId, AstFragment>,
|
2016-08-29 05:32:41 +00:00
|
|
|
}
|
|
|
|
|
2021-08-26 14:18:03 +00:00
|
|
|
impl PlaceholderExpander {
|
2024-08-27 02:40:38 +00:00
|
|
|
pub(crate) fn add(&mut self, id: ast::NodeId, mut fragment: AstFragment) {
|
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
|
|
|
fragment.mut_visit_with(self);
|
2018-06-19 23:08:08 +00:00
|
|
|
self.expanded_fragments.insert(id, fragment);
|
2016-09-02 03:35:59 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 23:08:08 +00:00
|
|
|
fn remove(&mut self, id: ast::NodeId) -> AstFragment {
|
|
|
|
self.expanded_fragments.remove(&id).unwrap()
|
2016-08-29 05:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 14:18:03 +00:00
|
|
|
impl MutVisitor for PlaceholderExpander {
|
2019-09-09 12:26:25 +00: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 {
|
2024-07-17 11:23:35 +00:00
|
|
|
walk_flat_map_arm(self, arm)
|
2019-09-09 12:26:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 21:36:07 +00:00
|
|
|
fn flat_map_expr_field(&mut self, field: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
|
2019-09-09 12:26:25 +00:00
|
|
|
if field.is_placeholder {
|
2021-03-15 21:36:07 +00:00
|
|
|
self.remove(field.id).make_expr_fields()
|
2019-09-09 12:26:25 +00:00
|
|
|
} else {
|
2024-07-17 11:23:35 +00:00
|
|
|
walk_flat_map_expr_field(self, field)
|
2019-09-09 12:26:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 21:36:07 +00:00
|
|
|
fn flat_map_pat_field(&mut self, fp: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
|
2019-09-09 12:26:25 +00:00
|
|
|
if fp.is_placeholder {
|
2021-03-15 21:36:07 +00:00
|
|
|
self.remove(fp.id).make_pat_fields()
|
2019-09-09 12:26:25 +00:00
|
|
|
} else {
|
2024-07-17 11:23:35 +00:00
|
|
|
walk_flat_map_pat_field(self, fp)
|
2019-09-09 12:26:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-07-17 11:23:35 +00:00
|
|
|
walk_flat_map_generic_param(self, param)
|
2019-09-09 12:26:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
|
|
|
|
if p.is_placeholder {
|
|
|
|
self.remove(p.id).make_params()
|
|
|
|
} else {
|
2024-07-17 11:23:35 +00:00
|
|
|
walk_flat_map_param(self, p)
|
2019-09-09 12:26:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 21:36:07 +00:00
|
|
|
fn flat_map_field_def(&mut self, sf: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
|
2019-09-09 12:26:25 +00:00
|
|
|
if sf.is_placeholder {
|
2021-03-15 21:36:07 +00:00
|
|
|
self.remove(sf.id).make_field_defs()
|
2019-09-09 12:26:25 +00:00
|
|
|
} else {
|
2024-07-17 11:23:35 +00:00
|
|
|
walk_flat_map_field_def(self, sf)
|
2019-09-09 12:26:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
|
|
|
|
if variant.is_placeholder {
|
|
|
|
self.remove(variant.id).make_variants()
|
|
|
|
} else {
|
2024-07-17 11:23:35 +00:00
|
|
|
walk_flat_map_variant(self, variant)
|
2019-09-09 12:26:25 +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]> {
|
2019-09-26 16:51:36 +00:00
|
|
|
match item.kind {
|
2020-12-31 14:59:09 +00:00
|
|
|
ast::ItemKind::MacCall(_) => self.remove(item.id).make_items(),
|
2024-07-22 14:34:45 +00:00
|
|
|
_ => walk_flat_map_item(self, item),
|
2016-08-29 05:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-08 10:40:37 +00:00
|
|
|
fn flat_map_assoc_item(
|
|
|
|
&mut self,
|
|
|
|
item: P<ast::AssocItem>,
|
|
|
|
ctxt: AssocCtxt,
|
|
|
|
) -> SmallVec<[P<ast::AssocItem>; 1]> {
|
2019-09-26 15:38:13 +00:00
|
|
|
match item.kind {
|
2024-07-08 10:40:37 +00:00
|
|
|
ast::AssocItemKind::MacCall(_) => {
|
|
|
|
let it = self.remove(item.id);
|
|
|
|
match ctxt {
|
|
|
|
AssocCtxt::Trait => it.make_trait_items(),
|
|
|
|
AssocCtxt::Impl => it.make_impl_items(),
|
|
|
|
}
|
|
|
|
}
|
2024-11-08 21:51:28 +00:00
|
|
|
_ => walk_flat_map_assoc_item(self, item, ctxt),
|
2016-08-29 05:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 05:41:18 +00:00
|
|
|
fn flat_map_foreign_item(
|
|
|
|
&mut self,
|
|
|
|
item: P<ast::ForeignItem>,
|
|
|
|
) -> SmallVec<[P<ast::ForeignItem>; 1]> {
|
2019-09-26 16:58:14 +00:00
|
|
|
match item.kind {
|
2020-02-29 16:32:20 +00:00
|
|
|
ast::ForeignItemKind::MacCall(_) => self.remove(item.id).make_foreign_items(),
|
2024-11-17 23:11:49 +00:00
|
|
|
_ => walk_flat_map_foreign_item(self, item),
|
2018-03-11 02:16:26 +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>) {
|
2019-09-26 13:39:48 +00:00
|
|
|
match expr.kind {
|
2020-02-29 16:32:20 +00:00
|
|
|
ast::ExprKind::MacCall(_) => *expr = self.remove(expr.id).make_expr(),
|
2024-07-17 11:23:35 +00:00
|
|
|
_ => walk_expr(self, expr),
|
2016-08-29 05:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 09:22:19 +00:00
|
|
|
fn visit_method_receiver_expr(&mut self, expr: &mut P<ast::Expr>) {
|
|
|
|
match expr.kind {
|
|
|
|
ast::ExprKind::MacCall(_) => *expr = self.remove(expr.id).make_method_receiver_expr(),
|
2024-07-17 11:23:35 +00:00
|
|
|
_ => walk_expr(self, expr),
|
2022-10-23 09:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
|
2019-09-26 13:39:48 +00:00
|
|
|
match expr.kind {
|
2020-02-29 16:32:20 +00:00
|
|
|
ast::ExprKind::MacCall(_) => self.remove(expr.id).make_opt_expr(),
|
2024-07-17 11:23:35 +00:00
|
|
|
_ => noop_filter_map_expr(self, expr),
|
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 04:20:55 +00:00
|
|
|
fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
|
2019-09-26 16:34:50 +00:00
|
|
|
let (style, mut stmts) = match stmt.kind {
|
2020-08-30 22:38:32 +00:00
|
|
|
ast::StmtKind::MacCall(mac) => (mac.style, self.remove(stmt.id).make_stmts()),
|
2024-07-17 11:23:35 +00:00
|
|
|
_ => return walk_flat_map_stmt(self, stmt),
|
2016-08-29 05:32:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if style == ast::MacStmtStyle::Semicolon {
|
2020-10-25 21:14:19 +00:00
|
|
|
// Implement the proposal described in
|
|
|
|
// https://github.com/rust-lang/rust/issues/61733#issuecomment-509626449
|
|
|
|
//
|
2020-11-07 23:25:10 +00:00
|
|
|
// The macro invocation expands to the list of statements. If the
|
|
|
|
// list of statements is empty, then 'parse' the trailing semicolon
|
|
|
|
// on the original invocation as an empty statement. That is:
|
2020-10-25 21:14:19 +00:00
|
|
|
//
|
|
|
|
// `empty();` is parsed as a single `StmtKind::Empty`
|
|
|
|
//
|
2020-11-07 23:25:10 +00:00
|
|
|
// If the list of statements is non-empty, see if the final
|
|
|
|
// statement already has a trailing semicolon.
|
2020-10-25 21:14:19 +00:00
|
|
|
//
|
2020-11-07 23:25:10 +00:00
|
|
|
// If it doesn't have a semicolon, then 'parse' the trailing
|
|
|
|
// semicolon from the invocation as part of the final statement,
|
2020-10-25 21:14:19 +00:00
|
|
|
// using `stmt.add_trailing_semicolon()`
|
|
|
|
//
|
|
|
|
// If it does have a semicolon, then 'parse' the trailing semicolon
|
|
|
|
// from the invocation as a new StmtKind::Empty
|
|
|
|
|
2020-11-07 23:25:10 +00:00
|
|
|
// FIXME: We will need to preserve the original semicolon token and
|
|
|
|
// span as part of #15701
|
2020-11-17 19:27:44 +00:00
|
|
|
let empty_stmt =
|
|
|
|
ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Empty, span: DUMMY_SP };
|
2020-10-25 21:14:19 +00:00
|
|
|
|
2018-06-19 23:08:08 +00:00
|
|
|
if let Some(stmt) = stmts.pop() {
|
2020-10-25 21:14:19 +00:00
|
|
|
if stmt.has_trailing_semicolon() {
|
|
|
|
stmts.push(stmt);
|
|
|
|
stmts.push(empty_stmt);
|
|
|
|
} else {
|
|
|
|
stmts.push(stmt.add_trailing_semicolon());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stmts.push(empty_stmt);
|
2016-08-29 05:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-19 23:08:08 +00: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 04:20:55 +00:00
|
|
|
fn visit_pat(&mut self, pat: &mut P<ast::Pat>) {
|
2019-09-26 15:18:31 +00:00
|
|
|
match pat.kind {
|
2020-02-29 16:32:20 +00:00
|
|
|
ast::PatKind::MacCall(_) => *pat = self.remove(pat.id).make_pat(),
|
2024-07-17 11:23:35 +00:00
|
|
|
_ => walk_pat(self, pat),
|
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 04:20:55 +00:00
|
|
|
fn visit_ty(&mut self, ty: &mut P<ast::Ty>) {
|
2019-09-26 16:25:31 +00:00
|
|
|
match ty.kind {
|
2020-02-29 16:32:20 +00:00
|
|
|
ast::TyKind::MacCall(_) => *ty = self.remove(ty.id).make_ty(),
|
2024-07-17 11:23:35 +00:00
|
|
|
_ => walk_ty(self, ty),
|
2016-08-29 05:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-17 16:32:34 +00:00
|
|
|
|
|
|
|
fn visit_crate(&mut self, krate: &mut ast::Crate) {
|
2022-01-05 08:09:55 +00:00
|
|
|
if krate.is_placeholder {
|
|
|
|
*krate = self.remove(krate.id).make_crate();
|
2021-10-17 16:32:34 +00:00
|
|
|
} else {
|
2024-07-17 11:23:35 +00:00
|
|
|
walk_crate(self, krate)
|
2021-10-17 16:32:34 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-29 05:32:41 +00:00
|
|
|
}
|