Skip to content

Commit 881faa2

Browse files
committed
error variant
1 parent 0fca693 commit 881faa2

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

crates/bevy_ecs/macros/src/component.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,15 @@ pub fn derive_component(input: TokenStream) -> TokenStream {
9292
Err(err) => err.into_compile_error().into(),
9393
};
9494

95-
let map_entities = map_entities(
95+
let map_entities = match map_entities(
9696
&ast.data,
9797
Ident::new("this", Span::call_site()),
9898
relationship.is_some(),
9999
relationship_target.is_some(),
100-
).map(|map_entities_impl| quote! {
100+
) {
101+
Ok(map) => map,
102+
Err(err) => return err.into_compile_error().into()
103+
}.map(|map_entities_impl| quote! {
101104
fn map_entities<M: #bevy_ecs_path::entity::EntityMapper>(this: &mut Self, mapper: &mut M) {
102105
use #bevy_ecs_path::entity::MapEntities;
103106
#map_entities_impl
@@ -308,7 +311,7 @@ pub(crate) fn map_entities(
308311
self_ident: Ident,
309312
is_relationship: bool,
310313
is_relationship_target: bool,
311-
) -> Option<TokenStream2> {
314+
) -> Result<Option<TokenStream2>> {
312315
match data {
313316
Data::Struct(DataStruct { fields, .. }) => {
314317
let mut map = Vec::with_capacity(fields.len());
@@ -334,16 +337,23 @@ pub(crate) fn map_entities(
334337
map.push(quote!(#self_ident.#field_member.map_entities(mapper);));
335338
});
336339
if map.is_empty() {
337-
return None;
340+
return Ok(None);
338341
};
339-
Some(quote!(
342+
Ok(Some(quote!(
340343
#(#map)*
341-
))
344+
)))
342345
}
343346
Data::Enum(DataEnum { variants, .. }) => {
344347
let mut map = Vec::with_capacity(variants.len());
345348

346349
for variant in variants.iter() {
350+
if let Some(attr) = variant.attrs.iter().find(|a| a.path().is_ident(ENTITIES)) {
351+
return Err(syn::Error::new(
352+
attr.span(),
353+
"`#[entities]` should be on the associated type, not on the variant.",
354+
));
355+
}
356+
347357
let field_members = variant
348358
.fields
349359
.iter()
@@ -371,17 +381,17 @@ pub(crate) fn map_entities(
371381
}
372382

373383
if map.is_empty() {
374-
return None;
384+
return Ok(None);
375385
};
376386

377-
Some(quote!(
387+
Ok(Some(quote!(
378388
match #self_ident {
379389
#(#map,)*
380390
_ => {}
381391
}
382-
))
392+
)))
383393
}
384-
Data::Union(_) => None,
394+
Data::Union(_) => Ok(None),
385395
}
386396
}
387397

crates/bevy_ecs/macros/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,15 @@ pub fn derive_bundle(input: TokenStream) -> TokenStream {
191191
pub fn derive_map_entities(input: TokenStream) -> TokenStream {
192192
let ast = parse_macro_input!(input as DeriveInput);
193193
let ecs_path = bevy_ecs_path();
194-
195-
let map_entities_impl = map_entities(
194+
let map_entities_impl = match map_entities(
196195
&ast.data,
197196
Ident::new("self", Span::call_site()),
198197
false,
199198
false,
200-
);
199+
) {
200+
Ok(map) => map,
201+
Err(err) => return err.into_compile_error().into(),
202+
};
201203

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

0 commit comments

Comments
 (0)