Skip to content

Commit ede7a6d

Browse files
committed
Give access to field attributes in ext::deriving
1 parent 8a60e56 commit ede7a6d

File tree

1 file changed

+33
-29
lines changed
  • src/libsyntax/ext/deriving/generic

1 file changed

+33
-29
lines changed

src/libsyntax/ext/deriving/generic/mod.rs

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ pub struct Substructure<'a> {
270270
}
271271

272272
/// Summary of the relevant parts of a struct/enum field.
273-
pub struct FieldInfo {
273+
pub struct FieldInfo<'a> {
274274
pub span: Span,
275275
/// None for tuple structs/normal enum variants, Some for normal
276276
/// structs/struct enum variants.
@@ -281,6 +281,8 @@ pub struct FieldInfo {
281281
/// The expressions corresponding to references to this field in
282282
/// the other `Self` arguments.
283283
pub other: Vec<P<Expr>>,
284+
/// The attributes on the field
285+
pub attrs: &'a [ast::Attribute],
284286
}
285287

286288
/// Fields for a static method
@@ -293,11 +295,11 @@ pub enum StaticFields {
293295

294296
/// A summary of the possible sets of fields.
295297
pub enum SubstructureFields<'a> {
296-
Struct(Vec<FieldInfo>),
298+
Struct(Vec<FieldInfo<'a>>),
297299
/// Matching variants of the enum: variant index, ast::Variant,
298300
/// fields: the field name is only non-`None` in the case of a struct
299301
/// variant.
300-
EnumMatching(usize, &'a ast::Variant, Vec<FieldInfo>),
302+
EnumMatching(usize, &'a ast::Variant, Vec<FieldInfo<'a>>),
301303

302304
/// Non-matching variants of the enum, but with all state hidden from
303305
/// the consequent code. The first component holds `Ident`s for all of
@@ -378,7 +380,7 @@ impl<'a> TraitDef<'a> {
378380
pub fn expand(&self,
379381
cx: &mut ExtCtxt,
380382
mitem: &ast::MetaItem,
381-
item: &ast::Item,
383+
item: &'a ast::Item,
382384
push: &mut FnMut(P<ast::Item>))
383385
{
384386
let newitem = match item.node {
@@ -609,7 +611,7 @@ impl<'a> TraitDef<'a> {
609611

610612
fn expand_struct_def(&self,
611613
cx: &mut ExtCtxt,
612-
struct_def: &StructDef,
614+
struct_def: &'a StructDef,
613615
type_ident: Ident,
614616
generics: &Generics) -> P<ast::Item> {
615617
let field_tys: Vec<P<ast::Ty>> = struct_def.fields.iter()
@@ -653,7 +655,7 @@ impl<'a> TraitDef<'a> {
653655

654656
fn expand_enum_def(&self,
655657
cx: &mut ExtCtxt,
656-
enum_def: &EnumDef,
658+
enum_def: &'a EnumDef,
657659
type_attrs: &[ast::Attribute],
658660
type_ident: Ident,
659661
generics: &Generics) -> P<ast::Item> {
@@ -885,10 +887,10 @@ impl<'a> MethodDef<'a> {
885887
/// }
886888
/// }
887889
/// ```
888-
fn expand_struct_method_body(&self,
890+
fn expand_struct_method_body<'b>(&self,
889891
cx: &mut ExtCtxt,
890-
trait_: &TraitDef,
891-
struct_def: &StructDef,
892+
trait_: &TraitDef<'b>,
893+
struct_def: &'b StructDef,
892894
type_ident: Ident,
893895
self_args: &[P<Expr>],
894896
nonself_args: &[P<Expr>])
@@ -914,18 +916,19 @@ impl<'a> MethodDef<'a> {
914916
let fields = if !raw_fields.is_empty() {
915917
let mut raw_fields = raw_fields.into_iter().map(|v| v.into_iter());
916918
let first_field = raw_fields.next().unwrap();
917-
let mut other_fields: Vec<vec::IntoIter<(Span, Option<Ident>, P<Expr>)>>
919+
let mut other_fields: Vec<vec::IntoIter<_>>
918920
= raw_fields.collect();
919-
first_field.map(|(span, opt_id, field)| {
921+
first_field.map(|(span, opt_id, field, attrs)| {
920922
FieldInfo {
921923
span: span,
922924
name: opt_id,
923925
self_: field,
924926
other: other_fields.iter_mut().map(|l| {
925927
match l.next().unwrap() {
926-
(_, _, ex) => ex
928+
(_, _, ex, _) => ex
927929
}
928-
}).collect()
930+
}).collect(),
931+
attrs: attrs,
929932
}
930933
}).collect()
931934
} else {
@@ -999,10 +1002,10 @@ impl<'a> MethodDef<'a> {
9991002
/// `PartialEq`, and those subcomputations will hopefully be removed
10001003
/// as their results are unused. The point of `__self_vi` and
10011004
/// `__arg_1_vi` is for `PartialOrd`; see #15503.)
1002-
fn expand_enum_method_body(&self,
1005+
fn expand_enum_method_body<'b>(&self,
10031006
cx: &mut ExtCtxt,
1004-
trait_: &TraitDef,
1005-
enum_def: &EnumDef,
1007+
trait_: &TraitDef<'b>,
1008+
enum_def: &'b EnumDef,
10061009
type_attrs: &[ast::Attribute],
10071010
type_ident: Ident,
10081011
self_args: Vec<P<Expr>>,
@@ -1038,11 +1041,11 @@ impl<'a> MethodDef<'a> {
10381041
/// }
10391042
/// }
10401043
/// ```
1041-
fn build_enum_match_tuple(
1044+
fn build_enum_match_tuple<'b>(
10421045
&self,
10431046
cx: &mut ExtCtxt,
1044-
trait_: &TraitDef,
1045-
enum_def: &EnumDef,
1047+
trait_: &TraitDef<'b>,
1048+
enum_def: &'b EnumDef,
10461049
type_attrs: &[ast::Attribute],
10471050
type_ident: Ident,
10481051
self_args: Vec<P<Expr>>,
@@ -1125,15 +1128,15 @@ impl<'a> MethodDef<'a> {
11251128
// arg fields of the variant for the first self pat.
11261129
let field_tuples = first_self_pat_idents.into_iter().enumerate()
11271130
// For each arg field of self, pull out its getter expr ...
1128-
.map(|(field_index, (sp, opt_ident, self_getter_expr))| {
1131+
.map(|(field_index, (sp, opt_ident, self_getter_expr, attrs))| {
11291132
// ... but FieldInfo also wants getter expr
11301133
// for matching other arguments of Self type;
11311134
// so walk across the *other* self_pats_idents
11321135
// and pull out getter for same field in each
11331136
// of them (using `field_index` tracked above).
11341137
// That is the heart of the transposition.
11351138
let others = self_pats_idents.iter().map(|fields| {
1136-
let (_, _opt_ident, ref other_getter_expr) =
1139+
let (_, _opt_ident, ref other_getter_expr, _) =
11371140
fields[field_index];
11381141

11391142
// All Self args have same variant, so
@@ -1149,6 +1152,7 @@ impl<'a> MethodDef<'a> {
11491152
name: opt_ident,
11501153
self_: self_getter_expr,
11511154
other: others,
1155+
attrs: attrs,
11521156
}
11531157
}).collect::<Vec<FieldInfo>>();
11541158

@@ -1400,10 +1404,10 @@ impl<'a> TraitDef<'a> {
14001404
fn create_struct_pattern(&self,
14011405
cx: &mut ExtCtxt,
14021406
struct_path: ast::Path,
1403-
struct_def: &StructDef,
1407+
struct_def: &'a StructDef,
14041408
prefix: &str,
14051409
mutbl: ast::Mutability)
1406-
-> (P<ast::Pat>, Vec<(Span, Option<Ident>, P<Expr>)>) {
1410+
-> (P<ast::Pat>, Vec<(Span, Option<Ident>, P<Expr>, &'a [ast::Attribute])>) {
14071411
if struct_def.fields.is_empty() {
14081412
return (cx.pat_enum(self.span, struct_path, vec![]), vec![]);
14091413
}
@@ -1433,15 +1437,15 @@ impl<'a> TraitDef<'a> {
14331437
paths.push(codemap::Spanned{span: sp, node: ident});
14341438
let val = cx.expr(
14351439
sp, ast::ExprParen(cx.expr_deref(sp, cx.expr_path(cx.path_ident(sp,ident)))));
1436-
ident_expr.push((sp, opt_id, val));
1440+
ident_expr.push((sp, opt_id, val, &struct_field.node.attrs[..]));
14371441
}
14381442

14391443
let subpats = self.create_subpatterns(cx, paths, mutbl);
14401444

14411445
// struct_type is definitely not Unknown, since struct_def.fields
14421446
// must be nonempty to reach here
14431447
let pattern = if struct_type == Record {
1444-
let field_pats = subpats.into_iter().zip(ident_expr.iter()).map(|(pat, &(_, id, _))| {
1448+
let field_pats = subpats.into_iter().zip(ident_expr.iter()).map(|(pat, &(_, id, _, _))| {
14451449
// id is guaranteed to be Some
14461450
codemap::Spanned {
14471451
span: pat.span,
@@ -1459,10 +1463,10 @@ impl<'a> TraitDef<'a> {
14591463
fn create_enum_variant_pattern(&self,
14601464
cx: &mut ExtCtxt,
14611465
enum_ident: ast::Ident,
1462-
variant: &ast::Variant,
1466+
variant: &'a ast::Variant,
14631467
prefix: &str,
14641468
mutbl: ast::Mutability)
1465-
-> (P<ast::Pat>, Vec<(Span, Option<Ident>, P<Expr>)>) {
1469+
-> (P<ast::Pat>, Vec<(Span, Option<Ident>, P<Expr>, &'a [ast::Attribute])>) {
14661470
let variant_ident = variant.node.name;
14671471
let variant_path = cx.path(variant.span, vec![enum_ident, variant_ident]);
14681472
match variant.node.kind {
@@ -1472,15 +1476,15 @@ impl<'a> TraitDef<'a> {
14721476
}
14731477

14741478
let mut paths = Vec::new();
1475-
let mut ident_expr = Vec::new();
1479+
let mut ident_expr: Vec<(_, _, _, &'a [ast::Attribute])> = Vec::new();
14761480
for (i, va) in variant_args.iter().enumerate() {
14771481
let sp = self.set_expn_info(cx, va.ty.span);
14781482
let ident = cx.ident_of(&format!("{}_{}", prefix, i));
14791483
let path1 = codemap::Spanned{span: sp, node: ident};
14801484
paths.push(path1);
14811485
let expr_path = cx.expr_path(cx.path_ident(sp, ident));
14821486
let val = cx.expr(sp, ast::ExprParen(cx.expr_deref(sp, expr_path)));
1483-
ident_expr.push((sp, None, val));
1487+
ident_expr.push((sp, None, val, &[]));
14841488
}
14851489

14861490
let subpats = self.create_subpatterns(cx, paths, mutbl);

0 commit comments

Comments
 (0)