From 5c3368fbcfdc93025a8e5560349bd80b0c81fd07 Mon Sep 17 00:00:00 2001 From: Wuketuke <59253838+Wuketuke@users.noreply.github.com> Date: Mon, 17 Mar 2025 18:13:55 +0000 Subject: [PATCH] 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); } ``` --- crates/bevy_reflect/derive/src/derive_data.rs | 5 ++++- crates/bevy_reflect/src/lib.rs | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/bevy_reflect/derive/src/derive_data.rs b/crates/bevy_reflect/derive/src/derive_data.rs index bcff582a4e..eeb6c6d24a 100644 --- a/crates/bevy_reflect/derive/src/derive_data.rs +++ b/crates/bevy_reflect/derive/src/derive_data.rs @@ -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! { let #this = ::as_remote(self); #FQResult::Ok(#bevy_reflect_path::__macro_exports::alloc_utils::Box::new(::into_wrapper(#inner))) diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index c3ad8c6bd6..64d07513ea 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -2643,6 +2643,14 @@ bevy_reflect::tests::Test { assert_not_impl_all!(Foo: Reflect); } + #[test] + fn should_allow_empty_enums() { + #[derive(Reflect)] + enum Empty {} + + assert_impl_all!(Empty: Reflect); + } + #[test] fn recursive_typed_storage_does_not_hang() { #[derive(Reflect)]