Skip to content

Error when #[entities] is on the wrong part #19445

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
30 changes: 20 additions & 10 deletions crates/bevy_ecs/macros/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,15 @@ pub fn derive_component(input: TokenStream) -> TokenStream {
Err(err) => err.into_compile_error().into(),
};

let map_entities = map_entities(
let map_entities = match map_entities(
&ast.data,
Ident::new("this", Span::call_site()),
relationship.is_some(),
relationship_target.is_some(),
).map(|map_entities_impl| quote! {
) {
Ok(map) => map,
Err(err) => return err.into_compile_error().into()
}.map(|map_entities_impl| quote! {
fn map_entities<M: #bevy_ecs_path::entity::EntityMapper>(this: &mut Self, mapper: &mut M) {
use #bevy_ecs_path::entity::MapEntities;
#map_entities_impl
Expand Down Expand Up @@ -308,7 +311,7 @@ pub(crate) fn map_entities(
self_ident: Ident,
is_relationship: bool,
is_relationship_target: bool,
) -> Option<TokenStream2> {
) -> Result<Option<TokenStream2>> {
match data {
Data::Struct(DataStruct { fields, .. }) => {
let mut map = Vec::with_capacity(fields.len());
Expand All @@ -334,16 +337,23 @@ pub(crate) fn map_entities(
map.push(quote!(#self_ident.#field_member.map_entities(mapper);));
});
if map.is_empty() {
return None;
return Ok(None);
};
Some(quote!(
Ok(Some(quote!(
#(#map)*
))
)))
}
Data::Enum(DataEnum { variants, .. }) => {
let mut map = Vec::with_capacity(variants.len());

for variant in variants.iter() {
if let Some(attr) = variant.attrs.iter().find(|a| a.path().is_ident(ENTITIES)) {
return Err(syn::Error::new(
attr.span(),
"`#[entities]` should be on the associated type, not on the variant.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of this message - it only applies for tuple variants. How about "entities should be on the fields in the enum variant, not on the variant"?

));
}

let field_members = variant
.fields
.iter()
Expand Down Expand Up @@ -371,17 +381,17 @@ pub(crate) fn map_entities(
}

if map.is_empty() {
return None;
return Ok(None);
};

Some(quote!(
Ok(Some(quote!(
match #self_ident {
#(#map,)*
_ => {}
}
))
)))
}
Data::Union(_) => None,
Data::Union(_) => Ok(None),
}
}

Expand Down
8 changes: 5 additions & 3 deletions crates/bevy_ecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,15 @@ pub fn derive_bundle(input: TokenStream) -> TokenStream {
pub fn derive_map_entities(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
let ecs_path = bevy_ecs_path();

let map_entities_impl = map_entities(
let map_entities_impl = match map_entities(
&ast.data,
Ident::new("self", Span::call_site()),
false,
false,
);
) {
Ok(map) => map,
Err(err) => return err.into_compile_error().into(),
};

let struct_name = &ast.ident;
let (impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl();
Expand Down