Skip to content

Commit

Permalink
bevy_derive: Fix #[deref] breaking other attributes (bevyengine#9551)
Browse files Browse the repository at this point in the history
# Objective

Fixes bevyengine#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 <[email protected]>
  • Loading branch information
MrGVSV and mockersf authored Aug 28, 2023
1 parent 5012a0f commit b7d6887
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion crates/bevy_derive/src/derefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ 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() {
let value = TupleStruct(123, "Hello world!".to_string());
let _: &String = &*value;

let value = Struct {
#[cfg(test)]
foo: 123,
bar: "Hello world!".to_string(),
baz: 321,
};
let _: &String = &*value;
}

0 comments on commit b7d6887

Please sign in to comment.