Skip to content

Commit

Permalink
Auto merge of #50383 - stevepentland:union-derive, r=oli-obk
Browse files Browse the repository at this point in the history
Add ability to apply custom derive to union types.

The Union item type has been included in the allowed types for a custom derive.
fyi @abonander

Closes #50223
  • Loading branch information
bors committed Jun 19, 2018
2 parents 1cfb628 + 14abb55 commit 1080203
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/libsyntax_ext/deriving/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,17 @@ impl MultiItemModifier for ProcMacroDerive {
Annotatable::Stmt(_) |
Annotatable::Expr(_) => {
ecx.span_err(span, "proc-macro derives may only be \
applied to struct/enum items");
applied to a struct, enum, or union");
return Vec::new()
}
};
match item.node {
ItemKind::Struct(..) |
ItemKind::Enum(..) => {},
ItemKind::Enum(..) |
ItemKind::Union(..) => {},
_ => {
ecx.span_err(span, "proc-macro derives may only be \
applied to struct/enum items");
applied to a struct, enum, or union");
return Vec::new()
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/run-pass-fulldeps/proc-macro/auxiliary/derive-union.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2018 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.

// no-prefer-dynamic

#![crate_type = "proc-macro"]

extern crate proc_macro;

use proc_macro::TokenStream;

#[proc_macro_derive(UnionTest)]
pub fn derive(input: TokenStream) -> TokenStream {
let input = input.to_string();
assert!(input.contains("#[repr(C)]"));
assert!(input.contains("union Test {"));
assert!(input.contains("a: u8,"));
assert!(input.contains("}"));
"".parse().unwrap()
}
25 changes: 25 additions & 0 deletions src/test/run-pass-fulldeps/proc-macro/derive-union.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2018 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.

// aux-build:derive-union.rs
// ignore-stage1

#[macro_use]
extern crate derive_union;

#[repr(C)]
#[derive(UnionTest)]
union Test {
a: u8,
}

fn main() {
let t = Test { a: 0 };
}

0 comments on commit 1080203

Please sign in to comment.