Skip to content

Commit 1080203

Browse files
committed
Auto merge of #50383 - stevepentland:union-derive, r=oli-obk
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
2 parents 1cfb628 + 14abb55 commit 1080203

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

src/libsyntax_ext/deriving/custom.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,17 @@ impl MultiItemModifier for ProcMacroDerive {
5757
Annotatable::Stmt(_) |
5858
Annotatable::Expr(_) => {
5959
ecx.span_err(span, "proc-macro derives may only be \
60-
applied to struct/enum items");
60+
applied to a struct, enum, or union");
6161
return Vec::new()
6262
}
6363
};
6464
match item.node {
6565
ItemKind::Struct(..) |
66-
ItemKind::Enum(..) => {},
66+
ItemKind::Enum(..) |
67+
ItemKind::Union(..) => {},
6768
_ => {
6869
ecx.span_err(span, "proc-macro derives may only be \
69-
applied to struct/enum items");
70+
applied to a struct, enum, or union");
7071
return Vec::new()
7172
}
7273
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2018 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+
// no-prefer-dynamic
12+
13+
#![crate_type = "proc-macro"]
14+
15+
extern crate proc_macro;
16+
17+
use proc_macro::TokenStream;
18+
19+
#[proc_macro_derive(UnionTest)]
20+
pub fn derive(input: TokenStream) -> TokenStream {
21+
let input = input.to_string();
22+
assert!(input.contains("#[repr(C)]"));
23+
assert!(input.contains("union Test {"));
24+
assert!(input.contains("a: u8,"));
25+
assert!(input.contains("}"));
26+
"".parse().unwrap()
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2018 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+
// aux-build:derive-union.rs
12+
// ignore-stage1
13+
14+
#[macro_use]
15+
extern crate derive_union;
16+
17+
#[repr(C)]
18+
#[derive(UnionTest)]
19+
union Test {
20+
a: u8,
21+
}
22+
23+
fn main() {
24+
let t = Test { a: 0 };
25+
}

0 commit comments

Comments
 (0)