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.
This commit is contained in:
Nicholas Nethercote 2019-02-05 15:20:55 +11:00
parent 970b5d189a
commit 9fcb1658ab
23 changed files with 1521 additions and 1620 deletions

View File

@ -16,7 +16,7 @@ use syntax::{
expand::ExpansionConfig,
hygiene::{self, Mark, SyntaxContext},
},
fold::{self, Folder},
mut_visit::{self, MutVisitor},
parse::ParseSess,
ptr::P,
symbol::Symbol
@ -28,10 +28,10 @@ use {AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS};
pub fn modify(
sess: &ParseSess,
resolver: &mut dyn Resolver,
krate: Crate,
krate: &mut Crate,
crate_name: String,
handler: &rustc_errors::Handler,
) -> ast::Crate {
) {
ExpandAllocatorDirectives {
handler,
sess,
@ -39,7 +39,7 @@ pub fn modify(
found: false,
crate_name: Some(crate_name),
in_submod: -1, // -1 to account for the "root" module
}.fold_crate(krate)
}.visit_crate(krate);
}
struct ExpandAllocatorDirectives<'a> {
@ -54,14 +54,14 @@ struct ExpandAllocatorDirectives<'a> {
in_submod: isize,
}
impl<'a> Folder for ExpandAllocatorDirectives<'a> {
fn fold_item(&mut self, item: P<Item>) -> SmallVec<[P<Item>; 1]> {
impl<'a> MutVisitor for ExpandAllocatorDirectives<'a> {
fn flat_map_item(&mut self, item: P<Item>) -> SmallVec<[P<Item>; 1]> {
debug!("in submodule {}", self.in_submod);
let name = if attr::contains_name(&item.attrs, "global_allocator") {
"global_allocator"
} else {
return fold::noop_fold_item(item, self);
return mut_visit::noop_flat_map_item(item, self);
};
match item.node {
ItemKind::Static(..) => {}
@ -139,25 +139,24 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
let name = f.kind.fn_name("allocator_abi");
let allocator_abi = Ident::with_empty_ctxt(Symbol::gensym(&name));
let module = f.cx.item_mod(span, span, allocator_abi, Vec::new(), items);
let module = f.cx.monotonic_expander().fold_item(module).pop().unwrap();
let module = f.cx.monotonic_expander().flat_map_item(module).pop().unwrap();
// Return the item and new submodule
smallvec![item, module]
}
// If we enter a submodule, take note.
fn fold_mod(&mut self, m: Mod) -> Mod {
fn visit_mod(&mut self, m: &mut Mod) {
debug!("enter submodule");
self.in_submod += 1;
let ret = fold::noop_fold_mod(m, self);
mut_visit::noop_visit_mod(m, self);
self.in_submod -= 1;
debug!("exit submodule");
ret
}
// `fold_mac` is disabled by default. Enable it here.
fn fold_mac(&mut self, mac: Mac) -> Mac {
fold::noop_fold_mac(mac, self)
// `visit_mac` is disabled by default. Enable it here.
fn visit_mac(&mut self, mac: &mut Mac) {
mut_visit::noop_visit_mac(mac, self)
}
}

View File

@ -39,6 +39,15 @@ impl<T> ::std::ops::Deref for ThinVec<T> {
}
}
impl<T> ::std::ops::DerefMut for ThinVec<T> {
fn deref_mut(&mut self) -> &mut [T] {
match *self {
ThinVec(None) => &mut [],
ThinVec(Some(ref mut vec)) => vec,
}
}
}
impl<T> Extend<T> for ThinVec<T> {
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
match *self {

View File

@ -32,7 +32,7 @@ use rustc_typeck as typeck;
use syntax::{self, ast, attr, diagnostics, visit};
use syntax::early_buffered_lints::BufferedEarlyLint;
use syntax::ext::base::ExtCtxt;
use syntax::fold::Folder;
use syntax::mut_visit::MutVisitor;
use syntax::parse::{self, PResult};
use syntax::util::node_count::NodeCounter;
use syntax::util::lev_distance::find_best_match_for_name;
@ -1000,12 +1000,12 @@ where
});
sess.profiler(|p| p.end_activity(ProfileCategory::Expansion));
krate = time(sess, "maybe building test harness", || {
time(sess, "maybe building test harness", || {
syntax::test::modify_for_testing(
&sess.parse_sess,
&mut resolver,
sess.opts.test,
krate,
&mut krate,
sess.diagnostic(),
&sess.features_untracked(),
)
@ -1014,7 +1014,7 @@ where
// If we're actually rustdoc then there's no need to actually compile
// anything, so switch everything to just looping
if sess.opts.actually_rustdoc {
krate = ReplaceBodyWithLoop::new(sess).fold_crate(krate);
ReplaceBodyWithLoop::new(sess).visit_crate(&mut krate);
}
let (has_proc_macro_decls, has_global_allocator) = time(sess, "AST validation", || {
@ -1045,11 +1045,11 @@ where
if has_global_allocator {
// Expand global allocators, which are treated as an in-tree proc macro
krate = time(sess, "creating allocators", || {
time(sess, "creating allocators", || {
allocator::expand::modify(
&sess.parse_sess,
&mut resolver,
krate,
&mut krate,
crate_name.to_string(),
sess.diagnostic(),
)

View File

@ -870,9 +870,9 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
control.after_hir_lowering.stop = Compilation::Stop;
control.after_parse.callback = box move |state| {
state.krate = Some(pretty::fold_crate(state.session,
state.krate.take().unwrap(),
ppm));
let mut krate = state.krate.take().unwrap();
pretty::visit_crate(state.session, &mut krate, ppm);
state.krate = Some(krate);
};
control.after_hir_lowering.callback = box move |state| {
pretty::print_after_hir_lowering(state.session,
@ -891,7 +891,8 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
control.after_parse.stop = Compilation::Stop;
control.after_parse.callback = box move |state| {
let krate = pretty::fold_crate(state.session, state.krate.take().unwrap(), ppm);
let mut krate = state.krate.take().unwrap();
pretty::visit_crate(state.session, &mut krate, ppm);
pretty::print_after_parsing(state.session,
state.input,
&krate,

View File

@ -16,7 +16,7 @@ use rustc_metadata::cstore::CStore;
use rustc_mir::util::{write_mir_pretty, write_mir_graphviz};
use syntax::ast::{self, BlockCheckMode};
use syntax::fold::{self, Folder};
use syntax::mut_visit::{*, MutVisitor, visit_clobber};
use syntax::print::{pprust};
use syntax::print::pprust::PrintState;
use syntax::ptr::P;
@ -28,6 +28,7 @@ use smallvec::SmallVec;
use std::cell::Cell;
use std::fs::File;
use std::io::{self, Write};
use std::ops::DerefMut;
use std::option;
use std::path::Path;
use std::str::FromStr;
@ -703,42 +704,42 @@ impl<'a> ReplaceBodyWithLoop<'a> {
}
}
impl<'a> fold::Folder for ReplaceBodyWithLoop<'a> {
fn fold_item_kind(&mut self, i: ast::ItemKind) -> ast::ItemKind {
impl<'a> MutVisitor for ReplaceBodyWithLoop<'a> {
fn visit_item_kind(&mut self, i: &mut ast::ItemKind) {
let is_const = match i {
ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => true,
ast::ItemKind::Fn(ref decl, ref header, _, _) =>
header.constness.node == ast::Constness::Const || Self::should_ignore_fn(decl),
_ => false,
};
self.run(is_const, |s| fold::noop_fold_item_kind(i, s))
self.run(is_const, |s| noop_visit_item_kind(i, s))
}
fn fold_trait_item(&mut self, i: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> {
fn flat_map_trait_item(&mut self, i: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> {
let is_const = match i.node {
ast::TraitItemKind::Const(..) => true,
ast::TraitItemKind::Method(ast::MethodSig { ref decl, ref header, .. }, _) =>
header.constness.node == ast::Constness::Const || Self::should_ignore_fn(decl),
_ => false,
};
self.run(is_const, |s| fold::noop_fold_trait_item(i, s))
self.run(is_const, |s| noop_flat_map_trait_item(i, s))
}
fn fold_impl_item(&mut self, i: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> {
fn flat_map_impl_item(&mut self, i: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> {
let is_const = match i.node {
ast::ImplItemKind::Const(..) => true,
ast::ImplItemKind::Method(ast::MethodSig { ref decl, ref header, .. }, _) =>
header.constness.node == ast::Constness::Const || Self::should_ignore_fn(decl),
_ => false,
};
self.run(is_const, |s| fold::noop_fold_impl_item(i, s))
self.run(is_const, |s| noop_flat_map_impl_item(i, s))
}
fn fold_anon_const(&mut self, c: ast::AnonConst) -> ast::AnonConst {
self.run(true, |s| fold::noop_fold_anon_const(c, s))
fn visit_anon_const(&mut self, c: &mut ast::AnonConst) {
self.run(true, |s| noop_visit_anon_const(c, s))
}
fn fold_block(&mut self, b: P<ast::Block>) -> P<ast::Block> {
fn visit_block(&mut self, b: &mut P<ast::Block>) {
fn stmt_to_block(rules: ast::BlockCheckMode,
s: Option<ast::Stmt>,
sess: &Session) -> ast::Block {
@ -780,14 +781,14 @@ impl<'a> fold::Folder for ReplaceBodyWithLoop<'a> {
};
if self.within_static_or_const {
fold::noop_fold_block(b, self)
noop_visit_block(b, self)
} else {
b.map(|b| {
visit_clobber(b.deref_mut(), |b| {
let mut stmts = vec![];
for s in b.stmts {
let old_blocks = self.nested_blocks.replace(vec![]);
stmts.extend(self.fold_stmt(s).into_iter().filter(|s| s.is_item()));
stmts.extend(self.flat_map_stmt(s).into_iter().filter(|s| s.is_item()));
// we put a Some in there earlier with that replace(), so this is valid
let new_blocks = self.nested_blocks.take().unwrap();
@ -818,9 +819,9 @@ impl<'a> fold::Folder for ReplaceBodyWithLoop<'a> {
}
// in general the pretty printer processes unexpanded code, so
// we override the default `fold_mac` method which panics.
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
fold::noop_fold_mac(mac, self)
// we override the default `visit_mac` method which panics.
fn visit_mac(&mut self, mac: &mut ast::Mac) {
noop_visit_mac(mac, self)
}
}
@ -889,12 +890,9 @@ fn print_flowgraph<'a, 'tcx, W: Write>(variants: Vec<borrowck_dot::Variant>,
}
}
pub fn fold_crate(sess: &Session, krate: ast::Crate, ppm: PpMode) -> ast::Crate {
pub fn visit_crate(sess: &Session, krate: &mut ast::Crate, ppm: PpMode) {
if let PpmSource(PpmEveryBodyLoops) = ppm {
let mut fold = ReplaceBodyWithLoop::new(sess);
fold.fold_crate(krate)
} else {
krate
ReplaceBodyWithLoop::new(sess).visit_crate(krate);
}
}

View File

@ -15,6 +15,7 @@ use ast;
use ast::{AttrId, Attribute, AttrStyle, Name, Ident, Path, PathSegment};
use ast::{MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
use ast::{Lit, LitKind, Expr, ExprKind, Item, Local, Stmt, StmtKind, GenericParam};
use mut_visit::visit_clobber;
use source_map::{BytePos, Spanned, respan, dummy_spanned};
use syntax_pos::{FileName, Span};
use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
@ -28,6 +29,7 @@ use tokenstream::{TokenStream, TokenTree, DelimSpan};
use GLOBALS;
use std::iter;
use std::ops::DerefMut;
pub fn mark_used(attr: &Attribute) {
debug!("Marking {:?} as used.", attr);
@ -695,13 +697,13 @@ impl LitKind {
pub trait HasAttrs: Sized {
fn attrs(&self) -> &[ast::Attribute];
fn map_attrs<F: FnOnce(Vec<ast::Attribute>) -> Vec<ast::Attribute>>(self, f: F) -> Self;
fn visit_attrs<F: FnOnce(&mut Vec<ast::Attribute>)>(&mut self, f: F);
}
impl<T: HasAttrs> HasAttrs for Spanned<T> {
fn attrs(&self) -> &[ast::Attribute] { self.node.attrs() }
fn map_attrs<F: FnOnce(Vec<ast::Attribute>) -> Vec<ast::Attribute>>(self, f: F) -> Self {
respan(self.span, self.node.map_attrs(f))
fn visit_attrs<F: FnOnce(&mut Vec<ast::Attribute>)>(&mut self, f: F) {
self.node.visit_attrs(f);
}
}
@ -709,7 +711,7 @@ impl HasAttrs for Vec<Attribute> {
fn attrs(&self) -> &[Attribute] {
self
}
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
fn visit_attrs<F: FnOnce(&mut Vec<Attribute>)>(&mut self, f: F) {
f(self)
}
}
@ -718,8 +720,12 @@ impl HasAttrs for ThinVec<Attribute> {
fn attrs(&self) -> &[Attribute] {
self
}
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
f(self.into()).into()
fn visit_attrs<F: FnOnce(&mut Vec<Attribute>)>(&mut self, f: F) {
visit_clobber(self, |this| {
let mut vec = this.into();
f(&mut vec);
vec.into()
});
}
}
@ -727,8 +733,8 @@ impl<T: HasAttrs + 'static> HasAttrs for P<T> {
fn attrs(&self) -> &[Attribute] {
(**self).attrs()
}
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
self.map(|t| t.map_attrs(f))
fn visit_attrs<F: FnOnce(&mut Vec<Attribute>)>(&mut self, f: F) {
(**self).visit_attrs(f);
}
}
@ -745,23 +751,27 @@ impl HasAttrs for StmtKind {
}
}
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
fn visit_attrs<F: FnOnce(&mut Vec<Attribute>)>(&mut self, f: F) {
match self {
StmtKind::Local(local) => StmtKind::Local(local.map_attrs(f)),
StmtKind::Item(..) => self,
StmtKind::Expr(expr) => StmtKind::Expr(expr.map_attrs(f)),
StmtKind::Semi(expr) => StmtKind::Semi(expr.map_attrs(f)),
StmtKind::Mac(mac) => StmtKind::Mac(mac.map(|(mac, style, attrs)| {
(mac, style, attrs.map_attrs(f))
})),
StmtKind::Local(local) => local.visit_attrs(f),
StmtKind::Item(..) => {}
StmtKind::Expr(expr) => expr.visit_attrs(f),
StmtKind::Semi(expr) => expr.visit_attrs(f),
StmtKind::Mac(mac) => {
let (_mac, _style, attrs) = mac.deref_mut();
attrs.visit_attrs(f);
}
}
}
}
impl HasAttrs for Stmt {
fn attrs(&self) -> &[ast::Attribute] { self.node.attrs() }
fn map_attrs<F: FnOnce(Vec<ast::Attribute>) -> Vec<ast::Attribute>>(self, f: F) -> Self {
Stmt { id: self.id, node: self.node.map_attrs(f), span: self.span }
fn attrs(&self) -> &[ast::Attribute] {
self.node.attrs()
}
fn visit_attrs<F: FnOnce(&mut Vec<ast::Attribute>)>(&mut self, f: F) {
self.node.visit_attrs(f);
}
}
@ -770,9 +780,8 @@ impl HasAttrs for GenericParam {
&self.attrs
}
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(mut self, f: F) -> Self {
self.attrs = self.attrs.map_attrs(f);
self
fn visit_attrs<F: FnOnce(&mut Vec<Attribute>)>(&mut self, f: F) {
self.attrs.visit_attrs(f);
}
}
@ -783,11 +792,8 @@ macro_rules! derive_has_attrs {
&self.attrs
}
fn map_attrs<F>(mut self, f: F) -> Self
where F: FnOnce(Vec<Attribute>) -> Vec<Attribute>,
{
self.attrs = self.attrs.map_attrs(f);
self
fn visit_attrs<F: FnOnce(&mut Vec<Attribute>)>(&mut self, f: F) {
self.attrs.visit_attrs(f);
}
}
)* }

View File

@ -6,16 +6,15 @@ use feature_gate::{
get_features,
GateIssue,
};
use {fold, attr};
use attr;
use ast;
use source_map::Spanned;
use edition::Edition;
use parse::{token, ParseSess};
use smallvec::SmallVec;
use errors::Applicability;
use util::move_map::MoveMap;
use mut_visit::*;
use parse::{token, ParseSess};
use ptr::P;
use smallvec::SmallVec;
use util::map_in_place::MapInPlace;
/// A folder that strips out items that do not belong in the current configuration.
pub struct StripUnconfigured<'a> {
@ -65,8 +64,8 @@ macro_rules! configure {
}
impl<'a> StripUnconfigured<'a> {
pub fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
let node = self.process_cfg_attrs(node);
pub fn configure<T: HasAttrs>(&mut self, mut node: T) -> Option<T> {
self.process_cfg_attrs(&mut node);
if self.in_cfg(node.attrs()) { Some(node) } else { None }
}
@ -76,10 +75,10 @@ impl<'a> StripUnconfigured<'a> {
/// Gives compiler warnigns if any `cfg_attr` does not contain any
/// attributes and is in the original source code. Gives compiler errors if
/// the syntax of any `cfg_attr` is incorrect.
pub fn process_cfg_attrs<T: HasAttrs>(&mut self, node: T) -> T {
node.map_attrs(|attrs| {
attrs.into_iter().flat_map(|attr| self.process_cfg_attr(attr)).collect()
})
pub fn process_cfg_attrs<T: HasAttrs>(&mut self, node: &mut T) {
node.visit_attrs(|attrs| {
attrs.flat_map_in_place(|attr| self.process_cfg_attr(attr));
});
}
/// Parse and expand a single `cfg_attr` attribute into a list of attributes
@ -218,70 +217,47 @@ impl<'a> StripUnconfigured<'a> {
}
}
pub fn configure_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
ast::ForeignMod {
abi: foreign_mod.abi,
items: foreign_mod.items.move_flat_map(|item| self.configure(item)),
}
pub fn configure_foreign_mod(&mut self, foreign_mod: &mut ast::ForeignMod) {
let ast::ForeignMod { abi: _, items } = foreign_mod;
items.flat_map_in_place(|item| self.configure(item));
}
fn configure_variant_data(&mut self, vdata: ast::VariantData) -> ast::VariantData {
fn configure_variant_data(&mut self, vdata: &mut ast::VariantData) {
match vdata {
ast::VariantData::Struct(fields, id) => {
let fields = fields.move_flat_map(|field| self.configure(field));
ast::VariantData::Struct(fields, id)
}
ast::VariantData::Tuple(fields, id) => {
let fields = fields.move_flat_map(|field| self.configure(field));
ast::VariantData::Tuple(fields, id)
}
ast::VariantData::Unit(id) => ast::VariantData::Unit(id)
ast::VariantData::Struct(fields, _id) |
ast::VariantData::Tuple(fields, _id) =>
fields.flat_map_in_place(|field| self.configure(field)),
ast::VariantData::Unit(_id) => {}
}
}
pub fn configure_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
pub fn configure_item_kind(&mut self, item: &mut ast::ItemKind) {
match item {
ast::ItemKind::Struct(def, generics) => {
ast::ItemKind::Struct(self.configure_variant_data(def), generics)
ast::ItemKind::Struct(def, _generics) |
ast::ItemKind::Union(def, _generics) => self.configure_variant_data(def),
ast::ItemKind::Enum(ast::EnumDef { variants }, _generics) => {
variants.flat_map_in_place(|variant| self.configure(variant));
for variant in variants {
self.configure_variant_data(&mut variant.node.data);
}
ast::ItemKind::Union(def, generics) => {
ast::ItemKind::Union(self.configure_variant_data(def), generics)
}
ast::ItemKind::Enum(def, generics) => {
let variants = def.variants.move_flat_map(|v| {
self.configure(v).map(|v| {
Spanned {
node: ast::Variant_ {
ident: v.node.ident,
attrs: v.node.attrs,
data: self.configure_variant_data(v.node.data),
disr_expr: v.node.disr_expr,
},
span: v.span
}
})
});
ast::ItemKind::Enum(ast::EnumDef { variants }, generics)
}
item => item,
_ => {}
}
}
pub fn configure_expr_kind(&mut self, expr_kind: ast::ExprKind) -> ast::ExprKind {
pub fn configure_expr_kind(&mut self, expr_kind: &mut ast::ExprKind) {
match expr_kind {
ast::ExprKind::Match(m, arms) => {
let arms = arms.move_flat_map(|a| self.configure(a));
ast::ExprKind::Match(m, arms)
ast::ExprKind::Match(_m, arms) => {
arms.flat_map_in_place(|arm| self.configure(arm));
}
ast::ExprKind::Struct(path, fields, base) => {
let fields = fields.move_flat_map(|field| self.configure(field));
ast::ExprKind::Struct(path, fields, base)
ast::ExprKind::Struct(_path, fields, _base) => {
fields.flat_map_in_place(|field| self.configure(field));
}
_ => expr_kind,
_ => {}
}
}
pub fn configure_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
pub fn configure_expr(&mut self, expr: &mut P<ast::Expr>) {
self.visit_expr_attrs(expr.attrs());
// If an expr is valid to cfg away it will have been removed by the
@ -289,8 +265,8 @@ impl<'a> StripUnconfigured<'a> {
// Anything else is always required, and thus has to error out
// in case of a cfg attr.
//
// N.B., this is intentionally not part of the fold_expr() function
// in order for fold_opt_expr() to be able to avoid this check
// N.B., this is intentionally not part of the visit_expr() function
// in order for filter_map_expr() to be able to avoid this check
if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a)) {
let msg = "removing an expression is not supported in this position";
self.sess.span_diagnostic.span_err(attr.span, msg);
@ -299,14 +275,10 @@ impl<'a> StripUnconfigured<'a> {
self.process_cfg_attrs(expr)
}
pub fn configure_pat(&mut self, pattern: P<ast::Pat>) -> P<ast::Pat> {
pattern.map(|mut pattern| {
if let ast::PatKind::Struct(path, fields, etc) = pattern.node {
let fields = fields.move_flat_map(|field| self.configure(field));
pattern.node = ast::PatKind::Struct(path, fields, etc);
pub fn configure_pat(&mut self, pat: &mut P<ast::Pat>) {
if let ast::PatKind::Struct(_path, fields, _etc) = &mut pat.node {
fields.flat_map_in_place(|field| self.configure(field));
}
pattern
})
}
// deny #[cfg] on generic parameters until we decide what to do with it.
@ -326,54 +298,54 @@ impl<'a> StripUnconfigured<'a> {
}
}
impl<'a> fold::Folder for StripUnconfigured<'a> {
fn fold_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
let foreign_mod = self.configure_foreign_mod(foreign_mod);
fold::noop_fold_foreign_mod(foreign_mod, self)
impl<'a> MutVisitor for StripUnconfigured<'a> {
fn visit_foreign_mod(&mut self, foreign_mod: &mut ast::ForeignMod) {
self.configure_foreign_mod(foreign_mod);
noop_visit_foreign_mod(foreign_mod, self);
}
fn fold_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
let item = self.configure_item_kind(item);
fold::noop_fold_item_kind(item, self)
fn visit_item_kind(&mut self, item: &mut ast::ItemKind) {
self.configure_item_kind(item);
noop_visit_item_kind(item, self);
}
fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
let mut expr = self.configure_expr(expr).into_inner();
expr.node = self.configure_expr_kind(expr.node);
P(fold::noop_fold_expr(expr, self))
fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
self.configure_expr(expr);
self.configure_expr_kind(&mut expr.node);
noop_visit_expr(expr, self);
}
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
let mut expr = configure!(self, expr).into_inner();
expr.node = self.configure_expr_kind(expr.node);
Some(P(fold::noop_fold_expr(expr, self)))
fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
let mut expr = configure!(self, expr);
self.configure_expr_kind(&mut expr.node);
noop_visit_expr(&mut expr, self);
Some(expr)
}
fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
fold::noop_fold_stmt(configure!(self, stmt), self)
fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
noop_flat_map_stmt(configure!(self, stmt), self)
}
fn fold_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
fold::noop_fold_item(configure!(self, item), self)
fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
noop_flat_map_item(configure!(self, item), self)
}
fn fold_impl_item(&mut self, item: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]>
{
fold::noop_fold_impl_item(configure!(self, item), self)
fn flat_map_impl_item(&mut self, item: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> {
noop_flat_map_impl_item(configure!(self, item), self)
}
fn fold_trait_item(&mut self, item: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> {
fold::noop_fold_trait_item(configure!(self, item), self)
fn flat_map_trait_item(&mut self, item: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> {
noop_flat_map_trait_item(configure!(self, item), self)
}
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
fn visit_mac(&mut self, _mac: &mut ast::Mac) {
// Don't configure interpolated AST (cf. issue #34171).
// Interpolated AST will get configured once the surrounding tokens are parsed.
mac
}
fn fold_pat(&mut self, pattern: P<ast::Pat>) -> P<ast::Pat> {
fold::noop_fold_pat(self.configure_pat(pattern), self)
fn visit_pat(&mut self, pat: &mut P<ast::Pat>) {
self.configure_pat(pat);
noop_visit_pat(pat, self)
}
}

View File

@ -8,7 +8,7 @@ use edition::Edition;
use errors::{DiagnosticBuilder, DiagnosticId};
use ext::expand::{self, AstFragment, Invocation};
use ext::hygiene::{self, Mark, SyntaxContext, Transparency};
use fold::{self, Folder};
use mut_visit::{self, MutVisitor};
use parse::{self, parser, DirectoryOwnership};
use parse::token;
use ptr::P;
@ -47,15 +47,14 @@ impl HasAttrs for Annotatable {
}
}
fn map_attrs<F: FnOnce(Vec<Attribute>) -> Vec<Attribute>>(self, f: F) -> Self {
fn visit_attrs<F: FnOnce(&mut Vec<Attribute>)>(&mut self, f: F) {
match self {
Annotatable::Item(item) => Annotatable::Item(item.map_attrs(f)),
Annotatable::TraitItem(trait_item) => Annotatable::TraitItem(trait_item.map_attrs(f)),
Annotatable::ImplItem(impl_item) => Annotatable::ImplItem(impl_item.map_attrs(f)),
Annotatable::ForeignItem(foreign_item) =>
Annotatable::ForeignItem(foreign_item.map_attrs(f)),
Annotatable::Stmt(stmt) => Annotatable::Stmt(stmt.map_attrs(f)),
Annotatable::Expr(expr) => Annotatable::Expr(expr.map_attrs(f)),
Annotatable::Item(item) => item.visit_attrs(f),
Annotatable::TraitItem(trait_item) => trait_item.visit_attrs(f),
Annotatable::ImplItem(impl_item) => impl_item.visit_attrs(f),
Annotatable::ForeignItem(foreign_item) => foreign_item.visit_attrs(f),
Annotatable::Stmt(stmt) => stmt.visit_attrs(f),
Annotatable::Expr(expr) => expr.visit_attrs(f),
}
}
}
@ -263,24 +262,24 @@ impl<F> TTMacroExpander for F
) -> Box<dyn MacResult+'cx> {
struct AvoidInterpolatedIdents;
impl Folder for AvoidInterpolatedIdents {
fn fold_tt(&mut self, tt: tokenstream::TokenTree) -> tokenstream::TokenTree {
if let tokenstream::TokenTree::Token(_, token::Interpolated(ref nt)) = tt {
impl MutVisitor for AvoidInterpolatedIdents {
fn visit_tt(&mut self, tt: &mut tokenstream::TokenTree) {
if let tokenstream::TokenTree::Token(_, token::Interpolated(nt)) = tt {
if let token::NtIdent(ident, is_raw) = nt.0 {
return tokenstream::TokenTree::Token(ident.span,
*tt = tokenstream::TokenTree::Token(ident.span,
token::Ident(ident, is_raw));
}
}
fold::noop_fold_tt(tt, self)
mut_visit::noop_visit_tt(tt, self)
}
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
fold::noop_fold_mac(mac, self)
fn visit_mac(&mut self, mac: &mut ast::Mac) {
mut_visit::noop_visit_mac(mac, self)
}
}
let input: Vec<_> =
input.trees().map(|tt| AvoidInterpolatedIdents.fold_tt(tt)).collect();
input.trees().map(|mut tt| { AvoidInterpolatedIdents.visit_tt(&mut tt); tt }).collect();
(*self)(ecx, span, &input)
}
}
@ -981,17 +980,14 @@ impl<'a> ExtCtxt<'a> {
/// compilation on error, merely emits a non-fatal error and returns None.
pub fn expr_to_spanned_string<'a>(
cx: &'a mut ExtCtxt,
expr: P<ast::Expr>,
mut expr: P<ast::Expr>,
err_msg: &str,
) -> Result<Spanned<(Symbol, ast::StrStyle)>, Option<DiagnosticBuilder<'a>>> {
// Update `expr.span`'s ctxt now in case expr is an `include!` macro invocation.
let expr = expr.map(|mut expr| {
expr.span = expr.span.apply_mark(cx.current_expansion.mark);
expr
});
// we want to be able to handle e.g., `concat!("foo", "bar")`
let expr = cx.expander().fold_expr(expr);
cx.expander().visit_expr(&mut expr);
Err(match expr.node {
ast::ExprKind::Lit(ref l) => match l.node {
ast::LitKind::Str(s, style) => return Ok(respan(expr.span, (s, style))),
@ -1055,7 +1051,9 @@ pub fn get_exprs_from_tts(cx: &mut ExtCtxt,
let mut p = cx.new_parser_from_tts(tts);
let mut es = Vec::new();
while p.token != token::Eof {
es.push(cx.expander().fold_expr(panictry!(p.parse_expr())));
let mut expr = panictry!(p.parse_expr());
cx.expander().visit_expr(&mut expr);
es.push(expr);
if p.eat(&token::Comma) {
continue;
}

View File

@ -40,7 +40,7 @@ pub fn collect_derives(cx: &mut ExtCtxt, attrs: &mut Vec<ast::Attribute>) -> Vec
result
}
pub fn add_derived_markers<T>(cx: &mut ExtCtxt, span: Span, traits: &[ast::Path], item: T) -> T
pub fn add_derived_markers<T>(cx: &mut ExtCtxt, span: Span, traits: &[ast::Path], item: &mut T)
where T: HasAttrs,
{
let (mut names, mut pretty_name) = (FxHashSet::default(), "derive(".to_owned());
@ -64,7 +64,7 @@ pub fn add_derived_markers<T>(cx: &mut ExtCtxt, span: Span, traits: &[ast::Path]
});
let span = span.with_ctxt(cx.backtrace());
item.map_attrs(|mut attrs| {
item.visit_attrs(|attrs| {
if names.contains(&Symbol::intern("Eq")) && names.contains(&Symbol::intern("PartialEq")) {
let meta = cx.meta_word(span, Symbol::intern("structural_match"));
attrs.push(cx.attribute(span, meta));
@ -73,6 +73,5 @@ pub fn add_derived_markers<T>(cx: &mut ExtCtxt, span: Span, traits: &[ast::Path]
let meta = cx.meta_word(span, Symbol::intern("rustc_copy_clone_marker"));
attrs.push(cx.attribute(span, meta));
}
attrs
})
});
}

View File

@ -9,7 +9,7 @@ use ext::derive::{add_derived_markers, collect_derives};
use ext::hygiene::{self, Mark, SyntaxContext};
use ext::placeholders::{placeholder, PlaceholderExpander};
use feature_gate::{self, Features, GateIssue, is_builtin_attr, emit_feature_err};
use fold::*;
use mut_visit::*;
use parse::{DirectoryOwnership, PResult, ParseSess};
use parse::token::{self, Token};
use parse::parser::Parser;
@ -21,11 +21,13 @@ use syntax_pos::{Span, DUMMY_SP, FileName};
use syntax_pos::hygiene::ExpnFormat;
use tokenstream::{TokenStream, TokenTree};
use visit::{self, Visitor};
use util::map_in_place::MapInPlace;
use rustc_data_structures::fx::FxHashMap;
use std::fs;
use std::io::ErrorKind;
use std::{iter, mem};
use std::ops::DerefMut;
use std::rc::Rc;
use std::path::PathBuf;
@ -35,8 +37,8 @@ macro_rules! ast_fragments {
$kind_name:expr;
// FIXME: HACK: this should be `$(one ...)?` and `$(many ...)?` but `?` macro
// repetition was removed from 2015 edition in #51587 because of ambiguities.
$(one fn $fold_ast:ident; fn $visit_ast:ident;)*
$(many fn $fold_ast_elt:ident; fn $visit_ast_elt:ident;)*
$(one fn $mut_visit_ast:ident; fn $visit_ast:ident;)*
$(many fn $flat_map_ast_elt:ident; fn $visit_ast_elt:ident;)*
fn $make_ast:ident;
})*
) => {
@ -86,16 +88,20 @@ macro_rules! ast_fragments {
}
})*
pub fn fold_with<F: Folder>(self, folder: &mut F) -> Self {
pub fn mut_visit_with<F: MutVisitor>(&mut self, vis: &mut F) {
match self {
AstFragment::OptExpr(expr) =>
AstFragment::OptExpr(expr.and_then(|expr| folder.fold_opt_expr(expr))),
AstFragment::OptExpr(opt_expr) => {
visit_clobber(opt_expr, |opt_expr| {
if let Some(expr) = opt_expr {
vis.filter_map_expr(expr)
} else {
None
}
});
}
$($(AstFragment::$Kind(ast) => vis.$mut_visit_ast(ast),)*)*
$($(AstFragment::$Kind(ast) =>
AstFragment::$Kind(folder.$fold_ast(ast)),)*)*
$($(AstFragment::$Kind(ast) =>
AstFragment::$Kind(ast.into_iter()
.flat_map(|ast| folder.$fold_ast_elt(ast))
.collect()),)*)*
ast.flat_map_in_place(|ast| vis.$flat_map_ast_elt(ast)),)*)*
}
}
@ -111,14 +117,14 @@ macro_rules! ast_fragments {
}
}
impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
impl<'a, 'b> MutVisitor for MacroExpander<'a, 'b> {
fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
self.expand_fragment(AstFragment::OptExpr(Some(expr))).make_opt_expr()
}
$($(fn $fold_ast(&mut self, ast: $AstTy) -> $AstTy {
self.expand_fragment(AstFragment::$Kind(ast)).$make_ast()
$($(fn $mut_visit_ast(&mut self, ast: &mut $AstTy) {
visit_clobber(ast, |ast| self.expand_fragment(AstFragment::$Kind(ast)).$make_ast());
})*)*
$($(fn $fold_ast_elt(&mut self, ast_elt: <$AstTy as IntoIterator>::Item) -> $AstTy {
$($(fn $flat_map_ast_elt(&mut self, ast_elt: <$AstTy as IntoIterator>::Item) -> $AstTy {
self.expand_fragment(AstFragment::$Kind(smallvec![ast_elt])).$make_ast()
})*)*
}
@ -133,23 +139,23 @@ macro_rules! ast_fragments {
}
ast_fragments! {
Expr(P<ast::Expr>) { "expression"; one fn fold_expr; fn visit_expr; fn make_expr; }
Pat(P<ast::Pat>) { "pattern"; one fn fold_pat; fn visit_pat; fn make_pat; }
Ty(P<ast::Ty>) { "type"; one fn fold_ty; fn visit_ty; fn make_ty; }
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; }
Stmts(SmallVec<[ast::Stmt; 1]>) {
"statement"; many fn fold_stmt; fn visit_stmt; fn make_stmts;
"statement"; many fn flat_map_stmt; fn visit_stmt; fn make_stmts;
}
Items(SmallVec<[P<ast::Item>; 1]>) {
"item"; many fn fold_item; fn visit_item; fn make_items;
"item"; many fn flat_map_item; fn visit_item; fn make_items;
}
TraitItems(SmallVec<[ast::TraitItem; 1]>) {
"trait item"; many fn fold_trait_item; fn visit_trait_item; fn make_trait_items;
"trait item"; many fn flat_map_trait_item; fn visit_trait_item; fn make_trait_items;
}
ImplItems(SmallVec<[ast::ImplItem; 1]>) {
"impl item"; many fn fold_impl_item; fn visit_impl_item; fn make_impl_items;
"impl item"; many fn flat_map_impl_item; fn visit_impl_item; fn make_impl_items;
}
ForeignItems(SmallVec<[ast::ForeignItem; 1]>) {
"foreign item"; many fn fold_foreign_item; fn visit_foreign_item; fn make_foreign_items;
"foreign item"; many fn flat_map_foreign_item; fn visit_foreign_item; fn make_foreign_items;
}
}
@ -297,7 +303,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
self.cx.current_expansion.depth = 0;
// Collect all macro invocations and replace them with placeholders.
let (fragment_with_placeholders, mut invocations)
let (mut fragment_with_placeholders, mut invocations)
= self.collect_invocations(input_fragment, &[]);
// Optimization: if we resolve all imports now,
@ -369,10 +375,10 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
err.emit();
}
let item = self.fully_configure(item)
.map_attrs(|mut attrs| { attrs.retain(|a| a.path != "derive"); attrs });
let item_with_markers =
add_derived_markers(&mut self.cx, item.span(), &traits, item.clone());
let mut item = self.fully_configure(item);
item.visit_attrs(|attrs| attrs.retain(|a| a.path != "derive"));
let mut item_with_markers = item.clone();
add_derived_markers(&mut self.cx, item.span(), &traits, &mut item_with_markers);
let derives = derives.entry(invoc.expansion_data.mark).or_default();
derives.reserve(traits.len());
@ -427,7 +433,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
expanded_fragment, derives);
}
}
fragment_with_placeholders.fold_with(&mut placeholder_expander)
fragment_with_placeholders.mut_visit_with(&mut placeholder_expander);
fragment_with_placeholders
}
fn resolve_imports(&mut self) {
@ -440,12 +447,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
/// 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.
fn collect_invocations(&mut self, fragment: AstFragment, derives: &[Mark])
fn collect_invocations(&mut self, mut fragment: AstFragment, derives: &[Mark])
-> (AstFragment, Vec<Invocation>) {
// Resolve `$crate`s in the fragment for pretty-printing.
self.cx.resolver.resolve_dollar_crates(&fragment);
let (fragment_with_placeholders, invocations) = {
let invocations = {
let mut collector = InvocationCollector {
cfg: StripUnconfigured {
sess: self.cx.parse_sess,
@ -455,16 +462,16 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
invocations: Vec::new(),
monotonic: self.monotonic,
};
(fragment.fold_with(&mut collector), collector.invocations)
fragment.mut_visit_with(&mut collector);
collector.invocations
};
if self.monotonic {
self.cx.resolver.visit_ast_fragment_with_placeholders(
self.cx.current_expansion.mark, &fragment_with_placeholders, derives
);
self.cx.current_expansion.mark, &fragment, derives);
}
(fragment_with_placeholders, invocations)
(fragment, invocations)
}
fn fully_configure(&mut self, item: Annotatable) -> Annotatable {
@ -476,24 +483,25 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
// we know that fold result vector will contain exactly one element
match item {
Annotatable::Item(item) => {
Annotatable::Item(cfg.fold_item(item).pop().unwrap())
Annotatable::Item(cfg.flat_map_item(item).pop().unwrap())
}
Annotatable::TraitItem(item) => {
Annotatable::TraitItem(item.map(|item| cfg.fold_trait_item(item).pop().unwrap()))
Annotatable::TraitItem(
item.map(|item| cfg.flat_map_trait_item(item).pop().unwrap()))
}
Annotatable::ImplItem(item) => {
Annotatable::ImplItem(item.map(|item| cfg.fold_impl_item(item).pop().unwrap()))
Annotatable::ImplItem(item.map(|item| cfg.flat_map_impl_item(item).pop().unwrap()))
}
Annotatable::ForeignItem(item) => {
Annotatable::ForeignItem(
item.map(|item| cfg.fold_foreign_item(item).pop().unwrap())
item.map(|item| cfg.flat_map_foreign_item(item).pop().unwrap())
)
}
Annotatable::Stmt(stmt) => {
Annotatable::Stmt(stmt.map(|stmt| cfg.fold_stmt(stmt).pop().unwrap()))
Annotatable::Stmt(stmt.map(|stmt| cfg.flat_map_stmt(stmt).pop().unwrap()))
}
Annotatable::Expr(expr) => {
Annotatable::Expr(cfg.fold_expr(expr))
Annotatable::Expr(mut expr) => {
Annotatable::Expr({ cfg.visit_expr(&mut expr); expr })
}
}
}
@ -535,7 +543,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
invoc: Invocation,
ext: &SyntaxExtension)
-> Option<AstFragment> {
let (attr, item) = match invoc.kind {
let (attr, mut item) = match invoc.kind {
InvocationKind::Attr { attr, item, .. } => (attr?, item),
_ => unreachable!(),
};
@ -558,7 +566,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
match *ext {
NonMacroAttr { .. } => {
attr::mark_known(&attr);
let item = item.map_attrs(|mut attrs| { attrs.push(attr); attrs });
item.visit_attrs(|attrs| attrs.push(attr));
Some(invoc.fragment_kind.expect_from_annotatables(iter::once(item)))
}
MultiModifier(ref mac) => {
@ -1113,34 +1121,32 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
}
/// If `item` is an attr invocation, remove and return the macro attribute and derive traits.
fn classify_item<T>(&mut self, mut item: T)
-> (Option<ast::Attribute>, Vec<Path>, T, /* after_derive */ bool)
fn classify_item<T>(&mut self, item: &mut T)
-> (Option<ast::Attribute>, Vec<Path>, /* after_derive */ bool)
where T: HasAttrs,
{
let (mut attr, mut traits, mut after_derive) = (None, Vec::new(), false);
item = item.map_attrs(|mut attrs| {
item.visit_attrs(|mut attrs| {
attr = self.find_attr_invoc(&mut attrs, &mut after_derive);
traits = collect_derives(&mut self.cx, &mut attrs);
attrs
});
(attr, traits, item, after_derive)
(attr, traits, after_derive)
}
/// Alternative of `classify_item()` that ignores `#[derive]` so invocations fallthrough
/// Alternative to `classify_item()` that ignores `#[derive]` so invocations fallthrough
/// to the unused-attributes lint (making it an error on statements and expressions
/// is a breaking change)
fn classify_nonitem<T: HasAttrs>(&mut self, mut item: T)
-> (Option<ast::Attribute>, T, /* after_derive */ bool) {
fn classify_nonitem<T: HasAttrs>(&mut self, nonitem: &mut T)
-> (Option<ast::Attribute>, /* after_derive */ bool) {
let (mut attr, mut after_derive) = (None, false);
item = item.map_attrs(|mut attrs| {
nonitem.visit_attrs(|mut attrs| {
attr = self.find_attr_invoc(&mut attrs, &mut after_derive);
attrs
});
(attr, item, after_derive)
(attr, after_derive)
}
fn configure<T: HasAttrs>(&mut self, node: T) -> Option<T> {
@ -1173,14 +1179,14 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
}
}
impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
let expr = self.cfg.configure_expr(expr);
expr.map(|mut expr| {
expr.node = self.cfg.configure_expr_kind(expr.node);
impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
self.cfg.configure_expr(expr);
visit_clobber(expr.deref_mut(), |mut expr| {
self.cfg.configure_expr_kind(&mut expr.node);
// ignore derives so they remain unused
let (attr, expr, after_derive) = self.classify_nonitem(expr);
let (attr, after_derive) = self.classify_nonitem(&mut expr);
if attr.is_some() {
// Collect the invoc regardless of whether or not attributes are permitted here
@ -1200,18 +1206,19 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
.make_expr()
.into_inner()
} else {
noop_fold_expr(expr, self)
noop_visit_expr(&mut expr, self);
expr
}
})
});
}
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
let expr = configure!(self, expr);
expr.filter_map(|mut expr| {
expr.node = self.cfg.configure_expr_kind(expr.node);
self.cfg.configure_expr_kind(&mut expr.node);
// Ignore derives so they remain unused.
let (attr, expr, after_derive) = self.classify_nonitem(expr);
let (attr, after_derive) = self.classify_nonitem(&mut expr);
if attr.is_some() {
attr.as_ref().map(|a| self.cfg.maybe_emit_expr_attr_err(a));
@ -1228,44 +1235,45 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
.make_opt_expr()
.map(|expr| expr.into_inner())
} else {
Some(noop_fold_expr(expr, self))
Some({ noop_visit_expr(&mut expr, self); expr })
}
})
}
fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> {
let pat = self.cfg.configure_pat(pat);
fn visit_pat(&mut self, pat: &mut P<ast::Pat>) {
self.cfg.configure_pat(pat);
match pat.node {
PatKind::Mac(_) => {}
_ => return noop_fold_pat(pat, self),
_ => return noop_visit_pat(pat, self),
}
pat.and_then(|pat| match pat.node {
PatKind::Mac(mac) => self.collect_bang(mac, pat.span, AstFragmentKind::Pat).make_pat(),
visit_clobber(pat, |mut pat| {
match mem::replace(&mut pat.node, PatKind::Wild) {
PatKind::Mac(mac) =>
self.collect_bang(mac, pat.span, AstFragmentKind::Pat).make_pat(),
_ => unreachable!(),
})
}
});
}
fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
let mut stmt = configure!(self, stmt);
// we'll expand attributes on expressions separately
if !stmt.is_expr() {
let (attr, derives, stmt_, after_derive) = if stmt.is_item() {
self.classify_item(stmt)
let (attr, derives, after_derive) = if stmt.is_item() {
self.classify_item(&mut stmt)
} else {
// ignore derives on non-item statements so it falls through
// to the unused-attributes lint
let (attr, stmt, after_derive) = self.classify_nonitem(stmt);
(attr, vec![], stmt, after_derive)
let (attr, after_derive) = self.classify_nonitem(&mut stmt);
(attr, vec![], after_derive)
};
if attr.is_some() || !derives.is_empty() {
return self.collect_attr(attr, derives, Annotatable::Stmt(P(stmt_)),
return self.collect_attr(attr, derives, Annotatable::Stmt(P(stmt)),
AstFragmentKind::Stmts, after_derive).make_stmts();
}
stmt = stmt_;
}
if let StmtKind::Mac(mac) = stmt.node {
@ -1287,24 +1295,23 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
// The placeholder expander gives ids to statements, so we avoid folding the id here.
let ast::Stmt { id, node, span } = stmt;
noop_fold_stmt_kind(node, self).into_iter().map(|node| {
noop_flat_map_stmt_kind(node, self).into_iter().map(|node| {
ast::Stmt { id, node, span }
}).collect()
}
fn fold_block(&mut self, block: P<Block>) -> P<Block> {
fn visit_block(&mut self, block: &mut P<Block>) {
let old_directory_ownership = self.cx.current_expansion.directory_ownership;
self.cx.current_expansion.directory_ownership = DirectoryOwnership::UnownedViaBlock;
let result = noop_fold_block(block, self);
noop_visit_block(block, self);
self.cx.current_expansion.directory_ownership = old_directory_ownership;
result
}
fn fold_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
let item = configure!(self, item);
fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
let mut item = configure!(self, item);
let (attr, traits, item, after_derive) = self.classify_item(item);
let (attr, traits, after_derive) = self.classify_item(&mut item);
if attr.is_some() || !traits.is_empty() {
return self.collect_attr(attr, traits, Annotatable::Item(item),
AstFragmentKind::Items, after_derive).make_items();
@ -1326,7 +1333,7 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
}
ast::ItemKind::Mod(ast::Mod { inner, .. }) => {
if item.ident == keywords::Invalid.ident() {
return noop_fold_item(item, self);
return noop_flat_map_item(item, self);
}
let orig_directory_ownership = self.cx.current_expansion.directory_ownership;
@ -1366,20 +1373,20 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
let orig_module =
mem::replace(&mut self.cx.current_expansion.module, Rc::new(module));
let result = noop_fold_item(item, self);
let result = noop_flat_map_item(item, self);
self.cx.current_expansion.module = orig_module;
self.cx.current_expansion.directory_ownership = orig_directory_ownership;
result
}
_ => noop_fold_item(item, self),
_ => noop_flat_map_item(item, self),
}
}
fn fold_trait_item(&mut self, item: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> {
let item = configure!(self, item);
fn flat_map_trait_item(&mut self, item: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> {
let mut item = configure!(self, item);
let (attr, traits, item, after_derive) = self.classify_item(item);
let (attr, traits, after_derive) = self.classify_item(&mut item);
if attr.is_some() || !traits.is_empty() {
return self.collect_attr(attr, traits, Annotatable::TraitItem(P(item)),
AstFragmentKind::TraitItems, after_derive).make_trait_items()
@ -1391,14 +1398,14 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
self.check_attributes(&attrs);
self.collect_bang(mac, span, AstFragmentKind::TraitItems).make_trait_items()
}
_ => noop_fold_trait_item(item, self),
_ => noop_flat_map_trait_item(item, self),
}
}
fn fold_impl_item(&mut self, item: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> {
let item = configure!(self, item);
fn flat_map_impl_item(&mut self, item: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> {
let mut item = configure!(self, item);
let (attr, traits, item, after_derive) = self.classify_item(item);
let (attr, traits, after_derive) = self.classify_item(&mut item);
if attr.is_some() || !traits.is_empty() {
return self.collect_attr(attr, traits, Annotatable::ImplItem(P(item)),
AstFragmentKind::ImplItems, after_derive).make_impl_items();
@ -1410,30 +1417,34 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
self.check_attributes(&attrs);
self.collect_bang(mac, span, AstFragmentKind::ImplItems).make_impl_items()
}
_ => noop_fold_impl_item(item, self),
_ => noop_flat_map_impl_item(item, self),
}
}
fn fold_ty(&mut self, ty: P<ast::Ty>) -> P<ast::Ty> {
let ty = match ty.node {
ast::TyKind::Mac(_) => ty.into_inner(),
_ => return noop_fold_ty(ty, self),
fn visit_ty(&mut self, ty: &mut P<ast::Ty>) {
match ty.node {
ast::TyKind::Mac(_) => {}
_ => return noop_visit_ty(ty, self),
};
match ty.node {
ast::TyKind::Mac(mac) => self.collect_bang(mac, ty.span, AstFragmentKind::Ty).make_ty(),
visit_clobber(ty, |mut ty| {
match mem::replace(&mut ty.node, ast::TyKind::Err) {
ast::TyKind::Mac(mac) =>
self.collect_bang(mac, ty.span, AstFragmentKind::Ty).make_ty(),
_ => unreachable!(),
}
});
}
fn fold_foreign_mod(&mut self, foreign_mod: ast::ForeignMod) -> ast::ForeignMod {
noop_fold_foreign_mod(self.cfg.configure_foreign_mod(foreign_mod), self)
fn visit_foreign_mod(&mut self, foreign_mod: &mut ast::ForeignMod) {
self.cfg.configure_foreign_mod(foreign_mod);
noop_visit_foreign_mod(foreign_mod, self);
}
fn fold_foreign_item(&mut self, foreign_item: ast::ForeignItem)
fn flat_map_foreign_item(&mut self, mut foreign_item: ast::ForeignItem)
-> SmallVec<[ast::ForeignItem; 1]>
{
let (attr, traits, foreign_item, after_derive) = self.classify_item(foreign_item);
let (attr, traits, after_derive) = self.classify_item(&mut foreign_item);
if attr.is_some() || !traits.is_empty() {
return self.collect_attr(attr, traits, Annotatable::ForeignItem(P(foreign_item)),
@ -1447,38 +1458,41 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
.make_foreign_items();
}
noop_fold_foreign_item(foreign_item, self)
noop_flat_map_foreign_item(foreign_item, self)
}
fn fold_item_kind(&mut self, item: ast::ItemKind) -> ast::ItemKind {
fn visit_item_kind(&mut self, item: &mut ast::ItemKind) {
match item {
ast::ItemKind::MacroDef(..) => item,
_ => noop_fold_item_kind(self.cfg.configure_item_kind(item), self),
ast::ItemKind::MacroDef(..) => {}
_ => {
self.cfg.configure_item_kind(item);
noop_visit_item_kind(item, self);
}
}
}
fn fold_generic_param(&mut self, param: ast::GenericParam) -> ast::GenericParam {
fn visit_generic_param(&mut self, param: &mut ast::GenericParam) {
self.cfg.disallow_cfg_on_generic_param(&param);
noop_fold_generic_param(param, self)
noop_visit_generic_param(param, self)
}
fn fold_attribute(&mut self, at: ast::Attribute) -> ast::Attribute {
fn visit_attribute(&mut self, at: &mut ast::Attribute) {
// turn `#[doc(include="filename")]` attributes into `#[doc(include(file="filename",
// contents="file contents")]` attributes
if !at.check_name("doc") {
return noop_fold_attribute(at, self);
return noop_visit_attribute(at, self);
}
if let Some(list) = at.meta_item_list() {
if !list.iter().any(|it| it.check_name("include")) {
return noop_fold_attribute(at, self);
return noop_visit_attribute(at, self);
}
let mut items = vec![];
for it in list {
for mut it in list {
if !it.check_name("include") {
items.push(noop_fold_meta_list_item(it, self));
items.push({ noop_visit_meta_list_item(&mut it, self); it });
continue;
}
@ -1487,7 +1501,7 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
self.check_attribute(&at);
if self.cx.parse_sess.span_diagnostic.err_count() > err_count {
// avoid loading the file if they haven't enabled the feature
return noop_fold_attribute(at, self);
return noop_visit_attribute(at, self);
}
let filename = self.cx.root_path.join(file.to_string());
@ -1582,20 +1596,18 @@ impl<'a, 'b> Folder for InvocationCollector<'a, 'b> {
let meta = attr::mk_list_item(DUMMY_SP, Ident::from_str("doc"), items);
match at.style {
ast::AttrStyle::Inner => attr::mk_spanned_attr_inner(at.span, at.id, meta),
ast::AttrStyle::Outer => attr::mk_spanned_attr_outer(at.span, at.id, meta),
ast::AttrStyle::Inner => *at = attr::mk_spanned_attr_inner(at.span, at.id, meta),
ast::AttrStyle::Outer => *at = attr::mk_spanned_attr_outer(at.span, at.id, meta),
}
} else {
noop_fold_attribute(at, self)
noop_visit_attribute(at, self)
}
}
fn new_id(&mut self, id: ast::NodeId) -> ast::NodeId {
fn visit_id(&mut self, id: &mut ast::NodeId) {
if self.monotonic {
assert_eq!(id, ast::DUMMY_NODE_ID);
self.cx.resolver.next_node_id()
} else {
id
debug_assert_eq!(*id, ast::DUMMY_NODE_ID);
*id = self.cx.resolver.next_node_id()
}
}
}
@ -1660,12 +1672,12 @@ impl<'feat> ExpansionConfig<'feat> {
#[derive(Debug)]
pub struct Marker(pub Mark);
impl Folder for Marker {
fn new_span(&mut self, span: Span) -> Span {
span.apply_mark(self.0)
impl MutVisitor for Marker {
fn visit_span(&mut self, span: &mut Span) {
*span = span.apply_mark(self.0)
}
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
noop_fold_mac(mac, self)
fn visit_mac(&mut self, mac: &mut ast::Mac) {
noop_visit_mac(mac, self)
}
}

View File

@ -4,12 +4,11 @@ use ext::base::ExtCtxt;
use ext::expand::{AstFragment, AstFragmentKind};
use ext::hygiene::Mark;
use tokenstream::TokenStream;
use fold::*;
use mut_visit::*;
use ptr::P;
use smallvec::SmallVec;
use symbol::keywords;
use ThinVec;
use util::move_map::MoveMap;
use rustc_data_structures::fx::FxHashMap;
@ -85,8 +84,8 @@ impl<'a, 'b> PlaceholderExpander<'a, 'b> {
}
}
pub fn add(&mut self, id: ast::NodeId, fragment: AstFragment, derives: Vec<Mark>) {
let mut fragment = fragment.fold_with(self);
pub fn add(&mut self, id: ast::NodeId, mut fragment: AstFragment, derives: Vec<Mark>) {
fragment.mut_visit_with(self);
if let AstFragment::Items(mut items) = fragment {
for derive in derives {
match self.remove(NodeId::placeholder_from_mark(derive)) {
@ -104,56 +103,56 @@ impl<'a, 'b> PlaceholderExpander<'a, 'b> {
}
}
impl<'a, 'b> Folder for PlaceholderExpander<'a, 'b> {
fn fold_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
impl<'a, 'b> MutVisitor for PlaceholderExpander<'a, 'b> {
fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
match item.node {
ast::ItemKind::Mac(_) => return self.remove(item.id).make_items(),
ast::ItemKind::MacroDef(_) => return smallvec![item],
_ => {}
}
noop_fold_item(item, self)
noop_flat_map_item(item, self)
}
fn fold_trait_item(&mut self, item: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> {
fn flat_map_trait_item(&mut self, item: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> {
match item.node {
ast::TraitItemKind::Macro(_) => self.remove(item.id).make_trait_items(),
_ => noop_fold_trait_item(item, self),
_ => noop_flat_map_trait_item(item, self),
}
}
fn fold_impl_item(&mut self, item: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> {
fn flat_map_impl_item(&mut self, item: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> {
match item.node {
ast::ImplItemKind::Macro(_) => self.remove(item.id).make_impl_items(),
_ => noop_fold_impl_item(item, self),
_ => noop_flat_map_impl_item(item, self),
}
}
fn fold_foreign_item(&mut self, item: ast::ForeignItem) -> SmallVec<[ast::ForeignItem; 1]> {
fn flat_map_foreign_item(&mut self, item: ast::ForeignItem) -> SmallVec<[ast::ForeignItem; 1]> {
match item.node {
ast::ForeignItemKind::Macro(_) => self.remove(item.id).make_foreign_items(),
_ => noop_fold_foreign_item(item, self),
_ => noop_flat_map_foreign_item(item, self),
}
}
fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
match expr.node {
ast::ExprKind::Mac(_) => self.remove(expr.id).make_expr(),
_ => expr.map(|expr| noop_fold_expr(expr, self)),
ast::ExprKind::Mac(_) => *expr = self.remove(expr.id).make_expr(),
_ => noop_visit_expr(expr, self),
}
}
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
match expr.node {
ast::ExprKind::Mac(_) => self.remove(expr.id).make_opt_expr(),
_ => noop_fold_opt_expr(expr, self),
_ => noop_filter_map_expr(expr, self),
}
}
fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
let (style, mut stmts) = match stmt.node {
ast::StmtKind::Mac(mac) => (mac.1, self.remove(stmt.id).make_stmts()),
_ => return noop_fold_stmt(stmt, self),
_ => return noop_flat_map_stmt(stmt, self),
};
if style == ast::MacStmtStyle::Semicolon {
@ -165,44 +164,40 @@ impl<'a, 'b> Folder for PlaceholderExpander<'a, 'b> {
stmts
}
fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> {
fn visit_pat(&mut self, pat: &mut P<ast::Pat>) {
match pat.node {
ast::PatKind::Mac(_) => self.remove(pat.id).make_pat(),
_ => noop_fold_pat(pat, self),
ast::PatKind::Mac(_) => *pat = self.remove(pat.id).make_pat(),
_ => noop_visit_pat(pat, self),
}
}
fn fold_ty(&mut self, ty: P<ast::Ty>) -> P<ast::Ty> {
fn visit_ty(&mut self, ty: &mut P<ast::Ty>) {
match ty.node {
ast::TyKind::Mac(_) => self.remove(ty.id).make_ty(),
_ => noop_fold_ty(ty, self),
ast::TyKind::Mac(_) => *ty = self.remove(ty.id).make_ty(),
_ => noop_visit_ty(ty, self),
}
}
fn fold_block(&mut self, block: P<ast::Block>) -> P<ast::Block> {
noop_fold_block(block, self).map(|mut block| {
block.stmts = block.stmts.move_map(|mut stmt| {
fn visit_block(&mut self, block: &mut P<ast::Block>) {
noop_visit_block(block, self);
for stmt in block.stmts.iter_mut() {
if self.monotonic {
assert_eq!(stmt.id, ast::DUMMY_NODE_ID);
stmt.id = self.cx.resolver.next_node_id();
}
stmt
});
block
})
}
}
fn fold_mod(&mut self, module: ast::Mod) -> ast::Mod {
let mut module = noop_fold_mod(module, self);
fn visit_mod(&mut self, module: &mut ast::Mod) {
noop_visit_mod(module, self);
module.items.retain(|item| match item.node {
ast::ItemKind::Mac(_) if !self.cx.ecfg.keep_macs => false, // remove macro definitions
_ => true,
});
module
}
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
mac
fn visit_mac(&mut self, _mac: &mut ast::Mac) {
// Do nothing.
}
}

View File

@ -3,7 +3,7 @@ use ext::base::ExtCtxt;
use ext::expand::Marker;
use ext::tt::macro_parser::{NamedMatch, MatchedSeq, MatchedNonterminal};
use ext::tt::quoted;
use fold::noop_fold_tt;
use mut_visit::noop_visit_tt;
use parse::token::{self, Token, NtTT};
use smallvec::SmallVec;
use syntax_pos::DUMMY_SP;
@ -170,7 +170,9 @@ pub fn transcribe(cx: &ExtCtxt,
}
quoted::TokenTree::Token(sp, tok) => {
let mut marker = Marker(cx.current_expansion.mark);
result.push(noop_fold_tt(TokenTree::Token(sp, tok), &mut marker).into())
let mut tt = TokenTree::Token(sp, tok);
noop_visit_tt(&mut tt, &mut marker);
result.push(tt.into());
}
quoted::TokenTree::MetaVarDecl(..) => panic!("unexpected `TokenTree::MetaVarDecl"),
}

File diff suppressed because it is too large Load Diff

View File

@ -133,7 +133,7 @@ pub mod util {
pub mod parser;
#[cfg(test)]
pub mod parser_testing;
pub mod move_map;
pub mod map_in_place;
}
pub mod json;
@ -151,7 +151,7 @@ pub mod source_map;
pub mod config;
pub mod entry;
pub mod feature_gate;
pub mod fold;
#[path="fold.rs"] pub mod mut_visit; // temporary
pub mod parse;
pub mod ptr;
pub mod show_span;

View File

@ -7046,7 +7046,8 @@ impl<'a> Parser<'a> {
sess: self.sess,
features: None, // don't perform gated feature checking
};
let outer_attrs = strip_unconfigured.process_cfg_attrs(outer_attrs.to_owned());
let mut outer_attrs = outer_attrs.to_owned();
strip_unconfigured.process_cfg_attrs(&mut outer_attrs);
(!self.cfg_mods || strip_unconfigured.in_cfg(&outer_attrs), outer_attrs)
};

View File

@ -735,7 +735,7 @@ impl fmt::Debug for LazyTokenStream {
}
impl LazyTokenStream {
fn new() -> Self {
pub fn new() -> Self {
LazyTokenStream(Lock::new(None))
}

View File

@ -20,10 +20,9 @@ use ext::base::{ExtCtxt, Resolver};
use ext::build::AstBuilder;
use ext::expand::ExpansionConfig;
use ext::hygiene::{self, Mark, SyntaxContext};
use fold::Folder;
use mut_visit::{*, ExpectOne};
use feature_gate::Features;
use util::move_map::MoveMap;
use fold::{self, ExpectOne};
use util::map_in_place::MapInPlace;
use parse::{token, ParseSess};
use print::pprust;
use ast::{self, Ident};
@ -57,9 +56,9 @@ struct TestCtxt<'a> {
pub fn modify_for_testing(sess: &ParseSess,
resolver: &mut dyn Resolver,
should_test: bool,
krate: ast::Crate,
krate: &mut ast::Crate,
span_diagnostic: &errors::Handler,
features: &Features) -> ast::Crate {
features: &Features) {
// Check for #[reexport_test_harness_main = "some_name"] which
// creates a `use __test::main as some_name;`. This needs to be
// unconditional, so that the attribute is still marked as used in
@ -75,8 +74,6 @@ pub fn modify_for_testing(sess: &ParseSess,
if should_test {
generate_test_harness(sess, resolver, reexport_test_harness_main,
krate, span_diagnostic, features, test_runner)
} else {
krate
}
}
@ -88,21 +85,20 @@ struct TestHarnessGenerator<'a> {
tested_submods: Vec<(Ident, Ident)>,
}
impl<'a> fold::Folder for TestHarnessGenerator<'a> {
fn fold_crate(&mut self, c: ast::Crate) -> ast::Crate {
let mut folded = fold::noop_fold_crate(c, self);
impl<'a> MutVisitor for TestHarnessGenerator<'a> {
fn visit_crate(&mut self, c: &mut ast::Crate) {
noop_visit_crate(c, self);
// Create a main function to run our tests
let test_main = {
let unresolved = mk_main(&mut self.cx);
self.cx.ext_cx.monotonic_expander().fold_item(unresolved).pop().unwrap()
self.cx.ext_cx.monotonic_expander().flat_map_item(unresolved).pop().unwrap()
};
folded.module.items.push(test_main);
folded
c.module.items.push(test_main);
}
fn fold_item(&mut self, i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
fn flat_map_item(&mut self, i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
let ident = i.ident;
if ident.name != keywords::Invalid.name() {
self.cx.path.push(ident);
@ -123,16 +119,16 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
// We don't want to recurse into anything other than mods, since
// mods or tests inside of functions will break things
if let ast::ItemKind::Mod(module) = item.node {
if let ast::ItemKind::Mod(mut module) = item.node {
let tests = mem::replace(&mut self.tests, Vec::new());
let tested_submods = mem::replace(&mut self.tested_submods, Vec::new());
let mut mod_folded = fold::noop_fold_mod(module, self);
noop_visit_mod(&mut module, self);
let tests = mem::replace(&mut self.tests, tests);
let tested_submods = mem::replace(&mut self.tested_submods, tested_submods);
if !tests.is_empty() || !tested_submods.is_empty() {
let (it, sym) = mk_reexport_mod(&mut self.cx, item.id, tests, tested_submods);
mod_folded.items.push(it);
module.items.push(it);
if !self.cx.path.is_empty() {
self.tested_submods.push((self.cx.path[self.cx.path.len()-1], sym));
@ -141,7 +137,7 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
self.cx.toplevel_reexport = Some(sym);
}
}
item.node = ast::ItemKind::Mod(mod_folded);
item.node = ast::ItemKind::Mod(module);
}
if ident.name != keywords::Invalid.name() {
self.cx.path.pop();
@ -149,7 +145,9 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
smallvec![P(item)]
}
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac { mac }
fn visit_mac(&mut self, _mac: &mut ast::Mac) {
// Do nothing.
}
}
/// A folder used to remove any entry points (like fn main) because the harness
@ -159,20 +157,20 @@ struct EntryPointCleaner {
depth: usize,
}
impl fold::Folder for EntryPointCleaner {
fn fold_item(&mut self, i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
impl MutVisitor for EntryPointCleaner {
fn flat_map_item(&mut self, i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
self.depth += 1;
let folded = fold::noop_fold_item(i, self).expect_one("noop did something");
let item = noop_flat_map_item(i, self).expect_one("noop did something");
self.depth -= 1;
// Remove any #[main] or #[start] from the AST so it doesn't
// clash with the one we're going to add, but mark it as
// #[allow(dead_code)] to avoid printing warnings.
let folded = match entry::entry_point_type(&folded, self.depth) {
let item = match entry::entry_point_type(&item, self.depth) {
EntryPointType::MainNamed |
EntryPointType::MainAttr |
EntryPointType::Start =>
folded.map(|ast::Item {id, ident, attrs, node, vis, span, tokens}| {
item.map(|ast::Item {id, ident, attrs, node, vis, span, tokens}| {
let allow_ident = Ident::from_str("allow");
let dc_nested = attr::mk_nested_word_item(Ident::from_str("dead_code"));
let allow_dead_code_item = attr::mk_list_item(DUMMY_SP, allow_ident,
@ -197,13 +195,15 @@ impl fold::Folder for EntryPointCleaner {
}
}),
EntryPointType::None |
EntryPointType::OtherMain => folded,
EntryPointType::OtherMain => item,
};
smallvec![folded]
smallvec![item]
}
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac { mac }
fn visit_mac(&mut self, _mac: &mut ast::Mac) {
// Do nothing.
}
}
/// Creates an item (specifically a module) that "pub use"s the tests passed in.
@ -235,7 +235,7 @@ fn mk_reexport_mod(cx: &mut TestCtxt,
let sym = Ident::with_empty_ctxt(Symbol::gensym("__test_reexports"));
let parent = if parent == ast::DUMMY_NODE_ID { ast::CRATE_NODE_ID } else { parent };
cx.ext_cx.current_expansion.mark = cx.ext_cx.resolver.get_module_scope(parent);
let it = cx.ext_cx.monotonic_expander().fold_item(P(ast::Item {
let it = cx.ext_cx.monotonic_expander().flat_map_item(P(ast::Item {
ident: sym,
attrs: Vec::new(),
id: ast::DUMMY_NODE_ID,
@ -252,13 +252,13 @@ fn mk_reexport_mod(cx: &mut TestCtxt,
fn generate_test_harness(sess: &ParseSess,
resolver: &mut dyn Resolver,
reexport_test_harness_main: Option<Symbol>,
krate: ast::Crate,
krate: &mut ast::Crate,
sd: &errors::Handler,
features: &Features,
test_runner: Option<ast::Path>) -> ast::Crate {
test_runner: Option<ast::Path>) {
// Remove the entry points
let mut cleaner = EntryPointCleaner { depth: 0 };
let krate = cleaner.fold_crate(krate);
cleaner.visit_crate(krate);
let mark = Mark::fresh(Mark::root());
@ -293,7 +293,7 @@ fn generate_test_harness(sess: &ParseSess,
cx,
tests: Vec::new(),
tested_submods: Vec::new(),
}.fold_crate(krate)
}.visit_crate(krate);
}
/// Craft a span that will be ignored by the stability lint's

View File

@ -147,7 +147,7 @@ impl TokenTree {
/// empty stream is represented with `None`; it may be represented as a `Some`
/// around an empty `Vec`.
#[derive(Clone, Debug)]
pub struct TokenStream(Option<Lrc<Vec<TreeAndJoint>>>);
pub struct TokenStream(pub Option<Lrc<Vec<TreeAndJoint>>>);
pub type TreeAndJoint = (TokenTree, IsJoint);

View File

@ -1,18 +1,18 @@
use std::ptr;
use smallvec::{Array, SmallVec};
pub trait MoveMap<T>: Sized {
fn move_map<F>(self, mut f: F) -> Self where F: FnMut(T) -> T {
self.move_flat_map(|e| Some(f(e)))
pub trait MapInPlace<T>: Sized {
fn map_in_place<F>(&mut self, mut f: F) where F: FnMut(T) -> T {
self.flat_map_in_place(|e| Some(f(e)))
}
fn move_flat_map<F, I>(self, f: F) -> Self
fn flat_map_in_place<F, I>(&mut self, f: F)
where F: FnMut(T) -> I,
I: IntoIterator<Item=T>;
}
impl<T> MoveMap<T> for Vec<T> {
fn move_flat_map<F, I>(mut self, mut f: F) -> Self
impl<T> MapInPlace<T> for Vec<T> {
fn flat_map_in_place<F, I>(&mut self, mut f: F)
where F: FnMut(T) -> I,
I: IntoIterator<Item=T>
{
@ -53,22 +53,11 @@ impl<T> MoveMap<T> for Vec<T> {
// write_i tracks the number of actually written new items.
self.set_len(write_i);
}
self
}
}
impl<T> MoveMap<T> for ::ptr::P<[T]> {
fn move_flat_map<F, I>(self, f: F) -> Self
where F: FnMut(T) -> I,
I: IntoIterator<Item=T>
{
::ptr::P::from_vec(self.into_vec().move_flat_map(f))
}
}
impl<T, A: Array<Item = T>> MoveMap<T> for SmallVec<A> {
fn move_flat_map<F, I>(mut self, mut f: F) -> Self
impl<T, A: Array<Item = T>> MapInPlace<T> for SmallVec<A> {
fn flat_map_in_place<F, I>(&mut self, mut f: F)
where F: FnMut(T) -> I,
I: IntoIterator<Item=T>
{
@ -109,7 +98,5 @@ impl<T, A: Array<Item = T>> MoveMap<T> for SmallVec<A> {
// write_i tracks the number of actually written new items.
self.set_len(write_i);
}
self
}
}

View File

@ -189,7 +189,7 @@ use syntax::attr;
use syntax::ext::base::{Annotatable, ExtCtxt};
use syntax::ext::build::AstBuilder;
use syntax::source_map::{self, respan};
use syntax::util::move_map::MoveMap;
use syntax::util::map_in_place::MapInPlace;
use syntax::ptr::P;
use syntax::symbol::{Symbol, keywords};
use syntax::parse::ParseSess;
@ -1184,7 +1184,7 @@ impl<'a> MethodDef<'a> {
enum_def: &'b EnumDef,
type_attrs: &[ast::Attribute],
type_ident: Ident,
self_args: Vec<P<Expr>>,
mut self_args: Vec<P<Expr>>,
nonself_args: &[P<Expr>])
-> P<Expr> {
let sp = trait_.span;
@ -1417,8 +1417,8 @@ impl<'a> MethodDef<'a> {
// them when they are fed as r-values into a tuple
// expression; here add a layer of borrowing, turning
// `(*self, *__arg_0, ...)` into `(&*self, &*__arg_0, ...)`.
let borrowed_self_args = self_args.move_map(|self_arg| cx.expr_addr_of(sp, self_arg));
let match_arg = cx.expr(sp, ast::ExprKind::Tup(borrowed_self_args));
self_args.map_in_place(|self_arg| cx.expr_addr_of(sp, self_arg));
let match_arg = cx.expr(sp, ast::ExprKind::Tup(self_args));
// Lastly we create an expression which branches on all discriminants being equal
// if discriminant_test {
@ -1494,8 +1494,8 @@ impl<'a> MethodDef<'a> {
// them when they are fed as r-values into a tuple
// expression; here add a layer of borrowing, turning
// `(*self, *__arg_0, ...)` into `(&*self, &*__arg_0, ...)`.
let borrowed_self_args = self_args.move_map(|self_arg| cx.expr_addr_of(sp, self_arg));
let match_arg = cx.expr(sp, ast::ExprKind::Tup(borrowed_self_args));
self_args.map_in_place(|self_arg| cx.expr_addr_of(sp, self_arg));
let match_arg = cx.expr(sp, ast::ExprKind::Tup(self_args));
cx.expr_match(sp, match_arg, match_arms)
}
}

View File

@ -9,7 +9,7 @@ use syntax::ext::base::ExtCtxt;
use syntax::ext::build::AstBuilder;
use syntax::ext::expand::ExpansionConfig;
use syntax::ext::hygiene::Mark;
use syntax::fold::Folder;
use syntax::mut_visit::MutVisitor;
use syntax::parse::ParseSess;
use syntax::ptr::P;
use syntax::symbol::Symbol;
@ -412,5 +412,5 @@ fn mk_decls(
i
});
cx.monotonic_expander().fold_item(module).pop().unwrap()
cx.monotonic_expander().flat_map_item(module).pop().unwrap()
}

View File

@ -27,7 +27,7 @@ use rustc_data_structures::thin_vec::ThinVec;
use syntax::ast::*;
use syntax::source_map::{Spanned, DUMMY_SP, FileName};
use syntax::source_map::FilePathMapping;
use syntax::fold::{self, Folder};
use syntax::mut_visit::{self, MutVisitor, visit_clobber};
use syntax::parse::{self, ParseSess};
use syntax::print::pprust;
use syntax::ptr::P;
@ -157,32 +157,34 @@ fn iter_exprs(depth: usize, f: &mut FnMut(P<Expr>)) {
// Folders for manipulating the placement of `Paren` nodes. See below for why this is needed.
/// Folder that removes all `ExprKind::Paren` nodes.
/// MutVisitor that removes all `ExprKind::Paren` nodes.
struct RemoveParens;
impl Folder for RemoveParens {
fn fold_expr(&mut self, e: P<Expr>) -> P<Expr> {
let e = match e.node {
ExprKind::Paren(ref inner) => inner.clone(),
_ => e.clone(),
impl MutVisitor for RemoveParens {
fn visit_expr(&mut self, e: &mut P<Expr>) {
match e.node.clone() {
ExprKind::Paren(inner) => *e = inner,
_ => {}
};
e.map(|e| fold::noop_fold_expr(e, self))
mut_visit::noop_visit_expr(e, self);
}
}
/// Folder that inserts `ExprKind::Paren` nodes around every `Expr`.
/// MutVisitor that inserts `ExprKind::Paren` nodes around every `Expr`.
struct AddParens;
impl Folder for AddParens {
fn fold_expr(&mut self, e: P<Expr>) -> P<Expr> {
let e = e.map(|e| fold::noop_fold_expr(e, self));
impl MutVisitor for AddParens {
fn visit_expr(&mut self, e: &mut P<Expr>) {
mut_visit::noop_visit_expr(e, self);
visit_clobber(e, |e| {
P(Expr {
id: DUMMY_NODE_ID,
node: ExprKind::Paren(e),
span: DUMMY_SP,
attrs: ThinVec::new(),
})
});
}
}
@ -193,13 +195,13 @@ fn main() {
fn run() {
let ps = ParseSess::new(FilePathMapping::empty());
iter_exprs(2, &mut |e| {
iter_exprs(2, &mut |mut e| {
// If the pretty printer is correct, then `parse(print(e))` should be identical to `e`,
// modulo placement of `Paren` nodes.
let printed = pprust::expr_to_string(&e);
println!("printed: {}", printed);
let parsed = parse_expr(&ps, &printed);
let mut parsed = parse_expr(&ps, &printed);
// We want to know if `parsed` is structurally identical to `e`, ignoring trivial
// differences like placement of `Paren`s or the exact ranges of node spans.
@ -207,10 +209,12 @@ fn run() {
// everywhere we can, then pretty-print. This should give an unambiguous representation of
// each `Expr`, and it bypasses nearly all of the parenthesization logic, so we aren't
// relying on the correctness of the very thing we're testing.
let e1 = AddParens.fold_expr(RemoveParens.fold_expr(e));
let text1 = pprust::expr_to_string(&e1);
let e2 = AddParens.fold_expr(RemoveParens.fold_expr(parsed));
let text2 = pprust::expr_to_string(&e2);
RemoveParens.visit_expr(&mut e);
AddParens.visit_expr(&mut e);
let text1 = pprust::expr_to_string(&e);
RemoveParens.visit_expr(&mut parsed);
AddParens.visit_expr(&mut parsed);
let text2 = pprust::expr_to_string(&parsed);
assert!(text1 == text2,
"exprs are not equal:\n e = {:?}\n parsed = {:?}",
text1, text2);

View File

@ -30,12 +30,12 @@ fn main() {
#[derive(Debug)] //~ WARN unused attribute
let _ = "Hello, world!";
// fold_expr
// visit_expr
let _ = #[derive(Debug)] "Hello, world!";
//~^ WARN unused attribute
let _ = [
// fold_opt_expr
// filter_map_expr
#[derive(Debug)] //~ WARN unused attribute
"Hello, world!"
];