Skip to content

Commit 13feac6

Browse files
authored
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.
1 parent c6b32a2 commit 13feac6

File tree

7 files changed

+13
-17
lines changed

7 files changed

+13
-17
lines changed

crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> proc_macro2::TokenStream
8383
},
8484
);
8585

86-
let type_path_impl = impl_type_path(reflect_enum.meta(), &where_clause_options);
86+
let type_path_impl = impl_type_path(reflect_enum.meta());
8787

8888
let get_type_registration_impl = reflect_enum
8989
.meta()

crates/bevy_reflect/bevy_reflect_derive/src/impls/structs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> proc_macro2::TokenS
8989
},
9090
);
9191

92-
let type_path_impl = impl_type_path(reflect_struct.meta(), &where_clause_options);
92+
let type_path_impl = impl_type_path(reflect_struct.meta());
9393

9494
let get_type_registration_impl = reflect_struct.get_type_registration(&where_clause_options);
9595

crates/bevy_reflect/bevy_reflect_derive/src/impls/tuple_structs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> proc_macro2::
8282
},
8383
);
8484

85-
let type_path_impl = impl_type_path(reflect_struct.meta(), &where_clause_options);
85+
let type_path_impl = impl_type_path(reflect_struct.meta());
8686

8787
let (impl_generics, ty_generics, where_clause) = reflect_struct
8888
.meta()

crates/bevy_reflect/bevy_reflect_derive/src/impls/typed.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ pub(crate) enum TypedProperty {
4848
TypePath,
4949
}
5050

51-
pub(crate) fn impl_type_path(
52-
meta: &ReflectMeta,
53-
where_clause_options: &WhereClauseOptions,
54-
) -> proc_macro2::TokenStream {
51+
pub(crate) fn impl_type_path(meta: &ReflectMeta) -> proc_macro2::TokenStream {
52+
// Use `WhereClauseOptions::new_value` here so we don't enforce reflection bounds,
53+
// ensuring the impl applies in the most cases possible.
54+
let where_clause_options = &WhereClauseOptions::new_value(meta);
55+
5556
if !meta.traits().type_path_attrs().should_auto_derive() {
5657
return proc_macro2::TokenStream::new();
5758
}

crates/bevy_reflect/bevy_reflect_derive/src/impls/values.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub(crate) fn impl_value(meta: &ReflectMeta) -> proc_macro2::TokenStream {
3131
},
3232
);
3333

34-
let type_path_impl = impl_type_path(meta, &where_clause_options);
34+
let type_path_impl = impl_type_path(meta);
3535

3636
let (impl_generics, ty_generics, where_clause) = type_path.generics().split_for_impl();
3737
let where_reflect_clause = extend_where_clause(where_clause, &where_clause_options);

crates/bevy_reflect/bevy_reflect_derive/src/lib.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ use reflect_value::ReflectValueDef;
3939
use syn::spanned::Spanned;
4040
use syn::{parse_macro_input, DeriveInput};
4141
use type_path::NamedTypePathDef;
42-
use utility::WhereClauseOptions;
4342

4443
pub(crate) static REFLECT_ATTRIBUTE_NAME: &str = "reflect";
4544
pub(crate) static REFLECT_VALUE_ATTRIBUTE_NAME: &str = "reflect_value";
@@ -283,11 +282,7 @@ pub fn derive_type_path(input: TokenStream) -> TokenStream {
283282
Err(err) => return err.into_compile_error().into(),
284283
};
285284

286-
let type_path_impl = impls::impl_type_path(
287-
derive_data.meta(),
288-
// Use `WhereClauseOptions::new_value` here so we don't enforce reflection bounds
289-
&WhereClauseOptions::new_value(derive_data.meta()),
290-
);
285+
let type_path_impl = impls::impl_type_path(derive_data.meta());
291286

292287
TokenStream::from(quote! {
293288
const _: () = {
@@ -613,7 +608,7 @@ pub fn impl_type_path(input: TokenStream) -> TokenStream {
613608

614609
let meta = ReflectMeta::new(type_path, ReflectTraits::default());
615610

616-
let type_path_impl = impls::impl_type_path(&meta, &WhereClauseOptions::new_value(&meta));
611+
let type_path_impl = impls::impl_type_path(&meta);
617612

618613
TokenStream::from(quote! {
619614
const _: () = {

crates/bevy_reflect/bevy_reflect_derive/src/utility.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ impl WhereClauseOptions {
132132
let custom_bounds = active_bounds(field).map(|bounds| quote!(+ #bounds));
133133

134134
let bounds = if is_from_reflect {
135-
quote!(#bevy_reflect_path::FromReflect + #bevy_reflect_path::TypePath #custom_bounds)
135+
quote!(#bevy_reflect_path::FromReflect #custom_bounds)
136136
} else {
137-
quote!(#bevy_reflect_path::Reflect + #bevy_reflect_path::TypePath #custom_bounds)
137+
quote!(#bevy_reflect_path::Reflect #custom_bounds)
138138
};
139139

140140
(ty, bounds)

0 commit comments

Comments
 (0)