From 1a6b480ba245dc8fbb1fd2ef15e4f7fdd5aee3c2 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Thu, 27 Mar 2025 14:20:08 -0700 Subject: [PATCH] Required Components: pass through all tokens in {} and () syntax (#18578) # Objective #18555 added improved require syntax, but inline structs didn't support `..Default::default()` syntax (for technical reasons we can't parse the struct directly, so there is manual logic that missed this case). ## Solution When a `{}` or `()` section is encountered for a required component, rather than trying to parse the fields directly, just pass _all_ of the tokens through. This ensures no tokens are dropped, protects us against any future syntax changes, and optimizes our parsing logic (as we're dropping the field parsing logic entirely). --- crates/bevy_ecs/macros/src/component.rs | 12 ++++++------ crates/bevy_ecs/src/component.rs | 12 ++++++++---- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/crates/bevy_ecs/macros/src/component.rs b/crates/bevy_ecs/macros/src/component.rs index ebfc2f5d3a..bea77e6afd 100644 --- a/crates/bevy_ecs/macros/src/component.rs +++ b/crates/bevy_ecs/macros/src/component.rs @@ -9,8 +9,8 @@ use syn::{ punctuated::Punctuated, spanned::Spanned, token::{Brace, Comma, Paren}, - Data, DataEnum, DataStruct, DeriveInput, Expr, ExprCall, ExprPath, Field, FieldValue, Fields, - Ident, LitStr, Member, Path, Result, Token, Type, Visibility, + Data, DataEnum, DataStruct, DeriveInput, Expr, ExprCall, ExprPath, Field, Fields, Ident, + LitStr, Member, Path, Result, Token, Type, Visibility, }; pub const EVENT: &str = "event"; @@ -603,16 +603,16 @@ impl Parse for Require { // This is a "value style" named-struct-like require let content; braced!(content in input); - let fields = Punctuated::::parse_terminated(&content)?; - let tokens: TokenStream = quote::quote! (|| #path { #fields }).into(); + let content = content.parse::()?; + let tokens: TokenStream = quote::quote! (|| #path { #content }).into(); Some(TokenStream2::from(tokens)) } else if input.peek(Paren) { // This is a "value style" tuple-struct-like require let content; parenthesized!(content in input); + let content = content.parse::()?; is_constructor_call = last_segment_is_lower; - let fields = Punctuated::::parse_terminated(&content)?; - let tokens: TokenStream = quote::quote! (|| #path (#fields)).into(); + let tokens: TokenStream = quote::quote! (|| #path (#content)).into(); Some(TokenStream2::from(tokens)) } else if is_enum { // if this is an enum, then it is an inline enum component declaration diff --git a/crates/bevy_ecs/src/component.rs b/crates/bevy_ecs/src/component.rs index bebac8f67b..22e9776839 100644 --- a/crates/bevy_ecs/src/component.rs +++ b/crates/bevy_ecs/src/component.rs @@ -166,7 +166,10 @@ use thiserror::Error; /// #[derive(Component)] /// #[require( /// B(1), // tuple structs -/// C { value: 1 }, // named-field structs +/// C { // named-field structs +/// x: 1, +/// ..Default::default() +/// }, /// D::One, // enum variants /// E::ONE, // associated consts /// F::new(1) // constructors @@ -176,9 +179,10 @@ use thiserror::Error; /// #[derive(Component, PartialEq, Eq, Debug)] /// struct B(u8); /// -/// #[derive(Component, PartialEq, Eq, Debug)] +/// #[derive(Component, PartialEq, Eq, Debug, Default)] /// struct C { -/// value: u8 +/// x: u8, +/// y: u8, /// } /// /// #[derive(Component, PartialEq, Eq, Debug)] @@ -206,7 +210,7 @@ use thiserror::Error; /// # let mut world = World::default(); /// let id = world.spawn(A).id(); /// assert_eq!(&B(1), world.entity(id).get::().unwrap()); -/// assert_eq!(&C { value: 1 }, world.entity(id).get::().unwrap()); +/// assert_eq!(&C { x: 1, y: 0 }, world.entity(id).get::().unwrap()); /// assert_eq!(&D::One, world.entity(id).get::().unwrap()); /// assert_eq!(&E(1), world.entity(id).get::().unwrap()); /// assert_eq!(&F(1), world.entity(id).get::().unwrap());