Skip to content

Commit

Permalink
Fix #[derive] for empty tuple structs/variants
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Aug 17, 2016
1 parent 7ac11ca commit 28ed8b1
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/libsyntax/ext/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
}
fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
let pat = if subpats.is_empty() {
PatKind::Path(None, path)
PatKind::Struct(path, Vec::new(), false)
} else {
PatKind::TupleStruct(path, subpats, None)
};
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax_ext/deriving/decodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn decodable_substructure(cx: &mut ExtCtxt,
return match *substr.fields {
StaticStruct(_, ref summary) => {
let nfields = match *summary {
Unnamed(ref fields) => fields.len(),
Unnamed(ref fields, _) => fields.len(),
Named(ref fields) => fields.len(),
};
let read_struct_field = cx.ident_of("read_struct_field");
Expand Down Expand Up @@ -193,9 +193,9 @@ fn decode_static_fields<F>(cx: &mut ExtCtxt,
where F: FnMut(&mut ExtCtxt, Span, InternedString, usize) -> P<Expr>
{
match *fields {
Unnamed(ref fields) => {
Unnamed(ref fields, is_tuple) => {
let path_expr = cx.expr_path(outer_pat_path);
if fields.is_empty() {
if !is_tuple {
path_expr
} else {
let fields = fields.iter()
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax_ext/deriving/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ fn default_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructur
return match *substr.fields {
StaticStruct(_, ref summary) => {
match *summary {
Unnamed(ref fields) => {
if fields.is_empty() {
Unnamed(ref fields, is_tuple) => {
if !is_tuple {
cx.expr_ident(trait_span, substr.type_ident)
} else {
let exprs = fields.iter().map(|sp| default_call(*sp)).collect();
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax_ext/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ pub struct FieldInfo<'a> {

/// Fields for a static method
pub enum StaticFields {
/// Tuple structs/enum variants like this.
Unnamed(Vec<Span>),
/// Tuple and unit structs/enum variants like this.
Unnamed(Vec<Span>, bool /*is tuple*/),
/// Normal structs/struct variants.
Named(Vec<(Ident, Span)>),
}
Expand Down Expand Up @@ -1470,7 +1470,7 @@ impl<'a> TraitDef<'a> {
(_, false) => Named(named_idents),
// empty structs
_ if struct_def.is_struct() => Named(named_idents),
_ => Unnamed(just_spans),
_ => Unnamed(just_spans, struct_def.is_tuple()),
}
}

Expand Down
24 changes: 23 additions & 1 deletion src/test/run-pass-fulldeps/empty-struct-braces-derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// `#[derive(Trait)]` works for empty structs/variants with braces
// `#[derive(Trait)]` works for empty structs/variants with braces or parens.

#![feature(relaxed_adts)]
#![feature(rustc_private)]

extern crate serialize as rustc_serialize;
Expand All @@ -18,11 +19,16 @@ extern crate serialize as rustc_serialize;
Default, Debug, RustcEncodable, RustcDecodable)]
struct S {}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash,
Default, Debug, RustcEncodable, RustcDecodable)]
struct Z();

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash,
Debug, RustcEncodable, RustcDecodable)]
enum E {
V {},
U,
W(),
}

fn main() {
Expand All @@ -34,11 +40,27 @@ fn main() {
assert!(!(s < s1));
assert_eq!(format!("{:?}", s), "S");

let z = Z();
let z1 = z;
let z2 = z.clone();
assert_eq!(z, z1);
assert_eq!(z, z2);
assert!(!(z < z1));
assert_eq!(format!("{:?}", z), "Z");

let e = E::V {};
let e1 = e;
let e2 = e.clone();
assert_eq!(e, e1);
assert_eq!(e, e2);
assert!(!(e < e1));
assert_eq!(format!("{:?}", e), "V");

let e = E::W();
let e1 = e;
let e2 = e.clone();
assert_eq!(e, e1);
assert_eq!(e, e2);
assert!(!(e < e1));
assert_eq!(format!("{:?}", e), "W");
}

0 comments on commit 28ed8b1

Please sign in to comment.