From 83ffc90c6cf517e7329b6f66efd332cfe5f9ab4f Mon Sep 17 00:00:00 2001 From: krunchington Date: Thu, 27 Mar 2025 14:35:47 -0700 Subject: [PATCH] Fix relationship macro for multiple named members fields (#18530) # Objective Fixes #18466 ## Solution Updated the macro generation pattern to place the comma in the correct place in the pattern. ## Testing - Tried named and unnamed fields in combination, and used rust expand macro tooling to see the generated code and verify its correctness (see screenshots in example below) --- ## Showcase Screenshot showing expanded macro with multiple named fields ![image](https://github.com/user-attachments/assets/7ecd324c-10ba-4b23-9b53-b94da03567d3) Screenshot showing expanded macro with single unnamed field ![image](https://github.com/user-attachments/assets/be72f061-5f07-4d19-b5f6-7ff6c35ec679) ## Migration Guide n/a --- crates/bevy_ecs/macros/src/component.rs | 4 +-- crates/bevy_ecs/src/relationship/mod.rs | 37 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/macros/src/component.rs b/crates/bevy_ecs/macros/src/component.rs index bea77e6afd..ae6f815aff 100644 --- a/crates/bevy_ecs/macros/src/component.rs +++ b/crates/bevy_ecs/macros/src/component.rs @@ -746,7 +746,7 @@ fn derive_relationship( #[inline] fn from(entity: #bevy_ecs_path::entity::Entity) -> Self { Self { - #(#members: core::default::Default::default(),),* + #(#members: core::default::Default::default(),)* #relationship_member: entity } } @@ -809,7 +809,7 @@ fn derive_relationship_target( #[inline] fn from_collection_risky(collection: Self::Collection) -> Self { Self { - #(#members: core::default::Default::default(),),* + #(#members: core::default::Default::default(),)* #relationship_member: collection } } diff --git a/crates/bevy_ecs/src/relationship/mod.rs b/crates/bevy_ecs/src/relationship/mod.rs index a7e4c57667..9136a36a3f 100644 --- a/crates/bevy_ecs/src/relationship/mod.rs +++ b/crates/bevy_ecs/src/relationship/mod.rs @@ -385,4 +385,41 @@ mod tests { assert!(!world.entity(b).contains::()); assert!(!world.entity(b).contains::()); } + + #[test] + fn relationship_with_multiple_non_target_fields_compiles() { + #[derive(Component)] + #[relationship(relationship_target=Target)] + #[expect(dead_code, reason = "test struct")] + struct Source { + #[relationship] + target: Entity, + foo: u8, + bar: u8, + } + + #[derive(Component)] + #[relationship_target(relationship=Source)] + struct Target(Vec); + + // No assert necessary, looking to make sure compilation works with the macros + } + #[test] + fn relationship_target_with_multiple_non_target_fields_compiles() { + #[derive(Component)] + #[relationship(relationship_target=Target)] + struct Source(Entity); + + #[derive(Component)] + #[relationship_target(relationship=Source)] + #[expect(dead_code, reason = "test struct")] + struct Target { + #[relationship] + target: Vec, + foo: u8, + bar: u8, + } + + // No assert necessary, looking to make sure compilation works with the macros + } }