Skip to content

Commit 8c00357

Browse files
committed
auto merge of #15999 : Kimundi/rust/fix_folder, r=nikomatsakis
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
2 parents 9826e80 + da6070d commit 8c00357

File tree

5 files changed

+430
-305
lines changed

5 files changed

+430
-305
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

+12-12
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ pub struct Crate {
261261

262262
pub type MetaItem = Spanned<MetaItem_>;
263263

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

438438
pub type Decl = Spanned<Decl_>;
439439

440-
#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
440+
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
441441
pub enum Decl_ {
442442
/// A local (let) binding:
443443
DeclLocal(Gc<Local>),
@@ -679,7 +679,7 @@ pub struct MutTy {
679679
pub mutbl: Mutability,
680680
}
681681

682-
#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
682+
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
683683
pub struct TypeField {
684684
pub ident: Ident,
685685
pub mt: MutTy,
@@ -963,15 +963,15 @@ pub enum ExplicitSelf_ {
963963

964964
pub type ExplicitSelf = Spanned<ExplicitSelf_>;
965965

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

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

10511051
pub type ViewPath = Spanned<ViewPath_>;
10521052

1053-
#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
1053+
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
10541054
pub enum ViewPath_ {
10551055

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

1174-
#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
1174+
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
11751175
pub struct StructDef {
11761176
/// Fields, not including ctor
11771177
pub fields: Vec<StructField>,
@@ -1221,7 +1221,7 @@ pub enum Item_ {
12211221
ItemMac(Mac),
12221222
}
12231223

1224-
#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
1224+
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
12251225
pub struct ForeignItem {
12261226
pub ident: Ident,
12271227
pub attrs: Vec<Attribute>,
@@ -1231,7 +1231,7 @@ pub struct ForeignItem {
12311231
pub vis: Visibility,
12321232
}
12331233

1234-
#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
1234+
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
12351235
pub enum ForeignItem_ {
12361236
ForeignItemFn(P<FnDecl>, Generics),
12371237
ForeignItemStatic(P<Ty>, /* is_mutbl */ bool),
@@ -1240,7 +1240,7 @@ pub enum ForeignItem_ {
12401240
/// The data we save and restore about an inlined item or method. This is not
12411241
/// part of the AST that we parse from a file, but it becomes part of the tree
12421242
/// that we trans.
1243-
#[deriving(PartialEq, Eq, Encodable, Decodable, Hash, Show)]
1243+
#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
12441244
pub enum InlinedItem {
12451245
IIItem(Gc<Item>),
12461246
IIMethod(DefId /* impl id */, bool /* is provided */, Gc<Method>),

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)
@@ -795,7 +795,7 @@ impl<'a> Folder for IdentRenamer<'a> {
795795
}
796796
}
797797
fn fold_mac(&mut self, macro: &ast::Mac) -> ast::Mac {
798-
fold::fold_mac(macro, self)
798+
fold::noop_fold_mac(macro, self)
799799
}
800800
}
801801

@@ -827,7 +827,7 @@ impl<'a> Folder for PatIdentRenamer<'a> {
827827
}
828828
}
829829
fn fold_mac(&mut self, macro: &ast::Mac) -> ast::Mac {
830-
fold::fold_mac(macro, self)
830+
fold::noop_fold_mac(macro, self)
831831
}
832832
}
833833

@@ -850,7 +850,7 @@ fn expand_method(m: &ast::Method, fld: &mut MacroExpander) -> SmallVector<Gc<ast
850850
id: id,
851851
span: fld.new_span(m.span),
852852
node: ast::MethDecl(fld.fold_ident(ident),
853-
fold_generics(generics, fld),
853+
noop_fold_generics(generics, fld),
854854
abi,
855855
fld.fold_explicit_self(explicit_self),
856856
fn_style,
@@ -1017,7 +1017,7 @@ impl Folder for Marker {
10171017
let macro = match m.node {
10181018
MacInvocTT(ref path, ref tts, ctxt) => {
10191019
MacInvocTT(self.fold_path(path),
1020-
fold_tts(tts.as_slice(), self),
1020+
self.fold_tts(tts.as_slice()),
10211021
mtwt::apply_mark(self.mark, ctxt))
10221022
}
10231023
};
@@ -1030,7 +1030,7 @@ impl Folder for Marker {
10301030

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

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

0 commit comments

Comments
 (0)