neon-bindings/neon · error

`neon::export` can only be applied to functions, consts…

Error message

`neon::export` can only be applied to functions, consts, statics, and classes (impl blocks).

What it means

The `#[neon::export]` attribute macro only supports a limited set of Rust items: functions, consts, statics, and classes (impl blocks). The `unsupported` function emits this compile error on any other item kind (e.g. modules, enums, type aliases, structs, traits).

Solutions

  1. Move `#[neon::export]` onto an individual `fn`, `const`, `static`, or an `impl` block (class)
  2. To export a struct, export an `impl` block for it instead of annotating the struct itself
  3. Remove the attribute from unsupported items and export individual functions from within them

Example fix

// before
#[neon::export]
struct Config { verbose: bool }
// after
struct Config { verbose: bool }
#[neon::export]
impl Config {
    fn new() -> Self { Config { verbose: false } }
}
Defensive patterns

Strategy: validation

Validate before calling

// Rust-side habit: verify the annotated item kind
// fn / const / static / impl are supported; struct, enum, mod, trait are not

Prevention

When it happens

Trigger: Writing `#[neon::export]` above a `mod`, `struct`, `enum`, `trait`, `type`, `use`, or `macro` item; the macro's `export` dispatcher matches the item type, fails all supported arms, and calls `unsupported` with the whole item.

Common situations: Trying to export a whole module or type at once; assuming the macro works like other export systems that accept types; copy-pasting the attribute onto the wrong item when adding native bindings.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of neon-bindings/neon@38960e4381 (2026-09-13). Data as JSON: /api/errors/bd7adc550f950592. Report an issue: GitHub.

Appendix: source

Thrown at crates/neon-macros/src/export/mod.rs:57

        }

        // Export a class (impl block)
        syn::Item::Impl(item) => {
            let meta = syn::parse_macro_input!(attr with class::meta::Parser);

            class::export(meta, item)
        }

        // Return an error span for all other types
        _ => unsupported(item),
    }
}

// Generate an error for unsupported item types
fn unsupported(item: syn::Item) -> proc_macro::TokenStream {
    let span = syn::spanned::Spanned::span(&item);
    let msg = "`neon::export` can only be applied to functions, consts, statics, and classes (impl blocks).";
    let err = syn::Error::new(span, msg);

    err.into_compile_error().into()
}

View on GitHub (pinned to 38960e4381)