Skip to content

Commit

Permalink
auto merge of #15999 : Kimundi/rust/fix_folder, r=nikomatsakis
Browse files Browse the repository at this point in the history
Note: This PR is motivated by an attempt to write an custom syntax extension that tried to use `syntax::fold`, and that could only do so by fixing bugs in it and copying out private functions.

---

Refactored `syntax::fold`

Prior to this, the code there had a few issues:

- Default implementations inconsistenly either had the prefix `noop_` or
  not.
- Some default methods where implemented in terms of a public noop function
  for user code to call, others where implemented directly on the trait
  and did not allow users of the trait to reuse the code.
- Some of the default implementations where private, and thus not reusable
  for other implementors.
- There where some bugs where default implemntations called other default
  implementations directly, rather than to the underlying Folder, with the
  result of some ast nodes never being visted even if the user implemented that
  method. (For example, the current Folder never folded struct fields)

This commit solves this situation somewhat radically by making __all__
`fold_...` functions in the module into Folder methods, and implementing
them all in terms of public `noop_...` functions for other implementors to
call out to.

Some public functions had to be renamed to fit the new system, so this is a
breaking change.

---

Also added a few trait implementations to `ast` types
  • Loading branch information
bors committed Jul 31, 2014
2 parents 9826e80 + da6070d commit 8c00357
Show file tree
Hide file tree
Showing 5 changed files with 430 additions and 305 deletions.
2 changes: 1 addition & 1 deletion src/librustc/front/config.rs
Expand Up @@ -44,7 +44,7 @@ impl<'a> fold::Folder for Context<'a> {
fold_expr(self, expr)
}
fn fold_mac(&mut self, mac: &ast::Mac) -> ast::Mac {
fold::fold_mac(mac, self)
fold::noop_fold_mac(mac, self)
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/libsyntax/ast.rs
Expand Up @@ -261,7 +261,7 @@ pub struct Crate {

pub type MetaItem = Spanned<MetaItem_>;

#[deriving(Clone, Encodable, Decodable, Eq, Hash, Show)]
#[deriving(Clone, Eq, Encodable, Decodable, Hash, Show)]
pub enum MetaItem_ {
MetaWord(InternedString),
MetaList(InternedString, Vec<Gc<MetaItem>>),
Expand Down Expand Up @@ -425,7 +425,7 @@ pub enum LocalSource {
// FIXME (pending discussion of #1697, #2178...): local should really be
// a refinement on pat.
/// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
pub struct Local {
pub ty: P<Ty>,
pub pat: Gc<Pat>,
Expand All @@ -437,7 +437,7 @@ pub struct Local {

pub type Decl = Spanned<Decl_>;

#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
pub enum Decl_ {
/// A local (let) binding:
DeclLocal(Gc<Local>),
Expand Down Expand Up @@ -679,7 +679,7 @@ pub struct MutTy {
pub mutbl: Mutability,
}

#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
pub struct TypeField {
pub ident: Ident,
pub mt: MutTy,
Expand Down Expand Up @@ -963,15 +963,15 @@ pub enum ExplicitSelf_ {

pub type ExplicitSelf = Spanned<ExplicitSelf_>;

#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
pub struct Method {
pub attrs: Vec<Attribute>,
pub id: NodeId,
pub span: Span,
pub node: Method_,
}

#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
pub enum Method_ {
/// Represents a method declaration
MethDecl(Ident,
Expand Down Expand Up @@ -1050,7 +1050,7 @@ pub type PathListItem = Spanned<PathListItem_>;

pub type ViewPath = Spanned<ViewPath_>;

#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
pub enum ViewPath_ {

/// `quux = foo::bar::baz`
Expand Down Expand Up @@ -1115,7 +1115,7 @@ pub struct Attribute_ {
/// that the ref_id is for. The impl_id maps to the "self type" of this impl.
/// If this impl is an ItemImpl, the impl_id is redundant (it could be the
/// same as the impl's node id).
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
pub struct TraitRef {
pub path: Path,
pub ref_id: NodeId,
Expand Down Expand Up @@ -1171,7 +1171,7 @@ impl StructFieldKind {
}
}

#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
pub struct StructDef {
/// Fields, not including ctor
pub fields: Vec<StructField>,
Expand Down Expand Up @@ -1221,7 +1221,7 @@ pub enum Item_ {
ItemMac(Mac),
}

#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
pub struct ForeignItem {
pub ident: Ident,
pub attrs: Vec<Attribute>,
Expand All @@ -1231,7 +1231,7 @@ pub struct ForeignItem {
pub vis: Visibility,
}

#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
pub enum ForeignItem_ {
ForeignItemFn(P<FnDecl>, Generics),
ForeignItemStatic(P<Ty>, /* is_mutbl */ bool),
Expand All @@ -1240,7 +1240,7 @@ pub enum ForeignItem_ {
/// The data we save and restore about an inlined item or method. This is not
/// part of the AST that we parse from a file, but it becomes part of the tree
/// that we trans.
#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
pub enum InlinedItem {
IIItem(Gc<Item>),
IIMethod(DefId /* impl id */, bool /* is provided */, Gc<Method>),
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ast_map/mod.rs
Expand Up @@ -606,7 +606,7 @@ impl<'a, F: FoldOps> Folder for Ctx<'a, F> {
}

fn fold_mac(&mut self, mac: &Mac) -> Mac {
fold::fold_mac(mac, self)
fold::noop_fold_mac(mac, self)
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/libsyntax/ext/expand.rs
Expand Up @@ -341,7 +341,7 @@ fn expand_item_underscore(item: &ast::Item_, fld: &mut MacroExpander) -> ast::It
ast::ItemFn(decl, fn_style, abi, ref generics, body) => {
let (rewritten_fn_decl, rewritten_body)
= expand_and_rename_fn_decl_and_block(&*decl, body, fld);
let expanded_generics = fold::fold_generics(generics,fld);
let expanded_generics = fold::noop_fold_generics(generics,fld);
ast::ItemFn(rewritten_fn_decl, fn_style, abi, expanded_generics, rewritten_body)
}
_ => noop_fold_item_underscore(&*item, fld)
Expand Down Expand Up @@ -795,7 +795,7 @@ impl<'a> Folder for IdentRenamer<'a> {
}
}
fn fold_mac(&mut self, macro: &ast::Mac) -> ast::Mac {
fold::fold_mac(macro, self)
fold::noop_fold_mac(macro, self)
}
}

Expand Down Expand Up @@ -827,7 +827,7 @@ impl<'a> Folder for PatIdentRenamer<'a> {
}
}
fn fold_mac(&mut self, macro: &ast::Mac) -> ast::Mac {
fold::fold_mac(macro, self)
fold::noop_fold_mac(macro, self)
}
}

Expand All @@ -850,7 +850,7 @@ fn expand_method(m: &ast::Method, fld: &mut MacroExpander) -> SmallVector<Gc<ast
id: id,
span: fld.new_span(m.span),
node: ast::MethDecl(fld.fold_ident(ident),
fold_generics(generics, fld),
noop_fold_generics(generics, fld),
abi,
fld.fold_explicit_self(explicit_self),
fn_style,
Expand Down Expand Up @@ -1017,7 +1017,7 @@ impl Folder for Marker {
let macro = match m.node {
MacInvocTT(ref path, ref tts, ctxt) => {
MacInvocTT(self.fold_path(path),
fold_tts(tts.as_slice(), self),
self.fold_tts(tts.as_slice()),
mtwt::apply_mark(self.mark, ctxt))
}
};
Expand All @@ -1030,7 +1030,7 @@ impl Folder for Marker {

// apply a given mark to the given token trees. Used prior to expansion of a macro.
fn mark_tts(tts: &[TokenTree], m: Mrk) -> Vec<TokenTree> {
fold_tts(tts, &mut Marker{mark:m})
noop_fold_tts(tts, &mut Marker{mark:m})
}

// apply a given mark to the given expr. Used following the expansion of a macro.
Expand Down

0 comments on commit 8c00357

Please sign in to comment.