bevyengine/bevy · error · syn::Error

Invalid key override attribute

Error message

Invalid key override attribute

What it means

The same `#[key(...)]` machinery as the previous error, but this message comes from the attribute-processing site in the specializer macro: when `syn::parse::<Key>` cannot parse the raw attribute tokens at all, the derive emits "Invalid key override attribute" pointing at the whole `#[key(...)]` attribute.

Source

Thrown at crates/bevy_render/macros/src/specializer.rs:151

            Member::Unnamed(Index {
                index: index as u32,
                span: field.span(),
            }),
            Member::Named,
        );
        let key_index = Index {
            index: used_count,
            span: field.span(),
        };

        let mut use_key_field = true;
        let mut key = Key::Index(key_index);
        for attr in &field.attrs {
            match &attr.meta {
                Meta::List(MetaList { path, tokens, .. }) if path.is_ident(&KEY_ATTR_IDENT) => {
                    let owned_tokens = tokens.clone().into();
                    let Ok(parsed_key) = syn::parse::<Key>(owned_tokens) else {
                        return Err(syn::Error::new(
                            attr.span(),
                            "Invalid key override attribute",
                        ));
                    };
                    key = parsed_key;
                    if matches!(
                        (&key, &targets),
                        (Key::Custom(_), SpecializeImplTargets::All)
                    ) {
                        return Err(syn::Error::new(
                            attr.span(),
                            "#[key(default)] is the only key override type allowed with #[specialize(all)]",
                        ));
                    }
                    use_key_field = false;
                }
                _ => {}
            }

View on GitHub (pinned to 396ca72708)

Solutions

  1. Put exactly one payload in `#[key(...)]`: either `default` or a single expression
  2. Keep one `#[key]` attribute per field; never combine payloads
  3. Compare against a known-good Specializer/SpecializerKey usage and mirror the shape

Example fix

// before
#[key(default, MyKey::A)]
mode: Mode,

// after
#[key(MyKey::A)]
mode: Mode,
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Token sequences inside `#[key(...)]` that are neither a single identifier nor a single expression, e.g. `#[key(default default)]`, `#[key(,)]`, or two comma-separated payloads merged into one attribute.

Common situations: Copy-pasting two key overrides into one attribute during refactor; merge artifacts leaving duplicated payloads; leftover commas.

Related errors


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