Fix #[derive] for empty tuple structs/variants

This commit is contained in:
Vadim Petrochenkov 2016-08-15 21:28:17 +03:00
parent 7ac11cad3f
commit 28ed8b1592
5 changed files with 32 additions and 10 deletions

View File

@ -844,7 +844,7 @@ fn pat_ident_binding_mode(&self,
}
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)
};

View File

@ -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");
@ -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()

View File

@ -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();

View File

@ -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)>),
}
@ -1470,7 +1470,7 @@ fn summarise_struct(&self, cx: &mut ExtCtxt, struct_def: &VariantData) -> Static
(_, false) => Named(named_idents),
// empty structs
_ if struct_def.is_struct() => Named(named_idents),
_ => Unnamed(just_spans),
_ => Unnamed(just_spans, struct_def.is_tuple()),
}
}

View File

@ -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;
@ -18,11 +19,16 @@
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() {
@ -34,6 +40,14 @@ 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();
@ -41,4 +55,12 @@ fn main() {
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");
}