bevyengine/bevy · error

{deref_kind} can only be derived on structs

Error message

{deref_kind} can only be derived on structs

What it means

Compile-time syn error from get_deref_field: #[derive(Deref)] or #[derive(DerefMut)] was applied to an enum or union rather than a struct, which the derive macro does not support — dereferencing requires a struct field to forward to.

Source

Thrown at crates/bevy_derive/src/derefs.rs:100

                            ),
                        ));
                    }

                    let member = to_member(field, index);
                    selected_field = Some((member, &field.ty));
                }
            }

            if let Some(selected_field) = selected_field {
                Ok(selected_field)
            } else {
                Err(syn::Error::new(
                    Span::call_site().into(),
                    format!("deriving {deref_kind} on multi-field structs requires one field to have the {deref_attr_str} attribute"),
                ))
            }
        }
        _ => Err(syn::Error::new(
            Span::call_site().into(),
            format!("{deref_kind} can only be derived on structs"),
        )),
    }
}

fn to_member(field: &Field, index: usize) -> Member {
    field
        .ident
        .as_ref()
        .map(|name| Member::Named(name.clone()))
        .unwrap_or_else(|| Member::Unnamed(Index::from(index)))
}

View on GitHub (pinned to 396ca72708)

Solutions

  1. Apply the derive only to structs
  2. Wrap the enum in a newtype struct and derive Deref on that
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/bevy_derive/src/derefs.rs:100 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of bevyengine/bevy@396ca72708 (2026-08-20). Data as JSON: /api/errors/2a2b856a45015c02. Report an issue: GitHub.