diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2016-08-24 21:10:19 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2016-09-03 13:39:35 +0300 |
| commit | 59ccb7b6dbaf3a590cf3a234661aa7dcc2188aed (patch) | |
| tree | 5df2908d7ef4fe47de984b612ab9cd62d50ac02e | |
| parent | 079c390d5089735b5eaa8b06ddb3beedcddbee7d (diff) | |
| download | rust-59ccb7b6dbaf3a590cf3a234661aa7dcc2188aed.tar.gz rust-59ccb7b6dbaf3a590cf3a234661aa7dcc2188aed.zip | |
Support deriving some traits for unions
| -rw-r--r-- | src/libsyntax_ext/deriving/generic/mod.rs | 11 | ||||
| -rw-r--r-- | src/test/compile-fail/union-derive.rs | 30 | ||||
| -rw-r--r-- | src/test/run-pass/union-derive.rs | 31 |
3 files changed, 71 insertions, 1 deletions
diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs index 5c636d43a71..b37d5332983 100644 --- a/src/libsyntax_ext/deriving/generic/mod.rs +++ b/src/libsyntax_ext/deriving/generic/mod.rs @@ -410,9 +410,18 @@ impl<'a> TraitDef<'a> { ast::ItemKind::Enum(ref enum_def, ref generics) => { self.expand_enum_def(cx, enum_def, &item.attrs, item.ident, generics) } + ast::ItemKind::Union(ref struct_def, ref generics) => { + if self.supports_unions { + self.expand_struct_def(cx, &struct_def, item.ident, generics) + } else { + cx.span_err(mitem.span, + "this trait cannot be derived for unions"); + return; + } + } _ => { cx.span_err(mitem.span, - "`derive` may only be applied to structs and enums"); + "`derive` may only be applied to structs, enums and unions"); return; } }; diff --git a/src/test/compile-fail/union-derive.rs b/src/test/compile-fail/union-derive.rs new file mode 100644 index 00000000000..0f78e96f640 --- /dev/null +++ b/src/test/compile-fail/union-derive.rs @@ -0,0 +1,30 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Most traits cannot be derived for unions. + +#![feature(untagged_unions)] + +#[derive( + Clone, //~ ERROR this trait cannot be derived for unions + PartialEq, //~ ERROR this trait cannot be derived for unions + Eq, //~ ERROR this trait cannot be derived for unions + PartialOrd, //~ ERROR this trait cannot be derived for unions + Ord, //~ ERROR this trait cannot be derived for unions + Hash, //~ ERROR this trait cannot be derived for unions + Default, //~ ERROR this trait cannot be derived for unions + Debug, //~ ERROR this trait cannot be derived for unions +)] +union U { + a: u8, + b: u16, +} + +fn main() {} diff --git a/src/test/run-pass/union-derive.rs b/src/test/run-pass/union-derive.rs new file mode 100644 index 00000000000..b71c23990a4 --- /dev/null +++ b/src/test/run-pass/union-derive.rs @@ -0,0 +1,31 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Some traits can be derived for unions. + +#![feature(untagged_unions)] + +#[derive( + Copy, +)] +union U { + a: u8, + b: u16, +} + +impl Clone for U { + fn clone(&self) -> Self { *self } +} + +fn main() { + let u = U { b: 0 }; + let u1 = u; + let u2 = u.clone(); +} |
