{"record":{"id":"dc796a47c87b4a70","repo":"neon-bindings/neon","slug":"constructor-cannot-have-a-self-receiver","errorCode":null,"errorMessage":"Constructor cannot have a `self` receiver","messagePattern":"Constructor cannot have a `self` receiver","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/neon-macros/src/class/mod.rs","lineNumber":668,"sourceCode":"    };\n\n    // Must be an identifier named `this`\n    match elem {\n        syn::Pat::Ident(ident) => ident.ident == THIS,\n        _ => false,\n    }\n}\n\n// Check if constructor has a context parameter using same heuristic as export functions\n// * If the `context` attribute is included, must have at least one argument\n// * Inferred to be context if first arg is `&mut FunctionContext` or `&mut Cx`\n// * Context argument must be a `&mut` reference\nfn check_constructor_context(opts: &meta::Meta, sig: &syn::Signature) -> syn::Result<bool> {\n    // Extract the first argument\n    let ty = match sig.inputs.first() {\n        Some(syn::FnArg::Typed(ty)) => ty,\n        Some(syn::FnArg::Receiver(_)) => {\n            return Err(syn::Error::new(\n                sig.inputs.span(),\n                \"Constructor cannot have a `self` receiver\",\n            ))\n        }\n        None if opts.context => {\n            return Err(syn::Error::new(\n                sig.inputs.span(),\n                \"Expected a context argument. Try removing the `context` attribute.\",\n            ))\n        }\n        None => return Ok(false),\n    };\n\n    // Extract the reference type\n    let ty = match &*ty.ty {\n        syn::Type::Reference(ty) => ty,\n\n        // Context needs to be a reference","sourceCodeStart":650,"sourceCodeEnd":686,"githubUrl":"https://github.com/neon-bindings/neon/blob/38960e4381d9ad13b551cdf2d261f609167c9bc2/crates/neon-macros/src/class/mod.rs#L650-L686","documentation":"This compile-time error comes from Neon's `#[constructor]` macro support in crates/neon-macros/src/class/mod.rs (check_constructor_context). A constructor wrapper function must take a context argument (such as `&mut FunctionContext`) as its first parameter; a `self` receiver is not allowed because constructors are static factory functions, not methods. The macro rejects any `impl`-style `self` argument at the first position of the constructor signature.","triggerScenarios":"Declaring a `#[constructor]`-annotated function (or a `new` method inside a `#[impl]` class block) whose signature starts with `self`, `&self`, `&mut self`, or `self: ...` instead of a context reference.","commonSituations":"Converting an existing Rust method into a JS constructor by adding `#[constructor]` without removing `&self`; copying a regular method's signature when authoring a new class; refactorings that turn an associated `new` fn into a method.","solutions":["Remove the `self` receiver from the constructor signature","Make the first parameter a context reference like `&mut FunctionContext` (or omit arguments entirely if no context is needed)","If the function genuinely needs `self`, it is not a constructor — use `#[method]` instead of `#[constructor]`"],"exampleFix":"// before\n#[constructor]\nfn new(&mut self, cx: &mut FunctionContext) -> JsResult<JsValue> { ... }\n// after\n#[constructor]\nfn new(cx: &mut FunctionContext) -> JsResult<JsValue> { ... }","handlingStrategy":"validation","validationCode":"fn validate_constructor_sig(sig: &syn::Signature) -> Result<(), String> {\n    match sig.inputs.first() {\n        Some(syn::FnArg::Receiver(_)) => Err(\"constructor must not take `self`\".into()),\n        Some(syn::FnArg::Typed(_)) | None => Ok(()),\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always write constructors as associated functions with no `self`","Reserve `self` receivers for `#[method]` functions only","Compile early with `cargo check` after adding `#[constructor]` to catch proc-macro diagnostics fast"],"tags":["rust","neon","proc-macro","constructor","signature"],"backgroundTag":"invalid-constructor-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"}