Skip to content

syntax::ext: Make type errors in deriving point to the field itself. #10355

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

Merged
merged 1 commit into from
Nov 8, 2013
Merged
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
10 changes: 5 additions & 5 deletions src/libsyntax/ext/deriving/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,21 @@ fn cs_clone(
}

match *all_fields {
[(None, _, _), .. _] => {
[FieldInfo { name: None, _ }, .. _] => {
// enum-like
let subcalls = all_fields.map(|&(_, self_f, _)| subcall(self_f));
let subcalls = all_fields.map(|field| subcall(field.self_));
cx.expr_call_ident(span, ctor_ident, subcalls)
},
_ => {
// struct-like
let fields = do all_fields.map |&(o_id, self_f, _)| {
let ident = match o_id {
let fields = do all_fields.map |field| {
let ident = match field.name {
Some(i) => i,
None => cx.span_bug(span,
format!("unnamed field in normal struct in `deriving({})`",
name))
};
cx.field_imm(span, ident, subcall(self_f))
cx.field_imm(span, ident, subcall(field.self_))
};

if fields.is_empty() {
Expand Down
81 changes: 35 additions & 46 deletions src/libsyntax/ext/deriving/decodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ The compiler code necessary for #[deriving(Decodable)]. See
encodable.rs for more.
*/

use std::vec;

use ast::{MetaItem, item, Expr, MutMutable};
use ast::{MetaItem, item, Expr, MutMutable, Ident};
use codemap::Span;
use ext::base::ExtCtxt;
use ext::build::AstBuilder;
Expand Down Expand Up @@ -66,37 +64,18 @@ fn decodable_substructure(cx: @ExtCtxt, span: Span,
return match *substr.fields {
StaticStruct(_, ref summary) => {
let nfields = match *summary {
Left(n) => n, Right(ref fields) => fields.len()
Unnamed(ref fields) => fields.len(),
Named(ref fields) => fields.len()
};
let read_struct_field = cx.ident_of("read_struct_field");

let getarg = |name: @str, field: uint| {
let result = do decode_static_fields(cx, span, substr.type_ident,
summary) |span, name, field| {
cx.expr_method_call(span, blkdecoder, read_struct_field,
~[cx.expr_str(span, name),
cx.expr_uint(span, field),
lambdadecode])
};

let result = match *summary {
Left(n) => {
if n == 0 {
cx.expr_ident(span, substr.type_ident)
} else {
let mut fields = vec::with_capacity(n);
for i in range(0, n) {
fields.push(getarg(format!("_field{}", i).to_managed(), i));
}
cx.expr_call_ident(span, substr.type_ident, fields)
}
}
Right(ref fields) => {
let fields = do fields.iter().enumerate().map |(i, f)| {
cx.field_imm(span, *f, getarg(cx.str_of(*f), i))
}.collect();
cx.expr_struct_ident(span, substr.type_ident, fields)
}
};

cx.expr_method_call(span, decoder, cx.ident_of("read_struct"),
~[cx.expr_str(span, cx.str_of(substr.type_ident)),
cx.expr_uint(span, nfields),
Expand All @@ -113,31 +92,13 @@ fn decodable_substructure(cx: @ExtCtxt, span: Span,
let (name, parts) = match *f { (i, ref p) => (i, p) };
variants.push(cx.expr_str(span, cx.str_of(name)));

let getarg = |field: uint| {
let decoded = do decode_static_fields(cx, span, name,
parts) |span, _, field| {
cx.expr_method_call(span, blkdecoder, rvariant_arg,
~[cx.expr_uint(span, field),
lambdadecode])
};

let decoded = match *parts {
Left(n) => {
if n == 0 {
cx.expr_ident(span, name)
} else {
let mut fields = vec::with_capacity(n);
for i in range(0u, n) {
fields.push(getarg(i));
}
cx.expr_call_ident(span, name, fields)
}
}
Right(ref fields) => {
let fields = do fields.iter().enumerate().map |(i, f)| {
cx.field_imm(span, *f, getarg(i))
}.collect();
cx.expr_struct_ident(span, name, fields)
}
};
arms.push(cx.arm(span,
~[cx.pat_lit(span, cx.expr_uint(span, i))],
decoded));
Expand All @@ -158,3 +119,31 @@ fn decodable_substructure(cx: @ExtCtxt, span: Span,
_ => cx.bug("expected StaticEnum or StaticStruct in deriving(Decodable)")
};
}

/// Create a decoder for a single enum variant/struct:
/// - `outer_pat_ident` is the name of this enum variant/struct
/// - `getarg` should retrieve the `uint`-th field with name `@str`.
fn decode_static_fields(cx: @ExtCtxt, outer_span: Span, outer_pat_ident: Ident,
fields: &StaticFields,
getarg: &fn(Span, @str, uint) -> @Expr) -> @Expr {
match *fields {
Unnamed(ref fields) => {
if fields.is_empty() {
cx.expr_ident(outer_span, outer_pat_ident)
} else {
let fields = do fields.iter().enumerate().map |(i, &span)| {
getarg(span, format!("_field{}", i).to_managed(), i)
}.collect();

cx.expr_call_ident(outer_span, outer_pat_ident, fields)
}
}
Named(ref fields) => {
// use the field's span to get nicer error messages.
let fields = do fields.iter().enumerate().map |(i, &(name, span))| {
cx.field_imm(span, name, getarg(span, cx.str_of(name), i))
}.collect();
cx.expr_struct_ident(outer_span, outer_pat_ident, fields)
}
}
}
16 changes: 7 additions & 9 deletions src/libsyntax/ext/deriving/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use ext::base::ExtCtxt;
use ext::build::AstBuilder;
use ext::deriving::generic::*;

use std::vec;

pub fn expand_deriving_default(cx: @ExtCtxt,
span: Span,
mitem: @MetaItem,
Expand Down Expand Up @@ -47,22 +45,22 @@ fn default_substructure(cx: @ExtCtxt, span: Span, substr: &Substructure) -> @Exp
cx.ident_of("Default"),
cx.ident_of("default")
];
let default_call = cx.expr_call_global(span, default_ident.clone(), ~[]);
let default_call = |span| cx.expr_call_global(span, default_ident.clone(), ~[]);

return match *substr.fields {
StaticStruct(_, ref summary) => {
match *summary {
Left(count) => {
if count == 0 {
Unnamed(ref fields) => {
if fields.is_empty() {
cx.expr_ident(span, substr.type_ident)
} else {
let exprs = vec::from_elem(count, default_call);
let exprs = fields.map(|sp| default_call(*sp));
cx.expr_call_ident(span, substr.type_ident, exprs)
}
}
Right(ref fields) => {
let default_fields = do fields.map |ident| {
cx.field_imm(span, *ident, default_call)
Named(ref fields) => {
let default_fields = do fields.map |&(ident, span)| {
cx.field_imm(span, ident, default_call(span))
};
cx.expr_struct_ident(span, substr.type_ident, default_fields)
}
Expand Down
11 changes: 5 additions & 6 deletions src/libsyntax/ext/deriving/encodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ fn encodable_substructure(cx: @ExtCtxt, span: Span,
let emit_struct_field = cx.ident_of("emit_struct_field");
let mut stmts = ~[];
for (i, f) in fields.iter().enumerate() {
let (name, val) = match *f {
(Some(id), e, _) => (cx.str_of(id), e),
(None, e, _) => (format!("_field{}", i).to_managed(), e)
let name = match f.name {
Some(id) => cx.str_of(id),
None => format!("_field{}", i).to_managed()
};
let enc = cx.expr_method_call(span, val, encode, ~[blkencoder]);
let enc = cx.expr_method_call(span, f.self_, encode, ~[blkencoder]);
let lambda = cx.lambda_expr_1(span, enc, blkarg);
let call = cx.expr_method_call(span, blkencoder,
emit_struct_field,
Expand All @@ -154,8 +154,7 @@ fn encodable_substructure(cx: @ExtCtxt, span: Span,
let emit_variant_arg = cx.ident_of("emit_enum_variant_arg");
let mut stmts = ~[];
for (i, f) in fields.iter().enumerate() {
let val = match *f { (_, e, _) => e };
let enc = cx.expr_method_call(span, val, encode, ~[blkencoder]);
let enc = cx.expr_method_call(span, f.self_, encode, ~[blkencoder]);
let lambda = cx.lambda_expr_1(span, enc, blkarg);
let call = cx.expr_method_call(span, blkencoder,
emit_variant_arg,
Expand Down
Loading