Skip to content

Commit 0110f5e

Browse files
author
Keegan McAllister
committed
syntax::fold: Allow removing attributes
1 parent 0ba9e1f commit 0110f5e

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

src/libsyntax/ext/expand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ fn expand_arm(arm: ast::Arm, fld: &mut MacroExpander) -> ast::Arm {
844844
arm.guard.map(|g| fld.fold_expr(rename_fld.fold_expr(g)));
845845
let rewritten_body = fld.fold_expr(rename_fld.fold_expr(arm.body));
846846
ast::Arm {
847-
attrs: arm.attrs.move_map(|x| fld.fold_attribute(x)),
847+
attrs: fold::fold_attrs(arm.attrs, fld),
848848
pats: rewritten_pats,
849849
guard: rewritten_guard,
850850
body: rewritten_body,
@@ -1273,7 +1273,7 @@ fn expand_method(m: P<ast::Method>, fld: &mut MacroExpander) -> SmallVector<P<as
12731273
let (rewritten_fn_decl, rewritten_body)
12741274
= expand_and_rename_fn_decl_and_block(decl, body, fld);
12751275
SmallVector::one(P(ast::Method {
1276-
attrs: m.attrs.move_map(|a| fld.fold_attribute(a)),
1276+
attrs: fold::fold_attrs(m.attrs, fld),
12771277
id: id,
12781278
span: fld.new_span(m.span),
12791279
node: ast::MethDecl(fld.fold_ident(ident),

src/libsyntax/fold.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ pub trait Folder : Sized {
223223
noop_fold_lifetime_def(l, self)
224224
}
225225

226-
fn fold_attribute(&mut self, at: Attribute) -> Attribute {
226+
fn fold_attribute(&mut self, at: Attribute) -> Option<Attribute> {
227227
noop_fold_attribute(at, self)
228228
}
229229

@@ -373,9 +373,13 @@ pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<
373373
})
374374
}
375375

376+
pub fn fold_attrs<T: Folder>(attrs: Vec<Attribute>, fld: &mut T) -> Vec<Attribute> {
377+
attrs.into_iter().flat_map(|x| fld.fold_attribute(x).into_iter()).collect()
378+
}
379+
376380
pub fn noop_fold_arm<T: Folder>(Arm {attrs, pats, guard, body}: Arm, fld: &mut T) -> Arm {
377381
Arm {
378-
attrs: attrs.move_map(|x| fld.fold_attribute(x)),
382+
attrs: fold_attrs(attrs, fld),
379383
pats: pats.move_map(|x| fld.fold_pat(x)),
380384
guard: guard.map(|x| fld.fold_expr(x)),
381385
body: fld.fold_expr(body),
@@ -475,7 +479,7 @@ pub fn noop_fold_variant<T: Folder>(v: P<Variant>, fld: &mut T) -> P<Variant> {
475479
node: Variant_ {
476480
id: fld.new_id(id),
477481
name: name,
478-
attrs: attrs.move_map(|x| fld.fold_attribute(x)),
482+
attrs: fold_attrs(attrs, fld),
479483
kind: match kind {
480484
TupleVariantKind(variant_args) => {
481485
TupleVariantKind(variant_args.move_map(|x|
@@ -553,17 +557,17 @@ pub fn noop_fold_local<T: Folder>(l: P<Local>, fld: &mut T) -> P<Local> {
553557
})
554558
}
555559

556-
pub fn noop_fold_attribute<T: Folder>(at: Attribute, fld: &mut T) -> Attribute {
560+
pub fn noop_fold_attribute<T: Folder>(at: Attribute, fld: &mut T) -> Option<Attribute> {
557561
let Spanned {node: Attribute_ {id, style, value, is_sugared_doc}, span} = at;
558-
Spanned {
562+
Some(Spanned {
559563
node: Attribute_ {
560564
id: id,
561565
style: style,
562566
value: fld.fold_meta_item(value),
563567
is_sugared_doc: is_sugared_doc
564568
},
565569
span: fld.new_span(span)
566-
}
570+
})
567571
}
568572

569573
pub fn noop_fold_explicit_self_underscore<T: Folder>(es: ExplicitSelf_, fld: &mut T)
@@ -843,8 +847,8 @@ pub fn noop_fold_typedef<T>(t: Typedef, folder: &mut T)
843847
where T: Folder {
844848
let new_id = folder.new_id(t.id);
845849
let new_span = folder.new_span(t.span);
846-
let new_attrs = t.attrs.iter().map(|attr| {
847-
folder.fold_attribute((*attr).clone())
850+
let new_attrs = t.attrs.iter().flat_map(|attr| {
851+
folder.fold_attribute((*attr).clone()).into_iter()
848852
}).collect();
849853
let new_ident = folder.fold_ident(t.ident);
850854
let new_type = folder.fold_ty(t.typ);
@@ -864,7 +868,7 @@ pub fn noop_fold_associated_type<T>(at: AssociatedType, folder: &mut T)
864868
{
865869
let new_attrs = at.attrs
866870
.iter()
867-
.map(|attr| folder.fold_attribute((*attr).clone()))
871+
.flat_map(|attr| folder.fold_attribute((*attr).clone()).into_iter())
868872
.collect();
869873
let new_param = folder.fold_ty_param(at.ty_param);
870874
ast::AssociatedType {
@@ -906,7 +910,7 @@ pub fn noop_fold_struct_field<T: Folder>(f: StructField, fld: &mut T) -> StructF
906910
id: fld.new_id(id),
907911
kind: kind,
908912
ty: fld.fold_ty(ty),
909-
attrs: attrs.move_map(|a| fld.fold_attribute(a))
913+
attrs: fold_attrs(attrs, fld),
910914
},
911915
span: fld.new_span(span)
912916
}
@@ -1069,7 +1073,7 @@ pub fn noop_fold_type_method<T: Folder>(m: TypeMethod, fld: &mut T) -> TypeMetho
10691073
TypeMethod {
10701074
id: fld.new_id(id),
10711075
ident: fld.fold_ident(ident),
1072-
attrs: attrs.move_map(|a| fld.fold_attribute(a)),
1076+
attrs: fold_attrs(attrs, fld),
10731077
unsafety: unsafety,
10741078
abi: abi,
10751079
decl: fld.fold_fn_decl(decl),
@@ -1151,7 +1155,7 @@ pub fn noop_fold_item_simple<T: Folder>(Item {id, ident, attrs, node, vis, span}
11511155
Item {
11521156
id: id,
11531157
ident: folder.fold_ident(ident),
1154-
attrs: attrs.move_map(|e| folder.fold_attribute(e)),
1158+
attrs: fold_attrs(attrs, folder),
11551159
node: node,
11561160
vis: vis,
11571161
span: folder.new_span(span)
@@ -1162,7 +1166,7 @@ pub fn noop_fold_foreign_item<T: Folder>(ni: P<ForeignItem>, folder: &mut T) ->
11621166
ni.map(|ForeignItem {id, ident, attrs, node, span, vis}| ForeignItem {
11631167
id: folder.new_id(id),
11641168
ident: folder.fold_ident(ident),
1165-
attrs: attrs.move_map(|x| folder.fold_attribute(x)),
1169+
attrs: fold_attrs(attrs, folder),
11661170
node: match node {
11671171
ForeignItemFn(fdec, generics) => {
11681172
ForeignItemFn(folder.fold_fn_decl(fdec), folder.fold_generics(generics))
@@ -1181,7 +1185,7 @@ pub fn noop_fold_foreign_item<T: Folder>(ni: P<ForeignItem>, folder: &mut T) ->
11811185
pub fn noop_fold_method<T: Folder>(m: P<Method>, folder: &mut T) -> SmallVector<P<Method>> {
11821186
SmallVector::one(m.map(|Method {id, attrs, node, span}| Method {
11831187
id: folder.new_id(id),
1184-
attrs: attrs.move_map(|a| folder.fold_attribute(a)),
1188+
attrs: fold_attrs(attrs, folder),
11851189
node: match node {
11861190
MethDecl(ident,
11871191
generics,

0 commit comments

Comments
 (0)