From 4e8e607d8469a8882944233e4bb3ee45d2a6435a Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Mon, 22 Feb 2016 21:24:32 +0300 Subject: Fix #[derive] for empty structs with braces --- src/libsyntax_ext/deriving/generic/mod.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'src/libsyntax_ext/deriving/generic') diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs index bdba49f2f29..c0237a5d29a 100644 --- a/src/libsyntax_ext/deriving/generic/mod.rs +++ b/src/libsyntax_ext/deriving/generic/mod.rs @@ -300,7 +300,7 @@ pub enum StaticFields { /// A summary of the possible sets of fields. pub enum SubstructureFields<'a> { - Struct(Vec>), + Struct(&'a ast::VariantData, Vec>), /// Matching variants of the enum: variant index, ast::Variant, /// fields: the field name is only non-`None` in the case of a struct /// variant. @@ -981,7 +981,7 @@ impl<'a> MethodDef<'a> { type_ident, self_args, nonself_args, - &Struct(fields)); + &Struct(struct_def, fields)); // make a series of nested matches, to destructure the // structs. This is actually right-to-left, but it shouldn't @@ -1460,8 +1460,9 @@ impl<'a> TraitDef<'a> { fields in generic `derive`"), // named fields (_, false) => Named(named_idents), - // tuple structs (includes empty structs) - (_, _) => Unnamed(just_spans) + // empty structs + _ if struct_def.is_struct() => Named(named_idents), + _ => Unnamed(just_spans), } } @@ -1486,7 +1487,11 @@ impl<'a> TraitDef<'a> { P, &'a [ast::Attribute])>) { if struct_def.fields().is_empty() { - return (cx.pat_enum(self.span, struct_path, vec![]), vec![]); + if struct_def.is_struct() { + return (cx.pat_struct(self.span, struct_path, vec![]), vec![]); + } else { + return (cx.pat_enum(self.span, struct_path, vec![]), vec![]); + } } let mut paths = Vec::new(); @@ -1521,7 +1526,7 @@ impl<'a> TraitDef<'a> { // struct_type is definitely not Unknown, since struct_def.fields // must be nonempty to reach here - let pattern = if struct_type == Record { + let pattern = if struct_def.is_struct() { let field_pats = subpats.into_iter().zip(&ident_expr) .map(|(pat, &(_, id, _, _))| { // id is guaranteed to be Some @@ -1566,7 +1571,7 @@ pub fn cs_fold(use_foldl: bool, F: FnMut(&mut ExtCtxt, Span, P, P, &[P]) -> P, { match *substructure.fields { - EnumMatching(_, _, ref all_fields) | Struct(ref all_fields) => { + EnumMatching(_, _, ref all_fields) | Struct(_, ref all_fields) => { if use_foldl { all_fields.iter().fold(base, |old, field| { f(cx, @@ -1612,7 +1617,7 @@ pub fn cs_same_method(f: F, F: FnOnce(&mut ExtCtxt, Span, Vec>) -> P, { match *substructure.fields { - EnumMatching(_, _, ref all_fields) | Struct(ref all_fields) => { + EnumMatching(_, _, ref all_fields) | Struct(_, ref all_fields) => { // call self_n.method(other_1_n, other_2_n, ...) let called = all_fields.iter().map(|field| { cx.expr_method_call(field.span, -- cgit 1.4.1-3-g733a5