2019-10-16 03:59:30 -05:00
|
|
|
use crate::base::*;
|
2019-12-22 16:42:04 -06:00
|
|
|
use crate::config::StripUnconfigured;
|
2020-03-08 16:32:25 -05:00
|
|
|
use crate::configure;
|
2020-11-14 05:47:14 -06:00
|
|
|
use crate::hygiene::SyntaxContext;
|
2019-10-16 03:59:30 -05:00
|
|
|
use crate::mbe::macro_rules::annotate_err_with_kind;
|
2021-02-22 10:06:36 -06:00
|
|
|
use crate::module::{mod_dir_path, parse_external_mod, DirOwnership, ParsedExternalMod};
|
2019-10-16 03:59:30 -05:00
|
|
|
use crate::placeholders::{placeholder, PlaceholderExpander};
|
|
|
|
|
2020-11-14 05:47:14 -06:00
|
|
|
use rustc_ast as ast;
|
2020-02-29 11:37:32 -06:00
|
|
|
use rustc_ast::mut_visit::*;
|
|
|
|
use rustc_ast::ptr::P;
|
|
|
|
use rustc_ast::token;
|
2020-03-03 14:22:32 -06:00
|
|
|
use rustc_ast::tokenstream::TokenStream;
|
2020-02-29 11:37:32 -06:00
|
|
|
use rustc_ast::visit::{self, AssocCtxt, Visitor};
|
2020-11-28 17:33:17 -06:00
|
|
|
use rustc_ast::{AstLike, AttrItem, Block, Inline, ItemKind, LitKind, MacArgs};
|
2021-02-16 15:56:07 -06:00
|
|
|
use rustc_ast::{MacCallStmt, MacStmtStyle, MetaItemKind, ModKind, NestedMetaItem};
|
2020-11-14 05:47:14 -06:00
|
|
|
use rustc_ast::{NodeId, PatKind, Path, StmtKind, Unsafe};
|
2020-01-11 10:02:46 -06:00
|
|
|
use rustc_ast_pretty::pprust;
|
2021-02-23 09:21:20 -06:00
|
|
|
use rustc_attr::{self as attr, is_builtin_attr};
|
2020-04-17 23:01:54 -05:00
|
|
|
use rustc_data_structures::map_in_place::MapInPlace;
|
2020-07-25 04:57:35 -05:00
|
|
|
use rustc_data_structures::stack::ensure_sufficient_stack;
|
2020-11-14 05:47:14 -06:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
|
|
|
use rustc_errors::{Applicability, PResult};
|
2019-11-29 17:23:38 -06:00
|
|
|
use rustc_feature::Features;
|
2020-11-21 15:12:05 -06:00
|
|
|
use rustc_parse::parser::{AttemptLocalParseRecovery, ForceCollect, Parser, RecoverComma};
|
2019-10-15 15:48:13 -05:00
|
|
|
use rustc_parse::validate_attr;
|
2020-02-15 06:29:45 -06:00
|
|
|
use rustc_session::lint::builtin::UNUSED_DOC_COMMENTS;
|
2020-02-21 18:46:14 -06:00
|
|
|
use rustc_session::lint::BuiltinLintDiagnostics;
|
2020-01-11 08:03:15 -06:00
|
|
|
use rustc_session::parse::{feature_err, ParseSess};
|
2020-05-26 13:48:08 -05:00
|
|
|
use rustc_session::Limit;
|
2020-04-19 06:00:18 -05:00
|
|
|
use rustc_span::symbol::{sym, Ident, Symbol};
|
2020-03-17 10:45:02 -05:00
|
|
|
use rustc_span::{ExpnId, FileName, Span, DUMMY_SP};
|
2019-02-06 11:33:01 -06:00
|
|
|
|
|
|
|
use smallvec::{smallvec, SmallVec};
|
2018-11-16 15:22:06 -06:00
|
|
|
use std::io::ErrorKind;
|
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-04 22:20:55 -06:00
|
|
|
use std::ops::DerefMut;
|
2017-12-14 01:09:19 -06:00
|
|
|
use std::path::PathBuf;
|
2019-12-22 16:42:04 -06:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::{iter, mem, slice};
|
2016-08-31 04:02:45 -05:00
|
|
|
|
2018-06-19 18:08:08 -05:00
|
|
|
macro_rules! ast_fragments {
|
2018-06-22 17:05:07 -05:00
|
|
|
(
|
|
|
|
$($Kind:ident($AstTy:ty) {
|
|
|
|
$kind_name:expr;
|
2019-02-20 01:10:11 -06:00
|
|
|
$(one fn $mut_visit_ast:ident; fn $visit_ast:ident;)?
|
2020-01-29 17:18:54 -06:00
|
|
|
$(many fn $flat_map_ast_elt:ident; fn $visit_ast_elt:ident($($args:tt)*);)?
|
2018-06-22 17:05:07 -05:00
|
|
|
fn $make_ast:ident;
|
|
|
|
})*
|
|
|
|
) => {
|
2018-06-19 18:08:08 -05: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 17:05:07 -05:00
|
|
|
pub enum AstFragment {
|
|
|
|
OptExpr(Option<P<ast::Expr>>),
|
|
|
|
$($Kind($AstTy),)*
|
|
|
|
}
|
2018-06-19 18:08:08 -05:00
|
|
|
|
|
|
|
/// "Discriminant" of an AST fragment.
|
2016-09-23 04:32:58 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
2018-06-22 17:05:07 -05:00
|
|
|
pub enum AstFragmentKind {
|
|
|
|
OptExpr,
|
|
|
|
$($Kind,)*
|
|
|
|
}
|
2016-08-27 00:27:59 -05:00
|
|
|
|
2018-06-19 18:08:08 -05:00
|
|
|
impl AstFragmentKind {
|
2016-09-23 04:32:58 -05:00
|
|
|
pub fn name(self) -> &'static str {
|
2016-08-27 00:27:59 -05:00
|
|
|
match self {
|
2018-06-19 18:08:08 -05:00
|
|
|
AstFragmentKind::OptExpr => "expression",
|
2018-06-22 17:05:07 -05:00
|
|
|
$(AstFragmentKind::$Kind => $kind_name,)*
|
2016-08-27 00:27:59 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-24 01:12:54 -05:00
|
|
|
|
2018-07-10 14:06:26 -05:00
|
|
|
fn make_from<'a>(self, result: Box<dyn MacResult + 'a>) -> Option<AstFragment> {
|
2016-08-27 00:27:59 -05:00
|
|
|
match self {
|
2018-06-19 18:08:08 -05:00
|
|
|
AstFragmentKind::OptExpr =>
|
|
|
|
result.make_expr().map(Some).map(AstFragment::OptExpr),
|
2018-06-22 17:05:07 -05:00
|
|
|
$(AstFragmentKind::$Kind => result.$make_ast().map(AstFragment::$Kind),)*
|
2016-08-27 00:27:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-17 06:02:42 -05:00
|
|
|
|
2018-06-19 18:08:08 -05:00
|
|
|
impl AstFragment {
|
2019-10-09 17:41:47 -05:00
|
|
|
pub fn add_placeholders(&mut self, placeholders: &[NodeId]) {
|
|
|
|
if placeholders.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
match self {
|
|
|
|
$($(AstFragment::$Kind(ast) => ast.extend(placeholders.iter().flat_map(|id| {
|
|
|
|
// We are repeating through arguments with `many`, to do that we have to
|
|
|
|
// mention some macro variable from those arguments even if it's not used.
|
|
|
|
macro _repeating($flat_map_ast_elt) {}
|
2019-11-23 07:54:24 -06:00
|
|
|
placeholder(AstFragmentKind::$Kind, *id, None).$make_ast()
|
2019-10-09 17:41:47 -05:00
|
|
|
})),)?)*
|
|
|
|
_ => panic!("unexpected AST fragment kind")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-29 00:32:41 -05:00
|
|
|
pub fn make_opt_expr(self) -> Option<P<ast::Expr>> {
|
2016-08-27 00:27:59 -05:00
|
|
|
match self {
|
2018-06-19 18:08:08 -05:00
|
|
|
AstFragment::OptExpr(expr) => expr,
|
|
|
|
_ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
|
2016-08-27 00:27:59 -05:00
|
|
|
}
|
|
|
|
}
|
2018-06-22 17:05:07 -05:00
|
|
|
|
|
|
|
$(pub fn $make_ast(self) -> $AstTy {
|
2016-08-27 00:27:59 -05:00
|
|
|
match self {
|
2018-06-22 17:05:07 -05:00
|
|
|
AstFragment::$Kind(ast) => ast,
|
2018-06-19 18:08:08 -05:00
|
|
|
_ => panic!("AstFragment::make_* called on the wrong kind of fragment"),
|
2016-08-27 00:27:59 -05:00
|
|
|
}
|
2018-06-22 17:05:07 -05:00
|
|
|
})*
|
2016-05-19 04:45:37 -05: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-04 22:20:55 -06:00
|
|
|
pub fn mut_visit_with<F: MutVisitor>(&mut self, vis: &mut F) {
|
2016-08-27 00:27:59 -05: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-04 22:20:55 -06: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 13:05:43 -05:00
|
|
|
$($(AstFragment::$Kind(ast) => vis.$mut_visit_ast(ast),)?)*
|
2018-06-22 17:05:07 -05:00
|
|
|
$($(AstFragment::$Kind(ast) =>
|
2019-05-29 13:05:43 -05:00
|
|
|
ast.flat_map_in_place(|ast| vis.$flat_map_ast_elt(ast)),)?)*
|
2016-08-27 00:27:59 -05:00
|
|
|
}
|
2016-05-19 04:45:37 -05:00
|
|
|
}
|
2016-09-07 18:21:59 -05:00
|
|
|
|
2016-12-06 04:26:52 -06:00
|
|
|
pub fn visit_with<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) {
|
2016-09-07 18:21:59 -05:00
|
|
|
match *self {
|
2018-06-19 18:08:08 -05:00
|
|
|
AstFragment::OptExpr(Some(ref expr)) => visitor.visit_expr(expr),
|
|
|
|
AstFragment::OptExpr(None) => {}
|
2019-05-29 13:05:43 -05:00
|
|
|
$($(AstFragment::$Kind(ref ast) => visitor.$visit_ast(ast),)?)*
|
2018-06-22 17:05:07 -05:00
|
|
|
$($(AstFragment::$Kind(ref ast) => for ast_elt in &ast[..] {
|
2020-01-29 17:18:54 -06:00
|
|
|
visitor.$visit_ast_elt(ast_elt, $($args)*);
|
2019-05-29 13:05:43 -05:00
|
|
|
})?)*
|
2016-09-07 18:21:59 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-19 04:45:37 -05:00
|
|
|
}
|
2016-09-02 04:12:47 -05:00
|
|
|
|
2019-10-16 03:59:30 -05:00
|
|
|
impl<'a> MacResult for crate::mbe::macro_rules::ParserAnyMacro<'a> {
|
|
|
|
$(fn $make_ast(self: Box<crate::mbe::macro_rules::ParserAnyMacro<'a>>)
|
2018-06-22 17:05:07 -05:00
|
|
|
-> Option<$AstTy> {
|
|
|
|
Some(self.make(AstFragmentKind::$Kind).$make_ast())
|
2016-09-23 04:32:58 -05:00
|
|
|
})*
|
|
|
|
}
|
2016-08-27 00:27:59 -05:00
|
|
|
}
|
2016-05-19 04:45:37 -05:00
|
|
|
}
|
|
|
|
|
2018-06-19 18:08:08 -05: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-04 22:20:55 -06: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 04:42:16 -05:00
|
|
|
Stmts(SmallVec<[ast::Stmt; 1]>) {
|
2020-01-29 17:18:54 -06:00
|
|
|
"statement"; many fn flat_map_stmt; fn visit_stmt(); fn make_stmts;
|
2018-08-30 04:42:16 -05:00
|
|
|
}
|
|
|
|
Items(SmallVec<[P<ast::Item>; 1]>) {
|
2020-01-29 17:18:54 -06:00
|
|
|
"item"; many fn flat_map_item; fn visit_item(); fn make_items;
|
2018-08-30 04:42:16 -05:00
|
|
|
}
|
2019-12-11 23:41:18 -06:00
|
|
|
TraitItems(SmallVec<[P<ast::AssocItem>; 1]>) {
|
2020-01-29 17:18:54 -06:00
|
|
|
"trait item";
|
|
|
|
many fn flat_map_trait_item;
|
|
|
|
fn visit_assoc_item(AssocCtxt::Trait);
|
|
|
|
fn make_trait_items;
|
2018-06-22 17:05:07 -05:00
|
|
|
}
|
2019-12-11 23:41:18 -06:00
|
|
|
ImplItems(SmallVec<[P<ast::AssocItem>; 1]>) {
|
2020-01-29 17:18:54 -06:00
|
|
|
"impl item";
|
|
|
|
many fn flat_map_impl_item;
|
|
|
|
fn visit_assoc_item(AssocCtxt::Impl);
|
|
|
|
fn make_impl_items;
|
2018-06-22 17:05:07 -05:00
|
|
|
}
|
2019-12-11 23:41:18 -06:00
|
|
|
ForeignItems(SmallVec<[P<ast::ForeignItem>; 1]>) {
|
2019-09-09 07:26:25 -05:00
|
|
|
"foreign item";
|
|
|
|
many fn flat_map_foreign_item;
|
2020-01-29 17:18:54 -06:00
|
|
|
fn visit_foreign_item();
|
2019-09-09 07:26:25 -05:00
|
|
|
fn make_foreign_items;
|
|
|
|
}
|
|
|
|
Arms(SmallVec<[ast::Arm; 1]>) {
|
2020-01-29 17:18:54 -06:00
|
|
|
"match arm"; many fn flat_map_arm; fn visit_arm(); fn make_arms;
|
2019-09-09 07:26:25 -05:00
|
|
|
}
|
2021-03-15 16:36:07 -05:00
|
|
|
Fields(SmallVec<[ast::ExprField; 1]>) {
|
|
|
|
"field expression"; many fn flat_map_expr_field; fn visit_expr_field(); fn make_expr_fields;
|
2019-09-09 07:26:25 -05:00
|
|
|
}
|
2021-03-15 16:36:07 -05:00
|
|
|
FieldPats(SmallVec<[ast::PatField; 1]>) {
|
2019-09-09 07:26:25 -05:00
|
|
|
"field pattern";
|
2021-03-15 16:36:07 -05:00
|
|
|
many fn flat_map_pat_field;
|
|
|
|
fn visit_pat_field();
|
|
|
|
fn make_pat_fields;
|
2019-09-09 07:26:25 -05:00
|
|
|
}
|
|
|
|
GenericParams(SmallVec<[ast::GenericParam; 1]>) {
|
|
|
|
"generic parameter";
|
|
|
|
many fn flat_map_generic_param;
|
2020-01-29 17:18:54 -06:00
|
|
|
fn visit_generic_param();
|
2019-09-09 07:26:25 -05:00
|
|
|
fn make_generic_params;
|
|
|
|
}
|
|
|
|
Params(SmallVec<[ast::Param; 1]>) {
|
2020-01-29 17:18:54 -06:00
|
|
|
"function parameter"; many fn flat_map_param; fn visit_param(); fn make_params;
|
2019-09-09 07:26:25 -05:00
|
|
|
}
|
2021-03-15 16:36:07 -05:00
|
|
|
StructFields(SmallVec<[ast::FieldDef; 1]>) {
|
2019-09-09 07:26:25 -05:00
|
|
|
"field";
|
2021-03-15 16:36:07 -05:00
|
|
|
many fn flat_map_field_def;
|
|
|
|
fn visit_field_def();
|
|
|
|
fn make_field_defs;
|
2019-09-09 07:26:25 -05:00
|
|
|
}
|
|
|
|
Variants(SmallVec<[ast::Variant; 1]>) {
|
2020-01-29 17:18:54 -06:00
|
|
|
"variant"; many fn flat_map_variant; fn visit_variant(); fn make_variants;
|
2018-06-22 17:05:07 -05:00
|
|
|
}
|
2016-05-19 04:45:37 -05:00
|
|
|
}
|
|
|
|
|
2021-03-01 15:02:09 -06:00
|
|
|
pub enum SupportsMacroExpansion {
|
|
|
|
No,
|
|
|
|
Yes { supports_inner_attrs: bool },
|
|
|
|
}
|
|
|
|
|
2018-06-19 18:08:08 -05:00
|
|
|
impl AstFragmentKind {
|
2020-03-17 02:55:28 -05:00
|
|
|
crate fn dummy(self, span: Span) -> AstFragment {
|
2019-07-07 10:29:22 -05:00
|
|
|
self.make_from(DummyResult::any(span)).expect("couldn't create a dummy AST fragment")
|
2016-06-11 17:59:33 -05:00
|
|
|
}
|
2016-09-02 01:14:38 -05:00
|
|
|
|
2021-03-01 15:02:09 -06:00
|
|
|
pub fn supports_macro_expansion(self) -> SupportsMacroExpansion {
|
2020-11-18 16:49:20 -06:00
|
|
|
match self {
|
|
|
|
AstFragmentKind::OptExpr
|
|
|
|
| AstFragmentKind::Expr
|
|
|
|
| AstFragmentKind::Stmts
|
2021-03-01 15:02:09 -06:00
|
|
|
| AstFragmentKind::Ty
|
|
|
|
| AstFragmentKind::Pat => SupportsMacroExpansion::Yes { supports_inner_attrs: false },
|
|
|
|
AstFragmentKind::Items
|
2020-11-18 16:49:20 -06:00
|
|
|
| AstFragmentKind::TraitItems
|
|
|
|
| AstFragmentKind::ImplItems
|
2021-03-01 15:02:09 -06:00
|
|
|
| AstFragmentKind::ForeignItems => {
|
|
|
|
SupportsMacroExpansion::Yes { supports_inner_attrs: true }
|
|
|
|
}
|
2020-11-18 16:49:20 -06:00
|
|
|
AstFragmentKind::Arms
|
|
|
|
| AstFragmentKind::Fields
|
|
|
|
| AstFragmentKind::FieldPats
|
|
|
|
| AstFragmentKind::GenericParams
|
|
|
|
| AstFragmentKind::Params
|
|
|
|
| AstFragmentKind::StructFields
|
2021-03-01 15:02:09 -06:00
|
|
|
| AstFragmentKind::Variants => SupportsMacroExpansion::No,
|
2020-11-18 16:49:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
fn expect_from_annotatables<I: IntoIterator<Item = Annotatable>>(
|
|
|
|
self,
|
|
|
|
items: I,
|
|
|
|
) -> AstFragment {
|
2018-04-18 01:19:21 -05:00
|
|
|
let mut items = items.into_iter();
|
2016-09-02 01:14:38 -05:00
|
|
|
match self {
|
2019-12-22 16:42:04 -06:00
|
|
|
AstFragmentKind::Arms => {
|
|
|
|
AstFragment::Arms(items.map(Annotatable::expect_arm).collect())
|
|
|
|
}
|
|
|
|
AstFragmentKind::Fields => {
|
2021-03-15 16:36:07 -05:00
|
|
|
AstFragment::Fields(items.map(Annotatable::expect_expr_field).collect())
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
|
|
|
AstFragmentKind::FieldPats => {
|
2021-03-15 16:36:07 -05:00
|
|
|
AstFragment::FieldPats(items.map(Annotatable::expect_pat_field).collect())
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
|
|
|
AstFragmentKind::GenericParams => {
|
|
|
|
AstFragment::GenericParams(items.map(Annotatable::expect_generic_param).collect())
|
|
|
|
}
|
|
|
|
AstFragmentKind::Params => {
|
|
|
|
AstFragment::Params(items.map(Annotatable::expect_param).collect())
|
|
|
|
}
|
|
|
|
AstFragmentKind::StructFields => {
|
2021-03-15 16:36:07 -05:00
|
|
|
AstFragment::StructFields(items.map(Annotatable::expect_field_def).collect())
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
|
|
|
AstFragmentKind::Variants => {
|
|
|
|
AstFragment::Variants(items.map(Annotatable::expect_variant).collect())
|
|
|
|
}
|
|
|
|
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())
|
|
|
|
}
|
2018-06-19 18:08:08 -05:00
|
|
|
AstFragmentKind::Expr => AstFragment::Expr(
|
2019-12-22 16:42:04 -06:00
|
|
|
items.next().expect("expected exactly one expression").expect_expr(),
|
2018-04-18 01:19:21 -05:00
|
|
|
),
|
2019-12-22 16:42:04 -06:00
|
|
|
AstFragmentKind::OptExpr => {
|
|
|
|
AstFragment::OptExpr(items.next().map(Annotatable::expect_expr))
|
|
|
|
}
|
|
|
|
AstFragmentKind::Pat | AstFragmentKind::Ty => {
|
|
|
|
panic!("patterns and types aren't annotatable")
|
|
|
|
}
|
2016-09-02 01:14:38 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 05:09:23 -05:00
|
|
|
}
|
2015-01-26 18:22:12 -06:00
|
|
|
|
2016-08-27 00:27:59 -05:00
|
|
|
pub struct Invocation {
|
2016-09-07 18:21:59 -05:00
|
|
|
pub kind: InvocationKind,
|
2019-09-09 07:26:25 -05:00
|
|
|
pub fragment_kind: AstFragmentKind,
|
2017-03-01 17:48:16 -06:00
|
|
|
pub expansion_data: ExpansionData,
|
2016-09-02 01:14:38 -05:00
|
|
|
}
|
|
|
|
|
2016-09-07 18:21:59 -05:00
|
|
|
pub enum InvocationKind {
|
2016-09-02 01:14:38 -05:00
|
|
|
Bang {
|
2020-02-29 10:32:20 -06:00
|
|
|
mac: ast::MacCall,
|
2016-09-02 01:14:38 -05:00
|
|
|
span: Span,
|
|
|
|
},
|
|
|
|
Attr {
|
2019-07-07 17:00:43 -05:00
|
|
|
attr: ast::Attribute,
|
2021-02-22 15:54:09 -06:00
|
|
|
// Re-insertion position for inert attributes.
|
|
|
|
pos: usize,
|
2016-09-02 01:14:38 -05:00
|
|
|
item: Annotatable,
|
2019-07-07 17:00:43 -05:00
|
|
|
// Required for resolving derive helper attributes.
|
|
|
|
derives: Vec<Path>,
|
2016-09-02 01:14:38 -05:00
|
|
|
},
|
2017-02-01 04:33:09 -06:00
|
|
|
Derive {
|
2017-03-08 17:13:35 -06:00
|
|
|
path: Path,
|
2017-02-01 04:33:09 -06:00
|
|
|
item: Annotatable,
|
|
|
|
},
|
2011-07-06 17:22:23 -05:00
|
|
|
}
|
2013-07-29 19:25:00 -05:00
|
|
|
|
2019-11-23 07:54:24 -06:00
|
|
|
impl InvocationKind {
|
|
|
|
fn placeholder_visibility(&self) -> Option<ast::Visibility> {
|
|
|
|
// HACK: For unnamed fields placeholders should have the same visibility as the actual
|
|
|
|
// fields because for tuple structs/variants resolve determines visibilities of their
|
|
|
|
// constructor using these field visibilities before attributes on them are are expanded.
|
|
|
|
// The assumption is that the attribute expansion cannot change field visibilities,
|
|
|
|
// and it holds because only inert attributes are supported in this position.
|
|
|
|
match self {
|
2021-03-15 16:36:07 -05:00
|
|
|
InvocationKind::Attr { item: Annotatable::FieldDef(field), .. }
|
|
|
|
| InvocationKind::Derive { item: Annotatable::FieldDef(field), .. }
|
2019-12-22 16:42:04 -06:00
|
|
|
if field.ident.is_none() =>
|
|
|
|
{
|
|
|
|
Some(field.vis.clone())
|
|
|
|
}
|
2019-11-23 07:54:24 -06:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-07 18:21:59 -05:00
|
|
|
impl Invocation {
|
2018-07-12 05:24:59 -05:00
|
|
|
pub fn span(&self) -> Span {
|
2019-07-07 17:00:43 -05:00
|
|
|
match &self.kind {
|
|
|
|
InvocationKind::Bang { span, .. } => *span,
|
|
|
|
InvocationKind::Attr { attr, .. } => attr.span,
|
|
|
|
InvocationKind::Derive { path, .. } => path.span,
|
2016-09-07 18:21:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 11:39:39 -05:00
|
|
|
pub struct MacroExpander<'a, 'b> {
|
2014-03-27 17:39:48 -05:00
|
|
|
pub cx: &'a mut ExtCtxt<'b>,
|
2018-10-21 18:45:24 -05:00
|
|
|
monotonic: bool, // cf. `cx.monotonic_expander()`
|
2014-12-13 20:42:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> MacroExpander<'a, 'b> {
|
2016-09-06 00:42:45 -05:00
|
|
|
pub fn new(cx: &'a mut ExtCtxt<'b>, monotonic: bool) -> Self {
|
2019-06-25 16:22:45 -05:00
|
|
|
MacroExpander { cx, monotonic }
|
2014-12-13 20:42:41 -06:00
|
|
|
}
|
2016-05-16 05:09:23 -05:00
|
|
|
|
2021-02-14 12:14:12 -06:00
|
|
|
// FIXME: Avoid visiting the crate as a `Mod` item,
|
|
|
|
// make crate a first class expansion target instead.
|
2016-09-28 19:22:46 -05:00
|
|
|
pub fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
|
2021-02-21 10:15:43 -06:00
|
|
|
let file_path = match self.cx.source_map().span_to_unmapped_path(krate.span) {
|
|
|
|
FileName::Real(name) => name.into_local_path(),
|
|
|
|
other => PathBuf::from(other.to_string()),
|
2016-09-28 19:22:46 -05:00
|
|
|
};
|
2021-02-21 10:15:43 -06:00
|
|
|
let dir_path = file_path.parent().unwrap_or(&file_path).to_owned();
|
|
|
|
self.cx.root_path = dir_path.clone();
|
|
|
|
self.cx.current_expansion.module = Rc::new(ModuleData {
|
|
|
|
mod_path: vec![Ident::from_str(&self.cx.ecfg.crate_name)],
|
|
|
|
file_path_stack: vec![file_path],
|
|
|
|
dir_path,
|
|
|
|
});
|
2016-09-02 04:12:47 -05:00
|
|
|
|
2018-08-13 14:15:16 -05:00
|
|
|
let krate_item = AstFragment::Items(smallvec![P(ast::Item {
|
2016-09-21 04:20:42 -05:00
|
|
|
attrs: krate.attrs,
|
|
|
|
span: krate.span,
|
2021-02-16 15:56:07 -06:00
|
|
|
kind: ast::ItemKind::Mod(
|
|
|
|
Unsafe::No,
|
|
|
|
ModKind::Loaded(krate.items, Inline::Yes, krate.span)
|
|
|
|
),
|
2019-05-11 11:08:09 -05:00
|
|
|
ident: Ident::invalid(),
|
2016-09-21 04:20:42 -05:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2020-08-21 18:11:00 -05:00
|
|
|
vis: ast::Visibility {
|
|
|
|
span: krate.span.shrink_to_lo(),
|
|
|
|
kind: ast::VisibilityKind::Public,
|
|
|
|
tokens: None,
|
|
|
|
},
|
2017-07-10 19:44:46 -05:00
|
|
|
tokens: None,
|
2018-08-13 14:15:16 -05:00
|
|
|
})]);
|
2016-09-21 04:20:42 -05:00
|
|
|
|
2019-08-13 17:59:14 -05:00
|
|
|
match self.fully_expand_fragment(krate_item).make_items().pop().map(P::into_inner) {
|
2021-02-16 15:56:07 -06:00
|
|
|
Some(ast::Item {
|
|
|
|
attrs,
|
|
|
|
kind: ast::ItemKind::Mod(_, ModKind::Loaded(items, ..)),
|
|
|
|
..
|
|
|
|
}) => {
|
2016-09-21 04:20:42 -05:00
|
|
|
krate.attrs = attrs;
|
2021-02-16 15:56:07 -06:00
|
|
|
krate.items = items;
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
2017-04-20 18:13:13 -05:00
|
|
|
None => {
|
|
|
|
// Resolution failed so we return an empty expansion
|
|
|
|
krate.attrs = vec![];
|
2021-02-14 12:14:12 -06:00
|
|
|
krate.items = vec![];
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
2020-02-01 20:04:07 -06:00
|
|
|
Some(ast::Item { span, kind, .. }) => {
|
2020-02-02 13:28:32 -06:00
|
|
|
krate.attrs = vec![];
|
2021-02-14 12:14:12 -06:00
|
|
|
krate.items = vec![];
|
2020-02-02 13:28:32 -06:00
|
|
|
self.cx.span_err(
|
2020-02-01 20:04:07 -06:00
|
|
|
span,
|
|
|
|
&format!(
|
2020-02-22 23:04:37 -06:00
|
|
|
"expected crate top-level item to be a module after macro expansion, found {} {}",
|
|
|
|
kind.article(), kind.descr()
|
2020-02-01 20:04:07 -06:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2016-09-05 20:45:23 -05:00
|
|
|
};
|
2017-05-05 23:49:59 -05:00
|
|
|
self.cx.trace_macros_diag();
|
2016-09-02 04:12:47 -05:00
|
|
|
krate
|
|
|
|
}
|
|
|
|
|
2019-08-13 17:59:14 -05:00
|
|
|
// Recursively expand all macro invocations in this AST fragment.
|
2019-08-13 18:30:09 -05:00
|
|
|
pub fn fully_expand_fragment(&mut self, input_fragment: AstFragment) -> AstFragment {
|
2016-09-07 18:21:59 -05:00
|
|
|
let orig_expansion_data = self.cx.current_expansion.clone();
|
2020-11-14 05:47:14 -06:00
|
|
|
let orig_force_mode = self.cx.force_mode;
|
2016-09-07 18:21:59 -05:00
|
|
|
self.cx.current_expansion.depth = 0;
|
|
|
|
|
2018-06-23 11:27:01 -05:00
|
|
|
// Collect all macro invocations and replace them with placeholders.
|
2019-12-22 16:42:04 -06:00
|
|
|
let (mut fragment_with_placeholders, mut invocations) =
|
|
|
|
self.collect_invocations(input_fragment, &[]);
|
2018-06-23 11:27:01 -05:00
|
|
|
|
|
|
|
// Optimization: if we resolve all imports now,
|
|
|
|
// we'll be able to immediately resolve most of imported macros.
|
2016-11-10 04:11:25 -06:00
|
|
|
self.resolve_imports();
|
2016-09-02 04:12:47 -05:00
|
|
|
|
2018-08-19 08:30:23 -05:00
|
|
|
// Resolve paths in all invocations and produce output expanded fragments for them, but
|
2018-06-23 11:27:01 -05: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 18:08:08 -05:00
|
|
|
let mut expanded_fragments = Vec::new();
|
2016-10-10 22:41:48 -05:00
|
|
|
let mut undetermined_invocations = Vec::new();
|
|
|
|
let (mut progress, mut force) = (false, !self.monotonic);
|
|
|
|
loop {
|
2020-11-14 05:47:14 -06:00
|
|
|
let (invoc, ext) = if let Some(invoc) = invocations.pop() {
|
2016-10-10 22:41:48 -05:00
|
|
|
invoc
|
|
|
|
} else {
|
2016-11-10 04:11:25 -06:00
|
|
|
self.resolve_imports();
|
2019-12-22 16:42:04 -06:00
|
|
|
if undetermined_invocations.is_empty() {
|
|
|
|
break;
|
|
|
|
}
|
2019-06-30 13:30:01 -05:00
|
|
|
invocations = mem::take(&mut undetermined_invocations);
|
2016-10-10 22:41:48 -05:00
|
|
|
force = !mem::replace(&mut progress, false);
|
2020-11-14 05:47:14 -06:00
|
|
|
if force && self.monotonic {
|
|
|
|
self.cx.sess.delay_span_bug(
|
|
|
|
invocations.last().unwrap().0.span(),
|
|
|
|
"expansion entered force mode without producing any errors",
|
|
|
|
);
|
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
continue;
|
2016-10-10 22:41:48 -05:00
|
|
|
};
|
|
|
|
|
2020-11-14 05:47:14 -06:00
|
|
|
let ext = match ext {
|
|
|
|
Some(ext) => ext,
|
2020-03-09 12:50:12 -05:00
|
|
|
None => {
|
|
|
|
let eager_expansion_root = if self.monotonic {
|
|
|
|
invoc.expansion_data.id
|
|
|
|
} else {
|
|
|
|
orig_expansion_data.id
|
|
|
|
};
|
|
|
|
match self.cx.resolver.resolve_macro_invocation(
|
|
|
|
&invoc,
|
|
|
|
eager_expansion_root,
|
|
|
|
force,
|
|
|
|
) {
|
2020-11-14 05:47:14 -06:00
|
|
|
Ok(ext) => ext,
|
2020-03-09 12:50:12 -05:00
|
|
|
Err(Indeterminate) => {
|
|
|
|
// Cannot resolve, will retry this invocation later.
|
|
|
|
undetermined_invocations.push((invoc, None));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2016-10-10 22:41:48 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-15 17:42:58 -05:00
|
|
|
let ExpansionData { depth, id: expn_id, .. } = invoc.expansion_data;
|
2016-09-07 18:21:59 -05:00
|
|
|
self.cx.current_expansion = invoc.expansion_data.clone();
|
2020-11-14 05:47:14 -06:00
|
|
|
self.cx.force_mode = force;
|
2019-07-03 04:47:24 -05:00
|
|
|
|
2020-11-18 16:54:19 -06:00
|
|
|
let fragment_kind = invoc.fragment_kind;
|
2020-11-14 05:47:14 -06:00
|
|
|
let (expanded_fragment, new_invocations) = match self.expand_invoc(invoc, &ext.kind) {
|
|
|
|
ExpandResult::Ready(fragment) => {
|
2021-04-08 20:35:24 -05:00
|
|
|
let mut derive_invocations = Vec::new();
|
2020-11-14 05:47:14 -06:00
|
|
|
let derive_placeholders = self
|
|
|
|
.cx
|
|
|
|
.resolver
|
|
|
|
.take_derive_resolutions(expn_id)
|
|
|
|
.map(|derives| {
|
|
|
|
enum AnnotatableRef<'a> {
|
|
|
|
Item(&'a P<ast::Item>),
|
|
|
|
Stmt(&'a ast::Stmt),
|
2020-11-23 00:43:55 -06:00
|
|
|
}
|
2020-11-14 05:47:14 -06:00
|
|
|
let item = match &fragment {
|
|
|
|
AstFragment::Items(items) => match &items[..] {
|
|
|
|
[item] => AnnotatableRef::Item(item),
|
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
|
|
|
AstFragment::Stmts(stmts) => match &stmts[..] {
|
|
|
|
[stmt] => AnnotatableRef::Stmt(stmt),
|
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2020-11-18 16:54:19 -06:00
|
|
|
|
2021-04-08 20:35:24 -05:00
|
|
|
derive_invocations.reserve(derives.len());
|
2020-11-14 05:47:14 -06:00
|
|
|
derives
|
|
|
|
.into_iter()
|
2021-03-08 06:05:03 -06:00
|
|
|
.map(|(path, _exts)| {
|
2020-11-14 05:47:14 -06:00
|
|
|
// FIXME: Consider using the derive resolutions (`_exts`)
|
|
|
|
// instead of enqueuing the derives to be resolved again later.
|
|
|
|
let expn_id = ExpnId::fresh(None);
|
2021-04-08 20:35:24 -05:00
|
|
|
derive_invocations.push((
|
2020-11-14 05:47:14 -06:00
|
|
|
Invocation {
|
|
|
|
kind: InvocationKind::Derive {
|
|
|
|
path,
|
|
|
|
item: match item {
|
|
|
|
AnnotatableRef::Item(item) => {
|
|
|
|
Annotatable::Item(item.clone())
|
|
|
|
}
|
|
|
|
AnnotatableRef::Stmt(stmt) => {
|
|
|
|
Annotatable::Stmt(P(stmt.clone()))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fragment_kind,
|
|
|
|
expansion_data: ExpansionData {
|
|
|
|
id: expn_id,
|
|
|
|
..self.cx.current_expansion.clone()
|
|
|
|
},
|
2020-11-18 16:54:19 -06:00
|
|
|
},
|
2020-11-14 05:47:14 -06:00
|
|
|
None,
|
|
|
|
));
|
|
|
|
NodeId::placeholder_from_expn_id(expn_id)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
})
|
|
|
|
.unwrap_or_default();
|
2019-08-24 13:12:13 -05:00
|
|
|
|
2021-04-08 20:35:24 -05:00
|
|
|
let (fragment, collected_invocations) =
|
|
|
|
self.collect_invocations(fragment, &derive_placeholders);
|
|
|
|
// We choose to expand any derive invocations associated with this macro invocation
|
|
|
|
// *before* any macro invocations collected from the output fragment
|
|
|
|
derive_invocations.extend(collected_invocations);
|
|
|
|
(fragment, derive_invocations)
|
2017-02-02 01:01:15 -06:00
|
|
|
}
|
2020-11-14 05:47:14 -06:00
|
|
|
ExpandResult::Retry(invoc) => {
|
|
|
|
if force {
|
|
|
|
self.cx.span_bug(
|
|
|
|
invoc.span(),
|
|
|
|
"expansion entered force mode but is still stuck",
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Cannot expand, will retry this invocation later.
|
|
|
|
undetermined_invocations.push((invoc, Some(ext)));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2016-09-07 18:21:59 -05:00
|
|
|
};
|
2016-09-02 04:12:47 -05:00
|
|
|
|
2020-03-09 12:50:12 -05:00
|
|
|
progress = true;
|
2018-06-19 18:08:08 -05:00
|
|
|
if expanded_fragments.len() < depth {
|
|
|
|
expanded_fragments.push(Vec::new());
|
2016-09-01 22:35:59 -05:00
|
|
|
}
|
2019-07-15 17:42:58 -05:00
|
|
|
expanded_fragments[depth - 1].push((expn_id, expanded_fragment));
|
2020-04-12 21:01:15 -05:00
|
|
|
invocations.extend(new_invocations.into_iter().rev());
|
2016-09-02 04:12:47 -05:00
|
|
|
}
|
|
|
|
|
2016-09-07 18:21:59 -05:00
|
|
|
self.cx.current_expansion = orig_expansion_data;
|
2020-11-14 05:47:14 -06:00
|
|
|
self.cx.force_mode = orig_force_mode;
|
2016-09-07 18:21:59 -05:00
|
|
|
|
2018-06-23 11:27:01 -05:00
|
|
|
// Finally incorporate all the expanded macros into the input AST fragment.
|
2016-09-06 00:42:45 -05:00
|
|
|
let mut placeholder_expander = PlaceholderExpander::new(self.cx, self.monotonic);
|
2018-06-19 18:08:08 -05:00
|
|
|
while let Some(expanded_fragments) = expanded_fragments.pop() {
|
2019-08-17 12:49:00 -05:00
|
|
|
for (expn_id, expanded_fragment) in expanded_fragments.into_iter().rev() {
|
2019-12-22 16:42:04 -06:00
|
|
|
placeholder_expander
|
|
|
|
.add(NodeId::placeholder_from_expn_id(expn_id), expanded_fragment);
|
2016-09-01 22:35:59 -05: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-04 22:20:55 -06:00
|
|
|
fragment_with_placeholders.mut_visit_with(&mut placeholder_expander);
|
|
|
|
fragment_with_placeholders
|
2016-09-02 04:12:47 -05:00
|
|
|
}
|
|
|
|
|
2016-11-10 04:11:25 -06:00
|
|
|
fn resolve_imports(&mut self) {
|
|
|
|
if self.monotonic {
|
|
|
|
self.cx.resolver.resolve_imports();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 07:53:55 -06:00
|
|
|
/// Collects all macro invocations reachable at this time in this AST fragment, and replace
|
2018-06-23 11:27:01 -05:00
|
|
|
/// them with "placeholders" - dummy macro invocations with specially crafted `NodeId`s.
|
|
|
|
/// Then call into resolver that builds a skeleton ("reduced graph") of the fragment and
|
|
|
|
/// prepares data for resolving paths of macro invocations.
|
2019-12-22 16:42:04 -06:00
|
|
|
fn collect_invocations(
|
|
|
|
&mut self,
|
|
|
|
mut fragment: AstFragment,
|
|
|
|
extra_placeholders: &[NodeId],
|
2020-11-14 05:47:14 -06:00
|
|
|
) -> (AstFragment, Vec<(Invocation, Option<Lrc<SyntaxExtension>>)>) {
|
2019-01-26 07:29:34 -06:00
|
|
|
// Resolve `$crate`s in the fragment for pretty-printing.
|
2019-07-04 19:09:24 -05:00
|
|
|
self.cx.resolver.resolve_dollar_crates();
|
2019-01-26 07:29:34 -06: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-04 22:20:55 -06:00
|
|
|
let invocations = {
|
2016-09-07 17:24:01 -05:00
|
|
|
let mut collector = InvocationCollector {
|
2020-11-28 17:33:17 -06:00
|
|
|
// Non-derive macro invocations cannot see the results of cfg expansion - they
|
|
|
|
// will either be removed along with the item, or invoked before the cfg/cfg_attr
|
|
|
|
// attribute is expanded. Therefore, we don't need to configure the tokens
|
|
|
|
// Derive macros *can* see the results of cfg-expansion - they are handled
|
|
|
|
// specially in `fully_expand_fragment`
|
2020-11-23 00:43:55 -06:00
|
|
|
cfg: StripUnconfigured {
|
|
|
|
sess: &self.cx.sess,
|
|
|
|
features: self.cx.ecfg.features,
|
2020-11-28 17:33:17 -06:00
|
|
|
config_tokens: false,
|
2020-11-23 00:43:55 -06:00
|
|
|
},
|
2016-09-07 17:24:01 -05:00
|
|
|
cx: self.cx,
|
|
|
|
invocations: Vec::new(),
|
2016-09-06 00:42:45 -05:00
|
|
|
monotonic: self.monotonic,
|
2016-09-07 17:24:01 -05: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-04 22:20:55 -06:00
|
|
|
fragment.mut_visit_with(&mut collector);
|
2019-10-09 17:41:47 -05:00
|
|
|
fragment.add_placeholders(extra_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-04 22:20:55 -06:00
|
|
|
collector.invocations
|
2016-09-07 17:24:01 -05:00
|
|
|
};
|
2016-09-07 18:21:59 -05:00
|
|
|
|
2016-09-19 02:27:20 -05:00
|
|
|
if self.monotonic {
|
2019-12-22 16:42:04 -06:00
|
|
|
self.cx
|
|
|
|
.resolver
|
|
|
|
.visit_ast_fragment_with_placeholders(self.cx.current_expansion.id, &fragment);
|
2016-09-19 02:27:20 -05: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-04 22:20:55 -06:00
|
|
|
(fragment, invocations)
|
2016-05-16 05:09:23 -05:00
|
|
|
}
|
2016-06-11 20:50:52 -05:00
|
|
|
|
2019-12-31 01:25:49 -06:00
|
|
|
fn error_recursion_limit_reached(&mut self) {
|
|
|
|
let expn_data = self.cx.current_expansion.id.expn_data();
|
|
|
|
let suggested_limit = self.cx.ecfg.recursion_limit * 2;
|
|
|
|
self.cx
|
|
|
|
.struct_span_err(
|
2019-12-22 16:42:04 -06:00
|
|
|
expn_data.call_site,
|
2020-01-20 17:02:01 -06:00
|
|
|
&format!("recursion limit reached while expanding `{}`", expn_data.kind.descr()),
|
2019-12-31 01:25:49 -06:00
|
|
|
)
|
|
|
|
.help(&format!(
|
2020-02-04 19:57:30 -06:00
|
|
|
"consider adding a `#![recursion_limit=\"{}\"]` attribute to your crate (`{}`)",
|
|
|
|
suggested_limit, self.cx.ecfg.crate_name,
|
2019-12-31 01:25:49 -06:00
|
|
|
))
|
|
|
|
.emit();
|
|
|
|
self.cx.trace_macros_diag();
|
|
|
|
}
|
|
|
|
|
2019-12-31 01:43:33 -06:00
|
|
|
/// A macro's expansion does not fit in this fragment kind.
|
|
|
|
/// For example, a non-type macro in a type position.
|
2020-02-29 10:32:20 -06:00
|
|
|
fn error_wrong_fragment_kind(&mut self, kind: AstFragmentKind, mac: &ast::MacCall, span: Span) {
|
2019-12-31 01:43:33 -06:00
|
|
|
let msg = format!(
|
|
|
|
"non-{kind} macro in {kind} position: {path}",
|
|
|
|
kind = kind.name(),
|
|
|
|
path = pprust::path_to_string(&mac.path),
|
|
|
|
);
|
|
|
|
self.cx.span_err(span, &msg);
|
|
|
|
self.cx.trace_macros_diag();
|
|
|
|
}
|
|
|
|
|
2020-03-09 12:50:12 -05:00
|
|
|
fn expand_invoc(
|
|
|
|
&mut self,
|
|
|
|
invoc: Invocation,
|
|
|
|
ext: &SyntaxExtensionKind,
|
|
|
|
) -> ExpandResult<AstFragment, Invocation> {
|
2020-02-26 16:43:49 -06:00
|
|
|
let recursion_limit =
|
|
|
|
self.cx.reduced_recursion_limit.unwrap_or(self.cx.ecfg.recursion_limit);
|
2020-05-26 13:48:08 -05:00
|
|
|
if !recursion_limit.value_within_limit(self.cx.current_expansion.depth) {
|
2020-02-26 16:43:49 -06:00
|
|
|
if self.cx.reduced_recursion_limit.is_none() {
|
|
|
|
self.error_recursion_limit_reached();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reduce the recursion limit by half each time it triggers.
|
|
|
|
self.cx.reduced_recursion_limit = Some(recursion_limit / 2);
|
|
|
|
|
|
|
|
return ExpandResult::Ready(invoc.fragment_kind.dummy(invoc.span()));
|
2016-09-01 02:01:45 -05:00
|
|
|
}
|
2017-03-16 23:04:41 -05:00
|
|
|
|
2019-08-26 18:07:26 -05:00
|
|
|
let (fragment_kind, span) = (invoc.fragment_kind, invoc.span());
|
2020-03-09 12:50:12 -05:00
|
|
|
ExpandResult::Ready(match invoc.kind {
|
2019-07-07 10:55:29 -05:00
|
|
|
InvocationKind::Bang { mac, .. } => match ext {
|
|
|
|
SyntaxExtensionKind::Bang(expander) => {
|
2020-03-17 04:09:18 -05:00
|
|
|
let tok_result = match expander.expand(self.cx, span, mac.args.inner_tokens()) {
|
|
|
|
Err(_) => return ExpandResult::Ready(fragment_kind.dummy(span)),
|
|
|
|
Ok(ts) => ts,
|
|
|
|
};
|
2019-08-31 07:22:53 -05:00
|
|
|
self.parse_ast_fragment(tok_result, fragment_kind, &mac.path, span)
|
2019-07-07 10:55:29 -05:00
|
|
|
}
|
|
|
|
SyntaxExtensionKind::LegacyBang(expander) => {
|
2019-07-18 20:36:19 -05:00
|
|
|
let prev = self.cx.current_expansion.prior_type_ascription;
|
2019-08-14 18:13:53 -05:00
|
|
|
self.cx.current_expansion.prior_type_ascription = mac.prior_type_ascription;
|
2019-12-01 06:55:32 -06:00
|
|
|
let tok_result = expander.expand(self.cx, span, mac.args.inner_tokens());
|
2019-07-18 20:36:19 -05:00
|
|
|
let result = if let Some(result) = fragment_kind.make_from(tok_result) {
|
2019-07-07 10:55:29 -05:00
|
|
|
result
|
|
|
|
} else {
|
2019-12-31 01:43:33 -06:00
|
|
|
self.error_wrong_fragment_kind(fragment_kind, &mac, span);
|
2019-07-07 10:55:29 -05:00
|
|
|
fragment_kind.dummy(span)
|
2019-07-18 20:36:19 -05:00
|
|
|
};
|
|
|
|
self.cx.current_expansion.prior_type_ascription = prev;
|
|
|
|
result
|
2019-06-16 10:58:39 -05:00
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
2021-02-22 15:54:09 -06:00
|
|
|
InvocationKind::Attr { attr, pos, mut item, derives } => match ext {
|
2019-07-07 10:55:29 -05:00
|
|
|
SyntaxExtensionKind::Attr(expander) => {
|
2019-09-07 17:42:12 -05:00
|
|
|
self.gate_proc_macro_input(&item);
|
2019-07-07 10:55:29 -05:00
|
|
|
self.gate_proc_macro_attr_item(span, &item);
|
2020-11-28 17:33:17 -06:00
|
|
|
let mut fake_tokens = false;
|
|
|
|
if let Annotatable::Item(item_inner) = &item {
|
|
|
|
if let ItemKind::Mod(_, mod_kind) = &item_inner.kind {
|
|
|
|
// FIXME: Collect tokens and use them instead of generating
|
|
|
|
// fake ones. These are unstable, so it needs to be
|
|
|
|
// fixed prior to stabilization
|
|
|
|
// Fake tokens when we are invoking an inner attribute, and:
|
|
|
|
fake_tokens = matches!(attr.style, ast::AttrStyle::Inner) &&
|
|
|
|
// We are invoking an attribute on the crate root, or an outline
|
|
|
|
// module
|
|
|
|
(item_inner.ident.name.is_empty() || !matches!(mod_kind, ast::ModKind::Loaded(_, Inline::Yes, _)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let tokens = if fake_tokens {
|
|
|
|
rustc_parse::fake_token_stream(
|
2020-11-23 00:43:55 -06:00
|
|
|
&self.cx.sess.parse_sess,
|
|
|
|
&item.into_nonterminal(),
|
2020-11-28 17:33:17 -06:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
item.into_tokens(&self.cx.sess.parse_sess)
|
2020-11-23 00:43:55 -06:00
|
|
|
};
|
2020-03-03 14:22:32 -06:00
|
|
|
let attr_item = attr.unwrap_normal_item();
|
|
|
|
if let MacArgs::Eq(..) = attr_item.args {
|
2019-12-01 08:07:38 -06:00
|
|
|
self.cx.span_err(span, "key-value macro attributes are not supported");
|
|
|
|
}
|
2020-03-17 04:56:00 -05:00
|
|
|
let inner_tokens = attr_item.args.inner_tokens();
|
|
|
|
let tok_result = match expander.expand(self.cx, span, inner_tokens, tokens) {
|
|
|
|
Err(_) => return ExpandResult::Ready(fragment_kind.dummy(span)),
|
|
|
|
Ok(ts) => ts,
|
|
|
|
};
|
2020-03-03 14:22:32 -06:00
|
|
|
self.parse_ast_fragment(tok_result, fragment_kind, &attr_item.path, span)
|
2019-07-07 10:55:29 -05:00
|
|
|
}
|
|
|
|
SyntaxExtensionKind::LegacyAttr(expander) => {
|
2020-07-29 20:27:50 -05:00
|
|
|
match validate_attr::parse_meta(&self.cx.sess.parse_sess, &attr) {
|
2019-07-07 10:55:29 -05:00
|
|
|
Ok(meta) => {
|
2020-03-09 12:50:12 -05:00
|
|
|
let items = match expander.expand(self.cx, span, &meta, item) {
|
|
|
|
ExpandResult::Ready(items) => items,
|
2020-11-14 05:47:14 -06:00
|
|
|
ExpandResult::Retry(item) => {
|
2020-03-09 12:50:12 -05:00
|
|
|
// Reassemble the original invocation for retrying.
|
2020-11-14 05:47:14 -06:00
|
|
|
return ExpandResult::Retry(Invocation {
|
2021-02-22 15:54:09 -06:00
|
|
|
kind: InvocationKind::Attr { attr, pos, item, derives },
|
2020-11-14 05:47:14 -06:00
|
|
|
..invoc
|
|
|
|
});
|
2020-03-09 12:50:12 -05:00
|
|
|
}
|
|
|
|
};
|
2021-04-03 10:46:25 -05:00
|
|
|
if fragment_kind == AstFragmentKind::Expr && items.is_empty() {
|
|
|
|
let msg =
|
|
|
|
"removing an expression is not supported in this position";
|
|
|
|
self.cx.span_err(span, msg);
|
|
|
|
fragment_kind.dummy(span)
|
|
|
|
} else {
|
|
|
|
fragment_kind.expect_from_annotatables(items)
|
|
|
|
}
|
2019-07-07 10:55:29 -05:00
|
|
|
}
|
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
|
|
|
fragment_kind.dummy(span)
|
|
|
|
}
|
2019-07-07 10:29:22 -05:00
|
|
|
}
|
2019-07-07 10:55:29 -05:00
|
|
|
}
|
|
|
|
SyntaxExtensionKind::NonMacroAttr { mark_used } => {
|
2020-07-29 20:27:50 -05:00
|
|
|
self.cx.sess.mark_attr_known(&attr);
|
2019-07-07 10:55:29 -05:00
|
|
|
if *mark_used {
|
2020-07-29 20:27:50 -05:00
|
|
|
self.cx.sess.mark_attr_used(&attr);
|
2019-07-07 10:29:22 -05:00
|
|
|
}
|
2021-02-22 15:54:09 -06:00
|
|
|
item.visit_attrs(|attrs| attrs.insert(pos, attr));
|
2019-07-07 10:55:29 -05:00
|
|
|
fragment_kind.expect_from_annotatables(iter::once(item))
|
2019-07-07 10:29:22 -05:00
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
2019-08-02 20:22:44 -05:00
|
|
|
InvocationKind::Derive { path, item } => match ext {
|
2019-12-22 16:42:04 -06:00
|
|
|
SyntaxExtensionKind::Derive(expander)
|
|
|
|
| SyntaxExtensionKind::LegacyDerive(expander) => {
|
2019-09-07 17:42:12 -05:00
|
|
|
if let SyntaxExtensionKind::Derive(..) = ext {
|
|
|
|
self.gate_proc_macro_input(&item);
|
|
|
|
}
|
2019-09-26 12:04:05 -05:00
|
|
|
let meta = ast::MetaItem { kind: ast::MetaItemKind::Word, span, path };
|
2020-03-09 12:50:12 -05:00
|
|
|
let items = match expander.expand(self.cx, span, &meta, item) {
|
|
|
|
ExpandResult::Ready(items) => items,
|
2020-11-14 05:47:14 -06:00
|
|
|
ExpandResult::Retry(item) => {
|
2020-03-09 12:50:12 -05:00
|
|
|
// Reassemble the original invocation for retrying.
|
2020-11-14 05:47:14 -06:00
|
|
|
return ExpandResult::Retry(Invocation {
|
|
|
|
kind: InvocationKind::Derive { path: meta.path, item },
|
|
|
|
..invoc
|
|
|
|
});
|
2020-03-09 12:50:12 -05:00
|
|
|
}
|
|
|
|
};
|
2019-07-07 10:55:29 -05:00
|
|
|
fragment_kind.expect_from_annotatables(items)
|
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
2020-03-09 12:50:12 -05:00
|
|
|
})
|
2016-09-01 02:01:45 -05: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 09:50:39 -05:00
|
|
|
fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) {
|
2019-09-07 17:42:12 -05:00
|
|
|
let kind = match item {
|
2019-09-07 17:42:12 -05:00
|
|
|
Annotatable::Item(_)
|
|
|
|
| Annotatable::TraitItem(_)
|
|
|
|
| Annotatable::ImplItem(_)
|
|
|
|
| Annotatable::ForeignItem(_) => return,
|
2020-11-24 13:47:49 -06:00
|
|
|
Annotatable::Stmt(stmt) => {
|
|
|
|
// Attributes are stable on item statements,
|
|
|
|
// but unstable on all other kinds of statements
|
|
|
|
if stmt.is_item() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
"statements"
|
|
|
|
}
|
2019-09-07 17:42:12 -05:00
|
|
|
Annotatable::Expr(_) => "expressions",
|
2019-09-09 07:26:25 -05:00
|
|
|
Annotatable::Arm(..)
|
2021-03-15 16:36:07 -05:00
|
|
|
| Annotatable::ExprField(..)
|
|
|
|
| Annotatable::PatField(..)
|
2019-09-09 07:26:25 -05:00
|
|
|
| Annotatable::GenericParam(..)
|
|
|
|
| Annotatable::Param(..)
|
2021-03-15 16:36:07 -05:00
|
|
|
| Annotatable::FieldDef(..)
|
2019-12-22 16:42:04 -06:00
|
|
|
| Annotatable::Variant(..) => panic!("unexpected annotatable"),
|
rustc: Tweak custom attribute capabilities
This commit starts to lay some groundwork for the stabilization of custom
attribute invocations and general procedural macros. It applies a number of
changes discussed on [internals] as well as a [recent issue][issue], namely:
* The path used to specify a custom attribute must be of length one and cannot
be a global path. This'll help future-proof us against any ambiguities and
give us more time to settle the precise syntax. In the meantime though a bare
identifier can be used and imported to invoke a custom attribute macro. A new
feature gate, `proc_macro_path_invoc`, was added to gate multi-segment paths
and absolute paths.
* The set of items which can be annotated by a custom procedural attribute has
been restricted. Statements, expressions, and modules are disallowed behind
two new feature gates: `proc_macro_expr` and `proc_macro_mod`.
* The input to procedural macro attributes has been restricted and adjusted.
Today an invocation like `#[foo(bar)]` will receive `(bar)` as the input token
stream, but after this PR it will only receive `bar` (the delimiters were
removed). Invocations like `#[foo]` are still allowed and will be invoked in
the same way as `#[foo()]`. This is a **breaking change** for all nightly
users as the syntax coming in to procedural macros will be tweaked slightly.
* Procedural macros (`foo!()` style) can only be expanded to item-like items by
default. A separate feature gate, `proc_macro_non_items`, is required to
expand to items like expressions, statements, etc.
Closes #50038
[internals]: https://internals.rust-lang.org/t/help-stabilize-a-subset-of-macros-2-0/7252
[issue]: https://github.com/rust-lang/rust/issues/50038
2018-04-20 09:50:39 -05:00
|
|
|
};
|
2019-09-07 17:42:12 -05:00
|
|
|
if self.cx.ecfg.proc_macro_hygiene() {
|
2019-12-22 16:42:04 -06:00
|
|
|
return;
|
2019-09-07 17:42:12 -05:00
|
|
|
}
|
2019-11-30 00:40:28 -06:00
|
|
|
feature_err(
|
2020-07-29 20:27:50 -05:00
|
|
|
&self.cx.sess.parse_sess,
|
2019-09-07 17:42:12 -05: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 09:50:39 -05:00
|
|
|
span,
|
|
|
|
&format!("custom attributes cannot be applied to {}", kind),
|
2019-11-30 00:40:28 -06:00
|
|
|
)
|
|
|
|
.emit();
|
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 09:50:39 -05:00
|
|
|
}
|
|
|
|
|
2019-09-07 17:42:12 -05:00
|
|
|
fn gate_proc_macro_input(&self, annotatable: &Annotatable) {
|
|
|
|
struct GateProcMacroInput<'a> {
|
|
|
|
parse_sess: &'a ParseSess,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ast, 'a> Visitor<'ast> for GateProcMacroInput<'a> {
|
|
|
|
fn visit_item(&mut self, item: &'ast ast::Item) {
|
|
|
|
match &item.kind {
|
2021-02-16 15:56:07 -06:00
|
|
|
ast::ItemKind::Mod(_, mod_kind)
|
|
|
|
if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _)) =>
|
|
|
|
{
|
2019-11-30 00:40:28 -06:00
|
|
|
feature_err(
|
2019-09-07 17:42:12 -05:00
|
|
|
self.parse_sess,
|
|
|
|
sym::proc_macro_hygiene,
|
|
|
|
item.span,
|
|
|
|
"non-inline modules in proc macro input are unstable",
|
2019-11-30 00:40:28 -06:00
|
|
|
)
|
|
|
|
.emit();
|
2019-09-07 17:42:12 -05:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
visit::walk_item(self, item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.cx.ecfg.proc_macro_hygiene() {
|
2020-07-29 20:27:50 -05:00
|
|
|
annotatable
|
|
|
|
.visit_with(&mut GateProcMacroInput { parse_sess: &self.cx.sess.parse_sess });
|
2019-09-07 17:42:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 20:24:00 -05:00
|
|
|
fn parse_ast_fragment(
|
|
|
|
&mut self,
|
|
|
|
toks: TokenStream,
|
|
|
|
kind: AstFragmentKind,
|
|
|
|
path: &Path,
|
|
|
|
span: Span,
|
|
|
|
) -> AstFragment {
|
2019-08-31 12:08:06 -05:00
|
|
|
let mut parser = self.cx.new_parser_from_tts(toks);
|
2019-12-03 11:47:44 -06:00
|
|
|
match parse_ast_fragment(&mut parser, kind) {
|
2018-06-19 18:08:08 -05:00
|
|
|
Ok(fragment) => {
|
2019-10-16 03:59:30 -05:00
|
|
|
ensure_complete_parse(&mut parser, path, kind.name(), span);
|
2019-07-07 10:29:22 -05:00
|
|
|
fragment
|
2017-12-26 01:47:32 -06:00
|
|
|
}
|
2016-09-25 23:16:55 -05:00
|
|
|
Err(mut err) => {
|
2021-01-31 14:21:28 -06:00
|
|
|
if err.span.is_dummy() {
|
|
|
|
err.set_span(span);
|
|
|
|
}
|
2019-08-09 11:39:30 -05:00
|
|
|
annotate_err_with_kind(&mut err, kind, span);
|
2016-09-25 23:16:55 -05:00
|
|
|
err.emit();
|
2017-09-02 11:13:25 -05:00
|
|
|
self.cx.trace_macros_diag();
|
2017-12-26 01:47:32 -06:00
|
|
|
kind.dummy(span)
|
2016-09-25 23:16:55 -05:00
|
|
|
}
|
2017-12-26 01:47:32 -06:00
|
|
|
}
|
2016-09-25 23:16:55 -05:00
|
|
|
}
|
2016-09-02 04:12:47 -05:00
|
|
|
}
|
|
|
|
|
2019-10-16 03:59:30 -05:00
|
|
|
pub fn parse_ast_fragment<'a>(
|
|
|
|
this: &mut Parser<'a>,
|
|
|
|
kind: AstFragmentKind,
|
|
|
|
) -> PResult<'a, AstFragment> {
|
|
|
|
Ok(match kind {
|
|
|
|
AstFragmentKind::Items => {
|
|
|
|
let mut items = SmallVec::new();
|
2021-01-18 15:47:37 -06:00
|
|
|
while let Some(item) = this.parse_item(ForceCollect::No)? {
|
2019-10-16 03:59:30 -05:00
|
|
|
items.push(item);
|
|
|
|
}
|
|
|
|
AstFragment::Items(items)
|
|
|
|
}
|
|
|
|
AstFragmentKind::TraitItems => {
|
|
|
|
let mut items = SmallVec::new();
|
2020-11-28 17:33:17 -06:00
|
|
|
while let Some(item) = this.parse_trait_item(ForceCollect::No)? {
|
2020-02-22 01:16:39 -06:00
|
|
|
items.extend(item);
|
2016-09-23 04:32:58 -05:00
|
|
|
}
|
2019-10-16 03:59:30 -05:00
|
|
|
AstFragment::TraitItems(items)
|
|
|
|
}
|
|
|
|
AstFragmentKind::ImplItems => {
|
|
|
|
let mut items = SmallVec::new();
|
2020-11-28 17:33:17 -06:00
|
|
|
while let Some(item) = this.parse_impl_item(ForceCollect::No)? {
|
2020-02-22 01:16:39 -06:00
|
|
|
items.extend(item);
|
2016-09-23 04:32:58 -05:00
|
|
|
}
|
2019-10-16 03:59:30 -05:00
|
|
|
AstFragment::ImplItems(items)
|
|
|
|
}
|
|
|
|
AstFragmentKind::ForeignItems => {
|
|
|
|
let mut items = SmallVec::new();
|
2020-11-28 17:33:17 -06:00
|
|
|
while let Some(item) = this.parse_foreign_item(ForceCollect::No)? {
|
2020-02-21 23:57:31 -06:00
|
|
|
items.extend(item);
|
2016-09-23 04:32:58 -05:00
|
|
|
}
|
2019-10-16 03:59:30 -05:00
|
|
|
AstFragment::ForeignItems(items)
|
|
|
|
}
|
|
|
|
AstFragmentKind::Stmts => {
|
|
|
|
let mut stmts = SmallVec::new();
|
2019-12-03 11:47:44 -06:00
|
|
|
// Won't make progress on a `}`.
|
|
|
|
while this.token != token::Eof && this.token != token::CloseDelim(token::Brace) {
|
2020-08-12 17:39:15 -05:00
|
|
|
if let Some(stmt) = this.parse_full_stmt(AttemptLocalParseRecovery::Yes)? {
|
2019-10-16 03:59:30 -05:00
|
|
|
stmts.push(stmt);
|
2018-10-23 12:07:34 -05:00
|
|
|
}
|
|
|
|
}
|
2019-10-16 03:59:30 -05:00
|
|
|
AstFragment::Stmts(stmts)
|
|
|
|
}
|
|
|
|
AstFragmentKind::Expr => AstFragment::Expr(this.parse_expr()?),
|
|
|
|
AstFragmentKind::OptExpr => {
|
|
|
|
if this.token != token::Eof {
|
|
|
|
AstFragment::OptExpr(Some(this.parse_expr()?))
|
|
|
|
} else {
|
|
|
|
AstFragment::OptExpr(None)
|
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
2019-10-16 03:59:30 -05:00
|
|
|
AstFragmentKind::Ty => AstFragment::Ty(this.parse_ty()?),
|
2021-02-07 20:40:33 -06:00
|
|
|
AstFragmentKind::Pat => {
|
2020-11-21 15:12:05 -06:00
|
|
|
AstFragment::Pat(this.parse_pat_allow_top_alt(None, RecoverComma::No)?)
|
2021-02-07 20:40:33 -06:00
|
|
|
}
|
2019-10-16 03:59:30 -05:00
|
|
|
AstFragmentKind::Arms
|
|
|
|
| AstFragmentKind::Fields
|
|
|
|
| AstFragmentKind::FieldPats
|
|
|
|
| AstFragmentKind::GenericParams
|
|
|
|
| AstFragmentKind::Params
|
|
|
|
| AstFragmentKind::StructFields
|
2019-12-22 16:42:04 -06:00
|
|
|
| AstFragmentKind::Variants => panic!("unexpected AST fragment kind"),
|
2019-10-16 03:59:30 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ensure_complete_parse<'a>(
|
|
|
|
this: &mut Parser<'a>,
|
|
|
|
macro_path: &Path,
|
|
|
|
kind_name: &str,
|
|
|
|
span: Span,
|
|
|
|
) {
|
|
|
|
if this.token != token::Eof {
|
2019-12-06 20:07:35 -06:00
|
|
|
let token = pprust::token_to_string(&this.token);
|
|
|
|
let msg = format!("macro expansion ignores token `{}` and any following", token);
|
2019-10-16 03:59:30 -05:00
|
|
|
// Avoid emitting backtrace info twice.
|
|
|
|
let def_site_span = this.token.span.with_ctxt(SyntaxContext::root());
|
|
|
|
let mut err = this.struct_span_err(def_site_span, &msg);
|
|
|
|
err.span_label(span, "caused by the macro expansion here");
|
|
|
|
let msg = format!(
|
|
|
|
"the usage of `{}!` is likely invalid in {} context",
|
|
|
|
pprust::path_to_string(macro_path),
|
|
|
|
kind_name,
|
|
|
|
);
|
|
|
|
err.note(&msg);
|
|
|
|
let semi_span = this.sess.source_map().next_point(span);
|
|
|
|
|
|
|
|
let semi_full_span = semi_span.to(this.sess.source_map().next_point(semi_span));
|
|
|
|
match this.sess.source_map().span_to_snippet(semi_full_span) {
|
|
|
|
Ok(ref snippet) if &snippet[..] != ";" && kind_name == "expression" => {
|
|
|
|
err.span_suggestion(
|
|
|
|
semi_span,
|
|
|
|
"you might be missing a semicolon here",
|
|
|
|
";".to_owned(),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => {}
|
2016-09-26 06:24:10 -05:00
|
|
|
}
|
2019-10-16 03:59:30 -05:00
|
|
|
err.emit();
|
2016-09-26 06:24:10 -05:00
|
|
|
}
|
2016-09-02 04:12:47 -05:00
|
|
|
}
|
|
|
|
|
2019-06-14 11:39:39 -05:00
|
|
|
struct InvocationCollector<'a, 'b> {
|
2016-09-02 04:12:47 -05:00
|
|
|
cx: &'a mut ExtCtxt<'b>,
|
2016-09-07 17:24:01 -05:00
|
|
|
cfg: StripUnconfigured<'a>,
|
2020-11-14 05:47:14 -06:00
|
|
|
invocations: Vec<(Invocation, Option<Lrc<SyntaxExtension>>)>,
|
2016-09-06 00:42:45 -05:00
|
|
|
monotonic: bool,
|
2016-09-02 04:12:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> InvocationCollector<'a, 'b> {
|
2018-06-19 18:08:08 -05:00
|
|
|
fn collect(&mut self, fragment_kind: AstFragmentKind, kind: InvocationKind) -> AstFragment {
|
2020-11-14 05:47:14 -06:00
|
|
|
let expn_id = ExpnId::fresh(None);
|
2019-11-23 07:54:24 -06:00
|
|
|
let vis = kind.placeholder_visibility();
|
2020-03-09 12:50:12 -05:00
|
|
|
self.invocations.push((
|
|
|
|
Invocation {
|
|
|
|
kind,
|
|
|
|
fragment_kind,
|
|
|
|
expansion_data: ExpansionData {
|
|
|
|
id: expn_id,
|
|
|
|
depth: self.cx.current_expansion.depth + 1,
|
|
|
|
..self.cx.current_expansion.clone()
|
|
|
|
},
|
2016-09-26 17:54:36 -05:00
|
|
|
},
|
2020-03-09 12:50:12 -05:00
|
|
|
None,
|
|
|
|
));
|
2019-11-23 07:54:24 -06:00
|
|
|
placeholder(fragment_kind, NodeId::placeholder_from_expn_id(expn_id), vis)
|
2016-09-02 04:12:47 -05:00
|
|
|
}
|
2016-09-01 02:01:45 -05:00
|
|
|
|
2020-02-29 10:32:20 -06:00
|
|
|
fn collect_bang(
|
|
|
|
&mut self,
|
|
|
|
mac: ast::MacCall,
|
|
|
|
span: Span,
|
|
|
|
kind: AstFragmentKind,
|
|
|
|
) -> AstFragment {
|
2019-06-30 17:08:49 -05:00
|
|
|
self.collect(kind, InvocationKind::Bang { mac, span })
|
2016-09-02 04:12:47 -05:00
|
|
|
}
|
2016-09-01 02:01:45 -05:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
fn collect_attr(
|
|
|
|
&mut self,
|
2021-02-22 15:54:09 -06:00
|
|
|
(attr, pos, derives): (ast::Attribute, usize, Vec<Path>),
|
2019-12-22 16:42:04 -06:00
|
|
|
item: Annotatable,
|
|
|
|
kind: AstFragmentKind,
|
|
|
|
) -> AstFragment {
|
2021-02-22 15:54:09 -06:00
|
|
|
self.collect(kind, InvocationKind::Attr { attr, pos, item, derives })
|
2016-09-02 04:12:47 -05:00
|
|
|
}
|
|
|
|
|
2020-11-14 05:47:14 -06:00
|
|
|
/// If `item` is an attribute invocation, remove the attribute and return it together with
|
2021-02-22 15:54:09 -06:00
|
|
|
/// its position and derives following it. We have to collect the derives in order to resolve
|
|
|
|
/// legacy derive helpers (helpers written before derives that introduce them).
|
|
|
|
fn take_first_attr(
|
|
|
|
&mut self,
|
2021-02-23 09:21:20 -06:00
|
|
|
item: &mut impl AstLike,
|
2021-02-22 15:54:09 -06:00
|
|
|
) -> Option<(ast::Attribute, usize, Vec<Path>)> {
|
2020-11-14 05:47:14 -06:00
|
|
|
let mut attr = None;
|
|
|
|
|
|
|
|
item.visit_attrs(|attrs| {
|
|
|
|
attr = attrs
|
|
|
|
.iter()
|
|
|
|
.position(|a| !self.cx.sess.is_attr_known(a) && !is_builtin_attr(a))
|
|
|
|
.map(|attr_pos| {
|
|
|
|
let attr = attrs.remove(attr_pos);
|
|
|
|
let following_derives = attrs[attr_pos..]
|
|
|
|
.iter()
|
|
|
|
.filter(|a| a.has_name(sym::derive))
|
|
|
|
.flat_map(|a| a.meta_item_list().unwrap_or_default())
|
|
|
|
.filter_map(|nested_meta| match nested_meta {
|
|
|
|
NestedMetaItem::MetaItem(ast::MetaItem {
|
|
|
|
kind: MetaItemKind::Word,
|
|
|
|
path,
|
|
|
|
..
|
|
|
|
}) => Some(path),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2021-02-22 15:54:09 -06:00
|
|
|
(attr, attr_pos, following_derives)
|
2020-11-14 05:47:14 -06:00
|
|
|
})
|
2018-04-18 01:19:21 -05:00
|
|
|
});
|
|
|
|
|
2020-11-14 05:47:14 -06:00
|
|
|
attr
|
2018-04-18 01:19:21 -05:00
|
|
|
}
|
|
|
|
|
2021-02-23 09:21:20 -06:00
|
|
|
fn configure<T: AstLike>(&mut self, node: T) -> Option<T> {
|
2016-09-07 17:24:01 -05:00
|
|
|
self.cfg.configure(node)
|
|
|
|
}
|
2016-12-01 05:20:04 -06: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();
|
2021-04-03 12:39:55 -05:00
|
|
|
let mut attrs = attrs.iter().peekable();
|
|
|
|
let mut span: Option<Span> = None;
|
|
|
|
while let Some(attr) = attrs.next() {
|
2020-07-29 20:27:50 -05:00
|
|
|
rustc_ast_passes::feature_gate::check_attribute(attr, self.cx.sess, features);
|
|
|
|
validate_attr::check_meta(&self.cx.sess.parse_sess, attr);
|
2021-04-03 12:39:55 -05:00
|
|
|
|
|
|
|
let current_span = if let Some(sp) = span { sp.to(attr.span) } else { attr.span };
|
|
|
|
span = Some(current_span);
|
|
|
|
|
|
|
|
if attrs.peek().map_or(false, |next_attr| next_attr.doc_str().is_some()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-02-15 06:29:45 -06:00
|
|
|
if attr.doc_str().is_some() {
|
2020-07-29 20:27:50 -05:00
|
|
|
self.cx.sess.parse_sess.buffer_lint_with_diagnostic(
|
2020-02-21 18:01:48 -06:00
|
|
|
&UNUSED_DOC_COMMENTS,
|
2021-04-03 12:39:55 -05:00
|
|
|
current_span,
|
2020-02-21 18:01:48 -06:00
|
|
|
ast::CRATE_NODE_ID,
|
|
|
|
"unused doc comment",
|
2020-02-21 18:46:14 -06:00
|
|
|
BuiltinLintDiagnostics::UnusedDocComment(attr.span),
|
|
|
|
);
|
2020-02-15 06:29:45 -06:00
|
|
|
}
|
2016-12-01 05:20:04 -06:00
|
|
|
}
|
|
|
|
}
|
2013-08-29 14:10:02 -05: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-04 22:20:55 -06: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| {
|
2020-11-14 05:47:14 -06:00
|
|
|
if let Some(attr) = self.take_first_attr(&mut expr) {
|
2018-11-20 03:16:57 -06:00
|
|
|
// Collect the invoc regardless of whether or not attributes are permitted here
|
|
|
|
// expansion will eat the attribute so it won't error later.
|
2020-11-14 05:47:14 -06:00
|
|
|
self.cfg.maybe_emit_expr_attr_err(&attr.0);
|
2018-11-20 03:16:57 -06:00
|
|
|
|
|
|
|
// AstFragmentKind::Expr requires the macro to emit an expression.
|
2019-12-22 16:42:04 -06:00
|
|
|
return self
|
2020-11-18 16:51:52 -06:00
|
|
|
.collect_attr(attr, Annotatable::Expr(P(expr)), AstFragmentKind::Expr)
|
2018-11-20 03:16:57 -06:00
|
|
|
.make_expr()
|
2019-12-22 16:42:04 -06:00
|
|
|
.into_inner();
|
2018-11-20 03:16:57 -06:00
|
|
|
}
|
2018-03-16 01:20:56 -05:00
|
|
|
|
2020-02-29 10:32:20 -06:00
|
|
|
if let ast::ExprKind::MacCall(mac) = expr.kind {
|
2018-11-20 03:16:57 -06:00
|
|
|
self.check_attributes(&expr.attrs);
|
2019-12-22 16:42:04 -06:00
|
|
|
self.collect_bang(mac, expr.span, AstFragmentKind::Expr).make_expr().into_inner()
|
2018-11-20 03:16:57 -06:00
|
|
|
} else {
|
2020-07-25 04:57:35 -05:00
|
|
|
ensure_sufficient_stack(|| noop_visit_expr(&mut expr, 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-04 22:20:55 -06:00
|
|
|
expr
|
2018-11-20 03:16:57 -06: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-04 22:20:55 -06:00
|
|
|
});
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 07:26:25 -05:00
|
|
|
fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
|
|
|
|
let mut arm = configure!(self, arm);
|
|
|
|
|
2020-11-18 16:51:52 -06:00
|
|
|
if let Some(attr) = self.take_first_attr(&mut arm) {
|
2019-12-22 16:42:04 -06:00
|
|
|
return self
|
2020-11-18 16:51:52 -06:00
|
|
|
.collect_attr(attr, Annotatable::Arm(arm), AstFragmentKind::Arms)
|
2019-12-22 16:42:04 -06:00
|
|
|
.make_arms();
|
2019-09-09 07:26:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
noop_flat_map_arm(arm, self)
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:36:07 -05:00
|
|
|
fn flat_map_expr_field(&mut self, field: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
|
2019-09-09 07:26:25 -05:00
|
|
|
let mut field = configure!(self, field);
|
|
|
|
|
2020-11-18 16:51:52 -06:00
|
|
|
if let Some(attr) = self.take_first_attr(&mut field) {
|
2019-12-22 16:42:04 -06:00
|
|
|
return self
|
2021-03-15 16:36:07 -05:00
|
|
|
.collect_attr(attr, Annotatable::ExprField(field), AstFragmentKind::Fields)
|
|
|
|
.make_expr_fields();
|
2019-09-09 07:26:25 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 16:36:07 -05:00
|
|
|
noop_flat_map_expr_field(field, self)
|
2019-09-09 07:26:25 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 16:36:07 -05:00
|
|
|
fn flat_map_pat_field(&mut self, fp: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
|
2019-09-09 07:26:25 -05:00
|
|
|
let mut fp = configure!(self, fp);
|
|
|
|
|
2020-11-18 16:51:52 -06:00
|
|
|
if let Some(attr) = self.take_first_attr(&mut fp) {
|
2019-12-22 16:42:04 -06:00
|
|
|
return self
|
2021-03-15 16:36:07 -05:00
|
|
|
.collect_attr(attr, Annotatable::PatField(fp), AstFragmentKind::FieldPats)
|
|
|
|
.make_pat_fields();
|
2019-09-09 07:26:25 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 16:36:07 -05:00
|
|
|
noop_flat_map_pat_field(fp, self)
|
2019-09-09 07:26:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
|
|
|
|
let mut p = configure!(self, p);
|
|
|
|
|
2020-11-18 16:51:52 -06:00
|
|
|
if let Some(attr) = self.take_first_attr(&mut p) {
|
2019-12-22 16:42:04 -06:00
|
|
|
return self
|
2020-11-18 16:51:52 -06:00
|
|
|
.collect_attr(attr, Annotatable::Param(p), AstFragmentKind::Params)
|
2019-12-22 16:42:04 -06:00
|
|
|
.make_params();
|
2019-09-09 07:26:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
noop_flat_map_param(p, self)
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:36:07 -05:00
|
|
|
fn flat_map_field_def(&mut self, sf: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
|
2019-09-09 07:26:25 -05:00
|
|
|
let mut sf = configure!(self, sf);
|
|
|
|
|
2020-11-18 16:51:52 -06:00
|
|
|
if let Some(attr) = self.take_first_attr(&mut sf) {
|
2019-12-22 16:42:04 -06:00
|
|
|
return self
|
2021-03-15 16:36:07 -05:00
|
|
|
.collect_attr(attr, Annotatable::FieldDef(sf), AstFragmentKind::StructFields)
|
|
|
|
.make_field_defs();
|
2019-09-09 07:26:25 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 16:36:07 -05:00
|
|
|
noop_flat_map_field_def(sf, self)
|
2019-09-09 07:26:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
|
|
|
|
let mut variant = configure!(self, variant);
|
|
|
|
|
2020-11-18 16:51:52 -06:00
|
|
|
if let Some(attr) = self.take_first_attr(&mut variant) {
|
2019-12-22 16:42:04 -06:00
|
|
|
return self
|
2020-11-18 16:51:52 -06:00
|
|
|
.collect_attr(attr, Annotatable::Variant(variant), AstFragmentKind::Variants)
|
2019-12-22 16:42:04 -06:00
|
|
|
.make_variants();
|
2019-09-09 07:26:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
noop_flat_map_variant(variant, self)
|
|
|
|
}
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-04 22:20:55 -06:00
|
|
|
fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
|
2018-11-20 03:16:57 -06:00
|
|
|
let expr = configure!(self, expr);
|
|
|
|
expr.filter_map(|mut expr| {
|
2020-11-14 05:47:14 -06:00
|
|
|
if let Some(attr) = self.take_first_attr(&mut expr) {
|
|
|
|
self.cfg.maybe_emit_expr_attr_err(&attr.0);
|
2018-03-16 01:20:56 -05:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
return self
|
2020-11-18 16:51:52 -06:00
|
|
|
.collect_attr(attr, Annotatable::Expr(P(expr)), AstFragmentKind::OptExpr)
|
2018-11-20 03:16:57 -06:00
|
|
|
.make_opt_expr()
|
2019-12-22 16:42:04 -06:00
|
|
|
.map(|expr| expr.into_inner());
|
2018-11-20 03:16:57 -06:00
|
|
|
}
|
2018-03-16 01:20:56 -05:00
|
|
|
|
2020-02-29 10:32:20 -06:00
|
|
|
if let ast::ExprKind::MacCall(mac) = expr.kind {
|
2018-11-20 03:16:57 -06:00
|
|
|
self.check_attributes(&expr.attrs);
|
|
|
|
self.collect_bang(mac, expr.span, AstFragmentKind::OptExpr)
|
|
|
|
.make_opt_expr()
|
|
|
|
.map(|expr| expr.into_inner())
|
|
|
|
} else {
|
2019-12-22 16:42:04 -06:00
|
|
|
Some({
|
|
|
|
noop_visit_expr(&mut expr, self);
|
|
|
|
expr
|
|
|
|
})
|
2018-11-20 03:16:57 -06:00
|
|
|
}
|
|
|
|
})
|
2013-08-29 14:10:02 -05: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-04 22:20:55 -06:00
|
|
|
fn visit_pat(&mut self, pat: &mut P<ast::Pat>) {
|
2019-09-26 10:18:31 -05:00
|
|
|
match pat.kind {
|
2020-02-29 10:32:20 -06:00
|
|
|
PatKind::MacCall(_) => {}
|
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-04 22:20:55 -06:00
|
|
|
_ => return noop_visit_pat(pat, self),
|
2016-08-30 18:03:52 -05:00
|
|
|
}
|
2014-05-19 15:59:35 -05:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
visit_clobber(pat, |mut pat| match mem::replace(&mut pat.kind, PatKind::Wild) {
|
2020-02-29 10:32:20 -06:00
|
|
|
PatKind::MacCall(mac) => {
|
|
|
|
self.collect_bang(mac, pat.span, AstFragmentKind::Pat).make_pat()
|
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
_ => unreachable!(),
|
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-04 22:20:55 -06:00
|
|
|
});
|
2013-08-29 14:10:02 -05: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-04 22:20:55 -06:00
|
|
|
fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
|
2019-02-04 22:18:29 -06:00
|
|
|
let mut stmt = configure!(self, stmt);
|
2016-09-07 17:24:01 -05:00
|
|
|
|
2018-03-16 01:20:56 -05:00
|
|
|
// we'll expand attributes on expressions separately
|
|
|
|
if !stmt.is_expr() {
|
2020-11-14 05:47:14 -06:00
|
|
|
if let Some(attr) = self.take_first_attr(&mut stmt) {
|
2019-12-22 16:42:04 -06:00
|
|
|
return self
|
2020-11-18 16:51:52 -06:00
|
|
|
.collect_attr(attr, Annotatable::Stmt(P(stmt)), AstFragmentKind::Stmts)
|
2019-12-22 16:42:04 -06:00
|
|
|
.make_stmts();
|
2018-03-16 01:20:56 -05:00
|
|
|
}
|
|
|
|
}
|
2016-08-30 18:03:52 -05:00
|
|
|
|
2020-02-29 10:32:20 -06:00
|
|
|
if let StmtKind::MacCall(mac) = stmt.kind {
|
2020-11-17 13:27:44 -06:00
|
|
|
let MacCallStmt { mac, style, attrs, tokens: _ } = mac.into_inner();
|
2018-03-16 01:20:56 -05:00
|
|
|
self.check_attributes(&attrs);
|
2019-12-22 16:42:04 -06:00
|
|
|
let mut placeholder =
|
|
|
|
self.collect_bang(mac, stmt.span, AstFragmentKind::Stmts).make_stmts();
|
2018-03-16 01:20:56 -05:00
|
|
|
|
|
|
|
// 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 18:03:52 -05:00
|
|
|
}
|
2018-03-16 01:20:56 -05:00
|
|
|
|
|
|
|
return placeholder;
|
2016-08-30 18:03:52 -05:00
|
|
|
}
|
|
|
|
|
2018-03-16 01:20:56 -05:00
|
|
|
// The placeholder expander gives ids to statements, so we avoid folding the id here.
|
2020-11-17 13:27:44 -06:00
|
|
|
let ast::Stmt { id, kind, span } = stmt;
|
2019-12-22 16:42:04 -06:00
|
|
|
noop_flat_map_stmt_kind(kind, self)
|
|
|
|
.into_iter()
|
2020-11-17 13:27:44 -06:00
|
|
|
.map(|kind| ast::Stmt { id, kind, span })
|
2019-12-22 16:42:04 -06:00
|
|
|
.collect()
|
2013-08-29 14:10:02 -05: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-04 22:20:55 -06:00
|
|
|
fn visit_block(&mut self, block: &mut P<Block>) {
|
2021-02-22 10:06:36 -06:00
|
|
|
let orig_dir_ownership = mem::replace(
|
|
|
|
&mut self.cx.current_expansion.dir_ownership,
|
|
|
|
DirOwnership::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-04 22:20:55 -06:00
|
|
|
noop_visit_block(block, self);
|
2021-02-22 10:06:36 -06:00
|
|
|
self.cx.current_expansion.dir_ownership = orig_dir_ownership;
|
2013-08-29 14:10:02 -05: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-04 22:20:55 -06: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 17:24:01 -05:00
|
|
|
|
2020-11-18 16:51:52 -06:00
|
|
|
if let Some(attr) = self.take_first_attr(&mut item) {
|
2019-12-22 16:42:04 -06:00
|
|
|
return self
|
2020-11-18 16:51:52 -06:00
|
|
|
.collect_attr(attr, Annotatable::Item(item), AstFragmentKind::Items)
|
2019-12-22 16:42:04 -06:00
|
|
|
.make_items();
|
2016-08-30 18:03:52 -05:00
|
|
|
}
|
|
|
|
|
2020-03-08 07:36:20 -05:00
|
|
|
let mut attrs = mem::take(&mut item.attrs); // We do this to please borrowck.
|
|
|
|
let ident = item.ident;
|
2020-03-09 05:16:00 -05:00
|
|
|
let span = item.span;
|
2020-03-08 07:36:20 -05:00
|
|
|
|
2019-09-26 11:51:36 -05:00
|
|
|
match item.kind {
|
2020-02-29 10:32:20 -06:00
|
|
|
ast::ItemKind::MacCall(..) => {
|
2020-03-08 07:36:20 -05:00
|
|
|
item.attrs = attrs;
|
2016-12-01 05:20:04 -06:00
|
|
|
self.check_attributes(&item.attrs);
|
2019-09-26 11:51:36 -05:00
|
|
|
item.and_then(|item| match item.kind {
|
2020-11-05 19:46:03 -06:00
|
|
|
ItemKind::MacCall(mac) => {
|
|
|
|
self.collect_bang(mac, span, AstFragmentKind::Items).make_items()
|
|
|
|
}
|
2016-08-30 18:03:52 -05:00
|
|
|
_ => unreachable!(),
|
|
|
|
})
|
|
|
|
}
|
2021-02-16 15:56:07 -06:00
|
|
|
ast::ItemKind::Mod(_, ref mut mod_kind) if ident != Ident::invalid() => {
|
2021-02-22 09:58:45 -06:00
|
|
|
let (file_path, dir_path, dir_ownership) = match mod_kind {
|
2021-02-22 11:11:30 -06:00
|
|
|
ModKind::Loaded(_, inline, _) => {
|
2021-02-16 15:56:07 -06:00
|
|
|
// Inline `mod foo { ... }`, but we still need to push directories.
|
2021-02-22 10:06:36 -06:00
|
|
|
let (dir_path, dir_ownership) = mod_dir_path(
|
2021-02-22 09:58:45 -06:00
|
|
|
&self.cx.sess,
|
|
|
|
ident,
|
|
|
|
&attrs,
|
|
|
|
&self.cx.current_expansion.module,
|
2021-02-22 10:06:36 -06:00
|
|
|
self.cx.current_expansion.dir_ownership,
|
2021-02-21 07:32:38 -06:00
|
|
|
*inline,
|
2021-02-22 09:58:45 -06:00
|
|
|
);
|
2021-02-16 15:56:07 -06:00
|
|
|
item.attrs = attrs;
|
2021-02-22 09:58:45 -06:00
|
|
|
(None, dir_path, dir_ownership)
|
2020-03-15 18:43:37 -05:00
|
|
|
}
|
2021-02-16 15:56:07 -06:00
|
|
|
ModKind::Unloaded => {
|
|
|
|
// We have an outline `mod foo;` so we need to parse the file.
|
2021-02-22 11:11:30 -06:00
|
|
|
let old_attrs_len = attrs.len();
|
2021-02-22 09:58:45 -06:00
|
|
|
let ParsedExternalMod {
|
2021-02-22 10:49:09 -06:00
|
|
|
mut items,
|
2021-02-22 09:58:45 -06:00
|
|
|
inner_span,
|
|
|
|
file_path,
|
|
|
|
dir_path,
|
|
|
|
dir_ownership,
|
|
|
|
} = parse_external_mod(
|
2021-02-21 10:15:43 -06:00
|
|
|
&self.cx.sess,
|
|
|
|
ident,
|
|
|
|
span,
|
2021-02-22 09:58:45 -06:00
|
|
|
&self.cx.current_expansion.module,
|
2021-02-22 10:06:36 -06:00
|
|
|
self.cx.current_expansion.dir_ownership,
|
2021-02-21 10:15:43 -06:00
|
|
|
&mut attrs,
|
|
|
|
);
|
2021-02-16 15:56:07 -06:00
|
|
|
|
|
|
|
if let Some(extern_mod_loaded) = self.cx.extern_mod_loaded {
|
2021-02-22 10:49:09 -06:00
|
|
|
(attrs, items) = extern_mod_loaded(ident, attrs, items, inner_span);
|
2021-02-16 15:56:07 -06:00
|
|
|
}
|
2020-03-15 18:43:37 -05:00
|
|
|
|
2021-02-22 10:49:09 -06:00
|
|
|
*mod_kind = ModKind::Loaded(items, Inline::No, inner_span);
|
|
|
|
item.attrs = attrs;
|
2021-02-22 11:11:30 -06:00
|
|
|
if item.attrs.len() > old_attrs_len {
|
|
|
|
// If we loaded an out-of-line module and added some inner attributes,
|
2021-02-21 07:32:38 -06:00
|
|
|
// then we need to re-configure it and re-collect attributes for
|
|
|
|
// resolution and expansion.
|
2021-02-22 11:11:30 -06:00
|
|
|
item = configure!(self, item);
|
2021-02-21 07:32:38 -06:00
|
|
|
|
|
|
|
if let Some(attr) = self.take_first_attr(&mut item) {
|
|
|
|
return self
|
|
|
|
.collect_attr(
|
|
|
|
attr,
|
|
|
|
Annotatable::Item(item),
|
|
|
|
AstFragmentKind::Items,
|
|
|
|
)
|
|
|
|
.make_items();
|
|
|
|
}
|
2021-02-22 11:11:30 -06:00
|
|
|
}
|
2021-02-22 09:58:45 -06:00
|
|
|
(Some(file_path), dir_path, dir_ownership)
|
2021-02-16 15:56:07 -06:00
|
|
|
}
|
2020-03-08 07:36:20 -05:00
|
|
|
};
|
2016-08-30 18:03:52 -05:00
|
|
|
|
2020-03-08 07:36:20 -05:00
|
|
|
// Set the module info before we flat map.
|
2021-02-22 09:58:45 -06:00
|
|
|
let mut module = self.cx.current_expansion.module.with_dir_path(dir_path);
|
2020-03-08 07:36:20 -05:00
|
|
|
module.mod_path.push(ident);
|
2021-02-21 10:15:43 -06:00
|
|
|
if let Some(file_path) = file_path {
|
|
|
|
module.file_path_stack.push(file_path);
|
|
|
|
}
|
|
|
|
|
2016-09-07 18:21:59 -05:00
|
|
|
let orig_module =
|
|
|
|
mem::replace(&mut self.cx.current_expansion.module, Rc::new(module));
|
2021-02-21 10:15:43 -06:00
|
|
|
let orig_dir_ownership =
|
2021-02-22 10:06:36 -06:00
|
|
|
mem::replace(&mut self.cx.current_expansion.dir_ownership, dir_ownership);
|
2020-03-08 07:36:20 -05: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-04 22:20:55 -06:00
|
|
|
let result = noop_flat_map_item(item, self);
|
2020-03-08 07:36:20 -05:00
|
|
|
|
|
|
|
// Restore the module info.
|
2021-02-22 10:06:36 -06:00
|
|
|
self.cx.current_expansion.dir_ownership = orig_dir_ownership;
|
2016-09-07 18:21:59 -05:00
|
|
|
self.cx.current_expansion.module = orig_module;
|
2021-02-21 10:15:43 -06:00
|
|
|
|
2017-05-12 13:05:39 -05:00
|
|
|
result
|
2016-09-07 18:21:59 -05:00
|
|
|
}
|
2020-03-08 07:36:20 -05:00
|
|
|
_ => {
|
|
|
|
item.attrs = attrs;
|
|
|
|
noop_flat_map_item(item, self)
|
|
|
|
}
|
2016-08-30 18:03:52 -05:00
|
|
|
}
|
2014-12-02 12:07:41 -06:00
|
|
|
}
|
|
|
|
|
2019-12-11 23:41:18 -06:00
|
|
|
fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 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-04 22:20:55 -06:00
|
|
|
let mut item = configure!(self, item);
|
2016-09-07 17:24:01 -05:00
|
|
|
|
2020-11-18 16:51:52 -06:00
|
|
|
if let Some(attr) = self.take_first_attr(&mut item) {
|
2019-12-22 16:42:04 -06:00
|
|
|
return self
|
2020-11-18 16:51:52 -06:00
|
|
|
.collect_attr(attr, Annotatable::TraitItem(item), AstFragmentKind::TraitItems)
|
2019-12-22 16:42:04 -06:00
|
|
|
.make_trait_items();
|
2016-08-30 18:03:52 -05:00
|
|
|
}
|
|
|
|
|
2019-09-26 11:07:54 -05:00
|
|
|
match item.kind {
|
2020-02-29 10:32:20 -06:00
|
|
|
ast::AssocItemKind::MacCall(..) => {
|
2019-12-11 23:41:18 -06:00
|
|
|
self.check_attributes(&item.attrs);
|
|
|
|
item.and_then(|item| match item.kind {
|
2020-02-29 10:32:20 -06:00
|
|
|
ast::AssocItemKind::MacCall(mac) => self
|
2019-12-11 23:41:18 -06:00
|
|
|
.collect_bang(mac, item.span, AstFragmentKind::TraitItems)
|
|
|
|
.make_trait_items(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
})
|
2016-08-30 18:03:52 -05:00
|
|
|
}
|
2019-12-01 09:58:37 -06:00
|
|
|
_ => noop_flat_map_assoc_item(item, self),
|
2016-08-30 18:03:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 23:41:18 -06:00
|
|
|
fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 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-04 22:20:55 -06:00
|
|
|
let mut item = configure!(self, item);
|
2016-09-07 17:24:01 -05:00
|
|
|
|
2020-11-18 16:51:52 -06:00
|
|
|
if let Some(attr) = self.take_first_attr(&mut item) {
|
2019-12-22 16:42:04 -06:00
|
|
|
return self
|
2020-11-18 16:51:52 -06:00
|
|
|
.collect_attr(attr, Annotatable::ImplItem(item), AstFragmentKind::ImplItems)
|
2019-12-22 16:42:04 -06:00
|
|
|
.make_impl_items();
|
2016-08-30 18:03:52 -05:00
|
|
|
}
|
|
|
|
|
2019-09-26 10:38:13 -05:00
|
|
|
match item.kind {
|
2020-02-29 10:32:20 -06:00
|
|
|
ast::AssocItemKind::MacCall(..) => {
|
2019-12-11 23:41:18 -06:00
|
|
|
self.check_attributes(&item.attrs);
|
|
|
|
item.and_then(|item| match item.kind {
|
2020-02-29 10:32:20 -06:00
|
|
|
ast::AssocItemKind::MacCall(mac) => self
|
2019-12-11 23:41:18 -06:00
|
|
|
.collect_bang(mac, item.span, AstFragmentKind::ImplItems)
|
|
|
|
.make_impl_items(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
})
|
2016-08-30 18:03:52 -05:00
|
|
|
}
|
2019-12-01 09:58:37 -06:00
|
|
|
_ => noop_flat_map_assoc_item(item, self),
|
2016-08-30 18:03:52 -05:00
|
|
|
}
|
2014-07-04 13:24:28 -05: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-04 22:20:55 -06:00
|
|
|
fn visit_ty(&mut self, ty: &mut P<ast::Ty>) {
|
2019-09-26 11:25:31 -05:00
|
|
|
match ty.kind {
|
2020-02-29 10:32:20 -06:00
|
|
|
ast::TyKind::MacCall(_) => {}
|
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-04 22:20:55 -06:00
|
|
|
_ => return noop_visit_ty(ty, self),
|
2016-08-30 18:03:52 -05:00
|
|
|
};
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
visit_clobber(ty, |mut ty| match mem::replace(&mut ty.kind, ast::TyKind::Err) {
|
2020-02-29 10:32:20 -06:00
|
|
|
ast::TyKind::MacCall(mac) => {
|
|
|
|
self.collect_bang(mac, ty.span, AstFragmentKind::Ty).make_ty()
|
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
_ => unreachable!(),
|
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-04 22:20:55 -06:00
|
|
|
});
|
2015-07-25 23:54:19 -05:00
|
|
|
}
|
2016-09-07 17:24:01 -05:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
fn flat_map_foreign_item(
|
|
|
|
&mut self,
|
2021-02-14 10:47:00 -06:00
|
|
|
foreign_item: P<ast::ForeignItem>,
|
2019-12-11 23:41:18 -06:00
|
|
|
) -> SmallVec<[P<ast::ForeignItem>; 1]> {
|
2021-02-14 10:47:00 -06:00
|
|
|
let mut foreign_item = configure!(self, foreign_item);
|
|
|
|
|
2020-11-18 16:51:52 -06:00
|
|
|
if let Some(attr) = self.take_first_attr(&mut foreign_item) {
|
2019-12-22 16:42:04 -06:00
|
|
|
return self
|
|
|
|
.collect_attr(
|
|
|
|
attr,
|
2019-12-11 23:41:18 -06:00
|
|
|
Annotatable::ForeignItem(foreign_item),
|
2019-12-22 16:42:04 -06:00
|
|
|
AstFragmentKind::ForeignItems,
|
|
|
|
)
|
|
|
|
.make_foreign_items();
|
2018-03-10 20:16:26 -06:00
|
|
|
}
|
|
|
|
|
2019-12-11 23:41:18 -06:00
|
|
|
match foreign_item.kind {
|
2020-02-29 10:32:20 -06:00
|
|
|
ast::ForeignItemKind::MacCall(..) => {
|
2019-12-11 23:41:18 -06:00
|
|
|
self.check_attributes(&foreign_item.attrs);
|
|
|
|
foreign_item.and_then(|item| match item.kind {
|
2020-02-29 10:32:20 -06:00
|
|
|
ast::ForeignItemKind::MacCall(mac) => self
|
2019-12-11 23:41:18 -06:00
|
|
|
.collect_bang(mac, item.span, AstFragmentKind::ForeignItems)
|
|
|
|
.make_foreign_items(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => noop_flat_map_foreign_item(foreign_item, self),
|
2018-03-10 20:16:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 07:26:25 -05:00
|
|
|
fn flat_map_generic_param(
|
|
|
|
&mut self,
|
2019-12-22 16:42:04 -06:00
|
|
|
param: ast::GenericParam,
|
|
|
|
) -> SmallVec<[ast::GenericParam; 1]> {
|
2019-09-09 07:26:25 -05:00
|
|
|
let mut param = configure!(self, param);
|
|
|
|
|
2020-11-18 16:51:52 -06:00
|
|
|
if let Some(attr) = self.take_first_attr(&mut param) {
|
2019-12-22 16:42:04 -06:00
|
|
|
return self
|
|
|
|
.collect_attr(
|
|
|
|
attr,
|
|
|
|
Annotatable::GenericParam(param),
|
|
|
|
AstFragmentKind::GenericParams,
|
|
|
|
)
|
|
|
|
.make_generic_params();
|
2019-09-09 07:26:25 -05:00
|
|
|
}
|
|
|
|
|
2019-08-24 11:54:40 -05:00
|
|
|
noop_flat_map_generic_param(param, self)
|
2018-06-01 16:10:29 -05: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-04 22:20:55 -06:00
|
|
|
fn visit_attribute(&mut self, at: &mut ast::Attribute) {
|
2017-09-21 22:37:00 -05:00
|
|
|
// turn `#[doc(include="filename")]` attributes into `#[doc(include(file="filename",
|
|
|
|
// contents="file contents")]` attributes
|
2020-07-29 20:27:50 -05:00
|
|
|
if !self.cx.sess.check_name(at, 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-04 22:20:55 -06:00
|
|
|
return noop_visit_attribute(at, self);
|
2017-09-21 22:37:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(list) = at.meta_item_list() {
|
2020-08-02 05:17:20 -05:00
|
|
|
if !list.iter().any(|it| it.has_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-04 22:20:55 -06:00
|
|
|
return noop_visit_attribute(at, self);
|
2017-09-21 22:37:00 -05: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-04 22:20:55 -06:00
|
|
|
for mut it in list {
|
2020-08-02 05:17:20 -05:00
|
|
|
if !it.has_name(sym::include) {
|
2019-12-22 16:42:04 -06:00
|
|
|
items.push({
|
|
|
|
noop_visit_meta_list_item(&mut it, self);
|
|
|
|
it
|
|
|
|
});
|
2017-09-21 22:37:00 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(file) = it.value_str() {
|
2020-07-29 20:27:50 -05:00
|
|
|
let err_count = self.cx.sess.parse_sess.span_diagnostic.err_count();
|
2019-09-14 13:29:59 -05:00
|
|
|
self.check_attributes(slice::from_ref(at));
|
2020-07-29 20:27:50 -05:00
|
|
|
if self.cx.sess.parse_sess.span_diagnostic.err_count() > err_count {
|
2017-09-21 22:37:00 -05:00
|
|
|
// 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-04 22:20:55 -06:00
|
|
|
return noop_visit_attribute(at, self);
|
2017-09-21 22:37:00 -05:00
|
|
|
}
|
|
|
|
|
2019-10-19 12:05:46 -05:00
|
|
|
let filename = match self.cx.resolve_path(&*file.as_str(), it.span()) {
|
|
|
|
Ok(filename) => filename,
|
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-13 11:51:32 -05:00
|
|
|
match self.cx.source_map().load_file(&filename) {
|
|
|
|
Ok(source_file) => {
|
2019-12-22 16:42:04 -06:00
|
|
|
let src = source_file
|
|
|
|
.src
|
|
|
|
.as_ref()
|
2019-08-13 11:51:32 -05:00
|
|
|
.expect("freshly loaded file should have a source");
|
|
|
|
let src_interned = Symbol::intern(src.as_str());
|
2017-12-19 16:43:32 -06:00
|
|
|
|
2017-09-21 22:37:00 -05:00
|
|
|
let include_info = vec![
|
2019-12-22 16:42:04 -06:00
|
|
|
ast::NestedMetaItem::MetaItem(attr::mk_name_value_item_str(
|
|
|
|
Ident::with_dummy_span(sym::file),
|
|
|
|
file,
|
|
|
|
DUMMY_SP,
|
|
|
|
)),
|
|
|
|
ast::NestedMetaItem::MetaItem(attr::mk_name_value_item_str(
|
|
|
|
Ident::with_dummy_span(sym::contents),
|
|
|
|
src_interned,
|
|
|
|
DUMMY_SP,
|
|
|
|
)),
|
2017-09-21 22:37:00 -05:00
|
|
|
];
|
|
|
|
|
2019-08-10 18:20:18 -05:00
|
|
|
let include_ident = Ident::with_dummy_span(sym::include);
|
2019-08-04 16:59:06 -05:00
|
|
|
let item = attr::mk_list_item(include_ident, include_info);
|
2019-03-03 11:56:24 -06:00
|
|
|
items.push(ast::NestedMetaItem::MetaItem(item));
|
2017-09-21 22:37:00 -05:00
|
|
|
}
|
2018-11-16 15:22:06 -06:00
|
|
|
Err(e) => {
|
2020-11-28 09:11:25 -06:00
|
|
|
let lit_span = it.name_value_literal_span().unwrap();
|
2018-11-28 13:54:08 -06:00
|
|
|
|
|
|
|
if e.kind() == ErrorKind::InvalidData {
|
|
|
|
self.cx
|
|
|
|
.struct_span_err(
|
2020-11-28 09:11:25 -06:00
|
|
|
lit_span,
|
2018-11-28 13:54:08 -06:00
|
|
|
&format!("{} wasn't a utf-8 file", filename.display()),
|
|
|
|
)
|
2020-11-28 09:11:25 -06:00
|
|
|
.span_label(lit_span, "contains invalid utf-8")
|
2018-11-28 13:54:08 -06:00
|
|
|
.emit();
|
|
|
|
} else {
|
|
|
|
let mut err = self.cx.struct_span_err(
|
2020-11-28 09:11:25 -06:00
|
|
|
lit_span,
|
2018-11-28 13:54:08 -06:00
|
|
|
&format!("couldn't read {}: {}", filename.display(), e),
|
|
|
|
);
|
2020-11-28 09:11:25 -06:00
|
|
|
err.span_label(lit_span, "couldn't read file");
|
2018-11-28 13:54:08 -06:00
|
|
|
|
|
|
|
err.emit();
|
|
|
|
}
|
2017-09-21 22:37:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-27 06:34:08 -06:00
|
|
|
let mut err = self
|
|
|
|
.cx
|
|
|
|
.struct_span_err(it.span(), "expected path to external documentation");
|
2018-11-28 11:11:45 -06:00
|
|
|
|
|
|
|
// Check if the user erroneously used `doc(include(...))` syntax.
|
|
|
|
let literal = it.meta_item_list().and_then(|list| {
|
|
|
|
if list.len() == 1 {
|
2019-09-26 10:56:53 -05:00
|
|
|
list[0].literal().map(|literal| &literal.kind)
|
2018-11-28 11:11:45 -06:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let (path, applicability) = match &literal {
|
|
|
|
Some(LitKind::Str(path, ..)) => {
|
|
|
|
(path.to_string(), Applicability::MachineApplicable)
|
|
|
|
}
|
|
|
|
_ => (String::from("<path>"), Applicability::HasPlaceholders),
|
|
|
|
};
|
|
|
|
|
2019-01-25 15:03:27 -06:00
|
|
|
err.span_suggestion(
|
2019-03-03 11:56:24 -06:00
|
|
|
it.span(),
|
2018-11-28 11:11:45 -06:00
|
|
|
"provide a file path with `=`",
|
|
|
|
format!("include = \"{}\"", path),
|
|
|
|
applicability,
|
|
|
|
);
|
|
|
|
|
|
|
|
err.emit();
|
2017-09-21 22:37:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-10 18:20:18 -05:00
|
|
|
let meta = attr::mk_list_item(Ident::with_dummy_span(sym::doc), items);
|
2020-01-11 02:59:14 -06:00
|
|
|
*at = ast::Attribute {
|
2020-11-05 11:27:48 -06:00
|
|
|
kind: ast::AttrKind::Normal(
|
|
|
|
AttrItem { path: meta.path, args: meta.kind.mac_args(meta.span), tokens: None },
|
|
|
|
None,
|
|
|
|
),
|
2019-07-30 12:50:22 -05:00
|
|
|
span: at.span,
|
|
|
|
id: at.id,
|
|
|
|
style: at.style,
|
|
|
|
};
|
2017-09-21 22:37:00 -05: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-04 22:20:55 -06:00
|
|
|
noop_visit_attribute(at, self)
|
2017-09-21 22:37:00 -05: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-04 22:20:55 -06:00
|
|
|
fn visit_id(&mut self, id: &mut ast::NodeId) {
|
2016-09-06 00:42:45 -05: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-04 22:20:55 -06:00
|
|
|
debug_assert_eq!(*id, ast::DUMMY_NODE_ID);
|
|
|
|
*id = self.cx.resolver.next_node_id()
|
2016-09-06 00:42:45 -05:00
|
|
|
}
|
2016-09-04 19:10:27 -05:00
|
|
|
}
|
2013-07-16 00:05:50 -05:00
|
|
|
}
|
|
|
|
|
2015-02-15 14:30:45 -06:00
|
|
|
pub struct ExpansionConfig<'feat> {
|
2014-06-06 15:21:18 -05:00
|
|
|
pub crate_name: String,
|
2015-02-15 14:30:45 -06:00
|
|
|
pub features: Option<&'feat Features>,
|
2020-05-26 13:48:08 -05:00
|
|
|
pub recursion_limit: Limit,
|
2015-04-14 08:36:38 -05:00
|
|
|
pub trace_mac: bool,
|
2021-02-14 10:43:54 -06:00
|
|
|
pub should_test: bool, // If false, strip `#[test]` nodes
|
|
|
|
pub span_debug: bool, // If true, use verbose debugging for `proc_macro::Span`
|
2020-08-30 21:17:24 -05:00
|
|
|
pub proc_macro_backtrace: bool, // If true, show backtraces for proc-macro panics
|
2014-09-26 19:14:23 -05:00
|
|
|
}
|
|
|
|
|
2015-02-15 14:30:45 -06:00
|
|
|
impl<'feat> ExpansionConfig<'feat> {
|
|
|
|
pub fn default(crate_name: String) -> ExpansionConfig<'static> {
|
2014-09-26 19:14:23 -05:00
|
|
|
ExpansionConfig {
|
2017-08-07 00:54:09 -05:00
|
|
|
crate_name,
|
2015-02-15 14:30:45 -06:00
|
|
|
features: None,
|
2020-05-26 13:48:08 -05:00
|
|
|
recursion_limit: Limit::new(1024),
|
2015-04-14 08:36:38 -05:00
|
|
|
trace_mac: false,
|
2016-05-31 20:27:12 -05:00
|
|
|
should_test: false,
|
2020-05-31 15:20:50 -05:00
|
|
|
span_debug: false,
|
2020-08-30 21:17:24 -05:00
|
|
|
proc_macro_backtrace: false,
|
2014-09-26 19:14:23 -05:00
|
|
|
}
|
|
|
|
}
|
2015-02-15 14:30:45 -06:00
|
|
|
|
2019-06-22 08:18:05 -05:00
|
|
|
fn proc_macro_hygiene(&self) -> bool {
|
|
|
|
self.features.map_or(false, |features| features.proc_macro_hygiene)
|
|
|
|
}
|
2014-03-01 01:17:38 -06:00
|
|
|
}
|