File tree 4 files changed +34
-6
lines changed 4 files changed +34
-6
lines changed Original file line number Diff line number Diff line change 1
1
use proc_macro2:: TokenStream as TokenStream2 ;
2
- use syn:: DeriveInput ;
2
+ use syn:: { DeriveInput , Fields } ;
3
3
4
4
pub ( crate ) fn derive_export ( input : & DeriveInput ) -> syn:: Result < TokenStream2 > {
5
5
let derived_enum = match & input. data {
@@ -17,11 +17,23 @@ pub(crate) fn derive_export(input: &DeriveInput) -> syn::Result<TokenStream2> {
17
17
}
18
18
19
19
fn impl_export ( enum_ty : & syn:: Ident , data : & syn:: DataEnum ) -> syn:: Result < TokenStream2 > {
20
- let mappings = data. variants . iter ( ) . map ( |variant| {
21
- let key = & variant. ident ;
22
- let val = quote ! { #enum_ty:: #key as i64 } ;
23
- quote ! { ( stringify!( #key) . to_string( ) , #val) }
24
- } ) ;
20
+ let mappings = {
21
+ let mut m = Vec :: with_capacity ( data. variants . len ( ) ) ;
22
+
23
+ for variant in & data. variants {
24
+ if !matches ! ( variant. fields, Fields :: Unit ) {
25
+ return Err ( syn:: Error :: new (
26
+ variant. ident . span ( ) ,
27
+ "#[derive(Export)] only supports fieldless enums" ,
28
+ ) ) ;
29
+ }
30
+ let key = & variant. ident ;
31
+ let val = quote ! { #enum_ty:: #key as i64 } ;
32
+ m. push ( quote ! { ( stringify!( #key) . to_string( ) , #val) } ) ;
33
+ }
34
+
35
+ m
36
+ } ;
25
37
let impl_block = quote ! {
26
38
impl :: gdnative:: export:: Export for #enum_ty {
27
39
type Hint = :: gdnative:: export:: hint:: IntHint <i64 >;
Original file line number Diff line number Diff line change @@ -40,6 +40,9 @@ fn ui_tests() {
40
40
t. compile_fail ( "tests/ui/from_variant_fail_07.rs" ) ;
41
41
t. compile_fail ( "tests/ui/from_variant_fail_08.rs" ) ;
42
42
t. compile_fail ( "tests/ui/from_variant_fail_09.rs" ) ;
43
+
44
+ // Export
45
+ t. compile_fail ( "tests/ui/export_fail_01.rs" ) ;
43
46
}
44
47
45
48
// FIXME(rust/issues/54725): Full path spans are only available on nightly as of now
Original file line number Diff line number Diff line change
1
+ use gdnative:: prelude:: * ;
2
+
3
+ #[ derive( Export , ToVariant ) ]
4
+ pub enum Foo {
5
+ Bar ( String ) ,
6
+ }
7
+
8
+ fn main ( ) { }
Original file line number Diff line number Diff line change
1
+ error: #[derive(Export)] only supports fieldless enums
2
+ --> tests/ui/export_fail_01.rs:5:5
3
+ |
4
+ 5 | Bar(String),
5
+ | ^^^
You can’t perform that action at this time.
0 commit comments