Skip to content

Commit

Permalink
Apply dtolnay fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gnzlbg committed Jan 29, 2017
1 parent 0c00ea3 commit 884133e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 61 deletions.
79 changes: 19 additions & 60 deletions src/fold.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
// Adapted from libsyntax.

//! A Folder represents an AST->AST fold; it accepts an AST piece,
//! and returns a piece of the same type. So, for instance, macro
//! expansion is a Folder that walks over an AST and produces another
//! AST.
//!
//! **Note**: using a `Folder` on an AST before macro expansion might be
//! problematic. For example, a folder renaming item names in a module will miss
//! all of those that are created by the expansion of a macro. Instead, _first_
//! apply the [`MacExpander`]() `Folder` to expand all the macros (and obtain an
//! AST without macros), and then apply your `Folder` on that.
//!
//! **Warning**: To catch this _very common_ mistake, the default `Folder`
//! implementation **panics by default on unexpanded macros!** (see
//! `Folder::fold_mac`). To fold an AST with unexpanded macros, like when
//! implementing the [`MacExpander`]() folder, override the `Folder::fold_mac`
//! implementation, for example, using `noop_fold_mac`.
//! and returns a piece of the same type.

use super::*;

Expand Down Expand Up @@ -54,24 +40,17 @@ pub trait Folder: Sized {
fn fold_ty_param_bound(&mut self, bound: TyParamBound) -> TyParamBound {
noop_fold_ty_param_bound(self, bound)
}
fn fold_poly_trait_ref(&mut self,
trait_ref: PolyTraitRef,
modifier: TraitBoundModifier)
-> PolyTraitRef {
noop_fold_poly_trait_ref(self, trait_ref, modifier)
fn fold_poly_trait_ref(&mut self, trait_ref: PolyTraitRef) -> PolyTraitRef {
noop_fold_poly_trait_ref(self, trait_ref)
}
fn fold_variant_data(&mut self,
data: VariantData,
_ident: Ident,
_generics: Generics)
-> VariantData {
noop_fold_variant_data(self, data, _ident, _generics)
fn fold_variant_data(&mut self, data: VariantData) -> VariantData {
noop_fold_variant_data(self, data)
}
fn fold_field(&mut self, field: Field) -> Field {
noop_fold_field(self, field)
}
fn fold_variant(&mut self, variant: Variant, generics: Generics) -> Variant {
noop_fold_variant(self, variant, generics)
fn fold_variant(&mut self, variant: Variant) -> Variant {
noop_fold_variant(self, variant)
}
fn fold_lifetime(&mut self, _lifetime: Lifetime) -> Lifetime {
noop_fold_lifetime(self, _lifetime)
Expand Down Expand Up @@ -104,21 +83,8 @@ pub trait Folder: Sized {
noop_fold_lit(self, _lit)
}

fn fold_mac(&mut self, mac: Mac) -> Mac { panic!("fold_mac disabled by
default!\n Using a Folder on an AST before macro expansion is not
typically what you want, since a Folder renaming item names in a module
will miss all of those that are created by the expansion of a macro. If
you have an AST with unexpanded macros, first use the `MacroExpander`
Folder to expand all the macros, and then apply your Folder on the
resulting AST.\n If you really want to implement a folder that works on
ASTs containing unexpanded macros (for example if you are implementing
something like the `MacroExpander` folder), override the default
`Folder::fold_mac` implementation using, e.g., the
`folder::noop_fold_mac` function.");
// If you really want a folder that works on macros, use this definition
// in your trait impl to override the `fold_mac` method:
//
//noop_fold_mac(self, mac)
fn fold_mac(&mut self, mac: Mac) -> Mac {
noop_fold_mac(self, mac)
}

#[cfg(feature = "full")]
Expand Down Expand Up @@ -161,8 +127,6 @@ pub trait Folder: Sized {
fn fold_stmt(&mut self, stmt: Stmt) -> Stmt {
noop_fold_stmt(self, stmt)
}

/// Prefer overriding fold_stmt instead!
#[cfg(feature = "full")]
fn fold_block(&mut self, block: Block) -> Block {
noop_fold_block(self, block)
Expand Down Expand Up @@ -220,15 +184,15 @@ pub fn noop_fold_derive_input<F: Folder>(folder: &mut F,
body }: DeriveInput) -> DeriveInput {
use self::Body::*;
DeriveInput {
ident: folder.fold_ident(ident.clone()),
ident: folder.fold_ident(ident),
vis: noop_fold_vis(folder, vis),
attrs: attrs.lift(|a| folder.fold_attribute(a)),
generics: folder.fold_generics(generics.clone()),
generics: folder.fold_generics(generics),
body: match body {
Enum(variants) => {
Enum(variants.lift(move |v| folder.fold_variant(v, generics.clone())))
Enum(variants.lift(move |v| folder.fold_variant(v)))
}
Struct(variant_data) => Struct(folder.fold_variant_data(variant_data, ident, generics)),
Struct(variant_data) => Struct(folder.fold_variant_data(variant_data)),
},
}
}
Expand Down Expand Up @@ -349,15 +313,13 @@ pub fn noop_fold_generics<F: Folder>(folder: &mut F,
pub fn noop_fold_ty_param_bound<F: Folder>(folder: &mut F, bound: TyParamBound) -> TyParamBound {
use self::TyParamBound::*;
match bound {
Trait(ty, modifier) => Trait(folder.fold_poly_trait_ref(ty, modifier), modifier),
Trait(ty, modifier) => Trait(folder.fold_poly_trait_ref(ty), modifier),
Region(lifetime) => Region(folder.fold_lifetime(lifetime)),
}
}

// -
pub fn noop_fold_poly_trait_ref<F: Folder>(folder: &mut F,
trait_ref: PolyTraitRef,
_: TraitBoundModifier)
pub fn noop_fold_poly_trait_ref<F: Folder>(folder: &mut F, trait_ref: PolyTraitRef)
-> PolyTraitRef {
PolyTraitRef {
bound_lifetimes: trait_ref.bound_lifetimes
Expand All @@ -368,9 +330,7 @@ pub fn noop_fold_poly_trait_ref<F: Folder>(folder: &mut F,

// -
pub fn noop_fold_variant_data<F: Folder>(folder: &mut F,
data: VariantData,
_ident: Ident,
_generics: Generics)
data: VariantData)
-> VariantData {
use self::VariantData::*;
match data {
Expand All @@ -392,13 +352,12 @@ pub fn noop_fold_field<F: Folder>(folder: &mut F, field: Field) -> Field {

// -
pub fn noop_fold_variant<F: Folder>(folder: &mut F,
Variant { ident, attrs, data, discriminant }: Variant,
generics: Generics)
Variant { ident, attrs, data, discriminant }: Variant)
-> Variant {
Variant {
ident: folder.fold_ident(ident.clone()),
ident: folder.fold_ident(ident),
attrs: attrs.lift(|v| folder.fold_attribute(v)),
data: folder.fold_variant_data(data, ident, generics),
data: folder.fold_variant_data(data),
discriminant: discriminant.map(|ce| folder.fold_const_expr(ce)),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub use ty::{Abi, AngleBracketedParameterData, BareFnArg, BareFnTy, FunctionRetT
#[cfg(feature = "visit")]
pub mod visit;

#[cfg(feature = "fold")]
//#[cfg(feature = "fold")]
pub mod fold;

#[cfg(feature = "parsing")]
Expand Down

0 comments on commit 884133e

Please sign in to comment.