Skip to content

Commit 3142a12

Browse files
committed
syntax: allow passing struct field attributes to deriving static handlers
1 parent fae4c2b commit 3142a12

File tree

5 files changed

+44
-24
lines changed

5 files changed

+44
-24
lines changed

src/libsyntax/ext/deriving/decodable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ fn decode_static_fields(cx: &mut ExtCtxt,
178178
}
179179
Named(ref fields) => {
180180
// use the field's span to get nicer error messages.
181-
let fields = fields.iter().enumerate().map(|(i, &(name, span))| {
182-
let arg = getarg(cx, span, token::get_ident(name), i);
183-
cx.field_imm(span, name, arg)
181+
let fields = fields.iter().enumerate().map(|(i, field)| {
182+
let arg = getarg(cx, field.span, token::get_ident(field.name), i);
183+
cx.field_imm(field.span, field.name, arg)
184184
}).collect();
185185
cx.expr_struct_ident(trait_span, outer_pat_ident, fields)
186186
}

src/libsyntax/ext/deriving/default.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ fn default_substructure(cx: &mut ExtCtxt, trait_span: Span,
6969
}
7070
}
7171
Named(ref fields) => {
72-
let default_fields = fields.iter().map(|&(ident, span)| {
73-
cx.field_imm(span, ident, default_call(span))
72+
let default_fields = fields.iter().map(|field| {
73+
cx.field_imm(field.span, field.name, default_call(field.span))
7474
}).collect();
7575
cx.expr_struct_ident(trait_span, substr.type_ident, default_fields)
7676
}

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

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,20 @@ pub struct FieldInfo<'a> {
274274
pub attrs: &'a [ast::Attribute],
275275
}
276276

277+
pub struct StaticFieldInfo<'a> {
278+
pub span: Span,
279+
/// The field's ident.
280+
pub name: Ident,
281+
/// The attributes attached to this field.
282+
pub attrs: &'a [ast::Attribute],
283+
}
284+
277285
/// Fields for a static method
278-
pub enum StaticFields {
286+
pub enum StaticFields<'a> {
279287
/// Tuple structs/enum variants like this
280288
Unnamed(Vec<Span>),
281289
/// Normal structs/struct variants.
282-
Named(Vec<(Ident, Span)>),
290+
Named(Vec<(StaticFieldInfo<'a>)>),
283291
}
284292

285293
/// A summary of the possible sets of fields. See above for details
@@ -304,9 +312,9 @@ pub enum SubstructureFields<'a> {
304312
EnumNonMatchingCollapsed(Vec<Ident>, &'a [Gc<ast::Variant>], &'a [Ident]),
305313

306314
/// A static method where Self is a struct.
307-
StaticStruct(&'a ast::StructDef, StaticFields),
315+
StaticStruct(&'a ast::StructDef, StaticFields<'a>),
308316
/// A static method where Self is an enum.
309-
StaticEnum(&'a ast::EnumDef, Vec<(Ident, Span, StaticFields)>),
317+
StaticEnum(&'a ast::EnumDef, Vec<(Ident, Span, StaticFields<'a>)>),
310318
}
311319

312320

@@ -1187,25 +1195,37 @@ impl<'a> TraitDef<'a> {
11871195
to_set
11881196
}
11891197

1190-
fn summarise_struct(&self,
1191-
cx: &mut ExtCtxt,
1192-
struct_def: &StructDef) -> StaticFields {
1193-
let mut named_idents = Vec::new();
1198+
fn summarise_struct<'b>(&self,
1199+
cx: &mut ExtCtxt,
1200+
struct_def: &'b StructDef) -> StaticFields<'b> {
1201+
let mut named_fields = Vec::new();
11941202
let mut just_spans = Vec::new();
1195-
for field in struct_def.fields.iter(){
1196-
let sp = self.set_expn_info(cx, field.span);
1203+
1204+
for field in struct_def.fields.iter() {
1205+
let span = self.set_expn_info(cx, field.span);
1206+
11971207
match field.node.kind {
1198-
ast::NamedField(ident, _) => named_idents.push((ident, sp)),
1199-
ast::UnnamedField(..) => just_spans.push(sp),
1208+
ast::NamedField(ident, _) => {
1209+
let field_info = StaticFieldInfo {
1210+
span: span,
1211+
name: ident,
1212+
attrs: field.node.attrs.as_slice(),
1213+
};
1214+
1215+
named_fields.push(field_info);
1216+
}
1217+
ast::UnnamedField(..) => {
1218+
just_spans.push(span);
1219+
}
12001220
}
12011221
}
12021222

1203-
match (just_spans.is_empty(), named_idents.is_empty()) {
1223+
match (just_spans.is_empty(), named_fields.is_empty()) {
12041224
(false, false) => cx.span_bug(self.span,
12051225
"a struct with named and unnamed \
12061226
fields in generic `deriving`"),
12071227
// named fields
1208-
(_, false) => Named(named_idents),
1228+
(_, false) => Named(named_fields),
12091229
// tuple structs (includes empty structs)
12101230
(_, _) => Unnamed(just_spans)
12111231
}

src/libsyntax/ext/deriving/rand.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ fn rand_thing(cx: &mut ExtCtxt,
149149
}
150150
}
151151
Named(ref fields) => {
152-
let rand_fields = fields.iter().map(|&(ident, span)| {
153-
let e = rand_call(cx, span);
154-
cx.field_imm(span, ident, e)
152+
let rand_fields = fields.iter().map(|field| {
153+
let e = rand_call(cx, field.span);
154+
cx.field_imm(field.span, field.name, e)
155155
}).collect();
156156
cx.expr_struct_ident(trait_span, ctor_ident, rand_fields)
157157
}

src/libsyntax/ext/deriving/zero.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ fn zero_substructure(cx: &mut ExtCtxt, trait_span: Span,
8585
}
8686
}
8787
Named(ref fields) => {
88-
let zero_fields = fields.iter().map(|&(ident, span)| {
89-
cx.field_imm(span, ident, zero_call(span))
88+
let zero_fields = fields.iter().map(|field| {
89+
cx.field_imm(field.span, field.name, zero_call(field.span))
9090
}).collect();
9191
cx.expr_struct_ident(trait_span, substr.type_ident, zero_fields)
9292
}

0 commit comments

Comments
 (0)