Fixed Reflect derive macro on empty enums (#18277) (#18298)

obtaining a reference to an empty enum is not possible in Rust, so I
just replaced any match on self with an `unreachable!()`
I checked if an enum is empty by checking if the `variant_patterns` vec
of the `EnumVariantOutputData` struct is empty
Fixes #18277

## Testing

I added one new unit test.
``` rust
#[test]
fn should_allow_empty_enums() {
    #[derive(Reflect)]
    enum Empty {}

    assert_impl_all!(Empty: Reflect);
}
```
This commit is contained in:
Wuketuke 2025-03-17 18:13:55 +00:00 committed by GitHub
parent af61ec2bc1
commit 5c3368fbcf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 1 deletions

View File

@ -937,7 +937,10 @@ impl<'a> ReflectEnum<'a> {
} }
}; };
let body = if self.meta.is_remote_wrapper() { let body = if variant_patterns.is_empty() {
// enum variant is empty, so &self will never exist
quote!(unreachable!())
} else if self.meta.is_remote_wrapper() {
quote! { quote! {
let #this = <Self as #bevy_reflect_path::ReflectRemote>::as_remote(self); let #this = <Self as #bevy_reflect_path::ReflectRemote>::as_remote(self);
#FQResult::Ok(#bevy_reflect_path::__macro_exports::alloc_utils::Box::new(<Self as #bevy_reflect_path::ReflectRemote>::into_wrapper(#inner))) #FQResult::Ok(#bevy_reflect_path::__macro_exports::alloc_utils::Box::new(<Self as #bevy_reflect_path::ReflectRemote>::into_wrapper(#inner)))

View File

@ -2643,6 +2643,14 @@ bevy_reflect::tests::Test {
assert_not_impl_all!(Foo<Baz>: Reflect); assert_not_impl_all!(Foo<Baz>: Reflect);
} }
#[test]
fn should_allow_empty_enums() {
#[derive(Reflect)]
enum Empty {}
assert_impl_all!(Empty: Reflect);
}
#[test] #[test]
fn recursive_typed_storage_does_not_hang() { fn recursive_typed_storage_does_not_hang() {
#[derive(Reflect)] #[derive(Reflect)]