Skip to content

Commit

Permalink
reflect: maximally relax TypePath bounds (#11037)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
soqb authored Dec 24, 2023
1 parent c6b32a2 commit 13feac6
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 17 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 5 additions & 4 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 2 additions & 7 deletions crates/bevy_reflect/bevy_reflect_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 _: () = {
Expand Down Expand Up @@ -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 _: () = {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_reflect/bevy_reflect_derive/src/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 13feac6

Please sign in to comment.