neon-bindings/neon · error

Expected a context argument. Try removing the `context`…

Error message

Expected a context argument. Try removing the `context` attribute.

What it means

In check_constructor_context (crates/neon-macros/src/class/mod.rs), this error fires when a constructor is declared with the `context` attribute (or opts.context is set) but its signature has no arguments at all. The `context` attribute promises that the first parameter is a Neon context (e.g. `&mut FunctionContext`), so an empty parameter list contradicts the attribute. The message suggests removing `context` if no context is actually needed.

Solutions

  1. Add a context parameter as the first argument, e.g. `cx: &mut FunctionContext`
  2. Or remove the `context` attribute if the constructor does not need a context

Example fix

// before
#[constructor(context)]
fn new() -> JsResult<JsObject> { ... }
// after (option A)
#[constructor(context)]
fn new(cx: &mut FunctionContext) -> JsResult<JsObject> { ... }
// after (option B)
#[constructor]
fn new() -> JsResult<JsObject> { ... }
Defensive patterns

Strategy: validation

Validate before calling

fn has_context_param_when_requested(with_context: bool, sig: &syn::Signature) -> Result<(), String> {
    if with_context && sig.inputs.is_empty() {
        Err("`context` attribute requires a first context parameter".into())
    } else { Ok(()) }
}

Prevention

When it happens

Trigger: `#[constructor(context)]` (or equivalent meta flag) applied to a zero-argument function, e.g. `fn new() -> ...`; copying a no-arg constructor while keeping the `context` attribute.

Common situations: Adding the `context` attribute for future use and forgetting to add the `cx` parameter; simplifying a constructor to take no inputs without removing the attribute; macro migrations where the attribute semantics changed between Neon versions.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at crates/neon-macros/src/class/mod.rs:674

    }
}

// Check if constructor has a context parameter using same heuristic as export functions
// * If the `context` attribute is included, must have at least one argument
// * Inferred to be context if first arg is `&mut FunctionContext` or `&mut Cx`
// * Context argument must be a `&mut` reference
fn check_constructor_context(opts: &meta::Meta, sig: &syn::Signature) -> syn::Result<bool> {
    // Extract the first argument
    let ty = match sig.inputs.first() {
        Some(syn::FnArg::Typed(ty)) => ty,
        Some(syn::FnArg::Receiver(_)) => {
            return Err(syn::Error::new(
                sig.inputs.span(),
                "Constructor cannot have a `self` receiver",
            ))
        }
        None if opts.context => {
            return Err(syn::Error::new(
                sig.inputs.span(),
                "Expected a context argument. Try removing the `context` attribute.",
            ))
        }
        None => return Ok(false),
    };

    // Extract the reference type
    let ty = match &*ty.ty {
        syn::Type::Reference(ty) => ty,

        // Context needs to be a reference
        _ if opts.context || is_context_type(&ty.ty) => {
            return Err(syn::Error::new(
                ty.ty.span(),
                "Context must be a `&mut` reference.",
            ))
        }

View on GitHub (pinned to 38960e4381)