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
This commit is contained in:
krunchington 2025-03-27 14:35:47 -07:00 committed by GitHub
parent a7e6578733
commit 83ffc90c6c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 2 deletions

View File

@ -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
}
}

View File

@ -385,4 +385,41 @@ mod tests {
assert!(!world.entity(b).contains::<Rel>());
assert!(!world.entity(b).contains::<RelTarget>());
}
#[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<Entity>);
// 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<Entity>,
foo: u8,
bar: u8,
}
// No assert necessary, looking to make sure compilation works with the macros
}
}