{"record":{"id":"96689251ed8521cd","repo":"neon-bindings/neon","slug":"expected-a-context-argument-after-self-when-using-neon","errorCode":null,"errorMessage":"Expected a context argument after `&self` when using `#[neon(context)]`. Add a parameter like `cx: &mut FunctionContext` or remove the `context` attribute.","messagePattern":"Expected a context argument after `&self` when using `#\\[neon\\(context\\)\\]`\\. Add a parameter like `cx: &mut FunctionContext` or remove the `context` attribute\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/neon-macros/src/class/mod.rs","lineNumber":481,"sourceCode":"            \"Context is not available in async functions. Try a `Channel` instead.\",\n        )),\n\n        _ => Ok(false),\n    }\n}\n\n// Extract the first argument (after &self) from a method signature\nfn first_arg<'a>(\n    opts: &meta::Meta,\n    sig: &'a syn::Signature,\n) -> syn::Result<Option<&'a syn::PatType>> {\n    // Extract the second argument (skip &self)\n    let arg = match sig.inputs.iter().nth(1) {\n        Some(arg) => arg,\n\n        // If context was forced, error to let the user know the mistake\n        None if opts.context => {\n            return Err(syn::Error::new(\n                sig.inputs.span(),\n                \"Expected a context argument after `&self` when using `#[neon(context)]`. Add a parameter like `cx: &mut FunctionContext` or remove the `context` attribute.\",\n            ))\n        }\n\n        None => return Ok(None),\n    };\n\n    // Expect a typed pattern; self receivers are not supported (but shouldn't appear here)\n    match arg {\n        syn::FnArg::Typed(ty) => Ok(Some(ty)),\n        syn::FnArg::Receiver(arg) => Err(syn::Error::new(\n            arg.span(),\n            \"Unexpected second receiver argument.\",\n        )),\n    }\n}\n","sourceCodeStart":463,"sourceCodeEnd":499,"githubUrl":"https://github.com/neon-bindings/neon/blob/38960e4381d9ad13b551cdf2d261f609167c9bc2/crates/neon-macros/src/class/mod.rs#L463-L499","documentation":"Compile-time error from Neon's `#[neon]` class macro, raised by `first_arg` when `#[neon(context)]` forces context mode. The macro skips `&self` and looks for a second argument; when the method has no argument after `&self` and `opts.context` is set, it reports that a context parameter (like `cx: &mut FunctionContext`) is mandatory under the `context` attribute.","triggerScenarios":"Annotating a `&self` method with `#[neon(context)]` (or building under a context-forced configuration) while the method signature has no parameter after `&self`, e.g. `fn size(&self) -> JsResult<...>` with `#[neon(context)]`.","commonSituations":"Adopting the `#[neon(context)]` attribute on existing zero-argument methods; enabling the context flag project-wide (e.g. via the neon `context` feature in Cargo.toml) without updating method signatures; forgetting that `&self` alone does not count as the context argument.","solutions":["Add a context parameter after `&self`: `fn foo(&self, cx: &mut FunctionContext)` (or the appropriate `&mut Cx` variant).","Remove the `#[neon(context)]` attribute from the method if a context is not needed.","If the context requirement comes from a project-wide setting, either update all exported methods or disable that setting."],"exampleFix":"// before\n#[neon(context)]\nfn size(&self) -> JsResult<JsNumber> {\n    ...\n}\n\n// after\n#[neon(context)]\nfn size(&self, cx: &mut FunctionContext) -> JsResult<JsNumber> {\n    ...\n}","handlingStrategy":"validation","validationCode":"// Under #[neon(context)], every &self method must declare a context parameter after &self.\nfn validate_context_method(inputs: &[&str]) -> Result<(), String> {\n    if inputs.len() < 2 {\n        return Err(\"#[neon(context)] requires a context parameter after &self, e.g. cx: &mut FunctionContext\".into());\n    }\n    Ok(())\n}","typeGuard":"fn has_context_arg(inputs: &[String]) -> bool {\n    inputs.get(1).map(|t| {\n        t.starts_with(\"&mut FunctionContext\") || t.starts_with(\"&mut Cx\")\n    }).unwrap_or(false)\n}","tryCatchPattern":null,"preventionTips":["Never apply `#[neon(context)]` to zero-parameter `&self` methods without adding a context argument.","If enabling project-wide context mode, update all exported method signatures first.","Prefer removing the attribute over guessing; only use `#[neon(context)]` when the method genuinely needs the context."],"tags":["rust","proc-macro","neon","context","compile-time"],"backgroundTag":"missing-required-argument","analyzedSha":"38960e4381d9ad13b551cdf2d261f609167c9bc2","analyzedAt":"2026-09-13T09:05:33.640Z","contentChangedAt":"2026-09-13T09:05:33.640Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}