From b7d68873eca08d88169c8acb4891b5c5044d1406 Mon Sep 17 00:00:00 2001 From: Gino Valente <49806985+MrGVSV@users.noreply.github.com> Date: Mon, 28 Aug 2023 10:36:18 -0700 Subject: [PATCH] bevy_derive: Fix `#[deref]` breaking other attributes (#9551) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Objective Fixes #9550 ## Solution Removes a check that asserts that _all_ attribute metas are path-only, rather than just the `#[deref]` attribute itself. --- ## Changelog - Fixes an issue where deriving `Deref` with `#[deref]` on a field causes other attributes to sometimes result in a compile error --------- Co-authored-by: François --- crates/bevy_derive/src/derefs.rs | 4 +++- .../tests/deref_derive/multiple_fields.pass.rs | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/bevy_derive/src/derefs.rs b/crates/bevy_derive/src/derefs.rs index 0e3dfc1bf0..90b00f7b71 100644 --- a/crates/bevy_derive/src/derefs.rs +++ b/crates/bevy_derive/src/derefs.rs @@ -68,10 +68,12 @@ fn get_deref_field(ast: &DeriveInput, is_mut: bool) -> syn::Result<(Member, &Typ let mut selected_field: Option<(Member, &Type)> = None; for (index, field) in data_struct.fields.iter().enumerate() { for attr in &field.attrs { - if !attr.meta.require_path_only()?.is_ident(DEREF_ATTR) { + if !attr.meta.path().is_ident(DEREF_ATTR) { continue; } + attr.meta.require_path_only()?; + if selected_field.is_some() { return Err(syn::Error::new_spanned( attr, diff --git a/crates/bevy_macros_compile_fail_tests/tests/deref_derive/multiple_fields.pass.rs b/crates/bevy_macros_compile_fail_tests/tests/deref_derive/multiple_fields.pass.rs index 2244e89b78..96662054f2 100644 --- a/crates/bevy_macros_compile_fail_tests/tests/deref_derive/multiple_fields.pass.rs +++ b/crates/bevy_macros_compile_fail_tests/tests/deref_derive/multiple_fields.pass.rs @@ -5,9 +5,13 @@ struct TupleStruct(usize, #[deref] String); #[derive(Deref)] struct Struct { + // Works with other attributes + #[cfg(test)] foo: usize, #[deref] bar: String, + /// Also works with doc comments. + baz: i32, } fn main() { @@ -15,8 +19,10 @@ fn main() { let _: &String = &*value; let value = Struct { + #[cfg(test)] foo: 123, bar: "Hello world!".to_string(), + baz: 321, }; let _: &String = &*value; }