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.
This commit is contained in:
radiish 2023-12-24 17:45:46 +00:00 committed by GitHub
parent c6b32a2140
commit 13feac6721
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 13 additions and 17 deletions

View file

@ -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 let get_type_registration_impl = reflect_enum
.meta() .meta()

View file

@ -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); let get_type_registration_impl = reflect_struct.get_type_registration(&where_clause_options);

View file

@ -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 let (impl_generics, ty_generics, where_clause) = reflect_struct
.meta() .meta()

View file

@ -48,10 +48,11 @@ pub(crate) enum TypedProperty {
TypePath, TypePath,
} }
pub(crate) fn impl_type_path( pub(crate) fn impl_type_path(meta: &ReflectMeta) -> proc_macro2::TokenStream {
meta: &ReflectMeta, // Use `WhereClauseOptions::new_value` here so we don't enforce reflection bounds,
where_clause_options: &WhereClauseOptions, // ensuring the impl applies in the most cases possible.
) -> proc_macro2::TokenStream { let where_clause_options = &WhereClauseOptions::new_value(meta);
if !meta.traits().type_path_attrs().should_auto_derive() { if !meta.traits().type_path_attrs().should_auto_derive() {
return proc_macro2::TokenStream::new(); return proc_macro2::TokenStream::new();
} }

View file

@ -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 (impl_generics, ty_generics, where_clause) = type_path.generics().split_for_impl();
let where_reflect_clause = extend_where_clause(where_clause, &where_clause_options); let where_reflect_clause = extend_where_clause(where_clause, &where_clause_options);

View file

@ -39,7 +39,6 @@ use reflect_value::ReflectValueDef;
use syn::spanned::Spanned; use syn::spanned::Spanned;
use syn::{parse_macro_input, DeriveInput}; use syn::{parse_macro_input, DeriveInput};
use type_path::NamedTypePathDef; use type_path::NamedTypePathDef;
use utility::WhereClauseOptions;
pub(crate) static REFLECT_ATTRIBUTE_NAME: &str = "reflect"; pub(crate) static REFLECT_ATTRIBUTE_NAME: &str = "reflect";
pub(crate) static REFLECT_VALUE_ATTRIBUTE_NAME: &str = "reflect_value"; 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(), Err(err) => return err.into_compile_error().into(),
}; };
let type_path_impl = impls::impl_type_path( let type_path_impl = impls::impl_type_path(derive_data.meta());
derive_data.meta(),
// Use `WhereClauseOptions::new_value` here so we don't enforce reflection bounds
&WhereClauseOptions::new_value(derive_data.meta()),
);
TokenStream::from(quote! { TokenStream::from(quote! {
const _: () = { const _: () = {
@ -613,7 +608,7 @@ pub fn impl_type_path(input: TokenStream) -> TokenStream {
let meta = ReflectMeta::new(type_path, ReflectTraits::default()); 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! { TokenStream::from(quote! {
const _: () = { const _: () = {

View file

@ -132,9 +132,9 @@ impl WhereClauseOptions {
let custom_bounds = active_bounds(field).map(|bounds| quote!(+ #bounds)); let custom_bounds = active_bounds(field).map(|bounds| quote!(+ #bounds));
let bounds = if is_from_reflect { 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 { } else {
quote!(#bevy_reflect_path::Reflect + #bevy_reflect_path::TypePath #custom_bounds) quote!(#bevy_reflect_path::Reflect #custom_bounds)
}; };
(ty, bounds) (ty, bounds)