From daa8bf20dfb0d5e3e2c8dbd032d532bde133f221 Mon Sep 17 00:00:00 2001 From: Gino Valente <49806985+MrGVSV@users.noreply.github.com> Date: Tue, 28 Nov 2023 18:46:09 -0700 Subject: [PATCH] Fix nested generics in Reflect derive (#10791) # Objective > Issue raised on [Discord](https://discord.com/channels/691052431525675048/1002362493634629796/1179182488787103776) Currently the following code fails due to a missing `TypePath` bound: ```rust #[derive(Reflect)] struct Foo(T); #[derive(Reflect)] struct Bar(Foo); #[derive(Reflect)] struct Baz(Bar>); ``` ## Solution Add `TypePath` to the per-field bounds instead of _just_ the generic type parameter bounds. ### Related Work It should be noted that #9046 would help make these kinds of issues easier to work around and/or avoid entirely. --- ## Changelog - Fixes missing `TypePath` requirement when deriving `Reflect` on nested generics --- .../bevy_reflect_derive/src/utility.rs | 4 ++-- .../tests/reflect_derive/nested.pass.rs | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 crates/bevy_reflect_compile_fail_tests/tests/reflect_derive/nested.pass.rs diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/utility.rs b/crates/bevy_reflect/bevy_reflect_derive/src/utility.rs index 0cd4c88b4c..1adc5787e5 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/utility.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/utility.rs @@ -132,9 +132,9 @@ impl WhereClauseOptions { let custom_bounds = active_bounds(field).map(|bounds| quote!(+ #bounds)); let bounds = if is_from_reflect { - quote!(#bevy_reflect_path::FromReflect #custom_bounds) + quote!(#bevy_reflect_path::FromReflect + #bevy_reflect_path::TypePath #custom_bounds) } else { - quote!(#bevy_reflect_path::Reflect #custom_bounds) + quote!(#bevy_reflect_path::Reflect + #bevy_reflect_path::TypePath #custom_bounds) }; (ty, bounds) diff --git a/crates/bevy_reflect_compile_fail_tests/tests/reflect_derive/nested.pass.rs b/crates/bevy_reflect_compile_fail_tests/tests/reflect_derive/nested.pass.rs new file mode 100644 index 0000000000..8be4d79e41 --- /dev/null +++ b/crates/bevy_reflect_compile_fail_tests/tests/reflect_derive/nested.pass.rs @@ -0,0 +1,16 @@ +use bevy_reflect::Reflect; + +mod nested_generics { + use super::*; + + #[derive(Reflect)] + struct Foo(T); + + #[derive(Reflect)] + struct Bar(Foo); + + #[derive(Reflect)] + struct Baz(Bar>); +} + +fn main() {}