File tree Expand file tree Collapse file tree 4 files changed +34
-6
lines changed Expand file tree Collapse file tree 4 files changed +34
-6
lines changed Original file line number Diff line number Diff line change 11use proc_macro2:: TokenStream as TokenStream2 ;
2- use syn:: DeriveInput ;
2+ use syn:: { DeriveInput , Fields } ;
33
44pub ( crate ) fn derive_export ( input : & DeriveInput ) -> syn:: Result < TokenStream2 > {
55 let derived_enum = match & input. data {
@@ -17,11 +17,23 @@ pub(crate) fn derive_export(input: &DeriveInput) -> syn::Result<TokenStream2> {
1717}
1818
1919fn 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+ } ;
2537 let impl_block = quote ! {
2638 impl :: gdnative:: export:: Export for #enum_ty {
2739 type Hint = :: gdnative:: export:: hint:: IntHint <i64 >;
Original file line number Diff line number Diff line change @@ -40,6 +40,9 @@ fn ui_tests() {
4040 t. compile_fail ( "tests/ui/from_variant_fail_07.rs" ) ;
4141 t. compile_fail ( "tests/ui/from_variant_fail_08.rs" ) ;
4242 t. compile_fail ( "tests/ui/from_variant_fail_09.rs" ) ;
43+
44+ // Export
45+ t. compile_fail ( "tests/ui/export_fail_01.rs" ) ;
4346}
4447
4548// 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