2019-02-06 17:33:01 +00:00
|
|
|
|
use crate::ast::{self, Block, Ident, LitKind, NodeId, PatKind, Path};
|
|
|
|
|
use crate::ast::{MacStmtStyle, StmtKind, ItemKind};
|
|
|
|
|
use crate::attr::{self, HasAttrs};
|
2019-08-14 22:56:44 +00:00
|
|
|
|
use crate::source_map::respan;
|
2019-02-06 17:33:01 +00:00
|
|
|
|
use crate::config::StripUnconfigured;
|
|
|
|
|
use crate::ext::base::*;
|
2019-08-24 18:12:13 +00:00
|
|
|
|
use crate::ext::proc_macro::{collect_derives, MarkAttrs};
|
2019-08-13 20:56:42 +00:00
|
|
|
|
use crate::ext::hygiene::{ExpnId, SyntaxContext, ExpnData, ExpnKind};
|
2019-09-22 15:25:29 +00:00
|
|
|
|
use crate::ext::mbe::macro_rules::annotate_err_with_kind;
|
2019-02-06 17:33:01 +00:00
|
|
|
|
use crate::ext::placeholders::{placeholder, PlaceholderExpander};
|
|
|
|
|
use crate::feature_gate::{self, Features, GateIssue, is_builtin_attr, emit_feature_err};
|
|
|
|
|
use crate::mut_visit::*;
|
|
|
|
|
use crate::parse::{DirectoryOwnership, PResult, ParseSess};
|
2019-05-23 23:04:56 +00:00
|
|
|
|
use crate::parse::token;
|
2019-02-06 17:33:01 +00:00
|
|
|
|
use crate::parse::parser::Parser;
|
|
|
|
|
use crate::ptr::P;
|
2019-06-30 22:08:49 +00:00
|
|
|
|
use crate::symbol::{sym, Symbol};
|
2019-02-06 17:33:01 +00:00
|
|
|
|
use crate::tokenstream::{TokenStream, TokenTree};
|
|
|
|
|
use crate::visit::{self, Visitor};
|
|
|
|
|
use crate::util::map_in_place::MapInPlace;
|
|
|
|
|
|
2019-02-09 02:24:02 +00:00
|
|
|
|
use errors::{Applicability, FatalError};
|
2019-02-06 17:33:01 +00:00
|
|
|
|
use smallvec::{smallvec, SmallVec};
|
2017-12-14 07:09:19 +00:00
|
|
|
|
use syntax_pos::{Span, DUMMY_SP, FileName};
|
2011-08-05 20:06:11 +00:00
|
|
|
|
|
2018-08-18 10:55:43 +00:00
|
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2019-02-14 22:10:02 +00:00
|
|
|
|
use rustc_data_structures::sync::Lrc;
|
2018-11-16 21:22:06 +00:00
|
|
|
|
use std::io::ErrorKind;
|
2019-09-14 18:29:59 +00:00
|
|
|
|
use std::{iter, mem, slice};
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
use std::ops::DerefMut;
|
2016-09-01 06:44:54 +00:00
|
|
|
|
use std::rc::Rc;
|
2017-12-14 07:09:19 +00:00
|
|
|
|
use std::path::PathBuf;
|
2016-08-31 09:02:45 +00:00
|
|
|
|
|
2018-06-19 23:08:08 +00:00
|
|
|
|
macro_rules! ast_fragments {
|
2018-06-22 22:05:07 +00:00
|
|
|
|
(
|
|
|
|
|
$($Kind:ident($AstTy:ty) {
|
|
|
|
|
$kind_name:expr;
|
2019-02-20 07:10:11 +00:00
|
|
|
|
$(one fn $mut_visit_ast:ident; fn $visit_ast:ident;)?
|
|
|
|
|
$(many fn $flat_map_ast_elt:ident; fn $visit_ast_elt:ident;)?
|
2018-06-22 22:05:07 +00:00
|
|
|
|
fn $make_ast:ident;
|
|
|
|
|
})*
|
|
|
|
|
) => {
|
2018-06-19 23:08:08 +00:00
|
|
|
|
/// A fragment of AST that can be produced by a single macro expansion.
|
|
|
|
|
/// Can also serve as an input and intermediate result for macro expansion operations.
|
2018-06-22 22:05:07 +00:00
|
|
|
|
pub enum AstFragment {
|
|
|
|
|
OptExpr(Option<P<ast::Expr>>),
|
|
|
|
|
$($Kind($AstTy),)*
|
|
|
|
|
}
|
2018-06-19 23:08:08 +00:00
|
|
|
|
|
|
|
|
|
/// "Discriminant" of an AST fragment.
|
2016-09-23 09:32:58 +00:00
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
2018-06-22 22:05:07 +00:00
|
|
|
|
pub enum AstFragmentKind {
|
|
|
|
|
OptExpr,
|
|
|
|
|
$($Kind,)*
|
|
|
|
|
}
|
2016-08-27 05:27:59 +00:00
|
|
|
|
|
2018-06-19 23:08:08 +00:00
|
|
|
|
impl AstFragmentKind {
|
2016-09-23 09:32:58 +00:00
|
|
|
|
pub fn name(self) -> &'static str {
|
2016-08-27 05:27:59 +00:00
|
|
|
|
match self {
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragmentKind::OptExpr => "expression",
|
2018-06-22 22:05:07 +00:00
|
|
|
|
$(AstFragmentKind::$Kind => $kind_name,)*
|
2016-08-27 05:27:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-24 06:12:54 +00:00
|
|
|
|
|
2018-07-10 19:06:26 +00:00
|
|
|
|
fn make_from<'a>(self, result: Box<dyn MacResult + 'a>) -> Option<AstFragment> {
|
2016-08-27 05:27:59 +00:00
|
|
|
|
match self {
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragmentKind::OptExpr =>
|
|
|
|
|
result.make_expr().map(Some).map(AstFragment::OptExpr),
|
2018-06-22 22:05:07 +00:00
|
|
|
|
$(AstFragmentKind::$Kind => result.$make_ast().map(AstFragment::$Kind),)*
|
2016-08-27 05:27:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-17 11:02:42 +00:00
|
|
|
|
|
2018-06-19 23:08:08 +00:00
|
|
|
|
impl AstFragment {
|
2016-08-29 05:32:41 +00:00
|
|
|
|
pub fn make_opt_expr(self) -> Option<P<ast::Expr>> {
|
2016-08-27 05:27:59 +00:00
|
|
|
|
match self {
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragment::OptExpr(expr) => expr,
|
|
|
|
|
_ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
|
2016-08-27 05:27:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-22 22:05:07 +00:00
|
|
|
|
|
|
|
|
|
$(pub fn $make_ast(self) -> $AstTy {
|
2016-08-27 05:27:59 +00:00
|
|
|
|
match self {
|
2018-06-22 22:05:07 +00:00
|
|
|
|
AstFragment::$Kind(ast) => ast,
|
2018-06-19 23:08:08 +00:00
|
|
|
|
_ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
|
2016-08-27 05:27:59 +00:00
|
|
|
|
}
|
2018-06-22 22:05:07 +00:00
|
|
|
|
})*
|
2016-05-19 09:45:37 +00:00
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
pub fn mut_visit_with<F: MutVisitor>(&mut self, vis: &mut F) {
|
2016-08-27 05:27:59 +00:00
|
|
|
|
match self {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
AstFragment::OptExpr(opt_expr) => {
|
|
|
|
|
visit_clobber(opt_expr, |opt_expr| {
|
|
|
|
|
if let Some(expr) = opt_expr {
|
|
|
|
|
vis.filter_map_expr(expr)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-05-29 18:05:43 +00:00
|
|
|
|
$($(AstFragment::$Kind(ast) => vis.$mut_visit_ast(ast),)?)*
|
2018-06-22 22:05:07 +00:00
|
|
|
|
$($(AstFragment::$Kind(ast) =>
|
2019-05-29 18:05:43 +00:00
|
|
|
|
ast.flat_map_in_place(|ast| vis.$flat_map_ast_elt(ast)),)?)*
|
2016-08-27 05:27:59 +00:00
|
|
|
|
}
|
2016-05-19 09:45:37 +00:00
|
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
|
|
2016-12-06 10:26:52 +00:00
|
|
|
|
pub fn visit_with<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) {
|
2016-09-07 23:21:59 +00:00
|
|
|
|
match *self {
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragment::OptExpr(Some(ref expr)) => visitor.visit_expr(expr),
|
|
|
|
|
AstFragment::OptExpr(None) => {}
|
2019-05-29 18:05:43 +00:00
|
|
|
|
$($(AstFragment::$Kind(ref ast) => visitor.$visit_ast(ast),)?)*
|
2018-06-22 22:05:07 +00:00
|
|
|
|
$($(AstFragment::$Kind(ref ast) => for ast_elt in &ast[..] {
|
|
|
|
|
visitor.$visit_ast_elt(ast_elt);
|
2019-05-29 18:05:43 +00:00
|
|
|
|
})?)*
|
2016-09-07 23:21:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-19 09:45:37 +00:00
|
|
|
|
}
|
2016-09-02 09:12:47 +00:00
|
|
|
|
|
2019-09-22 15:25:29 +00:00
|
|
|
|
impl<'a> MacResult for crate::ext::mbe::macro_rules::ParserAnyMacro<'a> {
|
|
|
|
|
$(fn $make_ast(self: Box<crate::ext::mbe::macro_rules::ParserAnyMacro<'a>>)
|
2018-06-22 22:05:07 +00:00
|
|
|
|
-> Option<$AstTy> {
|
|
|
|
|
Some(self.make(AstFragmentKind::$Kind).$make_ast())
|
2016-09-23 09:32:58 +00:00
|
|
|
|
})*
|
|
|
|
|
}
|
2016-08-27 05:27:59 +00:00
|
|
|
|
}
|
2016-05-19 09:45:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 23:08:08 +00:00
|
|
|
|
ast_fragments! {
|
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
|
|
|
|
Expr(P<ast::Expr>) { "expression"; one fn visit_expr; fn visit_expr; fn make_expr; }
|
|
|
|
|
Pat(P<ast::Pat>) { "pattern"; one fn visit_pat; fn visit_pat; fn make_pat; }
|
|
|
|
|
Ty(P<ast::Ty>) { "type"; one fn visit_ty; fn visit_ty; fn make_ty; }
|
2018-08-30 09:42:16 +00:00
|
|
|
|
Stmts(SmallVec<[ast::Stmt; 1]>) {
|
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
|
|
|
|
"statement"; many fn flat_map_stmt; fn visit_stmt; fn make_stmts;
|
2018-08-30 09:42:16 +00:00
|
|
|
|
}
|
|
|
|
|
Items(SmallVec<[P<ast::Item>; 1]>) {
|
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
|
|
|
|
"item"; many fn flat_map_item; fn visit_item; fn make_items;
|
2018-08-30 09:42:16 +00:00
|
|
|
|
}
|
|
|
|
|
TraitItems(SmallVec<[ast::TraitItem; 1]>) {
|
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
|
|
|
|
"trait item"; many fn flat_map_trait_item; fn visit_trait_item; fn make_trait_items;
|
2018-06-22 22:05:07 +00:00
|
|
|
|
}
|
2018-08-30 09:42:16 +00:00
|
|
|
|
ImplItems(SmallVec<[ast::ImplItem; 1]>) {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
"impl item"; many fn flat_map_impl_item; fn visit_impl_item; fn make_impl_items;
|
2018-06-22 22:05:07 +00:00
|
|
|
|
}
|
2018-08-30 09:42:16 +00:00
|
|
|
|
ForeignItems(SmallVec<[ast::ForeignItem; 1]>) {
|
2019-09-09 12:26:25 +00:00
|
|
|
|
"foreign item";
|
|
|
|
|
many fn flat_map_foreign_item;
|
|
|
|
|
fn visit_foreign_item;
|
|
|
|
|
fn make_foreign_items;
|
|
|
|
|
}
|
|
|
|
|
Arms(SmallVec<[ast::Arm; 1]>) {
|
|
|
|
|
"match arm"; many fn flat_map_arm; fn visit_arm; fn make_arms;
|
|
|
|
|
}
|
|
|
|
|
Fields(SmallVec<[ast::Field; 1]>) {
|
|
|
|
|
"field expression"; many fn flat_map_field; fn visit_field; fn make_fields;
|
|
|
|
|
}
|
|
|
|
|
FieldPats(SmallVec<[ast::FieldPat; 1]>) {
|
|
|
|
|
"field pattern";
|
|
|
|
|
many fn flat_map_field_pattern;
|
|
|
|
|
fn visit_field_pattern;
|
|
|
|
|
fn make_field_patterns;
|
|
|
|
|
}
|
|
|
|
|
GenericParams(SmallVec<[ast::GenericParam; 1]>) {
|
|
|
|
|
"generic parameter";
|
|
|
|
|
many fn flat_map_generic_param;
|
|
|
|
|
fn visit_generic_param;
|
|
|
|
|
fn make_generic_params;
|
|
|
|
|
}
|
|
|
|
|
Params(SmallVec<[ast::Param; 1]>) {
|
|
|
|
|
"function parameter"; many fn flat_map_param; fn visit_param; fn make_params;
|
|
|
|
|
}
|
|
|
|
|
StructFields(SmallVec<[ast::StructField; 1]>) {
|
|
|
|
|
"field";
|
|
|
|
|
many fn flat_map_struct_field;
|
|
|
|
|
fn visit_struct_field;
|
|
|
|
|
fn make_struct_fields;
|
|
|
|
|
}
|
|
|
|
|
Variants(SmallVec<[ast::Variant; 1]>) {
|
|
|
|
|
"variant"; many fn flat_map_variant; fn visit_variant; fn make_variants;
|
2018-06-22 22:05:07 +00:00
|
|
|
|
}
|
2016-05-19 09:45:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 23:08:08 +00:00
|
|
|
|
impl AstFragmentKind {
|
2019-07-07 15:29:22 +00:00
|
|
|
|
fn dummy(self, span: Span) -> AstFragment {
|
|
|
|
|
self.make_from(DummyResult::any(span)).expect("couldn't create a dummy AST fragment")
|
2016-06-11 22:59:33 +00:00
|
|
|
|
}
|
2016-09-02 06:14:38 +00:00
|
|
|
|
|
2018-06-19 23:08:08 +00:00
|
|
|
|
fn expect_from_annotatables<I: IntoIterator<Item = Annotatable>>(self, items: I)
|
|
|
|
|
-> AstFragment {
|
2018-04-18 06:19:21 +00:00
|
|
|
|
let mut items = items.into_iter();
|
2016-09-02 06:14:38 +00:00
|
|
|
|
match self {
|
2019-09-09 12:26:25 +00:00
|
|
|
|
AstFragmentKind::Arms =>
|
|
|
|
|
AstFragment::Arms(items.map(Annotatable::expect_arm).collect()),
|
|
|
|
|
AstFragmentKind::Fields =>
|
|
|
|
|
AstFragment::Fields(items.map(Annotatable::expect_field).collect()),
|
|
|
|
|
AstFragmentKind::FieldPats =>
|
|
|
|
|
AstFragment::FieldPats(items.map(Annotatable::expect_field_pattern).collect()),
|
|
|
|
|
AstFragmentKind::GenericParams =>
|
|
|
|
|
AstFragment::GenericParams(items.map(Annotatable::expect_generic_param).collect()),
|
|
|
|
|
AstFragmentKind::Params =>
|
|
|
|
|
AstFragment::Params(items.map(Annotatable::expect_param).collect()),
|
|
|
|
|
AstFragmentKind::StructFields => AstFragment::StructFields(
|
|
|
|
|
items.map(Annotatable::expect_struct_field).collect()
|
|
|
|
|
),
|
|
|
|
|
AstFragmentKind::Variants =>
|
|
|
|
|
AstFragment::Variants(items.map(Annotatable::expect_variant).collect()),
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragmentKind::Items =>
|
|
|
|
|
AstFragment::Items(items.map(Annotatable::expect_item).collect()),
|
|
|
|
|
AstFragmentKind::ImplItems =>
|
|
|
|
|
AstFragment::ImplItems(items.map(Annotatable::expect_impl_item).collect()),
|
|
|
|
|
AstFragmentKind::TraitItems =>
|
|
|
|
|
AstFragment::TraitItems(items.map(Annotatable::expect_trait_item).collect()),
|
|
|
|
|
AstFragmentKind::ForeignItems =>
|
|
|
|
|
AstFragment::ForeignItems(items.map(Annotatable::expect_foreign_item).collect()),
|
|
|
|
|
AstFragmentKind::Stmts =>
|
|
|
|
|
AstFragment::Stmts(items.map(Annotatable::expect_stmt).collect()),
|
|
|
|
|
AstFragmentKind::Expr => AstFragment::Expr(
|
2018-04-18 06:19:21 +00:00
|
|
|
|
items.next().expect("expected exactly one expression").expect_expr()
|
|
|
|
|
),
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragmentKind::OptExpr =>
|
|
|
|
|
AstFragment::OptExpr(items.next().map(Annotatable::expect_expr)),
|
|
|
|
|
AstFragmentKind::Pat | AstFragmentKind::Ty =>
|
2018-04-18 06:19:21 +00:00
|
|
|
|
panic!("patterns and types aren't annotatable"),
|
2016-09-02 06:14:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-16 10:09:23 +00:00
|
|
|
|
}
|
2015-01-27 00:22:12 +00:00
|
|
|
|
|
2016-08-27 05:27:59 +00:00
|
|
|
|
pub struct Invocation {
|
2016-09-07 23:21:59 +00:00
|
|
|
|
pub kind: InvocationKind,
|
2019-09-09 12:26:25 +00:00
|
|
|
|
pub fragment_kind: AstFragmentKind,
|
2017-03-01 23:48:16 +00:00
|
|
|
|
pub expansion_data: ExpansionData,
|
2016-09-02 06:14:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
|
pub enum InvocationKind {
|
2016-09-02 06:14:38 +00:00
|
|
|
|
Bang {
|
|
|
|
|
mac: ast::Mac,
|
|
|
|
|
span: Span,
|
|
|
|
|
},
|
|
|
|
|
Attr {
|
2019-07-07 22:00:43 +00:00
|
|
|
|
attr: ast::Attribute,
|
2016-09-02 06:14:38 +00:00
|
|
|
|
item: Annotatable,
|
2019-07-07 22:00:43 +00:00
|
|
|
|
// Required for resolving derive helper attributes.
|
|
|
|
|
derives: Vec<Path>,
|
2018-09-18 22:46:18 +00:00
|
|
|
|
// We temporarily report errors for attribute macros placed after derives
|
|
|
|
|
after_derive: bool,
|
2016-09-02 06:14:38 +00:00
|
|
|
|
},
|
2017-02-01 10:33:09 +00:00
|
|
|
|
Derive {
|
2017-03-08 23:13:35 +00:00
|
|
|
|
path: Path,
|
2017-02-01 10:33:09 +00:00
|
|
|
|
item: Annotatable,
|
|
|
|
|
},
|
2019-07-07 22:00:43 +00:00
|
|
|
|
/// "Invocation" that contains all derives from an item,
|
|
|
|
|
/// broken into multiple `Derive` invocations when expanded.
|
|
|
|
|
/// FIXME: Find a way to remove it.
|
|
|
|
|
DeriveContainer {
|
|
|
|
|
derives: Vec<Path>,
|
|
|
|
|
item: Annotatable,
|
|
|
|
|
},
|
2011-07-06 22:22:23 +00:00
|
|
|
|
}
|
2013-07-30 00:25:00 +00:00
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
|
impl Invocation {
|
2018-07-12 10:24:59 +00:00
|
|
|
|
pub fn span(&self) -> Span {
|
2019-07-07 22:00:43 +00:00
|
|
|
|
match &self.kind {
|
|
|
|
|
InvocationKind::Bang { span, .. } => *span,
|
|
|
|
|
InvocationKind::Attr { attr, .. } => attr.span,
|
|
|
|
|
InvocationKind::Derive { path, .. } => path.span,
|
|
|
|
|
InvocationKind::DeriveContainer { item, .. } => item.span(),
|
2016-09-07 23:21:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 16:39:39 +00:00
|
|
|
|
pub struct MacroExpander<'a, 'b> {
|
2014-03-27 22:39:48 +00:00
|
|
|
|
pub cx: &'a mut ExtCtxt<'b>,
|
2018-10-21 23:45:24 +00:00
|
|
|
|
monotonic: bool, // cf. `cx.monotonic_expander()`
|
2014-12-14 02:42:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, 'b> MacroExpander<'a, 'b> {
|
2016-09-06 05:42:45 +00:00
|
|
|
|
pub fn new(cx: &'a mut ExtCtxt<'b>, monotonic: bool) -> Self {
|
2019-06-25 21:22:45 +00:00
|
|
|
|
MacroExpander { cx, monotonic }
|
2014-12-14 02:42:41 +00:00
|
|
|
|
}
|
2016-05-16 10:09:23 +00:00
|
|
|
|
|
2016-09-29 00:22:46 +00:00
|
|
|
|
pub fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
|
|
|
|
|
let mut module = ModuleData {
|
2016-11-16 08:21:52 +00:00
|
|
|
|
mod_path: vec![Ident::from_str(&self.cx.ecfg.crate_name)],
|
2018-08-18 10:14:09 +00:00
|
|
|
|
directory: match self.cx.source_map().span_to_unmapped_path(krate.span) {
|
2017-12-14 07:09:19 +00:00
|
|
|
|
FileName::Real(path) => path,
|
|
|
|
|
other => PathBuf::from(other.to_string()),
|
|
|
|
|
},
|
2016-09-29 00:22:46 +00:00
|
|
|
|
};
|
|
|
|
|
module.directory.pop();
|
2017-09-22 03:37:00 +00:00
|
|
|
|
self.cx.root_path = module.directory.clone();
|
2016-09-29 00:22:46 +00:00
|
|
|
|
self.cx.current_expansion.module = Rc::new(module);
|
2016-09-02 09:12:47 +00:00
|
|
|
|
|
2017-04-20 23:13:13 +00:00
|
|
|
|
let orig_mod_span = krate.module.inner;
|
|
|
|
|
|
2018-08-13 19:15:16 +00:00
|
|
|
|
let krate_item = AstFragment::Items(smallvec![P(ast::Item {
|
2016-09-21 09:20:42 +00:00
|
|
|
|
attrs: krate.attrs,
|
|
|
|
|
span: krate.span,
|
2019-09-26 16:51:36 +00:00
|
|
|
|
kind: ast::ItemKind::Mod(krate.module),
|
2019-05-11 16:08:09 +00:00
|
|
|
|
ident: Ident::invalid(),
|
2016-09-21 09:20:42 +00:00
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2018-03-10 14:45:47 +00:00
|
|
|
|
vis: respan(krate.span.shrink_to_lo(), ast::VisibilityKind::Public),
|
2017-07-11 00:44:46 +00:00
|
|
|
|
tokens: None,
|
2018-08-13 19:15:16 +00:00
|
|
|
|
})]);
|
2016-09-21 09:20:42 +00:00
|
|
|
|
|
2019-08-13 22:59:14 +00:00
|
|
|
|
match self.fully_expand_fragment(krate_item).make_items().pop().map(P::into_inner) {
|
2019-09-26 16:51:36 +00:00
|
|
|
|
Some(ast::Item { attrs, kind: ast::ItemKind::Mod(module), .. }) => {
|
2016-09-21 09:20:42 +00:00
|
|
|
|
krate.attrs = attrs;
|
|
|
|
|
krate.module = module;
|
|
|
|
|
},
|
2017-04-20 23:13:13 +00:00
|
|
|
|
None => {
|
|
|
|
|
// Resolution failed so we return an empty expansion
|
|
|
|
|
krate.attrs = vec![];
|
|
|
|
|
krate.module = ast::Mod {
|
|
|
|
|
inner: orig_mod_span,
|
|
|
|
|
items: vec![],
|
2018-07-11 13:19:32 +00:00
|
|
|
|
inline: true,
|
2017-04-20 23:13:13 +00:00
|
|
|
|
};
|
|
|
|
|
},
|
2016-09-06 01:45:23 +00:00
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
2017-05-06 04:49:59 +00:00
|
|
|
|
self.cx.trace_macros_diag();
|
2016-09-02 09:12:47 +00:00
|
|
|
|
krate
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 22:59:14 +00:00
|
|
|
|
// Recursively expand all macro invocations in this AST fragment.
|
2019-08-13 23:30:09 +00:00
|
|
|
|
pub fn fully_expand_fragment(&mut self, input_fragment: AstFragment) -> AstFragment {
|
2016-09-07 23:21:59 +00:00
|
|
|
|
let orig_expansion_data = self.cx.current_expansion.clone();
|
|
|
|
|
self.cx.current_expansion.depth = 0;
|
|
|
|
|
|
2018-06-23 16:27:01 +00:00
|
|
|
|
// Collect all macro invocations and replace them with placeholders.
|
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
|
|
|
|
let (mut fragment_with_placeholders, mut invocations)
|
2018-06-19 23:08:08 +00:00
|
|
|
|
= self.collect_invocations(input_fragment, &[]);
|
2018-06-23 16:27:01 +00:00
|
|
|
|
|
|
|
|
|
// Optimization: if we resolve all imports now,
|
|
|
|
|
// we'll be able to immediately resolve most of imported macros.
|
2016-11-10 10:11:25 +00:00
|
|
|
|
self.resolve_imports();
|
2016-09-02 09:12:47 +00:00
|
|
|
|
|
2018-08-19 13:30:23 +00:00
|
|
|
|
// Resolve paths in all invocations and produce output expanded fragments for them, but
|
2018-06-23 16:27:01 +00:00
|
|
|
|
// do not insert them into our input AST fragment yet, only store in `expanded_fragments`.
|
|
|
|
|
// The output fragments also go through expansion recursively until no invocations are left.
|
|
|
|
|
// Unresolved macros produce dummy outputs as a recovery measure.
|
|
|
|
|
invocations.reverse();
|
2018-06-19 23:08:08 +00:00
|
|
|
|
let mut expanded_fragments = Vec::new();
|
2019-08-17 17:49:00 +00:00
|
|
|
|
let mut all_derive_placeholders: FxHashMap<ExpnId, Vec<_>> = FxHashMap::default();
|
2016-10-11 03:41:48 +00:00
|
|
|
|
let mut undetermined_invocations = Vec::new();
|
|
|
|
|
let (mut progress, mut force) = (false, !self.monotonic);
|
|
|
|
|
loop {
|
2018-08-02 23:30:03 +00:00
|
|
|
|
let invoc = if let Some(invoc) = invocations.pop() {
|
2016-10-11 03:41:48 +00:00
|
|
|
|
invoc
|
|
|
|
|
} else {
|
2016-11-10 10:11:25 +00:00
|
|
|
|
self.resolve_imports();
|
|
|
|
|
if undetermined_invocations.is_empty() { break }
|
2019-06-30 18:30:01 +00:00
|
|
|
|
invocations = mem::take(&mut undetermined_invocations);
|
2016-10-11 03:41:48 +00:00
|
|
|
|
force = !mem::replace(&mut progress, false);
|
|
|
|
|
continue
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-19 21:24:28 +00:00
|
|
|
|
let eager_expansion_root =
|
2019-07-15 22:42:58 +00:00
|
|
|
|
if self.monotonic { invoc.expansion_data.id } else { orig_expansion_data.id };
|
2019-08-24 18:12:13 +00:00
|
|
|
|
let res = match self.cx.resolver.resolve_macro_invocation(
|
2019-08-19 21:24:28 +00:00
|
|
|
|
&invoc, eager_expansion_root, force
|
|
|
|
|
) {
|
2019-08-24 18:12:13 +00:00
|
|
|
|
Ok(res) => res,
|
2019-07-03 08:44:57 +00:00
|
|
|
|
Err(Indeterminate) => {
|
2016-10-11 03:41:48 +00:00
|
|
|
|
undetermined_invocations.push(invoc);
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
progress = true;
|
2019-07-15 22:42:58 +00:00
|
|
|
|
let ExpansionData { depth, id: expn_id, .. } = invoc.expansion_data;
|
2016-09-07 23:21:59 +00:00
|
|
|
|
self.cx.current_expansion = invoc.expansion_data.clone();
|
2019-07-03 09:47:24 +00:00
|
|
|
|
|
2017-02-02 07:01:15 +00:00
|
|
|
|
// FIXME(jseyfried): Refactor out the following logic
|
2019-08-24 18:12:13 +00:00
|
|
|
|
let (expanded_fragment, new_invocations) = match res {
|
|
|
|
|
InvocationRes::Single(ext) => {
|
|
|
|
|
let fragment = self.expand_invoc(invoc, &ext.kind);
|
|
|
|
|
self.collect_invocations(fragment, &[])
|
2019-07-03 08:44:57 +00:00
|
|
|
|
}
|
2019-08-24 18:12:13 +00:00
|
|
|
|
InvocationRes::DeriveContainer(exts) => {
|
|
|
|
|
let (derives, item) = match invoc.kind {
|
|
|
|
|
InvocationKind::DeriveContainer { derives, item } => (derives, item),
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
|
|
|
|
if !item.derive_allowed() {
|
|
|
|
|
let attr = attr::find_by_name(item.attrs(), sym::derive)
|
|
|
|
|
.expect("`derive` attribute should exist");
|
|
|
|
|
let span = attr.span;
|
2019-09-23 20:28:14 +00:00
|
|
|
|
let mut err = self.cx.struct_span_err(span,
|
2019-08-24 18:12:13 +00:00
|
|
|
|
"`derive` may only be applied to structs, enums and unions");
|
|
|
|
|
if let ast::AttrStyle::Inner = attr.style {
|
|
|
|
|
let trait_list = derives.iter()
|
|
|
|
|
.map(|t| t.to_string()).collect::<Vec<_>>();
|
|
|
|
|
let suggestion = format!("#[derive({})]", trait_list.join(", "));
|
|
|
|
|
err.span_suggestion(
|
|
|
|
|
span, "try an outer attribute", suggestion,
|
|
|
|
|
// We don't 𝑘𝑛𝑜𝑤 that the following item is an ADT
|
|
|
|
|
Applicability::MaybeIncorrect
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
err.emit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut item = self.fully_configure(item);
|
|
|
|
|
item.visit_attrs(|attrs| attrs.retain(|a| a.path != sym::derive));
|
|
|
|
|
let mut helper_attrs = Vec::new();
|
|
|
|
|
let mut has_copy = false;
|
|
|
|
|
for ext in exts {
|
|
|
|
|
helper_attrs.extend(&ext.helper_attrs);
|
|
|
|
|
has_copy |= ext.is_derive_copy;
|
|
|
|
|
}
|
|
|
|
|
// Mark derive helpers inside this item as known and used.
|
|
|
|
|
// FIXME: This is a hack, derive helpers should be integrated with regular name
|
|
|
|
|
// resolution instead. For example, helpers introduced by a derive container
|
|
|
|
|
// can be in scope for all code produced by that container's expansion.
|
|
|
|
|
item.visit_with(&mut MarkAttrs(&helper_attrs));
|
|
|
|
|
if has_copy {
|
|
|
|
|
self.cx.resolver.add_derives(invoc.expansion_data.id, SpecialDerives::COPY);
|
|
|
|
|
}
|
2017-08-23 02:22:52 +00:00
|
|
|
|
|
2019-08-24 18:12:13 +00:00
|
|
|
|
let derive_placeholders =
|
|
|
|
|
all_derive_placeholders.entry(invoc.expansion_data.id).or_default();
|
|
|
|
|
derive_placeholders.reserve(derives.len());
|
|
|
|
|
invocations.reserve(derives.len());
|
|
|
|
|
for path in derives {
|
|
|
|
|
let expn_id = ExpnId::fresh(None);
|
|
|
|
|
derive_placeholders.push(NodeId::placeholder_from_expn_id(expn_id));
|
|
|
|
|
invocations.push(Invocation {
|
|
|
|
|
kind: InvocationKind::Derive { path, item: item.clone() },
|
|
|
|
|
fragment_kind: invoc.fragment_kind,
|
|
|
|
|
expansion_data: ExpansionData {
|
|
|
|
|
id: expn_id,
|
|
|
|
|
..invoc.expansion_data.clone()
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
let fragment = invoc.fragment_kind
|
|
|
|
|
.expect_from_annotatables(::std::iter::once(item));
|
|
|
|
|
self.collect_invocations(fragment, derive_placeholders)
|
2017-02-02 07:01:15 +00:00
|
|
|
|
}
|
2016-09-07 23:21:59 +00:00
|
|
|
|
};
|
2016-09-02 09:12:47 +00:00
|
|
|
|
|
2018-06-19 23:08:08 +00:00
|
|
|
|
if expanded_fragments.len() < depth {
|
|
|
|
|
expanded_fragments.push(Vec::new());
|
2016-09-02 03:35:59 +00:00
|
|
|
|
}
|
2019-07-15 22:42:58 +00:00
|
|
|
|
expanded_fragments[depth - 1].push((expn_id, expanded_fragment));
|
2016-09-12 09:47:54 +00:00
|
|
|
|
if !self.cx.ecfg.single_step {
|
2016-09-02 09:12:47 +00:00
|
|
|
|
invocations.extend(new_invocations.into_iter().rev());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
|
self.cx.current_expansion = orig_expansion_data;
|
|
|
|
|
|
2018-06-23 16:27:01 +00:00
|
|
|
|
// Finally incorporate all the expanded macros into the input AST fragment.
|
2016-09-06 05:42:45 +00:00
|
|
|
|
let mut placeholder_expander = PlaceholderExpander::new(self.cx, self.monotonic);
|
2018-06-19 23:08:08 +00:00
|
|
|
|
while let Some(expanded_fragments) = expanded_fragments.pop() {
|
2019-08-17 17:49:00 +00:00
|
|
|
|
for (expn_id, expanded_fragment) in expanded_fragments.into_iter().rev() {
|
|
|
|
|
let derive_placeholders =
|
|
|
|
|
all_derive_placeholders.remove(&expn_id).unwrap_or_else(Vec::new);
|
|
|
|
|
placeholder_expander.add(NodeId::placeholder_from_expn_id(expn_id),
|
|
|
|
|
expanded_fragment, derive_placeholders);
|
2016-09-02 03:35:59 +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
|
|
|
|
fragment_with_placeholders.mut_visit_with(&mut placeholder_expander);
|
|
|
|
|
fragment_with_placeholders
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-10 10:11:25 +00:00
|
|
|
|
fn resolve_imports(&mut self) {
|
|
|
|
|
if self.monotonic {
|
|
|
|
|
self.cx.resolver.resolve_imports();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
|
/// Collects all macro invocations reachable at this time in this AST fragment, and replace
|
2018-06-23 16:27:01 +00:00
|
|
|
|
/// them with "placeholders" - dummy macro invocations with specially crafted `NodeId`s.
|
|
|
|
|
/// Then call into resolver that builds a skeleton ("reduced graph") of the fragment and
|
|
|
|
|
/// prepares data for resolving paths of macro invocations.
|
2019-08-17 17:49:00 +00:00
|
|
|
|
fn collect_invocations(&mut self, mut fragment: AstFragment, extra_placeholders: &[NodeId])
|
2018-06-19 23:08:08 +00:00
|
|
|
|
-> (AstFragment, Vec<Invocation>) {
|
2019-01-26 13:29:34 +00:00
|
|
|
|
// Resolve `$crate`s in the fragment for pretty-printing.
|
2019-07-05 00:09:24 +00:00
|
|
|
|
self.cx.resolver.resolve_dollar_crates();
|
2019-01-26 13:29:34 +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
|
|
|
|
let invocations = {
|
2016-09-07 22:24:01 +00:00
|
|
|
|
let mut collector = InvocationCollector {
|
|
|
|
|
cfg: StripUnconfigured {
|
|
|
|
|
sess: self.cx.parse_sess,
|
|
|
|
|
features: self.cx.ecfg.features,
|
|
|
|
|
},
|
|
|
|
|
cx: self.cx,
|
|
|
|
|
invocations: Vec::new(),
|
2016-09-06 05:42:45 +00:00
|
|
|
|
monotonic: self.monotonic,
|
2016-09-07 22:24:01 +00:00
|
|
|
|
};
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
fragment.mut_visit_with(&mut collector);
|
|
|
|
|
collector.invocations
|
2016-09-07 22:24:01 +00:00
|
|
|
|
};
|
2016-09-07 23:21:59 +00:00
|
|
|
|
|
2019-08-17 17:49:00 +00:00
|
|
|
|
// FIXME: Merge `extra_placeholders` into the `fragment` as regular placeholders.
|
2016-09-19 07:27:20 +00:00
|
|
|
|
if self.monotonic {
|
2018-12-16 17:23:27 +00:00
|
|
|
|
self.cx.resolver.visit_ast_fragment_with_placeholders(
|
2019-08-17 17:49:00 +00:00
|
|
|
|
self.cx.current_expansion.id, &fragment, extra_placeholders);
|
2016-09-19 07:27:20 +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
|
|
|
|
(fragment, invocations)
|
2016-05-16 10:09:23 +00:00
|
|
|
|
}
|
2016-06-12 01:50:52 +00:00
|
|
|
|
|
2017-09-12 19:55:41 +00:00
|
|
|
|
fn fully_configure(&mut self, item: Annotatable) -> Annotatable {
|
|
|
|
|
let mut cfg = StripUnconfigured {
|
|
|
|
|
sess: self.cx.parse_sess,
|
|
|
|
|
features: self.cx.ecfg.features,
|
|
|
|
|
};
|
|
|
|
|
// Since the item itself has already been configured by the InvocationCollector,
|
|
|
|
|
// we know that fold result vector will contain exactly one element
|
|
|
|
|
match item {
|
|
|
|
|
Annotatable::Item(item) => {
|
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
|
|
|
|
Annotatable::Item(cfg.flat_map_item(item).pop().unwrap())
|
2017-09-12 19:55:41 +00:00
|
|
|
|
}
|
|
|
|
|
Annotatable::TraitItem(item) => {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
Annotatable::TraitItem(
|
|
|
|
|
item.map(|item| cfg.flat_map_trait_item(item).pop().unwrap()))
|
2017-09-12 19:55:41 +00:00
|
|
|
|
}
|
|
|
|
|
Annotatable::ImplItem(item) => {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
Annotatable::ImplItem(item.map(|item| cfg.flat_map_impl_item(item).pop().unwrap()))
|
2017-09-12 19:55:41 +00:00
|
|
|
|
}
|
2018-03-11 02:16:26 +00:00
|
|
|
|
Annotatable::ForeignItem(item) => {
|
|
|
|
|
Annotatable::ForeignItem(
|
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
|
|
|
|
item.map(|item| cfg.flat_map_foreign_item(item).pop().unwrap())
|
2018-03-11 02:16:26 +00:00
|
|
|
|
)
|
|
|
|
|
}
|
2018-03-16 06:20:56 +00:00
|
|
|
|
Annotatable::Stmt(stmt) => {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
Annotatable::Stmt(stmt.map(|stmt| cfg.flat_map_stmt(stmt).pop().unwrap()))
|
2018-03-16 06:20:56 +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
|
|
|
|
Annotatable::Expr(mut expr) => {
|
|
|
|
|
Annotatable::Expr({ cfg.visit_expr(&mut expr); expr })
|
2018-03-16 06:20:56 +00:00
|
|
|
|
}
|
2019-09-09 12:26:25 +00:00
|
|
|
|
Annotatable::Arm(arm) => {
|
|
|
|
|
Annotatable::Arm(cfg.flat_map_arm(arm).pop().unwrap())
|
|
|
|
|
}
|
|
|
|
|
Annotatable::Field(field) => {
|
|
|
|
|
Annotatable::Field(cfg.flat_map_field(field).pop().unwrap())
|
|
|
|
|
}
|
|
|
|
|
Annotatable::FieldPat(fp) => {
|
|
|
|
|
Annotatable::FieldPat(cfg.flat_map_field_pattern(fp).pop().unwrap())
|
|
|
|
|
}
|
|
|
|
|
Annotatable::GenericParam(param) => {
|
|
|
|
|
Annotatable::GenericParam(cfg.flat_map_generic_param(param).pop().unwrap())
|
|
|
|
|
}
|
|
|
|
|
Annotatable::Param(param) => {
|
|
|
|
|
Annotatable::Param(cfg.flat_map_param(param).pop().unwrap())
|
|
|
|
|
}
|
|
|
|
|
Annotatable::StructField(sf) => {
|
|
|
|
|
Annotatable::StructField(cfg.flat_map_struct_field(sf).pop().unwrap())
|
|
|
|
|
}
|
|
|
|
|
Annotatable::Variant(v) => {
|
|
|
|
|
Annotatable::Variant(cfg.flat_map_variant(v).pop().unwrap())
|
|
|
|
|
}
|
2017-09-12 19:55:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-07 15:55:29 +00:00
|
|
|
|
fn expand_invoc(&mut self, invoc: Invocation, ext: &SyntaxExtensionKind) -> AstFragment {
|
2017-03-17 04:04:41 +00:00
|
|
|
|
if self.cx.current_expansion.depth > self.cx.ecfg.recursion_limit {
|
2019-08-13 20:56:42 +00:00
|
|
|
|
let expn_data = self.cx.current_expansion.id.expn_data();
|
2017-03-17 04:04:41 +00:00
|
|
|
|
let suggested_limit = self.cx.ecfg.recursion_limit * 2;
|
2019-08-13 20:56:42 +00:00
|
|
|
|
let mut err = self.cx.struct_span_err(expn_data.call_site,
|
2017-03-17 04:04:41 +00:00
|
|
|
|
&format!("recursion limit reached while expanding the macro `{}`",
|
2019-08-13 20:56:42 +00:00
|
|
|
|
expn_data.kind.descr()));
|
2017-03-17 04:04:41 +00:00
|
|
|
|
err.help(&format!(
|
|
|
|
|
"consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate",
|
|
|
|
|
suggested_limit));
|
|
|
|
|
err.emit();
|
2017-08-25 18:46:49 +00:00
|
|
|
|
self.cx.trace_macros_diag();
|
2018-01-21 11:47:58 +00:00
|
|
|
|
FatalError.raise();
|
2016-09-01 07:01:45 +00:00
|
|
|
|
}
|
2017-03-17 04:04:41 +00:00
|
|
|
|
|
2019-08-26 23:07:26 +00:00
|
|
|
|
let (fragment_kind, span) = (invoc.fragment_kind, invoc.span());
|
2019-07-07 15:29:22 +00:00
|
|
|
|
match invoc.kind {
|
2019-07-07 15:55:29 +00:00
|
|
|
|
InvocationKind::Bang { mac, .. } => match ext {
|
|
|
|
|
SyntaxExtensionKind::Bang(expander) => {
|
|
|
|
|
self.gate_proc_macro_expansion_kind(span, fragment_kind);
|
2019-08-14 23:13:53 +00:00
|
|
|
|
let tok_result = expander.expand(self.cx, span, mac.stream());
|
2019-07-07 15:55:29 +00:00
|
|
|
|
let result =
|
2019-08-14 23:13:53 +00:00
|
|
|
|
self.parse_ast_fragment(tok_result, fragment_kind, &mac.path, span);
|
2019-07-07 15:55:29 +00:00
|
|
|
|
self.gate_proc_macro_expansion(span, &result);
|
|
|
|
|
result
|
|
|
|
|
}
|
|
|
|
|
SyntaxExtensionKind::LegacyBang(expander) => {
|
2019-07-19 01:36:19 +00:00
|
|
|
|
let prev = self.cx.current_expansion.prior_type_ascription;
|
2019-08-14 23:13:53 +00:00
|
|
|
|
self.cx.current_expansion.prior_type_ascription = mac.prior_type_ascription;
|
|
|
|
|
let tok_result = expander.expand(self.cx, span, mac.stream());
|
2019-07-19 01:36:19 +00:00
|
|
|
|
let result = if let Some(result) = fragment_kind.make_from(tok_result) {
|
2019-07-07 15:55:29 +00:00
|
|
|
|
result
|
|
|
|
|
} else {
|
|
|
|
|
let msg = format!("non-{kind} macro in {kind} position: {path}",
|
2019-08-14 23:13:53 +00:00
|
|
|
|
kind = fragment_kind.name(), path = mac.path);
|
2019-07-07 15:55:29 +00:00
|
|
|
|
self.cx.span_err(span, &msg);
|
|
|
|
|
self.cx.trace_macros_diag();
|
|
|
|
|
fragment_kind.dummy(span)
|
2019-07-19 01:36:19 +00:00
|
|
|
|
};
|
|
|
|
|
self.cx.current_expansion.prior_type_ascription = prev;
|
|
|
|
|
result
|
2019-06-16 15:58:39 +00:00
|
|
|
|
}
|
2019-07-07 15:55:29 +00:00
|
|
|
|
_ => unreachable!()
|
2018-07-22 23:52:51 +00:00
|
|
|
|
}
|
2019-07-07 22:00:43 +00:00
|
|
|
|
InvocationKind::Attr { attr, mut item, .. } => match ext {
|
2019-07-07 15:55:29 +00:00
|
|
|
|
SyntaxExtensionKind::Attr(expander) => {
|
|
|
|
|
self.gate_proc_macro_attr_item(span, &item);
|
|
|
|
|
let item_tok = TokenTree::token(token::Interpolated(Lrc::new(match item {
|
|
|
|
|
Annotatable::Item(item) => token::NtItem(item),
|
|
|
|
|
Annotatable::TraitItem(item) => token::NtTraitItem(item.into_inner()),
|
|
|
|
|
Annotatable::ImplItem(item) => token::NtImplItem(item.into_inner()),
|
|
|
|
|
Annotatable::ForeignItem(item) => token::NtForeignItem(item.into_inner()),
|
|
|
|
|
Annotatable::Stmt(stmt) => token::NtStmt(stmt.into_inner()),
|
|
|
|
|
Annotatable::Expr(expr) => token::NtExpr(expr),
|
2019-09-09 12:26:25 +00:00
|
|
|
|
Annotatable::Arm(..)
|
|
|
|
|
| Annotatable::Field(..)
|
|
|
|
|
| Annotatable::FieldPat(..)
|
|
|
|
|
| Annotatable::GenericParam(..)
|
|
|
|
|
| Annotatable::Param(..)
|
|
|
|
|
| Annotatable::StructField(..)
|
|
|
|
|
| Annotatable::Variant(..)
|
|
|
|
|
=> panic!("unexpected annotatable"),
|
2019-07-07 15:55:29 +00:00
|
|
|
|
})), DUMMY_SP).into();
|
|
|
|
|
let input = self.extract_proc_macro_attr_input(attr.tokens, span);
|
|
|
|
|
let tok_result = expander.expand(self.cx, span, input, item_tok);
|
|
|
|
|
let res = self.parse_ast_fragment(tok_result, fragment_kind, &attr.path, span);
|
|
|
|
|
self.gate_proc_macro_expansion(span, &res);
|
|
|
|
|
res
|
|
|
|
|
}
|
|
|
|
|
SyntaxExtensionKind::LegacyAttr(expander) => {
|
|
|
|
|
match attr.parse_meta(self.cx.parse_sess) {
|
|
|
|
|
Ok(meta) => {
|
|
|
|
|
let item = expander.expand(self.cx, span, &meta, item);
|
|
|
|
|
fragment_kind.expect_from_annotatables(item)
|
|
|
|
|
}
|
|
|
|
|
Err(mut err) => {
|
|
|
|
|
err.emit();
|
|
|
|
|
fragment_kind.dummy(span)
|
|
|
|
|
}
|
2019-07-07 15:29:22 +00:00
|
|
|
|
}
|
2019-07-07 15:55:29 +00:00
|
|
|
|
}
|
|
|
|
|
SyntaxExtensionKind::NonMacroAttr { mark_used } => {
|
|
|
|
|
attr::mark_known(&attr);
|
|
|
|
|
if *mark_used {
|
|
|
|
|
attr::mark_used(&attr);
|
2019-07-07 15:29:22 +00:00
|
|
|
|
}
|
2019-07-07 15:55:29 +00:00
|
|
|
|
item.visit_attrs(|attrs| attrs.push(attr));
|
|
|
|
|
fragment_kind.expect_from_annotatables(iter::once(item))
|
2019-07-07 15:29:22 +00:00
|
|
|
|
}
|
2019-07-07 15:55:29 +00:00
|
|
|
|
_ => unreachable!()
|
2016-09-01 07:01:45 +00:00
|
|
|
|
}
|
2019-08-03 01:22:44 +00:00
|
|
|
|
InvocationKind::Derive { path, item } => match ext {
|
2019-07-07 15:55:29 +00:00
|
|
|
|
SyntaxExtensionKind::Derive(expander) |
|
|
|
|
|
SyntaxExtensionKind::LegacyDerive(expander) => {
|
|
|
|
|
if !item.derive_allowed() {
|
|
|
|
|
return fragment_kind.dummy(span);
|
|
|
|
|
}
|
2019-09-26 17:04:05 +00:00
|
|
|
|
let meta = ast::MetaItem { kind: ast::MetaItemKind::Word, span, path };
|
2019-07-07 15:55:29 +00:00
|
|
|
|
let items = expander.expand(self.cx, span, &meta, item);
|
|
|
|
|
fragment_kind.expect_from_annotatables(items)
|
|
|
|
|
}
|
|
|
|
|
_ => unreachable!()
|
2016-08-29 04:16:43 +00:00
|
|
|
|
}
|
2019-07-07 22:00:43 +00:00
|
|
|
|
InvocationKind::DeriveContainer { .. } => unreachable!()
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
2016-09-01 07:01:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 14:50:39 +00:00
|
|
|
|
fn extract_proc_macro_attr_input(&self, tokens: TokenStream, span: Span) -> TokenStream {
|
|
|
|
|
let mut trees = tokens.trees();
|
|
|
|
|
match trees.next() {
|
2018-11-29 23:02:04 +00:00
|
|
|
|
Some(TokenTree::Delimited(_, _, tts)) => {
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 14:50:39 +00:00
|
|
|
|
if trees.next().is_none() {
|
2018-11-29 23:02:04 +00:00
|
|
|
|
return tts.into()
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 14:50:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Some(TokenTree::Token(..)) => {}
|
|
|
|
|
None => return TokenStream::empty(),
|
|
|
|
|
}
|
|
|
|
|
self.cx.span_err(span, "custom attribute invocations must be \
|
2019-07-23 18:03:20 +00:00
|
|
|
|
of the form `#[foo]` or `#[foo(..)]`, the macro name must only be \
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 14:50:39 +00:00
|
|
|
|
followed by a delimiter token");
|
|
|
|
|
TokenStream::empty()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) {
|
|
|
|
|
let (kind, gate) = match *item {
|
|
|
|
|
Annotatable::Item(ref item) => {
|
2019-09-26 16:51:36 +00:00
|
|
|
|
match item.kind {
|
2018-07-06 01:09:35 +00:00
|
|
|
|
ItemKind::Mod(_) if self.cx.ecfg.proc_macro_hygiene() => return,
|
2019-05-08 03:21:18 +00:00
|
|
|
|
ItemKind::Mod(_) => ("modules", sym::proc_macro_hygiene),
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 14:50:39 +00:00
|
|
|
|
_ => return,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Annotatable::TraitItem(_) => return,
|
|
|
|
|
Annotatable::ImplItem(_) => return,
|
|
|
|
|
Annotatable::ForeignItem(_) => return,
|
|
|
|
|
Annotatable::Stmt(_) |
|
2018-07-06 01:09:35 +00:00
|
|
|
|
Annotatable::Expr(_) if self.cx.ecfg.proc_macro_hygiene() => return,
|
2019-05-08 03:21:18 +00:00
|
|
|
|
Annotatable::Stmt(_) => ("statements", sym::proc_macro_hygiene),
|
|
|
|
|
Annotatable::Expr(_) => ("expressions", sym::proc_macro_hygiene),
|
2019-09-09 12:26:25 +00:00
|
|
|
|
Annotatable::Arm(..)
|
|
|
|
|
| Annotatable::Field(..)
|
|
|
|
|
| Annotatable::FieldPat(..)
|
|
|
|
|
| Annotatable::GenericParam(..)
|
|
|
|
|
| Annotatable::Param(..)
|
|
|
|
|
| Annotatable::StructField(..)
|
|
|
|
|
| Annotatable::Variant(..)
|
|
|
|
|
=> panic!("unexpected annotatable"),
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 14:50:39 +00:00
|
|
|
|
};
|
|
|
|
|
emit_feature_err(
|
|
|
|
|
self.cx.parse_sess,
|
|
|
|
|
gate,
|
|
|
|
|
span,
|
|
|
|
|
GateIssue::Language,
|
|
|
|
|
&format!("custom attributes cannot be applied to {}", kind),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-07 15:29:22 +00:00
|
|
|
|
fn gate_proc_macro_expansion(&self, span: Span, fragment: &AstFragment) {
|
2018-07-06 01:09:35 +00:00
|
|
|
|
if self.cx.ecfg.proc_macro_hygiene() {
|
2018-05-09 22:03:02 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-17 21:52:34 +00:00
|
|
|
|
fragment.visit_with(&mut DisallowMacros {
|
2018-05-09 22:03:02 +00:00
|
|
|
|
span,
|
|
|
|
|
parse_sess: self.cx.parse_sess,
|
|
|
|
|
});
|
|
|
|
|
|
2018-08-17 21:52:34 +00:00
|
|
|
|
struct DisallowMacros<'a> {
|
2018-05-09 22:03:02 +00:00
|
|
|
|
span: Span,
|
|
|
|
|
parse_sess: &'a ParseSess,
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-17 21:52:34 +00:00
|
|
|
|
impl<'ast, 'a> Visitor<'ast> for DisallowMacros<'a> {
|
2018-05-09 22:03:02 +00:00
|
|
|
|
fn visit_item(&mut self, i: &'ast ast::Item) {
|
2019-09-26 16:51:36 +00:00
|
|
|
|
if let ast::ItemKind::MacroDef(_) = i.kind {
|
2018-05-09 22:03:02 +00:00
|
|
|
|
emit_feature_err(
|
|
|
|
|
self.parse_sess,
|
2019-05-08 03:21:18 +00:00
|
|
|
|
sym::proc_macro_hygiene,
|
2018-05-09 22:03:02 +00:00
|
|
|
|
self.span,
|
|
|
|
|
GateIssue::Language,
|
2018-10-30 23:10:10 +00:00
|
|
|
|
"procedural macros cannot expand to macro definitions",
|
2018-05-09 22:03:02 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
visit::walk_item(self, i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn visit_mac(&mut self, _mac: &'ast ast::Mac) {
|
|
|
|
|
// ...
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 23:08:08 +00:00
|
|
|
|
fn gate_proc_macro_expansion_kind(&self, span: Span, kind: AstFragmentKind) {
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 14:50:39 +00:00
|
|
|
|
let kind = match kind {
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragmentKind::Expr => "expressions",
|
|
|
|
|
AstFragmentKind::OptExpr => "expressions",
|
|
|
|
|
AstFragmentKind::Pat => "patterns",
|
|
|
|
|
AstFragmentKind::Ty => "types",
|
|
|
|
|
AstFragmentKind::Stmts => "statements",
|
|
|
|
|
AstFragmentKind::Items => return,
|
|
|
|
|
AstFragmentKind::TraitItems => return,
|
|
|
|
|
AstFragmentKind::ImplItems => return,
|
|
|
|
|
AstFragmentKind::ForeignItems => return,
|
2019-09-09 12:26:25 +00:00
|
|
|
|
AstFragmentKind::Arms
|
|
|
|
|
| AstFragmentKind::Fields
|
|
|
|
|
| AstFragmentKind::FieldPats
|
|
|
|
|
| AstFragmentKind::GenericParams
|
|
|
|
|
| AstFragmentKind::Params
|
|
|
|
|
| AstFragmentKind::StructFields
|
|
|
|
|
| AstFragmentKind::Variants
|
|
|
|
|
=> panic!("unexpected AST fragment kind"),
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 14:50:39 +00:00
|
|
|
|
};
|
2018-07-06 01:09:35 +00:00
|
|
|
|
if self.cx.ecfg.proc_macro_hygiene() {
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 14:50:39 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
emit_feature_err(
|
|
|
|
|
self.cx.parse_sess,
|
2019-05-08 03:21:18 +00:00
|
|
|
|
sym::proc_macro_hygiene,
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 14:50:39 +00:00
|
|
|
|
span,
|
|
|
|
|
GateIssue::Language,
|
|
|
|
|
&format!("procedural macros cannot be expanded to {}", kind),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-09 01:24:00 +00:00
|
|
|
|
fn parse_ast_fragment(
|
|
|
|
|
&mut self,
|
|
|
|
|
toks: TokenStream,
|
|
|
|
|
kind: AstFragmentKind,
|
|
|
|
|
path: &Path,
|
|
|
|
|
span: Span,
|
|
|
|
|
) -> AstFragment {
|
2019-08-31 17:08:06 +00:00
|
|
|
|
let mut parser = self.cx.new_parser_from_tts(toks);
|
2018-06-19 23:08:08 +00:00
|
|
|
|
match parser.parse_ast_fragment(kind, false) {
|
|
|
|
|
Ok(fragment) => {
|
2017-12-26 07:47:32 +00:00
|
|
|
|
parser.ensure_complete_parse(path, kind.name(), span);
|
2019-07-07 15:29:22 +00:00
|
|
|
|
fragment
|
2017-12-26 07:47:32 +00:00
|
|
|
|
}
|
2016-09-26 04:16:55 +00:00
|
|
|
|
Err(mut err) => {
|
2018-03-16 06:20:56 +00:00
|
|
|
|
err.set_span(span);
|
2019-08-09 16:39:30 +00:00
|
|
|
|
annotate_err_with_kind(&mut err, kind, span);
|
2016-09-26 04:16:55 +00:00
|
|
|
|
err.emit();
|
2017-09-02 16:13:25 +00:00
|
|
|
|
self.cx.trace_macros_diag();
|
2017-12-26 07:47:32 +00:00
|
|
|
|
kind.dummy(span)
|
2016-09-26 04:16:55 +00:00
|
|
|
|
}
|
2017-12-26 07:47:32 +00:00
|
|
|
|
}
|
2016-09-26 04:16:55 +00:00
|
|
|
|
}
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-23 09:32:58 +00:00
|
|
|
|
impl<'a> Parser<'a> {
|
2018-06-19 23:08:08 +00:00
|
|
|
|
pub fn parse_ast_fragment(&mut self, kind: AstFragmentKind, macro_legacy_warnings: bool)
|
|
|
|
|
-> PResult<'a, AstFragment> {
|
2016-09-23 09:32:58 +00:00
|
|
|
|
Ok(match kind {
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragmentKind::Items => {
|
2018-08-30 09:42:16 +00:00
|
|
|
|
let mut items = SmallVec::new();
|
2016-09-23 09:32:58 +00:00
|
|
|
|
while let Some(item) = self.parse_item()? {
|
|
|
|
|
items.push(item);
|
|
|
|
|
}
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragment::Items(items)
|
2016-09-23 09:32:58 +00:00
|
|
|
|
}
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragmentKind::TraitItems => {
|
2018-08-30 09:42:16 +00:00
|
|
|
|
let mut items = SmallVec::new();
|
2016-09-23 09:32:58 +00:00
|
|
|
|
while self.token != token::Eof {
|
2017-04-13 19:37:05 +00:00
|
|
|
|
items.push(self.parse_trait_item(&mut false)?);
|
2016-09-23 09:32:58 +00:00
|
|
|
|
}
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragment::TraitItems(items)
|
2016-09-23 09:32:58 +00:00
|
|
|
|
}
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragmentKind::ImplItems => {
|
2018-08-30 09:42:16 +00:00
|
|
|
|
let mut items = SmallVec::new();
|
2016-09-23 09:32:58 +00:00
|
|
|
|
while self.token != token::Eof {
|
2017-04-13 19:37:05 +00:00
|
|
|
|
items.push(self.parse_impl_item(&mut false)?);
|
2016-09-23 09:32:58 +00:00
|
|
|
|
}
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragment::ImplItems(items)
|
2016-09-23 09:32:58 +00:00
|
|
|
|
}
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragmentKind::ForeignItems => {
|
2018-08-30 09:42:16 +00:00
|
|
|
|
let mut items = SmallVec::new();
|
2018-03-11 02:16:26 +00:00
|
|
|
|
while self.token != token::Eof {
|
2019-07-14 04:15:21 +00:00
|
|
|
|
items.push(self.parse_foreign_item(DUMMY_SP)?);
|
2018-03-11 02:16:26 +00:00
|
|
|
|
}
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragment::ForeignItems(items)
|
2018-03-11 02:16:26 +00:00
|
|
|
|
}
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragmentKind::Stmts => {
|
2018-08-30 09:42:16 +00:00
|
|
|
|
let mut stmts = SmallVec::new();
|
2016-10-18 04:01:36 +00:00
|
|
|
|
while self.token != token::Eof &&
|
|
|
|
|
// won't make progress on a `}`
|
|
|
|
|
self.token != token::CloseDelim(token::Brace) {
|
2016-09-26 04:16:55 +00:00
|
|
|
|
if let Some(stmt) = self.parse_full_stmt(macro_legacy_warnings)? {
|
2016-09-23 09:32:58 +00:00
|
|
|
|
stmts.push(stmt);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragment::Stmts(stmts)
|
2016-09-23 09:32:58 +00:00
|
|
|
|
}
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragmentKind::Expr => AstFragment::Expr(self.parse_expr()?),
|
|
|
|
|
AstFragmentKind::OptExpr => {
|
2018-03-16 06:20:56 +00:00
|
|
|
|
if self.token != token::Eof {
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragment::OptExpr(Some(self.parse_expr()?))
|
2018-03-16 06:20:56 +00:00
|
|
|
|
} else {
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragment::OptExpr(None)
|
2018-03-16 06:20:56 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
2018-06-19 23:08:08 +00:00
|
|
|
|
AstFragmentKind::Ty => AstFragment::Ty(self.parse_ty()?),
|
2018-10-28 18:54:31 +00:00
|
|
|
|
AstFragmentKind::Pat => AstFragment::Pat(self.parse_pat(None)?),
|
2019-09-09 12:26:25 +00:00
|
|
|
|
AstFragmentKind::Arms
|
|
|
|
|
| AstFragmentKind::Fields
|
|
|
|
|
| AstFragmentKind::FieldPats
|
|
|
|
|
| AstFragmentKind::GenericParams
|
|
|
|
|
| AstFragmentKind::Params
|
|
|
|
|
| AstFragmentKind::StructFields
|
|
|
|
|
| AstFragmentKind::Variants
|
|
|
|
|
=> panic!("unexpected AST fragment kind"),
|
2016-09-23 09:32:58 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
2016-09-26 11:24:10 +00:00
|
|
|
|
|
2017-03-08 23:13:35 +00:00
|
|
|
|
pub fn ensure_complete_parse(&mut self, macro_path: &Path, kind_name: &str, span: Span) {
|
2016-09-26 11:24:10 +00:00
|
|
|
|
if self.token != token::Eof {
|
|
|
|
|
let msg = format!("macro expansion ignores token `{}` and any following",
|
|
|
|
|
self.this_token_to_string());
|
2017-07-31 20:04:34 +00:00
|
|
|
|
// Avoid emitting backtrace info twice.
|
2019-08-10 22:44:55 +00:00
|
|
|
|
let def_site_span = self.token.span.with_ctxt(SyntaxContext::root());
|
2017-03-28 05:32:43 +00:00
|
|
|
|
let mut err = self.diagnostic().struct_span_err(def_site_span, &msg);
|
2018-10-23 17:07:34 +00:00
|
|
|
|
err.span_label(span, "caused by the macro expansion here");
|
|
|
|
|
let msg = format!(
|
|
|
|
|
"the usage of `{}!` is likely invalid in {} context",
|
|
|
|
|
macro_path,
|
|
|
|
|
kind_name,
|
|
|
|
|
);
|
|
|
|
|
err.note(&msg);
|
|
|
|
|
let semi_span = self.sess.source_map().next_point(span);
|
2018-10-24 19:52:24 +00:00
|
|
|
|
|
|
|
|
|
let semi_full_span = semi_span.to(self.sess.source_map().next_point(semi_span));
|
|
|
|
|
match self.sess.source_map().span_to_snippet(semi_full_span) {
|
2018-10-23 17:07:34 +00:00
|
|
|
|
Ok(ref snippet) if &snippet[..] != ";" && kind_name == "expression" => {
|
2019-01-25 21:03:27 +00:00
|
|
|
|
err.span_suggestion(
|
2018-10-23 17:07:34 +00:00
|
|
|
|
semi_span,
|
|
|
|
|
"you might be missing a semicolon here",
|
|
|
|
|
";".to_owned(),
|
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
err.emit();
|
2016-09-26 11:24:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 16:39:39 +00:00
|
|
|
|
struct InvocationCollector<'a, 'b> {
|
2016-09-02 09:12:47 +00:00
|
|
|
|
cx: &'a mut ExtCtxt<'b>,
|
2016-09-07 22:24:01 +00:00
|
|
|
|
cfg: StripUnconfigured<'a>,
|
2016-09-02 09:12:47 +00:00
|
|
|
|
invocations: Vec<Invocation>,
|
2016-09-06 05:42:45 +00:00
|
|
|
|
monotonic: bool,
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, 'b> InvocationCollector<'a, 'b> {
|
2018-06-19 23:08:08 +00:00
|
|
|
|
fn collect(&mut self, fragment_kind: AstFragmentKind, kind: InvocationKind) -> AstFragment {
|
2019-08-13 20:56:42 +00:00
|
|
|
|
// Expansion data for all the collected invocations is set upon their resolution,
|
2019-07-07 22:00:43 +00:00
|
|
|
|
// with exception of the derive container case which is not resolved and can get
|
2019-08-13 20:56:42 +00:00
|
|
|
|
// its expansion data immediately.
|
|
|
|
|
let expn_data = match &kind {
|
|
|
|
|
InvocationKind::DeriveContainer { item, .. } => Some(ExpnData {
|
2019-08-13 00:34:46 +00:00
|
|
|
|
parent: self.cx.current_expansion.id,
|
2019-08-13 20:56:42 +00:00
|
|
|
|
..ExpnData::default(
|
2019-08-13 00:34:46 +00:00
|
|
|
|
ExpnKind::Macro(MacroKind::Attr, sym::derive),
|
|
|
|
|
item.span(), self.cx.parse_sess.edition,
|
|
|
|
|
)
|
|
|
|
|
}),
|
2019-07-07 13:45:41 +00:00
|
|
|
|
_ => None,
|
|
|
|
|
};
|
2019-08-13 20:56:42 +00:00
|
|
|
|
let expn_id = ExpnId::fresh(expn_data);
|
2016-09-02 09:12:47 +00:00
|
|
|
|
self.invocations.push(Invocation {
|
2017-08-07 05:54:09 +00:00
|
|
|
|
kind,
|
2018-06-19 23:08:08 +00:00
|
|
|
|
fragment_kind,
|
2016-09-26 22:54:36 +00:00
|
|
|
|
expansion_data: ExpansionData {
|
2019-07-15 22:42:58 +00:00
|
|
|
|
id: expn_id,
|
2016-09-26 22:54:36 +00:00
|
|
|
|
depth: self.cx.current_expansion.depth + 1,
|
|
|
|
|
..self.cx.current_expansion.clone()
|
|
|
|
|
},
|
2016-09-01 07:01:45 +00:00
|
|
|
|
});
|
2019-07-15 22:42:58 +00:00
|
|
|
|
placeholder(fragment_kind, NodeId::placeholder_from_expn_id(expn_id))
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
2016-09-01 07:01:45 +00:00
|
|
|
|
|
2018-06-19 23:08:08 +00:00
|
|
|
|
fn collect_bang(&mut self, mac: ast::Mac, span: Span, kind: AstFragmentKind) -> AstFragment {
|
2019-06-30 22:08:49 +00:00
|
|
|
|
self.collect(kind, InvocationKind::Bang { mac, span })
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
2016-09-01 07:01:45 +00:00
|
|
|
|
|
2017-02-02 07:01:15 +00:00
|
|
|
|
fn collect_attr(&mut self,
|
|
|
|
|
attr: Option<ast::Attribute>,
|
2019-07-07 22:00:43 +00:00
|
|
|
|
derives: Vec<Path>,
|
2017-02-02 07:01:15 +00:00
|
|
|
|
item: Annotatable,
|
2018-09-16 14:15:07 +00:00
|
|
|
|
kind: AstFragmentKind,
|
2018-09-18 22:46:18 +00:00
|
|
|
|
after_derive: bool)
|
2018-06-19 23:08:08 +00:00
|
|
|
|
-> AstFragment {
|
2019-07-07 22:00:43 +00:00
|
|
|
|
self.collect(kind, match attr {
|
|
|
|
|
Some(attr) => InvocationKind::Attr { attr, item, derives, after_derive },
|
|
|
|
|
None => InvocationKind::DeriveContainer { derives, item },
|
|
|
|
|
})
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-18 22:46:18 +00:00
|
|
|
|
fn find_attr_invoc(&self, attrs: &mut Vec<ast::Attribute>, after_derive: &mut bool)
|
2018-09-16 14:15:07 +00:00
|
|
|
|
-> Option<ast::Attribute> {
|
2018-09-09 22:54:51 +00:00
|
|
|
|
let attr = attrs.iter()
|
2018-09-16 14:15:07 +00:00
|
|
|
|
.position(|a| {
|
2019-05-07 06:03:44 +00:00
|
|
|
|
if a.path == sym::derive {
|
2018-09-18 22:46:18 +00:00
|
|
|
|
*after_derive = true;
|
2018-09-16 14:15:07 +00:00
|
|
|
|
}
|
|
|
|
|
!attr::is_known(a) && !is_builtin_attr(a)
|
|
|
|
|
})
|
2018-09-09 22:54:51 +00:00
|
|
|
|
.map(|i| attrs.remove(i));
|
|
|
|
|
if let Some(attr) = &attr {
|
2019-06-22 13:18:05 +00:00
|
|
|
|
if !self.cx.ecfg.custom_inner_attributes() &&
|
2019-05-07 06:03:44 +00:00
|
|
|
|
attr.style == ast::AttrStyle::Inner && attr.path != sym::test {
|
2019-05-08 03:21:18 +00:00
|
|
|
|
emit_feature_err(&self.cx.parse_sess, sym::custom_inner_attributes,
|
2018-09-09 22:54:51 +00:00
|
|
|
|
attr.span, GateIssue::Language,
|
|
|
|
|
"non-builtin inner attributes are unstable");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
attr
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 06:19:21 +00:00
|
|
|
|
/// If `item` is an attr invocation, remove and return the macro attribute and derive traits.
|
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 classify_item<T>(&mut self, item: &mut T)
|
|
|
|
|
-> (Option<ast::Attribute>, Vec<Path>, /* after_derive */ bool)
|
2017-02-02 07:01:15 +00:00
|
|
|
|
where T: HasAttrs,
|
|
|
|
|
{
|
2018-09-18 22:46:18 +00:00
|
|
|
|
let (mut attr, mut traits, mut after_derive) = (None, Vec::new(), false);
|
2017-02-01 10:33:09 +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
|
|
|
|
item.visit_attrs(|mut attrs| {
|
2018-09-18 22:46:18 +00:00
|
|
|
|
attr = self.find_attr_invoc(&mut attrs, &mut after_derive);
|
2017-02-02 07:01:15 +00:00
|
|
|
|
traits = collect_derives(&mut self.cx, &mut attrs);
|
2016-09-02 09:12:47 +00:00
|
|
|
|
});
|
2017-02-01 10:33:09 +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
|
|
|
|
(attr, traits, after_derive)
|
2016-09-02 09:12:47 +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
|
|
|
|
/// Alternative to `classify_item()` that ignores `#[derive]` so invocations fallthrough
|
2018-04-18 06:19:21 +00:00
|
|
|
|
/// to the unused-attributes lint (making it an error on statements and expressions
|
|
|
|
|
/// is a breaking change)
|
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 classify_nonitem<T: HasAttrs>(&mut self, nonitem: &mut T)
|
|
|
|
|
-> (Option<ast::Attribute>, /* after_derive */ bool) {
|
2018-09-18 22:46:18 +00:00
|
|
|
|
let (mut attr, mut after_derive) = (None, false);
|
2018-04-18 06:19:21 +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
|
|
|
|
nonitem.visit_attrs(|mut attrs| {
|
2018-09-18 22:46:18 +00:00
|
|
|
|
attr = self.find_attr_invoc(&mut attrs, &mut after_derive);
|
2018-04-18 06:19:21 +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
|
|
|
|
(attr, after_derive)
|
2018-04-18 06:19:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 22:24:01 +00:00
|
|
|
|
fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
|
|
|
|
|
self.cfg.configure(node)
|
|
|
|
|
}
|
2016-12-01 11:20:04 +00:00
|
|
|
|
|
|
|
|
|
// Detect use of feature-gated or invalid attributes on macro invocations
|
|
|
|
|
// since they will not be detected after macro expansion.
|
|
|
|
|
fn check_attributes(&mut self, attrs: &[ast::Attribute]) {
|
|
|
|
|
let features = self.cx.ecfg.features.unwrap();
|
|
|
|
|
for attr in attrs.iter() {
|
2019-09-14 18:29:59 +00:00
|
|
|
|
feature_gate::check_attribute(attr, self.cx.parse_sess, features);
|
2018-04-18 06:19:21 +00:00
|
|
|
|
|
|
|
|
|
// macros are expanded before any lint passes so this warning has to be hardcoded
|
2019-05-07 06:03:44 +00:00
|
|
|
|
if attr.path == sym::derive {
|
2018-04-18 06:19:21 +00:00
|
|
|
|
self.cx.struct_span_warn(attr.span, "`#[derive]` does nothing on macro invocations")
|
|
|
|
|
.note("this may become a hard error in a future release")
|
|
|
|
|
.emit();
|
|
|
|
|
}
|
2016-12-01 11:20:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-29 19:10:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
|
|
|
|
|
fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
|
|
|
|
|
self.cfg.configure_expr(expr);
|
|
|
|
|
visit_clobber(expr.deref_mut(), |mut expr| {
|
2019-09-26 13:39:48 +00:00
|
|
|
|
self.cfg.configure_expr_kind(&mut expr.kind);
|
2018-11-20 09:16:57 +00:00
|
|
|
|
|
|
|
|
|
// ignore derives so they remain unused
|
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
|
|
|
|
let (attr, after_derive) = self.classify_nonitem(&mut expr);
|
2018-11-20 09:16:57 +00:00
|
|
|
|
|
|
|
|
|
if attr.is_some() {
|
|
|
|
|
// Collect the invoc regardless of whether or not attributes are permitted here
|
|
|
|
|
// expansion will eat the attribute so it won't error later.
|
|
|
|
|
attr.as_ref().map(|a| self.cfg.maybe_emit_expr_attr_err(a));
|
|
|
|
|
|
|
|
|
|
// AstFragmentKind::Expr requires the macro to emit an expression.
|
|
|
|
|
return self.collect_attr(attr, vec![], Annotatable::Expr(P(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 04:20:55 +00:00
|
|
|
|
AstFragmentKind::Expr, after_derive)
|
2018-11-20 09:16:57 +00:00
|
|
|
|
.make_expr()
|
|
|
|
|
.into_inner()
|
|
|
|
|
}
|
2018-03-16 06:20:56 +00:00
|
|
|
|
|
2019-09-26 13:39:48 +00:00
|
|
|
|
if let ast::ExprKind::Mac(mac) = expr.kind {
|
2018-11-20 09:16:57 +00:00
|
|
|
|
self.check_attributes(&expr.attrs);
|
|
|
|
|
self.collect_bang(mac, expr.span, AstFragmentKind::Expr)
|
|
|
|
|
.make_expr()
|
|
|
|
|
.into_inner()
|
|
|
|
|
} else {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
noop_visit_expr(&mut expr, self);
|
|
|
|
|
expr
|
2018-11-20 09:16:57 +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
|
|
|
|
});
|
2013-08-29 19:10:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 12:26:25 +00:00
|
|
|
|
fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
|
|
|
|
|
let mut arm = configure!(self, arm);
|
|
|
|
|
|
|
|
|
|
let (attr, traits, after_derive) = self.classify_item(&mut arm);
|
|
|
|
|
if attr.is_some() || !traits.is_empty() {
|
|
|
|
|
return self.collect_attr(attr, traits, Annotatable::Arm(arm),
|
|
|
|
|
AstFragmentKind::Arms, after_derive)
|
|
|
|
|
.make_arms();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
noop_flat_map_arm(arm, self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn flat_map_field(&mut self, field: ast::Field) -> SmallVec<[ast::Field; 1]> {
|
|
|
|
|
let mut field = configure!(self, field);
|
|
|
|
|
|
|
|
|
|
let (attr, traits, after_derive) = self.classify_item(&mut field);
|
|
|
|
|
if attr.is_some() || !traits.is_empty() {
|
|
|
|
|
return self.collect_attr(attr, traits, Annotatable::Field(field),
|
|
|
|
|
AstFragmentKind::Fields, after_derive)
|
|
|
|
|
.make_fields();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
noop_flat_map_field(field, self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn flat_map_field_pattern(&mut self, fp: ast::FieldPat) -> SmallVec<[ast::FieldPat; 1]> {
|
|
|
|
|
let mut fp = configure!(self, fp);
|
|
|
|
|
|
|
|
|
|
let (attr, traits, after_derive) = self.classify_item(&mut fp);
|
|
|
|
|
if attr.is_some() || !traits.is_empty() {
|
|
|
|
|
return self.collect_attr(attr, traits, Annotatable::FieldPat(fp),
|
|
|
|
|
AstFragmentKind::FieldPats, after_derive)
|
|
|
|
|
.make_field_patterns();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
noop_flat_map_field_pattern(fp, self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
|
|
|
|
|
let mut p = configure!(self, p);
|
|
|
|
|
|
|
|
|
|
let (attr, traits, after_derive) = self.classify_item(&mut p);
|
|
|
|
|
if attr.is_some() || !traits.is_empty() {
|
|
|
|
|
return self.collect_attr(attr, traits, Annotatable::Param(p),
|
|
|
|
|
AstFragmentKind::Params, after_derive)
|
|
|
|
|
.make_params();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
noop_flat_map_param(p, self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn flat_map_struct_field(&mut self, sf: ast::StructField) -> SmallVec<[ast::StructField; 1]> {
|
|
|
|
|
let mut sf = configure!(self, sf);
|
|
|
|
|
|
|
|
|
|
let (attr, traits, after_derive) = self.classify_item(&mut sf);
|
|
|
|
|
if attr.is_some() || !traits.is_empty() {
|
|
|
|
|
return self.collect_attr(attr, traits, Annotatable::StructField(sf),
|
|
|
|
|
AstFragmentKind::StructFields, after_derive)
|
|
|
|
|
.make_struct_fields();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
noop_flat_map_struct_field(sf, self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
|
|
|
|
|
let mut variant = configure!(self, variant);
|
|
|
|
|
|
|
|
|
|
let (attr, traits, after_derive) = self.classify_item(&mut variant);
|
|
|
|
|
if attr.is_some() || !traits.is_empty() {
|
|
|
|
|
return self.collect_attr(attr, traits, Annotatable::Variant(variant),
|
|
|
|
|
AstFragmentKind::Variants, after_derive)
|
|
|
|
|
.make_variants();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 04:20:55 +00:00
|
|
|
|
fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
|
2018-11-20 09:16:57 +00:00
|
|
|
|
let expr = configure!(self, expr);
|
|
|
|
|
expr.filter_map(|mut expr| {
|
2019-09-26 13:39:48 +00:00
|
|
|
|
self.cfg.configure_expr_kind(&mut expr.kind);
|
2016-09-07 22:24:01 +00:00
|
|
|
|
|
2018-11-20 09:16:57 +00:00
|
|
|
|
// Ignore derives so they remain unused.
|
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
|
|
|
|
let (attr, after_derive) = self.classify_nonitem(&mut expr);
|
2018-03-16 06:20:56 +00:00
|
|
|
|
|
2018-11-20 09:16:57 +00:00
|
|
|
|
if attr.is_some() {
|
|
|
|
|
attr.as_ref().map(|a| self.cfg.maybe_emit_expr_attr_err(a));
|
2018-03-16 06:20:56 +00:00
|
|
|
|
|
2018-11-20 09:16:57 +00:00
|
|
|
|
return self.collect_attr(attr, vec![], Annotatable::Expr(P(expr)),
|
|
|
|
|
AstFragmentKind::OptExpr, after_derive)
|
|
|
|
|
.make_opt_expr()
|
|
|
|
|
.map(|expr| expr.into_inner())
|
|
|
|
|
}
|
2018-03-16 06:20:56 +00:00
|
|
|
|
|
2019-09-26 13:39:48 +00:00
|
|
|
|
if let ast::ExprKind::Mac(mac) = expr.kind {
|
2018-11-20 09:16:57 +00:00
|
|
|
|
self.check_attributes(&expr.attrs);
|
|
|
|
|
self.collect_bang(mac, expr.span, AstFragmentKind::OptExpr)
|
|
|
|
|
.make_opt_expr()
|
|
|
|
|
.map(|expr| expr.into_inner())
|
|
|
|
|
} else {
|
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
|
|
|
|
Some({ noop_visit_expr(&mut expr, self); expr })
|
2018-11-20 09:16:57 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
2013-08-29 19:10:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
fn visit_pat(&mut self, pat: &mut P<ast::Pat>) {
|
|
|
|
|
self.cfg.configure_pat(pat);
|
2019-09-26 15:18:31 +00:00
|
|
|
|
match pat.kind {
|
2016-08-30 23:03:52 +00:00
|
|
|
|
PatKind::Mac(_) => {}
|
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
|
|
|
|
_ => return noop_visit_pat(pat, self),
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
2014-05-19 20:59:35 +00:00
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
visit_clobber(pat, |mut pat| {
|
2019-09-26 15:18:31 +00:00
|
|
|
|
match mem::replace(&mut pat.kind, PatKind::Wild) {
|
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
|
|
|
|
PatKind::Mac(mac) =>
|
|
|
|
|
self.collect_bang(mac, pat.span, AstFragmentKind::Pat).make_pat(),
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-08-29 19:10:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
|
2019-02-05 04:18:29 +00:00
|
|
|
|
let mut stmt = configure!(self, stmt);
|
2016-09-07 22:24:01 +00:00
|
|
|
|
|
2018-03-16 06:20:56 +00:00
|
|
|
|
// we'll expand attributes on expressions separately
|
|
|
|
|
if !stmt.is_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 04:20:55 +00:00
|
|
|
|
let (attr, derives, after_derive) = if stmt.is_item() {
|
|
|
|
|
self.classify_item(&mut stmt)
|
2018-04-18 06:19:21 +00:00
|
|
|
|
} else {
|
|
|
|
|
// ignore derives on non-item statements so it falls through
|
|
|
|
|
// to the unused-attributes lint
|
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
|
|
|
|
let (attr, after_derive) = self.classify_nonitem(&mut stmt);
|
|
|
|
|
(attr, vec![], after_derive)
|
2018-04-18 06:19:21 +00:00
|
|
|
|
};
|
2018-03-16 06:20:56 +00:00
|
|
|
|
|
|
|
|
|
if attr.is_some() || !derives.is_empty() {
|
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
|
|
|
|
return self.collect_attr(attr, derives, Annotatable::Stmt(P(stmt)),
|
2018-09-18 22:46:18 +00:00
|
|
|
|
AstFragmentKind::Stmts, after_derive).make_stmts();
|
2018-03-16 06:20:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-30 23:03:52 +00:00
|
|
|
|
|
2019-09-26 16:34:50 +00:00
|
|
|
|
if let StmtKind::Mac(mac) = stmt.kind {
|
2018-03-16 06:20:56 +00:00
|
|
|
|
let (mac, style, attrs) = mac.into_inner();
|
|
|
|
|
self.check_attributes(&attrs);
|
2018-06-19 23:08:08 +00:00
|
|
|
|
let mut placeholder = self.collect_bang(mac, stmt.span, AstFragmentKind::Stmts)
|
2018-03-16 06:20:56 +00:00
|
|
|
|
.make_stmts();
|
|
|
|
|
|
|
|
|
|
// If this is a macro invocation with a semicolon, then apply that
|
|
|
|
|
// semicolon to the final statement produced by expansion.
|
|
|
|
|
if style == MacStmtStyle::Semicolon {
|
|
|
|
|
if let Some(stmt) = placeholder.pop() {
|
|
|
|
|
placeholder.push(stmt.add_trailing_semicolon());
|
|
|
|
|
}
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
2018-03-16 06:20:56 +00:00
|
|
|
|
|
|
|
|
|
return placeholder;
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-16 06:20:56 +00:00
|
|
|
|
// The placeholder expander gives ids to statements, so we avoid folding the id here.
|
2019-09-26 16:34:50 +00:00
|
|
|
|
let ast::Stmt { id, kind, span } = stmt;
|
|
|
|
|
noop_flat_map_stmt_kind(kind, self).into_iter().map(|kind| {
|
|
|
|
|
ast::Stmt { id, kind, span }
|
2018-03-16 06:20:56 +00:00
|
|
|
|
}).collect()
|
|
|
|
|
|
2013-08-29 19:10:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
fn visit_block(&mut self, block: &mut P<Block>) {
|
2016-11-05 04:16:26 +00:00
|
|
|
|
let old_directory_ownership = self.cx.current_expansion.directory_ownership;
|
|
|
|
|
self.cx.current_expansion.directory_ownership = DirectoryOwnership::UnownedViaBlock;
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
noop_visit_block(block, self);
|
2016-11-05 04:16:26 +00:00
|
|
|
|
self.cx.current_expansion.directory_ownership = old_directory_ownership;
|
2013-08-29 19:10:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
|
|
|
|
|
let mut item = configure!(self, item);
|
2016-09-07 22:24:01 +00:00
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
let (attr, traits, after_derive) = self.classify_item(&mut item);
|
2017-02-02 07:01:15 +00:00
|
|
|
|
if attr.is_some() || !traits.is_empty() {
|
2018-09-16 14:15:07 +00:00
|
|
|
|
return self.collect_attr(attr, traits, Annotatable::Item(item),
|
2018-09-18 22:46:18 +00:00
|
|
|
|
AstFragmentKind::Items, after_derive).make_items();
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-26 16:51:36 +00:00
|
|
|
|
match item.kind {
|
2016-08-30 23:03:52 +00:00
|
|
|
|
ast::ItemKind::Mac(..) => {
|
2016-12-01 11:20:04 +00:00
|
|
|
|
self.check_attributes(&item.attrs);
|
2019-09-26 16:51:36 +00:00
|
|
|
|
item.and_then(|item| match item.kind {
|
2019-06-30 22:08:49 +00:00
|
|
|
|
ItemKind::Mac(mac) => self.collect(
|
|
|
|
|
AstFragmentKind::Items, InvocationKind::Bang { mac, span: item.span }
|
|
|
|
|
).make_items(),
|
2016-08-30 23:03:52 +00:00
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
ast::ItemKind::Mod(ast::Mod { inner, .. }) => {
|
2019-05-11 16:08:09 +00:00
|
|
|
|
if item.ident == Ident::invalid() {
|
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
|
|
|
|
return noop_flat_map_item(item, self);
|
2016-09-06 01:45:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-05 04:16:26 +00:00
|
|
|
|
let orig_directory_ownership = self.cx.current_expansion.directory_ownership;
|
2016-09-07 23:21:59 +00:00
|
|
|
|
let mut module = (*self.cx.current_expansion.module).clone();
|
|
|
|
|
module.mod_path.push(item.ident);
|
2016-09-05 04:08:38 +00:00
|
|
|
|
|
|
|
|
|
// Detect if this is an inline module (`mod m { ... }` as opposed to `mod m;`).
|
2018-11-27 02:59:49 +00:00
|
|
|
|
// In the non-inline case, `inner` is never the dummy span (cf. `parse_item_mod`).
|
2016-09-05 04:08:38 +00:00
|
|
|
|
// Thus, if `inner` is the dummy span, we know the module is inline.
|
2018-06-24 22:00:21 +00:00
|
|
|
|
let inline_module = item.span.contains(inner) || inner.is_dummy();
|
2016-09-05 04:08:38 +00:00
|
|
|
|
|
|
|
|
|
if inline_module {
|
2019-05-08 03:21:18 +00:00
|
|
|
|
if let Some(path) = attr::first_attr_value_str_by_name(&item.attrs, sym::path) {
|
2017-11-28 02:14:24 +00:00
|
|
|
|
self.cx.current_expansion.directory_ownership =
|
|
|
|
|
DirectoryOwnership::Owned { relative: None };
|
2016-11-16 10:52:37 +00:00
|
|
|
|
module.directory.push(&*path.as_str());
|
2016-09-27 21:14:45 +00:00
|
|
|
|
} else {
|
2018-05-26 12:12:38 +00:00
|
|
|
|
module.directory.push(&*item.ident.as_str());
|
2016-09-27 21:14:45 +00:00
|
|
|
|
}
|
2016-08-30 23:03:52 +00:00
|
|
|
|
} else {
|
2018-08-18 10:14:09 +00:00
|
|
|
|
let path = self.cx.parse_sess.source_map().span_to_unmapped_path(inner);
|
2017-12-14 07:09:19 +00:00
|
|
|
|
let mut path = match path {
|
|
|
|
|
FileName::Real(path) => path,
|
|
|
|
|
other => PathBuf::from(other.to_string()),
|
|
|
|
|
};
|
2016-11-05 04:16:26 +00:00
|
|
|
|
let directory_ownership = match path.file_name().unwrap().to_str() {
|
2017-11-28 02:14:24 +00:00
|
|
|
|
Some("mod.rs") => DirectoryOwnership::Owned { relative: None },
|
|
|
|
|
Some(_) => DirectoryOwnership::Owned {
|
|
|
|
|
relative: Some(item.ident),
|
|
|
|
|
},
|
|
|
|
|
None => DirectoryOwnership::UnownedViaMod(false),
|
2016-11-05 04:16:26 +00:00
|
|
|
|
};
|
|
|
|
|
path.pop();
|
|
|
|
|
module.directory = path;
|
|
|
|
|
self.cx.current_expansion.directory_ownership = directory_ownership;
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
|
let orig_module =
|
|
|
|
|
mem::replace(&mut self.cx.current_expansion.module, Rc::new(module));
|
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
|
|
|
|
let result = noop_flat_map_item(item, self);
|
2016-09-07 23:21:59 +00:00
|
|
|
|
self.cx.current_expansion.module = orig_module;
|
2016-11-05 04:16:26 +00:00
|
|
|
|
self.cx.current_expansion.directory_ownership = orig_directory_ownership;
|
2017-05-12 18:05:39 +00:00
|
|
|
|
result
|
2016-09-07 23:21:59 +00:00
|
|
|
|
}
|
2018-07-21 01:04:02 +00:00
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
_ => noop_flat_map_item(item, self),
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
2014-12-02 18:07: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_trait_item(&mut self, item: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> {
|
|
|
|
|
let mut item = configure!(self, item);
|
2016-09-07 22:24:01 +00:00
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
let (attr, traits, after_derive) = self.classify_item(&mut item);
|
2017-02-02 07:01:15 +00:00
|
|
|
|
if attr.is_some() || !traits.is_empty() {
|
2018-09-16 14:15:07 +00:00
|
|
|
|
return self.collect_attr(attr, traits, Annotatable::TraitItem(P(item)),
|
2018-09-18 22:46:18 +00:00
|
|
|
|
AstFragmentKind::TraitItems, after_derive).make_trait_items()
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-26 16:07:54 +00:00
|
|
|
|
match item.kind {
|
2016-08-30 23:03:52 +00:00
|
|
|
|
ast::TraitItemKind::Macro(mac) => {
|
|
|
|
|
let ast::TraitItem { attrs, span, .. } = item;
|
2016-12-01 11:54:01 +00:00
|
|
|
|
self.check_attributes(&attrs);
|
2018-06-19 23:08:08 +00:00
|
|
|
|
self.collect_bang(mac, span, AstFragmentKind::TraitItems).make_trait_items()
|
2016-08-30 23:03:52 +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
|
|
|
|
_ => noop_flat_map_trait_item(item, self),
|
2016-08-30 23:03:52 +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_impl_item(&mut self, item: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> {
|
|
|
|
|
let mut item = configure!(self, item);
|
2016-09-07 22:24:01 +00:00
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
let (attr, traits, after_derive) = self.classify_item(&mut item);
|
2017-02-02 07:01:15 +00:00
|
|
|
|
if attr.is_some() || !traits.is_empty() {
|
2018-09-16 14:15:07 +00:00
|
|
|
|
return self.collect_attr(attr, traits, Annotatable::ImplItem(P(item)),
|
2018-09-18 22:46:18 +00:00
|
|
|
|
AstFragmentKind::ImplItems, after_derive).make_impl_items();
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-26 15:38:13 +00:00
|
|
|
|
match item.kind {
|
2016-08-30 23:03:52 +00:00
|
|
|
|
ast::ImplItemKind::Macro(mac) => {
|
|
|
|
|
let ast::ImplItem { attrs, span, .. } = item;
|
2016-12-01 11:54:01 +00:00
|
|
|
|
self.check_attributes(&attrs);
|
2018-06-19 23:08:08 +00:00
|
|
|
|
self.collect_bang(mac, span, AstFragmentKind::ImplItems).make_impl_items()
|
2016-08-30 23:03:52 +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
|
|
|
|
_ => noop_flat_map_impl_item(item, self),
|
2016-08-30 23:03:52 +00:00
|
|
|
|
}
|
2014-07-04 18:24:28 +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 {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
ast::TyKind::Mac(_) => {}
|
|
|
|
|
_ => return noop_visit_ty(ty, self),
|
2016-08-30 23:03:52 +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
|
|
|
|
visit_clobber(ty, |mut ty| {
|
2019-09-26 16:25:31 +00:00
|
|
|
|
match mem::replace(&mut ty.kind, ast::TyKind::Err) {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
ast::TyKind::Mac(mac) =>
|
|
|
|
|
self.collect_bang(mac, ty.span, AstFragmentKind::Ty).make_ty(),
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-07-26 04:54:19 +00:00
|
|
|
|
}
|
2016-09-07 22:24:01 +00:00
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
fn visit_foreign_mod(&mut self, foreign_mod: &mut ast::ForeignMod) {
|
|
|
|
|
self.cfg.configure_foreign_mod(foreign_mod);
|
|
|
|
|
noop_visit_foreign_mod(foreign_mod, self);
|
2016-09-07 22:24:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
fn flat_map_foreign_item(&mut self, mut foreign_item: ast::ForeignItem)
|
2018-08-30 09:42:16 +00:00
|
|
|
|
-> SmallVec<[ast::ForeignItem; 1]>
|
|
|
|
|
{
|
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
|
|
|
|
let (attr, traits, after_derive) = self.classify_item(&mut foreign_item);
|
2018-03-11 02:16:26 +00:00
|
|
|
|
|
2018-08-04 02:17:51 +00:00
|
|
|
|
if attr.is_some() || !traits.is_empty() {
|
2018-09-16 14:15:07 +00:00
|
|
|
|
return self.collect_attr(attr, traits, Annotatable::ForeignItem(P(foreign_item)),
|
2018-09-18 22:46:18 +00:00
|
|
|
|
AstFragmentKind::ForeignItems, after_derive)
|
2018-09-16 14:15:07 +00:00
|
|
|
|
.make_foreign_items();
|
2018-03-11 02:16:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-26 16:58:14 +00:00
|
|
|
|
if let ast::ForeignItemKind::Macro(mac) = foreign_item.kind {
|
2018-03-11 02:16:26 +00:00
|
|
|
|
self.check_attributes(&foreign_item.attrs);
|
2018-06-19 23:08:08 +00:00
|
|
|
|
return self.collect_bang(mac, foreign_item.span, AstFragmentKind::ForeignItems)
|
2018-03-11 02:16:26 +00:00
|
|
|
|
.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 04:20:55 +00:00
|
|
|
|
noop_flat_map_foreign_item(foreign_item, self)
|
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_item_kind(&mut self, item: &mut ast::ItemKind) {
|
2017-03-05 05:15:58 +00:00
|
|
|
|
match item {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
ast::ItemKind::MacroDef(..) => {}
|
|
|
|
|
_ => {
|
|
|
|
|
self.cfg.configure_item_kind(item);
|
|
|
|
|
noop_visit_item_kind(item, self);
|
|
|
|
|
}
|
2017-03-05 05:15:58 +00:00
|
|
|
|
}
|
2016-09-07 22:24:01 +00:00
|
|
|
|
}
|
2016-09-05 00:10:27 +00:00
|
|
|
|
|
2019-09-09 12:26:25 +00:00
|
|
|
|
fn flat_map_generic_param(
|
|
|
|
|
&mut self,
|
|
|
|
|
param: ast::GenericParam
|
2019-08-24 16:54:40 +00:00
|
|
|
|
) -> SmallVec<[ast::GenericParam; 1]>
|
|
|
|
|
{
|
2019-09-09 12:26:25 +00:00
|
|
|
|
let mut param = configure!(self, param);
|
|
|
|
|
|
|
|
|
|
let (attr, traits, after_derive) = self.classify_item(&mut param);
|
|
|
|
|
if attr.is_some() || !traits.is_empty() {
|
|
|
|
|
return self.collect_attr(attr, traits, Annotatable::GenericParam(param),
|
|
|
|
|
AstFragmentKind::GenericParams, after_derive)
|
|
|
|
|
.make_generic_params();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-24 16:54:40 +00:00
|
|
|
|
noop_flat_map_generic_param(param, self)
|
2018-06-01 21:10:29 +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_attribute(&mut self, at: &mut ast::Attribute) {
|
2017-09-22 03:37:00 +00:00
|
|
|
|
// turn `#[doc(include="filename")]` attributes into `#[doc(include(file="filename",
|
|
|
|
|
// contents="file contents")]` attributes
|
2019-05-08 03:21:18 +00:00
|
|
|
|
if !at.check_name(sym::doc) {
|
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
|
|
|
|
return noop_visit_attribute(at, self);
|
2017-09-22 03:37:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(list) = at.meta_item_list() {
|
2019-05-08 03:21:18 +00:00
|
|
|
|
if !list.iter().any(|it| it.check_name(sym::include)) {
|
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
|
|
|
|
return noop_visit_attribute(at, self);
|
2017-09-22 03:37:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut items = vec![];
|
|
|
|
|
|
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
|
|
|
|
for mut it in list {
|
2019-05-08 03:21:18 +00:00
|
|
|
|
if !it.check_name(sym::include) {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
items.push({ noop_visit_meta_list_item(&mut it, self); it });
|
2017-09-22 03:37:00 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(file) = it.value_str() {
|
|
|
|
|
let err_count = self.cx.parse_sess.span_diagnostic.err_count();
|
2019-09-14 18:29:59 +00:00
|
|
|
|
self.check_attributes(slice::from_ref(at));
|
2017-09-22 03:37:00 +00:00
|
|
|
|
if self.cx.parse_sess.span_diagnostic.err_count() > err_count {
|
|
|
|
|
// avoid loading the file if they haven't enabled the feature
|
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
|
|
|
|
return noop_visit_attribute(at, self);
|
2017-09-22 03:37:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-22 19:51:51 +00:00
|
|
|
|
let filename = self.cx.resolve_path(&*file.as_str(), it.span());
|
2019-08-13 16:51:32 +00:00
|
|
|
|
match self.cx.source_map().load_file(&filename) {
|
|
|
|
|
Ok(source_file) => {
|
|
|
|
|
let src = source_file.src.as_ref()
|
|
|
|
|
.expect("freshly loaded file should have a source");
|
|
|
|
|
let src_interned = Symbol::intern(src.as_str());
|
2017-12-19 22:43:32 +00:00
|
|
|
|
|
2017-09-22 03:37:00 +00:00
|
|
|
|
let include_info = vec![
|
2019-03-03 17:56:24 +00:00
|
|
|
|
ast::NestedMetaItem::MetaItem(
|
2018-11-16 21:22:06 +00:00
|
|
|
|
attr::mk_name_value_item_str(
|
2019-08-10 23:20:18 +00:00
|
|
|
|
Ident::with_dummy_span(sym::file),
|
2019-08-14 22:56:44 +00:00
|
|
|
|
file,
|
|
|
|
|
DUMMY_SP,
|
2018-11-16 21:22:06 +00:00
|
|
|
|
),
|
2019-03-03 17:56:24 +00:00
|
|
|
|
),
|
|
|
|
|
ast::NestedMetaItem::MetaItem(
|
2018-11-16 21:22:06 +00:00
|
|
|
|
attr::mk_name_value_item_str(
|
2019-08-10 23:20:18 +00:00
|
|
|
|
Ident::with_dummy_span(sym::contents),
|
2019-08-14 22:56:44 +00:00
|
|
|
|
src_interned,
|
|
|
|
|
DUMMY_SP,
|
2018-11-16 21:22:06 +00:00
|
|
|
|
),
|
2019-03-03 17:56:24 +00:00
|
|
|
|
),
|
2017-09-22 03:37:00 +00:00
|
|
|
|
];
|
|
|
|
|
|
2019-08-10 23:20:18 +00:00
|
|
|
|
let include_ident = Ident::with_dummy_span(sym::include);
|
2019-08-04 21:59:06 +00:00
|
|
|
|
let item = attr::mk_list_item(include_ident, include_info);
|
2019-03-03 17:56:24 +00:00
|
|
|
|
items.push(ast::NestedMetaItem::MetaItem(item));
|
2017-09-22 03:37:00 +00:00
|
|
|
|
}
|
2018-11-16 21:22:06 +00:00
|
|
|
|
Err(e) => {
|
2018-11-28 19:54:08 +00:00
|
|
|
|
let lit = it
|
|
|
|
|
.meta_item()
|
|
|
|
|
.and_then(|item| item.name_value_literal())
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
if e.kind() == ErrorKind::InvalidData {
|
|
|
|
|
self.cx
|
|
|
|
|
.struct_span_err(
|
|
|
|
|
lit.span,
|
|
|
|
|
&format!("{} wasn't a utf-8 file", filename.display()),
|
|
|
|
|
)
|
|
|
|
|
.span_label(lit.span, "contains invalid utf-8")
|
|
|
|
|
.emit();
|
|
|
|
|
} else {
|
|
|
|
|
let mut err = self.cx.struct_span_err(
|
|
|
|
|
lit.span,
|
|
|
|
|
&format!("couldn't read {}: {}", filename.display(), e),
|
|
|
|
|
);
|
|
|
|
|
err.span_label(lit.span, "couldn't read file");
|
|
|
|
|
|
|
|
|
|
err.emit();
|
|
|
|
|
}
|
2017-09-22 03:37:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-11-28 17:11:45 +00:00
|
|
|
|
let mut err = self.cx.struct_span_err(
|
2019-03-03 17:56:24 +00:00
|
|
|
|
it.span(),
|
2018-11-28 17:11:45 +00:00
|
|
|
|
&format!("expected path to external documentation"),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Check if the user erroneously used `doc(include(...))` syntax.
|
|
|
|
|
let literal = it.meta_item_list().and_then(|list| {
|
|
|
|
|
if list.len() == 1 {
|
2019-09-26 15:56:53 +00:00
|
|
|
|
list[0].literal().map(|literal| &literal.kind)
|
2018-11-28 17:11:45 +00:00
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let (path, applicability) = match &literal {
|
|
|
|
|
Some(LitKind::Str(path, ..)) => {
|
|
|
|
|
(path.to_string(), Applicability::MachineApplicable)
|
|
|
|
|
}
|
|
|
|
|
_ => (String::from("<path>"), Applicability::HasPlaceholders),
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-25 21:03:27 +00:00
|
|
|
|
err.span_suggestion(
|
2019-03-03 17:56:24 +00:00
|
|
|
|
it.span(),
|
2018-11-28 17:11:45 +00:00
|
|
|
|
"provide a file path with `=`",
|
|
|
|
|
format!("include = \"{}\"", path),
|
|
|
|
|
applicability,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
err.emit();
|
2017-09-22 03:37:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 23:20:18 +00:00
|
|
|
|
let meta = attr::mk_list_item(Ident::with_dummy_span(sym::doc), items);
|
2019-07-30 17:50:22 +00:00
|
|
|
|
*at = attr::Attribute {
|
|
|
|
|
span: at.span,
|
|
|
|
|
id: at.id,
|
|
|
|
|
style: at.style,
|
|
|
|
|
path: meta.path,
|
2019-09-26 17:04:05 +00:00
|
|
|
|
tokens: meta.kind.tokens(meta.span),
|
2019-07-30 17:50:22 +00:00
|
|
|
|
is_sugared_doc: false,
|
|
|
|
|
};
|
2017-09-22 03:37:00 +00:00
|
|
|
|
} else {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
noop_visit_attribute(at, self)
|
2017-09-22 03:37:00 +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_id(&mut self, id: &mut ast::NodeId) {
|
2016-09-06 05:42:45 +00:00
|
|
|
|
if self.monotonic {
|
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
|
|
|
|
debug_assert_eq!(*id, ast::DUMMY_NODE_ID);
|
|
|
|
|
*id = self.cx.resolver.next_node_id()
|
2016-09-06 05:42:45 +00:00
|
|
|
|
}
|
2016-09-05 00:10:27 +00:00
|
|
|
|
}
|
2019-06-09 10:58:40 +00:00
|
|
|
|
|
|
|
|
|
fn visit_fn_decl(&mut self, mut fn_decl: &mut P<ast::FnDecl>) {
|
|
|
|
|
self.cfg.configure_fn_decl(&mut fn_decl);
|
|
|
|
|
noop_visit_fn_decl(fn_decl, self);
|
|
|
|
|
}
|
2013-07-16 05:05:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-15 20:30:45 +00:00
|
|
|
|
pub struct ExpansionConfig<'feat> {
|
2014-06-06 20:21:18 +00:00
|
|
|
|
pub crate_name: String,
|
2015-02-15 20:30:45 +00:00
|
|
|
|
pub features: Option<&'feat Features>,
|
2015-01-17 23:33:05 +00:00
|
|
|
|
pub recursion_limit: usize,
|
2015-04-14 13:36:38 +00:00
|
|
|
|
pub trace_mac: bool,
|
2016-06-01 01:27:12 +00:00
|
|
|
|
pub should_test: bool, // If false, strip `#[test]` nodes
|
2016-09-12 09:47:54 +00:00
|
|
|
|
pub single_step: bool,
|
|
|
|
|
pub keep_macs: bool,
|
2014-09-27 00:14:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-15 20:30:45 +00:00
|
|
|
|
impl<'feat> ExpansionConfig<'feat> {
|
|
|
|
|
pub fn default(crate_name: String) -> ExpansionConfig<'static> {
|
2014-09-27 00:14:23 +00:00
|
|
|
|
ExpansionConfig {
|
2017-08-07 05:54:09 +00:00
|
|
|
|
crate_name,
|
2015-02-15 20:30:45 +00:00
|
|
|
|
features: None,
|
2017-05-01 17:49:15 +00:00
|
|
|
|
recursion_limit: 1024,
|
2015-04-14 13:36:38 +00:00
|
|
|
|
trace_mac: false,
|
2016-06-01 01:27:12 +00:00
|
|
|
|
should_test: false,
|
2016-09-12 09:47:54 +00:00
|
|
|
|
single_step: false,
|
|
|
|
|
keep_macs: false,
|
2014-09-27 00:14:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-15 20:30:45 +00:00
|
|
|
|
|
2019-06-22 13:18:05 +00:00
|
|
|
|
fn proc_macro_hygiene(&self) -> bool {
|
|
|
|
|
self.features.map_or(false, |features| features.proc_macro_hygiene)
|
|
|
|
|
}
|
|
|
|
|
fn custom_inner_attributes(&self) -> bool {
|
2019-06-29 11:06:22 +00:00
|
|
|
|
self.features.map_or(false, |features| features.custom_inner_attributes)
|
2018-09-09 22:54:51 +00:00
|
|
|
|
}
|
2014-03-01 07:17:38 +00:00
|
|
|
|
}
|