Skip to content

Commit fae4c2b

Browse files
committed
syntax: add attributes to FieldInfo
1 parent 7df71b0 commit fae4c2b

File tree

1 file changed

+83
-79
lines changed
  • src/libsyntax/ext/deriving/generic

1 file changed

+83
-79
lines changed

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

Lines changed: 83 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ pub struct Substructure<'a> {
259259
}
260260

261261
/// Summary of the relevant parts of a struct/enum field.
262-
pub struct FieldInfo {
262+
pub struct FieldInfo<'a> {
263263
pub span: Span,
264264
/// None for tuple structs/normal enum variants, Some for normal
265265
/// structs/struct enum variants.
@@ -270,6 +270,8 @@ pub struct FieldInfo {
270270
/// The expressions corresponding to references to this field in
271271
/// the other Self arguments.
272272
pub other: Vec<Gc<Expr>>,
273+
/// The attributes attached to this field.
274+
pub attrs: &'a [ast::Attribute],
273275
}
274276

275277
/// Fields for a static method
@@ -283,13 +285,13 @@ pub enum StaticFields {
283285
/// A summary of the possible sets of fields. See above for details
284286
/// and examples
285287
pub enum SubstructureFields<'a> {
286-
Struct(Vec<FieldInfo>),
288+
Struct(Vec<FieldInfo<'a>>),
287289
/**
288290
Matching variants of the enum: variant index, ast::Variant,
289291
fields: the field name is only non-`None` in the case of a struct
290292
variant.
291293
*/
292-
EnumMatching(uint, &'a ast::Variant, Vec<FieldInfo>),
294+
EnumMatching(uint, &'a ast::Variant, Vec<FieldInfo<'a>>),
293295

294296
/**
295297
non-matching variants of the enum, but with all state hidden from
@@ -472,11 +474,11 @@ impl<'a> TraitDef<'a> {
472474
}).collect()))
473475
}
474476

475-
fn expand_struct_def(&self,
476-
cx: &mut ExtCtxt,
477-
struct_def: &StructDef,
478-
type_ident: Ident,
479-
generics: &Generics) -> Gc<ast::Item> {
477+
fn expand_struct_def<'b>(&self,
478+
cx: &mut ExtCtxt,
479+
struct_def: &'b StructDef,
480+
type_ident: Ident,
481+
generics: &Generics) -> Gc<ast::Item> {
480482
let methods = self.methods.iter().map(|method_def| {
481483
let (explicit_self, self_args, nonself_args, tys) =
482484
method_def.split_self_nonself_args(
@@ -512,11 +514,11 @@ impl<'a> TraitDef<'a> {
512514
self.create_derived_impl(cx, type_ident, generics, methods)
513515
}
514516

515-
fn expand_enum_def(&self,
516-
cx: &mut ExtCtxt,
517-
enum_def: &EnumDef,
518-
type_ident: Ident,
519-
generics: &Generics) -> Gc<ast::Item> {
517+
fn expand_enum_def<'b>(&self,
518+
cx: &mut ExtCtxt,
519+
enum_def: &'b EnumDef,
520+
type_ident: Ident,
521+
generics: &Generics) -> Gc<ast::Item> {
520522
let methods = self.methods.iter().map(|method_def| {
521523
let (explicit_self, self_args, nonself_args, tys) =
522524
method_def.split_self_nonself_args(cx, self,
@@ -563,13 +565,13 @@ fn variant_to_pat(cx: &mut ExtCtxt, sp: Span, variant: &ast::Variant)
563565
}
564566

565567
impl<'a> MethodDef<'a> {
566-
fn call_substructure_method(&self,
567-
cx: &mut ExtCtxt,
568-
trait_: &TraitDef,
569-
type_ident: Ident,
570-
self_args: &[Gc<Expr>],
571-
nonself_args: &[Gc<Expr>],
572-
fields: SubstructureFields)
568+
fn call_substructure_method<'b>(&self,
569+
cx: &mut ExtCtxt,
570+
trait_: &TraitDef,
571+
type_ident: Ident,
572+
self_args: &'b [Gc<Expr>],
573+
nonself_args: &'b [Gc<Expr>],
574+
fields: SubstructureFields<'b>)
573575
-> Gc<Expr> {
574576
let substructure = Substructure {
575577
type_ident: type_ident,
@@ -715,13 +717,13 @@ impl<'a> MethodDef<'a> {
715717
}
716718
~~~
717719
*/
718-
fn expand_struct_method_body(&self,
719-
cx: &mut ExtCtxt,
720-
trait_: &TraitDef,
721-
struct_def: &StructDef,
722-
type_ident: Ident,
723-
self_args: &[Gc<Expr>],
724-
nonself_args: &[Gc<Expr>])
720+
fn expand_struct_method_body<'b>(&self,
721+
cx: &mut ExtCtxt,
722+
trait_: &TraitDef,
723+
struct_def: &'b StructDef,
724+
type_ident: Ident,
725+
self_args: &'b [Gc<Expr>],
726+
nonself_args: &'b [Gc<Expr>])
725727
-> Gc<Expr> {
726728

727729
let mut raw_fields = Vec::new(); // ~[[fields of self],
@@ -744,17 +746,18 @@ impl<'a> MethodDef<'a> {
744746
raw_fields.get(0)
745747
.iter()
746748
.enumerate()
747-
.map(|(i, &(span, opt_id, field))| {
749+
.map(|(i, &(span, opt_id, field, attrs))| {
748750
let other_fields = raw_fields.tail().iter().map(|l| {
749751
match l.get(i) {
750-
&(_, _, ex) => ex
752+
&(_, _, ex, _) => ex
751753
}
752754
}).collect();
753755
FieldInfo {
754756
span: span,
755757
name: opt_id,
756758
self_: field,
757-
other: other_fields
759+
other: other_fields,
760+
attrs: attrs,
758761
}
759762
}).collect()
760763
} else {
@@ -782,13 +785,13 @@ impl<'a> MethodDef<'a> {
782785
body
783786
}
784787

785-
fn expand_static_struct_method_body(&self,
786-
cx: &mut ExtCtxt,
787-
trait_: &TraitDef,
788-
struct_def: &StructDef,
789-
type_ident: Ident,
790-
self_args: &[Gc<Expr>],
791-
nonself_args: &[Gc<Expr>])
788+
fn expand_static_struct_method_body<'b>(&self,
789+
cx: &mut ExtCtxt,
790+
trait_: &TraitDef,
791+
struct_def: &'b StructDef,
792+
type_ident: Ident,
793+
self_args: &[Gc<Expr>],
794+
nonself_args: &[Gc<Expr>])
792795
-> Gc<Expr> {
793796
let summary = trait_.summarise_struct(cx, struct_def);
794797

@@ -830,14 +833,14 @@ impl<'a> MethodDef<'a> {
830833
as their results are unused. The point of `__self_vi` and
831834
`__arg_1_vi` is for `PartialOrd`; see #15503.)
832835
*/
833-
fn expand_enum_method_body(&self,
834-
cx: &mut ExtCtxt,
835-
trait_: &TraitDef,
836-
enum_def: &EnumDef,
837-
type_ident: Ident,
838-
self_args: &[Gc<Expr>],
839-
nonself_args: &[Gc<Expr>])
840-
-> Gc<Expr> {
836+
fn expand_enum_method_body<'b>(&self,
837+
cx: &mut ExtCtxt,
838+
trait_: &TraitDef,
839+
enum_def: &'b EnumDef,
840+
type_ident: Ident,
841+
self_args: &[Gc<Expr>],
842+
nonself_args: &[Gc<Expr>])
843+
-> Gc<Expr> {
841844
self.build_enum_match_tuple(
842845
cx, trait_, enum_def, type_ident, self_args, nonself_args)
843846
}
@@ -870,14 +873,14 @@ impl<'a> MethodDef<'a> {
870873
}
871874
~~~
872875
*/
873-
fn build_enum_match_tuple(
874-
&self,
875-
cx: &mut ExtCtxt,
876-
trait_: &TraitDef,
877-
enum_def: &EnumDef,
878-
type_ident: Ident,
879-
self_args: &[Gc<Expr>],
880-
nonself_args: &[Gc<Expr>]) -> Gc<Expr> {
876+
fn build_enum_match_tuple<'b>(&self,
877+
cx: &mut ExtCtxt,
878+
trait_: &TraitDef,
879+
enum_def: &'b EnumDef,
880+
type_ident: Ident,
881+
self_args: &[Gc<Expr>],
882+
nonself_args: &[Gc<Expr>])
883+
-> Gc<Expr> {
881884

882885
let sp = trait_.span;
883886
let variants = &enum_def.variants;
@@ -920,7 +923,7 @@ impl<'a> MethodDef<'a> {
920923

921924
// These self_pats have form Variant1, Variant2, ...
922925
let self_pats : Vec<(Gc<ast::Pat>,
923-
Vec<(Span, Option<Ident>, Gc<Expr>)>)>;
926+
Vec<(Span, Option<Ident>, Gc<Expr>, &[ast::Attribute])>)>;
924927
self_pats = self_arg_names.iter()
925928
.map(|self_arg_name|
926929
trait_.create_enum_variant_pattern(
@@ -954,7 +957,7 @@ impl<'a> MethodDef<'a> {
954957

955958
field_tuples = self_arg_fields.iter().enumerate()
956959
// For each arg field of self, pull out its getter expr ...
957-
.map(|(field_index, &(sp, opt_ident, self_getter_expr))| {
960+
.map(|(field_index, &(sp, opt_ident, self_getter_expr, attrs))| {
958961
// ... but FieldInfo also wants getter expr
959962
// for matching other arguments of Self type;
960963
// so walk across the *other* self_pats and
@@ -964,7 +967,7 @@ impl<'a> MethodDef<'a> {
964967
let others = self_pats.tail().iter()
965968
.map(|&(_pat, ref fields)| {
966969

967-
let &(_, _opt_ident, other_getter_expr) =
970+
let &(_, _opt_ident, other_getter_expr, _) =
968971
fields.get(field_index);
969972

970973
// All Self args have same variant, so
@@ -980,6 +983,7 @@ impl<'a> MethodDef<'a> {
980983
name: opt_ident,
981984
self_: self_getter_expr,
982985
other: others,
986+
attrs: attrs,
983987
}
984988
}).collect::<Vec<FieldInfo>>();
985989

@@ -1132,13 +1136,13 @@ impl<'a> MethodDef<'a> {
11321136
cx.expr_match(sp, match_arg, match_arms)
11331137
}
11341138

1135-
fn expand_static_enum_method_body(&self,
1136-
cx: &mut ExtCtxt,
1137-
trait_: &TraitDef,
1138-
enum_def: &EnumDef,
1139-
type_ident: Ident,
1140-
self_args: &[Gc<Expr>],
1141-
nonself_args: &[Gc<Expr>])
1139+
fn expand_static_enum_method_body<'b>(&self,
1140+
cx: &mut ExtCtxt,
1141+
trait_: &TraitDef,
1142+
enum_def: &'b EnumDef,
1143+
type_ident: Ident,
1144+
self_args: &[Gc<Expr>],
1145+
nonself_args: &[Gc<Expr>])
11421146
-> Gc<Expr> {
11431147
let summary = enum_def.variants.iter().map(|v| {
11441148
let ident = v.node.name;
@@ -1218,13 +1222,13 @@ impl<'a> TraitDef<'a> {
12181222
}).collect()
12191223
}
12201224

1221-
fn create_struct_pattern(&self,
1222-
cx: &mut ExtCtxt,
1223-
struct_ident: Ident,
1224-
struct_def: &StructDef,
1225-
prefix: &str,
1226-
mutbl: ast::Mutability)
1227-
-> (Gc<ast::Pat>, Vec<(Span, Option<Ident>, Gc<Expr>)>) {
1225+
fn create_struct_pattern<'b>(&self,
1226+
cx: &mut ExtCtxt,
1227+
struct_ident: Ident,
1228+
struct_def: &'b StructDef,
1229+
prefix: &str,
1230+
mutbl: ast::Mutability)
1231+
-> (Gc<ast::Pat>, Vec<(Span, Option<Ident>, Gc<Expr>, &'b [ast::Attribute])>) {
12281232
if struct_def.fields.is_empty() {
12291233
return (
12301234
cx.pat_ident_binding_mode(
@@ -1259,15 +1263,15 @@ impl<'a> TraitDef<'a> {
12591263
paths.push(codemap::Spanned{span: sp, node: ident});
12601264
let val = cx.expr(
12611265
sp, ast::ExprParen(cx.expr_deref(sp, cx.expr_path(cx.path_ident(sp,ident)))));
1262-
ident_expr.push((sp, opt_id, val));
1266+
ident_expr.push((sp, opt_id, val, struct_field.node.attrs.as_slice()));
12631267
}
12641268

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

12671271
// struct_type is definitely not Unknown, since struct_def.fields
12681272
// must be nonempty to reach here
12691273
let pattern = if struct_type == Record {
1270-
let field_pats = subpats.iter().zip(ident_expr.iter()).map(|(&pat, &(_, id, _))| {
1274+
let field_pats = subpats.iter().zip(ident_expr.iter()).map(|(&pat, &(_, id, _, _))| {
12711275
// id is guaranteed to be Some
12721276
ast::FieldPat { ident: id.unwrap(), pat: pat }
12731277
}).collect();
@@ -1279,12 +1283,12 @@ impl<'a> TraitDef<'a> {
12791283
(pattern, ident_expr)
12801284
}
12811285

1282-
fn create_enum_variant_pattern(&self,
1283-
cx: &mut ExtCtxt,
1284-
variant: &ast::Variant,
1285-
prefix: &str,
1286-
mutbl: ast::Mutability)
1287-
-> (Gc<ast::Pat>, Vec<(Span, Option<Ident>, Gc<Expr>)> ) {
1286+
fn create_enum_variant_pattern<'a>(&self,
1287+
cx: &mut ExtCtxt,
1288+
variant: &'a ast::Variant,
1289+
prefix: &str,
1290+
mutbl: ast::Mutability)
1291+
-> (Gc<ast::Pat>, Vec<(Span, Option<Ident>, Gc<Expr>, &'a [ast::Attribute])>) {
12881292
let variant_ident = variant.node.name;
12891293
match variant.node.kind {
12901294
ast::TupleVariantKind(ref variant_args) => {
@@ -1305,7 +1309,7 @@ impl<'a> TraitDef<'a> {
13051309
paths.push(path1);
13061310
let expr_path = cx.expr_path(cx.path_ident(sp, ident));
13071311
let val = cx.expr(sp, ast::ExprParen(cx.expr_deref(sp, expr_path)));
1308-
ident_expr.push((sp, None, val));
1312+
ident_expr.push((sp, None, val, variant.node.attrs.as_slice()));
13091313
}
13101314

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

0 commit comments

Comments
 (0)