Skip to content

Commit 59ccb7b

Browse files
committed
Support deriving some traits for unions
1 parent 079c390 commit 59ccb7b

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

src/libsyntax_ext/deriving/generic/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,18 @@ impl<'a> TraitDef<'a> {
410410
ast::ItemKind::Enum(ref enum_def, ref generics) => {
411411
self.expand_enum_def(cx, enum_def, &item.attrs, item.ident, generics)
412412
}
413+
ast::ItemKind::Union(ref struct_def, ref generics) => {
414+
if self.supports_unions {
415+
self.expand_struct_def(cx, &struct_def, item.ident, generics)
416+
} else {
417+
cx.span_err(mitem.span,
418+
"this trait cannot be derived for unions");
419+
return;
420+
}
421+
}
413422
_ => {
414423
cx.span_err(mitem.span,
415-
"`derive` may only be applied to structs and enums");
424+
"`derive` may only be applied to structs, enums and unions");
416425
return;
417426
}
418427
};

src/test/compile-fail/union-derive.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Most traits cannot be derived for unions.
12+
13+
#![feature(untagged_unions)]
14+
15+
#[derive(
16+
Clone, //~ ERROR this trait cannot be derived for unions
17+
PartialEq, //~ ERROR this trait cannot be derived for unions
18+
Eq, //~ ERROR this trait cannot be derived for unions
19+
PartialOrd, //~ ERROR this trait cannot be derived for unions
20+
Ord, //~ ERROR this trait cannot be derived for unions
21+
Hash, //~ ERROR this trait cannot be derived for unions
22+
Default, //~ ERROR this trait cannot be derived for unions
23+
Debug, //~ ERROR this trait cannot be derived for unions
24+
)]
25+
union U {
26+
a: u8,
27+
b: u16,
28+
}
29+
30+
fn main() {}

src/test/run-pass/union-derive.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Some traits can be derived for unions.
12+
13+
#![feature(untagged_unions)]
14+
15+
#[derive(
16+
Copy,
17+
)]
18+
union U {
19+
a: u8,
20+
b: u16,
21+
}
22+
23+
impl Clone for U {
24+
fn clone(&self) -> Self { *self }
25+
}
26+
27+
fn main() {
28+
let u = U { b: 0 };
29+
let u1 = u;
30+
let u2 = u.clone();
31+
}

0 commit comments

Comments
 (0)