Skip to content

Commit 26a39f2

Browse files
committed
Refactored syntax::fold.
Prior to this, the code there had a few issues: - Default implementations inconsistently 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 implementations called other default implementations directly, rather than to the underlying Folder, with the result of some AST nodes never being visited 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. [breaking-change]
1 parent 7375f4d commit 26a39f2

File tree

5 files changed

+420
-295
lines changed

5 files changed

+420
-295
lines changed

src/librustc/front/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl<'a> fold::Folder for Context<'a> {
4444
fold_expr(self, expr)
4545
}
4646
fn fold_mac(&mut self, mac: &ast::Mac) -> ast::Mac {
47-
fold::fold_mac(mac, self)
47+
fold::noop_fold_mac(mac, self)
4848
}
4949
}
5050

src/libsyntax/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -961,15 +961,15 @@ pub enum ExplicitSelf_ {
961961

962962
pub type ExplicitSelf = Spanned<ExplicitSelf_>;
963963

964-
#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
964+
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
965965
pub struct Method {
966966
pub attrs: Vec<Attribute>,
967967
pub id: NodeId,
968968
pub span: Span,
969969
pub node: Method_,
970970
}
971971

972-
#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
972+
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
973973
pub enum Method_ {
974974
/// Represents a method declaration
975975
MethDecl(Ident,

src/libsyntax/ast_map/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ impl<'a, F: FoldOps> Folder for Ctx<'a, F> {
606606
}
607607

608608
fn fold_mac(&mut self, mac: &Mac) -> Mac {
609-
fold::fold_mac(mac, self)
609+
fold::noop_fold_mac(mac, self)
610610
}
611611
}
612612

src/libsyntax/ext/expand.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ fn expand_item_underscore(item: &ast::Item_, fld: &mut MacroExpander) -> ast::It
341341
ast::ItemFn(decl, fn_style, abi, ref generics, body) => {
342342
let (rewritten_fn_decl, rewritten_body)
343343
= expand_and_rename_fn_decl_and_block(&*decl, body, fld);
344-
let expanded_generics = fold::fold_generics(generics,fld);
344+
let expanded_generics = fold::noop_fold_generics(generics,fld);
345345
ast::ItemFn(rewritten_fn_decl, fn_style, abi, expanded_generics, rewritten_body)
346346
}
347347
_ => noop_fold_item_underscore(&*item, fld)
@@ -792,7 +792,7 @@ impl<'a> Folder for IdentRenamer<'a> {
792792
}
793793
}
794794
fn fold_mac(&mut self, macro: &ast::Mac) -> ast::Mac {
795-
fold::fold_mac(macro, self)
795+
fold::noop_fold_mac(macro, self)
796796
}
797797
}
798798

@@ -824,7 +824,7 @@ impl<'a> Folder for PatIdentRenamer<'a> {
824824
}
825825
}
826826
fn fold_mac(&mut self, macro: &ast::Mac) -> ast::Mac {
827-
fold::fold_mac(macro, self)
827+
fold::noop_fold_mac(macro, self)
828828
}
829829
}
830830

@@ -847,7 +847,7 @@ fn expand_method(m: &ast::Method, fld: &mut MacroExpander) -> SmallVector<Gc<ast
847847
id: id,
848848
span: fld.new_span(m.span),
849849
node: ast::MethDecl(fld.fold_ident(ident),
850-
fold_generics(generics, fld),
850+
noop_fold_generics(generics, fld),
851851
abi,
852852
fld.fold_explicit_self(explicit_self),
853853
fn_style,
@@ -1014,7 +1014,7 @@ impl Folder for Marker {
10141014
let macro = match m.node {
10151015
MacInvocTT(ref path, ref tts, ctxt) => {
10161016
MacInvocTT(self.fold_path(path),
1017-
fold_tts(tts.as_slice(), self),
1017+
self.fold_tts(tts.as_slice()),
10181018
mtwt::apply_mark(self.mark, ctxt))
10191019
}
10201020
};
@@ -1027,7 +1027,7 @@ impl Folder for Marker {
10271027

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

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

0 commit comments

Comments
 (0)