From 13feac6721aa285e22d377b5f8767efa67cf1738 Mon Sep 17 00:00:00 2001 From: radiish Date: Sun, 24 Dec 2023 17:45:46 +0000 Subject: [PATCH] reflect: maximally relax `TypePath` bounds (#11037) # Objective - Provides an alternate solution to the one implemented in #10791 without breaking changes. ## Solution - Changes the bounds of macro-generated `TypePath` implementations to universally ignore the types of fields, rather than use the same bounds as other implementations. I think this is a more holistic solution than #10791 because it totally erases the finicky bounds we currently generate, helping to untangle the reflection trait system. --- .../bevy_reflect/bevy_reflect_derive/src/impls/enums.rs | 2 +- .../bevy_reflect_derive/src/impls/structs.rs | 2 +- .../bevy_reflect_derive/src/impls/tuple_structs.rs | 2 +- .../bevy_reflect/bevy_reflect_derive/src/impls/typed.rs | 9 +++++---- .../bevy_reflect/bevy_reflect_derive/src/impls/values.rs | 2 +- crates/bevy_reflect/bevy_reflect_derive/src/lib.rs | 9 ++------- crates/bevy_reflect/bevy_reflect_derive/src/utility.rs | 4 ++-- 7 files changed, 13 insertions(+), 17 deletions(-) diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs b/crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs index a733ec2e26..2d2bebfc9c 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs @@ -83,7 +83,7 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> proc_macro2::TokenStream }, ); - let type_path_impl = impl_type_path(reflect_enum.meta(), &where_clause_options); + let type_path_impl = impl_type_path(reflect_enum.meta()); let get_type_registration_impl = reflect_enum .meta() diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/impls/structs.rs b/crates/bevy_reflect/bevy_reflect_derive/src/impls/structs.rs index fb1565f001..9aef44d350 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/impls/structs.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/impls/structs.rs @@ -89,7 +89,7 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> proc_macro2::TokenS }, ); - let type_path_impl = impl_type_path(reflect_struct.meta(), &where_clause_options); + let type_path_impl = impl_type_path(reflect_struct.meta()); let get_type_registration_impl = reflect_struct.get_type_registration(&where_clause_options); diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/impls/tuple_structs.rs b/crates/bevy_reflect/bevy_reflect_derive/src/impls/tuple_structs.rs index 7a097a1a1a..14af4851fd 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/impls/tuple_structs.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/impls/tuple_structs.rs @@ -82,7 +82,7 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> proc_macro2:: }, ); - let type_path_impl = impl_type_path(reflect_struct.meta(), &where_clause_options); + let type_path_impl = impl_type_path(reflect_struct.meta()); let (impl_generics, ty_generics, where_clause) = reflect_struct .meta() diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/impls/typed.rs b/crates/bevy_reflect/bevy_reflect_derive/src/impls/typed.rs index 5894ef82e5..46edd1895c 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/impls/typed.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/impls/typed.rs @@ -48,10 +48,11 @@ pub(crate) enum TypedProperty { TypePath, } -pub(crate) fn impl_type_path( - meta: &ReflectMeta, - where_clause_options: &WhereClauseOptions, -) -> proc_macro2::TokenStream { +pub(crate) fn impl_type_path(meta: &ReflectMeta) -> proc_macro2::TokenStream { + // Use `WhereClauseOptions::new_value` here so we don't enforce reflection bounds, + // ensuring the impl applies in the most cases possible. + let where_clause_options = &WhereClauseOptions::new_value(meta); + if !meta.traits().type_path_attrs().should_auto_derive() { return proc_macro2::TokenStream::new(); } diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/impls/values.rs b/crates/bevy_reflect/bevy_reflect_derive/src/impls/values.rs index a9f43cbbfe..17e0838d79 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/impls/values.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/impls/values.rs @@ -31,7 +31,7 @@ pub(crate) fn impl_value(meta: &ReflectMeta) -> proc_macro2::TokenStream { }, ); - let type_path_impl = impl_type_path(meta, &where_clause_options); + let type_path_impl = impl_type_path(meta); let (impl_generics, ty_generics, where_clause) = type_path.generics().split_for_impl(); let where_reflect_clause = extend_where_clause(where_clause, &where_clause_options); diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs b/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs index e87d3ccf5c..8620103ca5 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs @@ -39,7 +39,6 @@ use reflect_value::ReflectValueDef; use syn::spanned::Spanned; use syn::{parse_macro_input, DeriveInput}; use type_path::NamedTypePathDef; -use utility::WhereClauseOptions; pub(crate) static REFLECT_ATTRIBUTE_NAME: &str = "reflect"; pub(crate) static REFLECT_VALUE_ATTRIBUTE_NAME: &str = "reflect_value"; @@ -283,11 +282,7 @@ pub fn derive_type_path(input: TokenStream) -> TokenStream { Err(err) => return err.into_compile_error().into(), }; - let type_path_impl = impls::impl_type_path( - derive_data.meta(), - // Use `WhereClauseOptions::new_value` here so we don't enforce reflection bounds - &WhereClauseOptions::new_value(derive_data.meta()), - ); + let type_path_impl = impls::impl_type_path(derive_data.meta()); TokenStream::from(quote! { const _: () = { @@ -613,7 +608,7 @@ pub fn impl_type_path(input: TokenStream) -> TokenStream { let meta = ReflectMeta::new(type_path, ReflectTraits::default()); - let type_path_impl = impls::impl_type_path(&meta, &WhereClauseOptions::new_value(&meta)); + let type_path_impl = impls::impl_type_path(&meta); TokenStream::from(quote! { const _: () = { diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/utility.rs b/crates/bevy_reflect/bevy_reflect_derive/src/utility.rs index 1adc5787e5..0cd4c88b4c 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 + #bevy_reflect_path::TypePath #custom_bounds) + quote!(#bevy_reflect_path::FromReflect #custom_bounds) } else { - quote!(#bevy_reflect_path::Reflect + #bevy_reflect_path::TypePath #custom_bounds) + quote!(#bevy_reflect_path::Reflect #custom_bounds) }; (ty, bounds)