Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reflect: maximally relax TypePath bounds #11037

Merged
merged 2 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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