Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend #[deriving(...)] helpers to expose field attributes #17089

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ pub enum Ty_ {
TyProc(Gc<ClosureTy>),
TyBareFn(Gc<BareFnTy>),
TyUnboxedFn(Gc<UnboxedFnTy>),
TyTup(Vec<P<Ty>> ),
TyTup(Vec<P<Ty>>),
TyPath(Path, Option<TyParamBounds>, NodeId), // for #7264; see above
/// No-op; kept solely so that we can pretty-print faithfully
TyParen(P<Ty>),
Expand Down
30 changes: 12 additions & 18 deletions src/libsyntax/ext/deriving/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt,
ret_ty: Self,
attributes: attrs,
combine_substructure: combine_substructure(|c, s, sub| {
cs_clone("Clone", c, s, sub)
cs_clone(c, s, sub)
}),
}
)
Expand All @@ -50,16 +50,17 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt,
}

fn cs_clone(
name: &str,
cx: &mut ExtCtxt, trait_span: Span,
cx: &mut ExtCtxt,
trait_span: Span,
substr: &Substructure) -> Gc<Expr> {
let clone_ident = substr.method_ident;
let ctor_ident;
let all_fields;
let subcall = |field: &FieldInfo|
cx.expr_method_call(field.span, field.self_, clone_ident, Vec::new());
let subcall = |field: &FieldInfo| {
cx.expr_method_call(field.span, field.self_, clone_ident, Vec::new())
};

match *substr.fields {
match substr.fields {
Struct(ref af) => {
ctor_ident = substr.type_ident;
all_fields = af;
Expand All @@ -68,16 +69,11 @@ fn cs_clone(
ctor_ident = variant.node.name;
all_fields = af;
},
EnumNonMatchingCollapsed (..) => {
cx.span_bug(trait_span,
format!("non-matching enum variants in \
`deriving({})`",
name).as_slice())
EnumNonMatchingCollapsed(..) => {
cx.span_bug(trait_span, "non-matching enum variants in `#[deriving(Clone)]`")
}
StaticEnum(..) | StaticStruct(..) => {
cx.span_bug(trait_span,
format!("static method in `deriving({})`",
name).as_slice())
cx.span_bug(trait_span, "static method in `#[deriving(Clone)]`")
}
}

Expand All @@ -91,10 +87,8 @@ fn cs_clone(
let ident = match field.name {
Some(i) => i,
None => {
cx.span_bug(trait_span,
format!("unnamed field in normal struct in \
`deriving({})`",
name).as_slice())
cx.span_bug(trait_span, "unnamed field in normal struct in \
`#[deriving(Clone)]`")
}
};
cx.field_imm(field.span, ident, subcall(field))
Expand Down
10 changes: 5 additions & 5 deletions src/libsyntax/ext/deriving/decodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn decodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
let calldecode = cx.expr_call_global(trait_span, recurse, vec!(blkdecoder));
let lambdadecode = cx.lambda_expr_1(trait_span, calldecode, blkarg);

return match *substr.fields {
match substr.fields {
StaticStruct(_, ref summary) => {
let nfields = match *summary {
Unnamed(ref fields) => fields.len(),
Expand Down Expand Up @@ -149,7 +149,7 @@ fn decodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
))
}
_ => cx.bug("expected StaticEnum or StaticStruct in deriving(Decodable)")
};
}
}

/// Create a decoder for a single enum variant/struct:
Expand Down Expand Up @@ -178,9 +178,9 @@ fn decode_static_fields(cx: &mut ExtCtxt,
}
Named(ref fields) => {
// use the field's span to get nicer error messages.
let fields = fields.iter().enumerate().map(|(i, &(name, span))| {
let arg = getarg(cx, span, token::get_ident(name), i);
cx.field_imm(span, name, arg)
let fields = fields.iter().enumerate().map(|(i, field)| {
let arg = getarg(cx, field.span, token::get_ident(field.name), i);
cx.field_imm(field.span, field.name, arg)
}).collect();
cx.expr_struct_ident(trait_span, outer_pat_ident, fields)
}
Expand Down
8 changes: 4 additions & 4 deletions src/libsyntax/ext/deriving/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn default_substructure(cx: &mut ExtCtxt, trait_span: Span,
);
let default_call = |span| cx.expr_call_global(span, default_ident.clone(), Vec::new());

return match *substr.fields {
match substr.fields {
StaticStruct(_, ref summary) => {
match *summary {
Unnamed(ref fields) => {
Expand All @@ -69,8 +69,8 @@ fn default_substructure(cx: &mut ExtCtxt, trait_span: Span,
}
}
Named(ref fields) => {
let default_fields = fields.iter().map(|&(ident, span)| {
cx.field_imm(span, ident, default_call(span))
let default_fields = fields.iter().map(|field| {
cx.field_imm(field.span, field.name, default_call(field.span))
}).collect();
cx.expr_struct_ident(trait_span, substr.type_ident, default_fields)
}
Expand All @@ -82,5 +82,5 @@ fn default_substructure(cx: &mut ExtCtxt, trait_span: Span,
cx.expr_uint(trait_span, 0)
}
_ => cx.span_bug(trait_span, "Non-static method in `deriving(Default)`")
};
}
}
4 changes: 2 additions & 2 deletions src/libsyntax/ext/deriving/encodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
let blkencoder = cx.expr_ident(trait_span, blkarg);
let encode = cx.ident_of("encode");

return match *substr.fields {
match substr.fields {
Struct(ref fields) => {
let emit_struct_field = cx.ident_of("emit_struct_field");
let mut stmts = Vec::new();
Expand Down Expand Up @@ -245,5 +245,5 @@ fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span,
}

_ => cx.bug("expected Struct or EnumMatching in deriving(Encodable)")
};
}
}
Loading