{"record":{"id":"5a952f521f60c96b","repo":"bevyengine/bevy","slug":"entityevent-can-only-be-derived-for-structs","errorCode":null,"errorMessage":"EntityEvent can only be derived for structs.","messagePattern":"EntityEvent can only be derived for structs\\.","errorType":"validation","errorClass":"syn::Error","httpStatus":null,"severity":"error","filePath":"crates/bevy_ecs/macros/src/event.rs","lineNumber":170,"sourceCode":"        impl #impl_generics #bevy_ecs_path::event::Event for #struct_name #type_generics #where_clause {\n            type Trigger<'a> = #trigger;\n        }\n\n        impl #impl_generics #bevy_ecs_path::event::EntityEvent for #struct_name #type_generics #where_clause {\n            fn event_target(&self) -> #bevy_ecs_path::entity::Entity {\n                #bevy_ecs_path::entity::ContainsEntity::entity(&self.#entity_field)\n            }\n        }\n\n        #set_entity_event_target_impl\n    })\n}\n\n/// Returns the field with the `#[event_target]` attribute, the only field if unnamed,\n/// or the field with the name \"entity\".\nfn get_event_target_field(ast: &DeriveInput) -> Result<Member> {\n    let Data::Struct(DataStruct { fields, .. }) = &ast.data else {\n        return Err(syn::Error::new(\n            ast.span(),\n            \"EntityEvent can only be derived for structs.\",\n        ));\n    };\n    match fields {\n        Fields::Named(fields) => fields.named.iter().find_map(|field| {\n            if field.ident.as_ref().is_some_and(|i| i == \"entity\") || field\n                .attrs\n                .iter()\n                .any(|attr| attr.path().is_ident(EVENT_TARGET)) {\n                    Some(Member::Named(field.ident.clone()?))\n                } else {\n                    None\n                }\n        }).ok_or(syn::Error::new(\n            fields.span(),\n            \"EntityEvent derive expected a field name 'entity' or a field annotated with #[event_target].\"\n        )),","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_ecs/macros/src/event.rs#L152-L188","documentation":"Compile error from get_event_target_field: the DeriveInput's data is not a DataStruct (it is an enum or a union), so the pattern `let Data::Struct(...)` failed and the derive cannot locate an entity-target field. It is a shape guard raised before any field inspection.","triggerScenarios":"Thrown at crates/bevy_ecs/macros/src/event.rs:170 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Derive EntityEvent only on structs, e.g. struct Attack(Entity);","Use a newtype or named struct wrapping the Entity target","For enums, wrap each variant payload in a struct and derive on that"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}