cube-js/cube · error · syn::Error

NativeBridgeStatic can only be derived for structs

Error message

NativeBridgeStatic can only be derived for structs

What it means

The `NativeBridgeStatic` derive macro only supports struct inputs. `collect_static_field_meta_entries` matches on `Data::Struct` and, for enums or unions, emits this compile-time error because it needs named fields to generate the static metadata (field names/types) for the bridge.

Source

Thrown at rust/cube/cubesqlplanner/nativebridge/src/lib.rs:906

        impl #struct_ident {
            pub fn static_fields_meta() -> &'static [cubenativeutils::wrappers::bridge_meta::BridgeFieldMeta] {
                static FIELDS: &[cubenativeutils::wrappers::bridge_meta::BridgeFieldMeta] = &[
                    #( #entries ),*
                ];
                FIELDS
            }
        }
    };
    TokenStream::from(expanded)
}

fn collect_static_field_meta_entries(
    input: &DeriveInput,
) -> syn::Result<Vec<proc_macro2::TokenStream>> {
    let data = match &input.data {
        Data::Struct(d) => d,
        _ => {
            return Err(syn::Error::new(
                input.span(),
                "NativeBridgeStatic can only be derived for structs",
            ))
        }
    };
    let fields = match &data.fields {
        Fields::Named(named) => &named.named,
        _ => {
            return Err(syn::Error::new(
                input.span(),
                "NativeBridgeStatic requires a struct with named fields",
            ))
        }
    };

    let mut entries = Vec::new();
    for field in fields {
        let ident = field.ident.as_ref().unwrap();

View on GitHub (pinned to 7d981676b3)

Solutions

  1. Convert the enum/union to a struct, or remove the `NativeBridgeStatic` derive from it.
  2. If you need enum support, wrap the enum in a struct field or handle it with a different derive/macro.
  3. Check which item the derive is attached to — the error span points at the offending input.

Example fix

// before
#[derive(NativeBridgeStatic)]
enum Config { A, B }
// after
#[derive(NativeBridgeStatic)]
struct Config { a: u32, b: String }
Defensive patterns

Strategy: validation

Validate before calling

// Before adding the derive, confirm the target is a struct:
// grep -B2 'derive(NativeBridgeStatic)' src/**.rs  -> each item must be 'struct X { ... }'
fn is_struct_item(decl: &str) -> bool { decl.trim_start().starts_with("struct ") }

Type guard

fn derive_target_is_struct(item_kind: &str) -> bool { item_kind == "struct" }

Prevention

When it happens

Trigger: Applying `#[derive(NativeBridgeStatic)]` to an `enum` or a `union` instead of a `struct`.

Common situations: Refactoring a struct into an enum while keeping the derive; copy-pasting the derive attribute onto an enum variant container; misunderstanding that the derive is struct-only.

Related errors


AI-assisted analysis of cube-js/cube@7d981676b3 (2026-09-02). Data as JSON: /api/errors/664e35c2a72cc72b. Report an issue: GitHub.