mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-04 19:29:07 +00:00
Fix #[derive] for empty tuple structs/variants
This commit is contained in:
parent
7ac11cad3f
commit
28ed8b1592
@ -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> {
|
fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
|
||||||
let pat = if subpats.is_empty() {
|
let pat = if subpats.is_empty() {
|
||||||
PatKind::Path(None, path)
|
PatKind::Struct(path, Vec::new(), false)
|
||||||
} else {
|
} else {
|
||||||
PatKind::TupleStruct(path, subpats, None)
|
PatKind::TupleStruct(path, subpats, None)
|
||||||
};
|
};
|
||||||
|
@ -110,7 +110,7 @@ fn decodable_substructure(cx: &mut ExtCtxt,
|
|||||||
return match *substr.fields {
|
return match *substr.fields {
|
||||||
StaticStruct(_, ref summary) => {
|
StaticStruct(_, ref summary) => {
|
||||||
let nfields = match *summary {
|
let nfields = match *summary {
|
||||||
Unnamed(ref fields) => fields.len(),
|
Unnamed(ref fields, _) => fields.len(),
|
||||||
Named(ref fields) => fields.len(),
|
Named(ref fields) => fields.len(),
|
||||||
};
|
};
|
||||||
let read_struct_field = cx.ident_of("read_struct_field");
|
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>
|
where F: FnMut(&mut ExtCtxt, Span, InternedString, usize) -> P<Expr>
|
||||||
{
|
{
|
||||||
match *fields {
|
match *fields {
|
||||||
Unnamed(ref fields) => {
|
Unnamed(ref fields, is_tuple) => {
|
||||||
let path_expr = cx.expr_path(outer_pat_path);
|
let path_expr = cx.expr_path(outer_pat_path);
|
||||||
if fields.is_empty() {
|
if !is_tuple {
|
||||||
path_expr
|
path_expr
|
||||||
} else {
|
} else {
|
||||||
let fields = fields.iter()
|
let fields = fields.iter()
|
||||||
|
@ -57,8 +57,8 @@ fn default_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructur
|
|||||||
return match *substr.fields {
|
return match *substr.fields {
|
||||||
StaticStruct(_, ref summary) => {
|
StaticStruct(_, ref summary) => {
|
||||||
match *summary {
|
match *summary {
|
||||||
Unnamed(ref fields) => {
|
Unnamed(ref fields, is_tuple) => {
|
||||||
if fields.is_empty() {
|
if !is_tuple {
|
||||||
cx.expr_ident(trait_span, substr.type_ident)
|
cx.expr_ident(trait_span, substr.type_ident)
|
||||||
} else {
|
} else {
|
||||||
let exprs = fields.iter().map(|sp| default_call(*sp)).collect();
|
let exprs = fields.iter().map(|sp| default_call(*sp)).collect();
|
||||||
|
@ -294,8 +294,8 @@ pub struct FieldInfo<'a> {
|
|||||||
|
|
||||||
/// Fields for a static method
|
/// Fields for a static method
|
||||||
pub enum StaticFields {
|
pub enum StaticFields {
|
||||||
/// Tuple structs/enum variants like this.
|
/// Tuple and unit structs/enum variants like this.
|
||||||
Unnamed(Vec<Span>),
|
Unnamed(Vec<Span>, bool /*is tuple*/),
|
||||||
/// Normal structs/struct variants.
|
/// Normal structs/struct variants.
|
||||||
Named(Vec<(Ident, Span)>),
|
Named(Vec<(Ident, Span)>),
|
||||||
}
|
}
|
||||||
@ -1470,7 +1470,7 @@ impl<'a> TraitDef<'a> {
|
|||||||
(_, false) => Named(named_idents),
|
(_, false) => Named(named_idents),
|
||||||
// empty structs
|
// empty structs
|
||||||
_ if struct_def.is_struct() => Named(named_idents),
|
_ if struct_def.is_struct() => Named(named_idents),
|
||||||
_ => Unnamed(just_spans),
|
_ => Unnamed(just_spans, struct_def.is_tuple()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,8 +8,9 @@
|
|||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// 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)]
|
#![feature(rustc_private)]
|
||||||
|
|
||||||
extern crate serialize as rustc_serialize;
|
extern crate serialize as rustc_serialize;
|
||||||
@ -18,11 +19,16 @@ extern crate serialize as rustc_serialize;
|
|||||||
Default, Debug, RustcEncodable, RustcDecodable)]
|
Default, Debug, RustcEncodable, RustcDecodable)]
|
||||||
struct S {}
|
struct S {}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash,
|
||||||
|
Default, Debug, RustcEncodable, RustcDecodable)]
|
||||||
|
struct Z();
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash,
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash,
|
||||||
Debug, RustcEncodable, RustcDecodable)]
|
Debug, RustcEncodable, RustcDecodable)]
|
||||||
enum E {
|
enum E {
|
||||||
V {},
|
V {},
|
||||||
U,
|
U,
|
||||||
|
W(),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -34,6 +40,14 @@ fn main() {
|
|||||||
assert!(!(s < s1));
|
assert!(!(s < s1));
|
||||||
assert_eq!(format!("{:?}", s), "S");
|
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 e = E::V {};
|
||||||
let e1 = e;
|
let e1 = e;
|
||||||
let e2 = e.clone();
|
let e2 = e.clone();
|
||||||
@ -41,4 +55,12 @@ fn main() {
|
|||||||
assert_eq!(e, e2);
|
assert_eq!(e, e2);
|
||||||
assert!(!(e < e1));
|
assert!(!(e < e1));
|
||||||
assert_eq!(format!("{:?}", e), "V");
|
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");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user