Skip to content

Commit 7cc466d

Browse files
authored
Error on UDT enums with 0-element tuple variants (#863)
### What Emit a compiler error when creating UDT enums with 0-element tuple variants. ### Why Per #861, making empty-tuple variants and unit-variants unambiguous would introduce new complexity for anybody interpreting the XDR specs, so we just disallow the presumably rare empty-tuple variant form. ### Known limitations Hard to test compiler errors. Tested manually. Fixes #861
1 parent 7da5c73 commit 7cc466d

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

soroban-sdk-macros/src/derive_enum.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ pub fn derive_type_enum(
4444
Fields::Named(_) => {
4545
errors.push(Error::new(v.fields.span(), format!("enum variant {} has unsupported named fields", ident)));
4646
}
47+
Fields::Unnamed(_) if v.fields.is_empty() => {
48+
// Empty tuples are unsupported because it would require extra complexity
49+
// to distinguish them from unit-style variants.
50+
errors.push(Error::new(v.fields.span(), format!("enum variant {} is unsupported 0-element tuple", ident)));
51+
}
4752
_ => { }
4853
}
4954
let discriminant_const_sym_ident = format_ident!("DISCRIMINANT_SYM_{}", name.to_uppercase());
@@ -58,8 +63,8 @@ pub fn derive_type_enum(
5863
#discriminant_const_sym
5964
#discriminant_const_u64
6065
};
61-
let has_fields = v.fields.iter().next().is_some();
62-
if has_fields {
66+
let is_unit_variant = v.fields == Fields::Unit;
67+
if !is_unit_variant {
6368
let VariantTokens {
6469
spec_case, try_from, try_into, try_from_xdr, into_xdr
6570
} = map_tuple_variant(

0 commit comments

Comments
 (0)