astral-sh/ruff · error · syn::Error

CacheKey does not support unions. Only structs and enums…

Error message

CacheKey does not support unions. Only structs and enums are supported

What it means

A syn compile error from the CacheKey derive macro: the derive was applied to a union. The generated cache-key logic only knows how to walk structs and enums field by field, so unions are rejected at compile time.

Solutions

  1. Implement CacheKey manually for the union
  2. Change the type to a struct or enum so the derive applies
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/ruff_macros/src/cache_key.rs:92 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of astral-sh/ruff@26f38c119c (2026-09-05). Data as JSON: /api/errors/fcbb994373ed6732. Report an issue: GitHub.

Appendix: source

Thrown at crates/ruff_macros/src/cache_key.rs:92

                        continue;
                    }
                }

                let field_attr = if let Some(ident) = &field.ident {
                    quote!(self.#ident)
                } else {
                    let index = syn::Index::from(i);
                    quote!(self.#index)
                };

                fields.push(quote!(#field_attr.cache_key(key);));
            }

            quote! {#(#fields)*}
        }

        Data::Union(_) => {
            return Err(Error::new(
                item.span(),
                "CacheKey does not support unions. Only structs and enums are supported",
            ));
        }
    };

    let name = &item.ident;
    let (impl_generics, ty_generics, where_clause) = &item.generics.split_for_impl();

    Ok(quote!(
        #[automatically_derived]
        impl #impl_generics ruff_cache::CacheKey for #name #ty_generics #where_clause {
            fn cache_key(&self, key: &mut ruff_cache::CacheKeyHasher) {
                use std::hash::Hasher;
                use ruff_cache::CacheKey;
                #fields
            }
        }

View on GitHub (pinned to 26f38c119c)