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-06-16 15:58:39 +00:00
|
|
|
|
use crate::source_map::{dummy_spanned, respan};
|
2019-02-06 17:33:01 +00:00
|
|
|
|
use crate::config::StripUnconfigured;
|
|
|
|
|
use crate::ext::base::*;
|
|
|
|
|
use crate::ext::derive::{add_derived_markers, collect_derives};
|
2019-04-05 22:15:49 +00:00
|
|
|
|
use crate::ext::hygiene::{Mark, SyntaxContext};
|
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::fs;
|
|
|
|
|
use std::io::ErrorKind;
|
2018-07-22 23:52:51 +00:00
|
|
|
|
use std::{iter, mem};
|
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
|
|
|
|
|
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 MacroExpander<'a, 'b> {
|
|
|
|
|
fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
|
2018-06-22 22:05:07 +00:00
|
|
|
|
self.expand_fragment(AstFragment::OptExpr(Some(expr))).make_opt_expr()
|
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
|
|
|
|
$($(fn $mut_visit_ast(&mut self, ast: &mut $AstTy) {
|
|
|
|
|
visit_clobber(ast, |ast| self.expand_fragment(AstFragment::$Kind(ast)).$make_ast());
|
2019-05-29 18:05:43 +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_ast_elt(&mut self, ast_elt: <$AstTy as IntoIterator>::Item) -> $AstTy {
|
2018-08-13 19:15:16 +00:00
|
|
|
|
self.expand_fragment(AstFragment::$Kind(smallvec![ast_elt])).$make_ast()
|
2019-05-29 18:05:43 +00:00
|
|
|
|
})?)*
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
2016-09-23 09:32:58 +00:00
|
|
|
|
|
2019-02-06 17:33:01 +00:00
|
|
|
|
impl<'a> MacResult for crate::ext::tt::macro_rules::ParserAnyMacro<'a> {
|
|
|
|
|
$(fn $make_ast(self: Box<crate::ext::tt::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]>) {
|
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
|
|
|
|
"foreign item"; many fn flat_map_foreign_item; fn visit_foreign_item; fn make_foreign_items;
|
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 {
|
|
|
|
|
fn dummy(self, span: Span) -> Option<AstFragment> {
|
2017-12-26 07:47:32 +00:00
|
|
|
|
self.make_from(DummyResult::any(span))
|
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 {
|
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,
|
2018-06-19 23:08:08 +00:00
|
|
|
|
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 {
|
2017-02-02 07:01:15 +00:00
|
|
|
|
attr: Option<ast::Attribute>,
|
2017-03-08 23:13:35 +00:00
|
|
|
|
traits: Vec<Path>,
|
2016-09-02 06:14:38 +00:00
|
|
|
|
item: Annotatable,
|
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-06-28 23:30:53 +00:00
|
|
|
|
item_with_markers: Annotatable,
|
2017-02-01 10:33:09 +00:00
|
|
|
|
},
|
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 {
|
2016-09-07 23:21:59 +00:00
|
|
|
|
match self.kind {
|
|
|
|
|
InvocationKind::Bang { span, .. } => span,
|
2017-02-02 07:01:15 +00:00
|
|
|
|
InvocationKind::Attr { attr: Some(ref attr), .. } => attr.span,
|
2017-03-08 23:13:35 +00:00
|
|
|
|
InvocationKind::Attr { attr: None, .. } => DUMMY_SP,
|
|
|
|
|
InvocationKind::Derive { ref path, .. } => path.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,
|
|
|
|
|
node: 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
|
|
|
|
|
2018-06-22 22:05:07 +00:00
|
|
|
|
match self.expand_fragment(krate_item).make_items().pop().map(P::into_inner) {
|
2017-04-20 23:13:13 +00:00
|
|
|
|
Some(ast::Item { attrs, node: 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
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 23:08:08 +00:00
|
|
|
|
// Fully expand all macro invocations in this AST fragment.
|
2018-06-22 22:05:07 +00:00
|
|
|
|
fn 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();
|
2018-08-18 10:55:43 +00:00
|
|
|
|
let mut derives: FxHashMap<Mark, 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
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let scope =
|
|
|
|
|
if self.monotonic { invoc.expansion_data.mark } else { orig_expansion_data.mark };
|
2018-08-15 00:51:12 +00:00
|
|
|
|
let ext = match self.cx.resolver.resolve_macro_invocation(&invoc, scope, force) {
|
2019-07-03 08:44:57 +00:00
|
|
|
|
Ok(ext) => ext,
|
|
|
|
|
Err(Indeterminate) => {
|
2016-10-11 03:41:48 +00:00
|
|
|
|
undetermined_invocations.push(invoc);
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
progress = true;
|
2016-09-07 23:21:59 +00:00
|
|
|
|
let ExpansionData { depth, mark, .. } = invoc.expansion_data;
|
|
|
|
|
self.cx.current_expansion = invoc.expansion_data.clone();
|
2016-09-19 07:27:20 +00:00
|
|
|
|
self.cx.current_expansion.mark = scope;
|
2019-07-03 09:47:24 +00:00
|
|
|
|
|
2017-02-02 07:01:15 +00:00
|
|
|
|
// FIXME(jseyfried): Refactor out the following logic
|
2018-06-19 23:08:08 +00:00
|
|
|
|
let (expanded_fragment, new_invocations) = if let Some(ext) = ext {
|
2019-07-03 08:44:57 +00:00
|
|
|
|
let (invoc_fragment_kind, invoc_span) = (invoc.fragment_kind, invoc.span());
|
2019-07-03 09:47:24 +00:00
|
|
|
|
let fragment = self.expand_invoc(invoc, &ext).unwrap_or_else(|| {
|
2019-07-03 08:44:57 +00:00
|
|
|
|
invoc_fragment_kind.dummy(invoc_span).unwrap()
|
|
|
|
|
});
|
|
|
|
|
self.collect_invocations(fragment, &[])
|
|
|
|
|
} else if let InvocationKind::Attr { attr: None, traits, item, .. } = invoc.kind {
|
|
|
|
|
if !item.derive_allowed() {
|
|
|
|
|
let attr = attr::find_by_name(item.attrs(), sym::derive)
|
|
|
|
|
.expect("`derive` attribute should exist");
|
|
|
|
|
let span = attr.span;
|
|
|
|
|
let mut err = self.cx.mut_span_err(span,
|
|
|
|
|
"`derive` may only be applied to \
|
|
|
|
|
structs, enums and unions");
|
|
|
|
|
if let ast::AttrStyle::Inner = attr.style {
|
|
|
|
|
let trait_list = traits.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
|
|
|
|
|
);
|
2017-08-23 02:22:52 +00:00
|
|
|
|
}
|
2019-07-03 08:44:57 +00:00
|
|
|
|
err.emit();
|
|
|
|
|
}
|
2017-08-23 02:22:52 +00:00
|
|
|
|
|
2019-07-03 08:44:57 +00:00
|
|
|
|
let mut item = self.fully_configure(item);
|
|
|
|
|
item.visit_attrs(|attrs| attrs.retain(|a| a.path != sym::derive));
|
|
|
|
|
let mut item_with_markers = item.clone();
|
|
|
|
|
add_derived_markers(&mut self.cx, item.span(), &traits, &mut item_with_markers);
|
|
|
|
|
let derives = derives.entry(invoc.expansion_data.mark).or_default();
|
|
|
|
|
|
|
|
|
|
derives.reserve(traits.len());
|
|
|
|
|
invocations.reserve(traits.len());
|
|
|
|
|
for path in traits {
|
2019-07-06 18:02:45 +00:00
|
|
|
|
let mark = Mark::fresh(self.cx.current_expansion.mark, None);
|
2019-07-03 08:44:57 +00:00
|
|
|
|
derives.push(mark);
|
|
|
|
|
invocations.push(Invocation {
|
|
|
|
|
kind: InvocationKind::Derive {
|
|
|
|
|
path,
|
|
|
|
|
item: item.clone(),
|
|
|
|
|
item_with_markers: item_with_markers.clone(),
|
|
|
|
|
},
|
|
|
|
|
fragment_kind: invoc.fragment_kind,
|
|
|
|
|
expansion_data: ExpansionData {
|
|
|
|
|
mark,
|
|
|
|
|
..invoc.expansion_data.clone()
|
|
|
|
|
},
|
|
|
|
|
});
|
2017-02-02 07:01:15 +00:00
|
|
|
|
}
|
2019-07-03 08:44:57 +00:00
|
|
|
|
let fragment = invoc.fragment_kind
|
|
|
|
|
.expect_from_annotatables(::std::iter::once(item_with_markers));
|
|
|
|
|
self.collect_invocations(fragment, derives)
|
2017-02-02 07:01:15 +00:00
|
|
|
|
} else {
|
2019-07-03 08:44:57 +00:00
|
|
|
|
unreachable!()
|
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
|
|
|
|
}
|
2018-06-19 23:08:08 +00:00
|
|
|
|
expanded_fragments[depth - 1].push((mark, 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() {
|
|
|
|
|
for (mark, expanded_fragment) in expanded_fragments.into_iter().rev() {
|
2017-02-02 07:01:15 +00:00
|
|
|
|
let derives = derives.remove(&mark).unwrap_or_else(Vec::new);
|
2018-06-19 23:08:08 +00:00
|
|
|
|
placeholder_expander.add(NodeId::placeholder_from_mark(mark),
|
|
|
|
|
expanded_fragment, derives);
|
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.
|
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 collect_invocations(&mut self, mut fragment: AstFragment, derives: &[Mark])
|
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
|
|
|
|
|
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(
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
self.cx.current_expansion.mark, &fragment, derives);
|
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
|
|
|
|
}
|
2017-09-12 19:55:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 23:08:08 +00:00
|
|
|
|
fn expand_invoc(&mut self, invoc: Invocation, ext: &SyntaxExtension) -> Option<AstFragment> {
|
2018-08-04 02:17:51 +00:00
|
|
|
|
if invoc.fragment_kind == AstFragmentKind::ForeignItems &&
|
2019-06-22 13:18:05 +00:00
|
|
|
|
!self.cx.ecfg.macros_in_extern() {
|
2019-06-16 15:58:39 +00:00
|
|
|
|
if let SyntaxExtensionKind::NonMacroAttr { .. } = ext.kind {} else {
|
2019-05-08 03:21:18 +00:00
|
|
|
|
emit_feature_err(&self.cx.parse_sess, sym::macros_in_extern,
|
2018-08-04 02:17:51 +00:00
|
|
|
|
invoc.span(), GateIssue::Language,
|
|
|
|
|
"macro invocations in `extern {}` blocks are experimental");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-17 04:04:41 +00:00
|
|
|
|
let result = match invoc.kind {
|
2017-12-26 07:47:32 +00:00
|
|
|
|
InvocationKind::Bang { .. } => self.expand_bang_invoc(invoc, ext)?,
|
|
|
|
|
InvocationKind::Attr { .. } => self.expand_attr_invoc(invoc, ext)?,
|
|
|
|
|
InvocationKind::Derive { .. } => self.expand_derive_invoc(invoc, ext)?,
|
2017-03-17 04:04:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if self.cx.current_expansion.depth > self.cx.ecfg.recursion_limit {
|
|
|
|
|
let info = self.cx.current_expansion.mark.expn_info().unwrap();
|
|
|
|
|
let suggested_limit = self.cx.ecfg.recursion_limit * 2;
|
2017-09-02 16:13:25 +00:00
|
|
|
|
let mut err = self.cx.struct_span_err(info.call_site,
|
2017-03-17 04:04:41 +00:00
|
|
|
|
&format!("recursion limit reached while expanding the macro `{}`",
|
2019-06-18 22:08:45 +00:00
|
|
|
|
info.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
|
|
|
|
|
2017-12-26 07:47:32 +00:00
|
|
|
|
Some(result)
|
2016-09-01 07:01:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:47:32 +00:00
|
|
|
|
fn expand_attr_invoc(&mut self,
|
|
|
|
|
invoc: Invocation,
|
2018-02-27 16:11:14 +00:00
|
|
|
|
ext: &SyntaxExtension)
|
2018-06-19 23:08:08 +00:00
|
|
|
|
-> Option<AstFragment> {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
let (attr, mut item) = match invoc.kind {
|
2017-12-26 07:47:32 +00:00
|
|
|
|
InvocationKind::Attr { attr, item, .. } => (attr?, item),
|
2016-09-01 07:01:45 +00:00
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-16 15:58:39 +00:00
|
|
|
|
match &ext.kind {
|
|
|
|
|
SyntaxExtensionKind::NonMacroAttr { mark_used } => {
|
2018-07-22 23:52:51 +00:00
|
|
|
|
attr::mark_known(&attr);
|
2019-06-16 15:58:39 +00:00
|
|
|
|
if *mark_used {
|
|
|
|
|
attr::mark_used(&attr);
|
|
|
|
|
}
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
item.visit_attrs(|attrs| attrs.push(attr));
|
2018-07-22 23:52:51 +00:00
|
|
|
|
Some(invoc.fragment_kind.expect_from_annotatables(iter::once(item)))
|
|
|
|
|
}
|
2019-06-16 15:58:39 +00:00
|
|
|
|
SyntaxExtensionKind::LegacyAttr(expander) => {
|
2018-03-13 19:12:15 +00:00
|
|
|
|
let meta = attr.parse_meta(self.cx.parse_sess)
|
|
|
|
|
.map_err(|mut e| { e.emit(); }).ok()?;
|
2019-06-16 15:58:39 +00:00
|
|
|
|
let item = expander.expand(self.cx, attr.span, &meta, item);
|
2018-06-19 23:08:08 +00:00
|
|
|
|
Some(invoc.fragment_kind.expect_from_annotatables(item))
|
2016-09-01 07:01:45 +00:00
|
|
|
|
}
|
2019-06-16 15:58:39 +00:00
|
|
|
|
SyntaxExtensionKind::Attr(expander) => {
|
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
|
|
|
|
self.gate_proc_macro_attr_item(attr.span, &item);
|
2019-06-05 10:25:26 +00:00
|
|
|
|
let item_tok = TokenTree::token(token::Interpolated(Lrc::new(match item {
|
2017-03-29 01:55:01 +00:00
|
|
|
|
Annotatable::Item(item) => token::NtItem(item),
|
2017-12-16 23:21:29 +00:00
|
|
|
|
Annotatable::TraitItem(item) => token::NtTraitItem(item.into_inner()),
|
|
|
|
|
Annotatable::ImplItem(item) => token::NtImplItem(item.into_inner()),
|
2018-03-11 02:16:26 +00:00
|
|
|
|
Annotatable::ForeignItem(item) => token::NtForeignItem(item.into_inner()),
|
2018-03-16 06:20:56 +00:00
|
|
|
|
Annotatable::Stmt(stmt) => token::NtStmt(stmt.into_inner()),
|
|
|
|
|
Annotatable::Expr(expr) => token::NtExpr(expr),
|
2019-06-05 10:25:26 +00:00
|
|
|
|
})), DUMMY_SP).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
|
|
|
|
let input = self.extract_proc_macro_attr_input(attr.tokens, attr.span);
|
2019-06-16 15:58:39 +00:00
|
|
|
|
let tok_result = expander.expand(self.cx, attr.span, input, item_tok);
|
2018-06-19 23:08:08 +00:00
|
|
|
|
let res = self.parse_ast_fragment(tok_result, invoc.fragment_kind,
|
|
|
|
|
&attr.path, attr.span);
|
2018-05-09 22:03:02 +00:00
|
|
|
|
self.gate_proc_macro_expansion(attr.span, &res);
|
|
|
|
|
res
|
2016-08-29 04:16:43 +00:00
|
|
|
|
}
|
2019-07-03 09:47:24 +00:00
|
|
|
|
_ => 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 \
|
|
|
|
|
of the form #[foo] or #[foo(..)], the macro name must only be \
|
|
|
|
|
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) => {
|
|
|
|
|
match item.node {
|
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),
|
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),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 23:08:08 +00:00
|
|
|
|
fn gate_proc_macro_expansion(&self, span: Span, fragment: &Option<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-06-19 23:08:08 +00:00
|
|
|
|
let fragment = match fragment {
|
|
|
|
|
Some(fragment) => fragment,
|
2018-05-09 22:03:02 +00:00
|
|
|
|
None => 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) {
|
2018-08-17 21:52:34 +00:00
|
|
|
|
if let ast::ItemKind::MacroDef(_) = i.node {
|
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
|
|
|
|
/// Expand a macro invocation. Returns the resulting expanded AST fragment.
|
2017-12-26 07:47:32 +00:00
|
|
|
|
fn expand_bang_invoc(&mut self,
|
|
|
|
|
invoc: Invocation,
|
2018-02-27 16:11:14 +00:00
|
|
|
|
ext: &SyntaxExtension)
|
2018-06-19 23:08:08 +00:00
|
|
|
|
-> Option<AstFragment> {
|
2019-06-18 07:48:44 +00:00
|
|
|
|
let kind = invoc.fragment_kind;
|
2019-06-30 22:08:49 +00:00
|
|
|
|
let (mac, span) = match invoc.kind {
|
|
|
|
|
InvocationKind::Bang { mac, span } => (mac, span),
|
2016-09-01 07:01:45 +00:00
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
2017-02-21 05:05:59 +00:00
|
|
|
|
let path = &mac.node.path;
|
2016-09-01 07:01:45 +00:00
|
|
|
|
|
2019-06-16 15:58:39 +00:00
|
|
|
|
let opt_expanded = match &ext.kind {
|
2019-06-20 19:00:47 +00:00
|
|
|
|
SyntaxExtensionKind::Bang(expander) => {
|
|
|
|
|
self.gate_proc_macro_expansion_kind(span, kind);
|
|
|
|
|
let tok_result = expander.expand(self.cx, span, mac.node.stream());
|
|
|
|
|
let result = self.parse_ast_fragment(tok_result, kind, path, span);
|
|
|
|
|
self.gate_proc_macro_expansion(span, &result);
|
|
|
|
|
result
|
|
|
|
|
}
|
2019-06-16 15:58:39 +00:00
|
|
|
|
SyntaxExtensionKind::LegacyBang(expander) => {
|
2019-06-30 00:05:52 +00:00
|
|
|
|
let tok_result = expander.expand(self.cx, span, mac.node.stream());
|
2019-06-20 19:00:47 +00:00
|
|
|
|
kind.make_from(tok_result)
|
2016-09-01 07:01:45 +00:00
|
|
|
|
}
|
2019-07-03 09:47:24 +00:00
|
|
|
|
_ => unreachable!()
|
2016-09-01 07:01:45 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-12-26 07:47:32 +00:00
|
|
|
|
if opt_expanded.is_some() {
|
|
|
|
|
opt_expanded
|
|
|
|
|
} else {
|
2016-09-01 07:01:45 +00:00
|
|
|
|
let msg = format!("non-{kind} macro in {kind} position: {name}",
|
2018-03-18 00:53:41 +00:00
|
|
|
|
name = path.segments[0].ident.name, kind = kind.name());
|
2016-09-01 07:01:45 +00:00
|
|
|
|
self.cx.span_err(path.span, &msg);
|
2017-08-25 18:46:49 +00:00
|
|
|
|
self.cx.trace_macros_diag();
|
2017-03-28 05:32:43 +00:00
|
|
|
|
kind.dummy(span)
|
2017-12-26 07:47:32 +00:00
|
|
|
|
}
|
2016-09-02 09:12:47 +00:00
|
|
|
|
}
|
2016-09-26 04:16:55 +00:00
|
|
|
|
|
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,
|
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),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 23:08:08 +00:00
|
|
|
|
/// Expand a derive invocation. Returns the resulting expanded AST fragment.
|
2017-12-26 07:47:32 +00:00
|
|
|
|
fn expand_derive_invoc(&mut self,
|
|
|
|
|
invoc: Invocation,
|
2018-02-27 16:11:14 +00:00
|
|
|
|
ext: &SyntaxExtension)
|
2018-06-19 23:08:08 +00:00
|
|
|
|
-> Option<AstFragment> {
|
2017-03-08 23:13:35 +00:00
|
|
|
|
let (path, item) = match invoc.kind {
|
2019-06-28 23:30:53 +00:00
|
|
|
|
InvocationKind::Derive { path, item, item_with_markers } => match ext.kind {
|
|
|
|
|
SyntaxExtensionKind::LegacyDerive(..) => (path, item_with_markers),
|
|
|
|
|
_ => (path, item),
|
|
|
|
|
}
|
2017-02-01 10:33:09 +00:00
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
2017-12-26 07:47:32 +00:00
|
|
|
|
if !item.derive_allowed() {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
2017-02-01 10:33:09 +00:00
|
|
|
|
|
2019-06-16 15:58:39 +00:00
|
|
|
|
match &ext.kind {
|
|
|
|
|
SyntaxExtensionKind::Derive(expander) |
|
|
|
|
|
SyntaxExtensionKind::LegacyDerive(expander) => {
|
|
|
|
|
let meta = ast::MetaItem { node: ast::MetaItemKind::Word, span: path.span, path };
|
|
|
|
|
let span = meta.span.with_ctxt(self.cx.backtrace());
|
2019-06-06 20:21:44 +00:00
|
|
|
|
let items = expander.expand(self.cx, span, &meta, item);
|
2018-06-19 23:08:08 +00:00
|
|
|
|
Some(invoc.fragment_kind.expect_from_annotatables(items))
|
2017-02-01 10:33:09 +00:00
|
|
|
|
}
|
2019-07-03 09:47:24 +00:00
|
|
|
|
_ => unreachable!()
|
2017-02-01 10:33:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-19 23:08:08 +00:00
|
|
|
|
fn parse_ast_fragment(&mut self,
|
|
|
|
|
toks: TokenStream,
|
|
|
|
|
kind: AstFragmentKind,
|
|
|
|
|
path: &Path,
|
|
|
|
|
span: Span)
|
|
|
|
|
-> Option<AstFragment> {
|
2017-02-18 06:18:29 +00:00
|
|
|
|
let mut parser = self.cx.new_parser_from_tts(&toks.into_trees().collect::<Vec<_>>());
|
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);
|
2018-06-19 23:08:08 +00:00
|
|
|
|
Some(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);
|
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 {
|
2018-10-03 21:24:31 +00:00
|
|
|
|
items.push(self.parse_foreign_item()?);
|
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)?),
|
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-06-07 10:31:13 +00:00
|
|
|
|
let def_site_span = self.token.span.with_ctxt(SyntaxContext::empty());
|
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-07-06 18:02:45 +00:00
|
|
|
|
let mark = Mark::fresh(self.cx.current_expansion.mark, None);
|
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 {
|
2017-08-07 05:54:09 +00:00
|
|
|
|
mark,
|
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
|
|
|
|
});
|
2018-06-19 23:08:08 +00:00
|
|
|
|
placeholder(fragment_kind, NodeId::placeholder_from_mark(mark))
|
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>,
|
2017-03-08 23:13:35 +00:00
|
|
|
|
traits: 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 {
|
2018-09-18 22:46:18 +00:00
|
|
|
|
self.collect(kind, InvocationKind::Attr { attr, traits, item, after_derive })
|
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() {
|
2018-01-30 10:28:55 +00:00
|
|
|
|
self.check_attribute_inner(attr, 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
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-22 03:37:00 +00:00
|
|
|
|
|
|
|
|
|
fn check_attribute(&mut self, at: &ast::Attribute) {
|
|
|
|
|
let features = self.cx.ecfg.features.unwrap();
|
2018-01-30 10:28:55 +00:00
|
|
|
|
self.check_attribute_inner(at, features);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn check_attribute_inner(&mut self, at: &ast::Attribute, features: &Features) {
|
2017-09-22 03:37:00 +00:00
|
|
|
|
feature_gate::check_attribute(at, self.cx.parse_sess, features);
|
|
|
|
|
}
|
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| {
|
|
|
|
|
self.cfg.configure_expr_kind(&mut expr.node);
|
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
|
|
|
|
|
2018-11-20 09:16:57 +00:00
|
|
|
|
if let ast::ExprKind::Mac(mac) = expr.node {
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
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| {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
self.cfg.configure_expr_kind(&mut expr.node);
|
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
|
|
|
|
|
2018-11-20 09:16:57 +00:00
|
|
|
|
if let ast::ExprKind::Mac(mac) = expr.node {
|
|
|
|
|
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);
|
2016-08-30 23:03:52 +00:00
|
|
|
|
match pat.node {
|
|
|
|
|
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| {
|
|
|
|
|
match mem::replace(&mut pat.node, PatKind::Wild) {
|
|
|
|
|
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
|
|
|
|
|
2018-03-16 06:20:56 +00:00
|
|
|
|
if let StmtKind::Mac(mac) = stmt.node {
|
|
|
|
|
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.
|
|
|
|
|
let ast::Stmt { id, node, span } = 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
|
|
|
|
noop_flat_map_stmt_kind(node, self).into_iter().map(|node| {
|
2018-03-16 06:20:56 +00:00
|
|
|
|
ast::Stmt { id, node, span }
|
|
|
|
|
}).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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match item.node {
|
|
|
|
|
ast::ItemKind::Mac(..) => {
|
2016-12-01 11:20:04 +00:00
|
|
|
|
self.check_attributes(&item.attrs);
|
2017-03-05 05:15:58 +00:00
|
|
|
|
item.and_then(|item| match item.node {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match item.node {
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match item.node {
|
|
|
|
|
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>) {
|
|
|
|
|
match ty.node {
|
|
|
|
|
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| {
|
|
|
|
|
match mem::replace(&mut ty.node, ast::TyKind::Err) {
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let ast::ForeignItemKind::Macro(mac) = foreign_item.node {
|
|
|
|
|
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-06-05 14:59:37 +00:00
|
|
|
|
fn visit_generic_params(&mut self, params: &mut Vec<ast::GenericParam>) {
|
|
|
|
|
self.cfg.configure_generic_params(params);
|
|
|
|
|
noop_visit_generic_params(params, 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();
|
|
|
|
|
self.check_attribute(&at);
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let filename = self.cx.root_path.join(file.to_string());
|
2018-11-16 21:22:06 +00:00
|
|
|
|
match fs::read_to_string(&filename) {
|
2017-09-22 03:37:00 +00:00
|
|
|
|
Ok(src) => {
|
2018-05-23 14:19:20 +00:00
|
|
|
|
let src_interned = Symbol::intern(&src);
|
|
|
|
|
|
2017-12-19 22:43:32 +00:00
|
|
|
|
// Add this input file to the code map to make it available as
|
|
|
|
|
// dependency information
|
2018-08-18 10:14:09 +00:00
|
|
|
|
self.cx.source_map().new_source_file(filename.into(), src);
|
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-05-17 08:37:53 +00:00
|
|
|
|
Ident::with_empty_ctxt(sym::file),
|
2018-11-16 21:22:06 +00:00
|
|
|
|
dummy_spanned(file),
|
|
|
|
|
),
|
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-05-17 08:37:53 +00:00
|
|
|
|
Ident::with_empty_ctxt(sym::contents),
|
2018-11-16 21:22:06 +00:00
|
|
|
|
dummy_spanned(src_interned),
|
|
|
|
|
),
|
2019-03-03 17:56:24 +00:00
|
|
|
|
),
|
2017-09-22 03:37:00 +00:00
|
|
|
|
];
|
|
|
|
|
|
2019-05-17 08:37:53 +00:00
|
|
|
|
let include_ident = Ident::with_empty_ctxt(sym::include);
|
2018-03-24 18:17:27 +00:00
|
|
|
|
let item = attr::mk_list_item(DUMMY_SP, 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");
|
|
|
|
|
|
|
|
|
|
if e.kind() == ErrorKind::NotFound {
|
|
|
|
|
err.help("external doc paths are relative to the crate root");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
list[0].literal().map(|literal| &literal.node)
|
|
|
|
|
} 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-05-17 08:37:53 +00:00
|
|
|
|
let meta = attr::mk_list_item(DUMMY_SP, Ident::with_empty_ctxt(sym::doc), items);
|
2017-09-22 03:37:00 +00:00
|
|
|
|
match at.style {
|
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::AttrStyle::Inner => *at = attr::mk_spanned_attr_inner(at.span, at.id, meta),
|
|
|
|
|
ast::AttrStyle::Outer => *at = attr::mk_spanned_attr_outer(at.span, at.id, meta),
|
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 macros_in_extern(&self) -> bool {
|
|
|
|
|
self.features.map_or(false, |features| features.macros_in_extern)
|
Add #[allow_internal_unstable] to track stability for macros better.
Unstable items used in a macro expansion will now always trigger
stability warnings, *unless* the unstable items are directly inside a
macro marked with `#[allow_internal_unstable]`. IOW, the compiler warns
unless the span of the unstable item is a subspan of the definition of a
macro marked with that attribute.
E.g.
#[allow_internal_unstable]
macro_rules! foo {
($e: expr) => {{
$e;
unstable(); // no warning
only_called_by_foo!();
}}
}
macro_rules! only_called_by_foo {
() => { unstable() } // warning
}
foo!(unstable()) // warning
The unstable inside `foo` is fine, due to the attribute. But the
`unstable` inside `only_called_by_foo` is not, since that macro doesn't
have the attribute, and the `unstable` passed into `foo` is also not
fine since it isn't contained in the macro itself (that is, even though
it is only used directly in the macro).
In the process this makes the stability tracking much more precise,
e.g. previously `println!("{}", unstable())` got no warning, but now it
does. As such, this is a bug fix that may cause [breaking-change]s.
The attribute is definitely feature gated, since it explicitly allows
side-stepping the feature gating system.
2015-03-01 03:09:28 +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
|
|
|
|
}
|
|
|
|
|
|
2017-03-17 04:04:41 +00:00
|
|
|
|
// A Marker adds the given mark to the syntax context.
|
2017-07-20 04:54:01 +00:00
|
|
|
|
#[derive(Debug)]
|
2017-03-28 05:32:43 +00:00
|
|
|
|
pub struct Marker(pub Mark);
|
2013-07-09 22:56: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
|
|
|
|
impl MutVisitor for Marker {
|
|
|
|
|
fn visit_span(&mut self, span: &mut Span) {
|
|
|
|
|
*span = span.apply_mark(self.0)
|
2013-07-09 22:56:21 +00:00
|
|
|
|
}
|
2017-03-17 04:04:41 +00:00
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
|
fn visit_mac(&mut self, mac: &mut ast::Mac) {
|
|
|
|
|
noop_visit_mac(mac, self)
|
2017-03-17 04:04:41 +00:00
|
|
|
|
}
|
2013-07-09 22:56:21 +00:00
|
|
|
|
}
|