{"record":{"id":"bacae02e27834753","repo":"swc-project/swc","slug":"method-of-fold-visitmut-must-accept-two-paramete","errorCode":null,"errorMessage":"method of Fold / VisitMut must accept two parameters","messagePattern":"method of Fold / VisitMut must accept two parameters","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_ecma_transforms_macros/src/fast.rs","lineNumber":92,"sourceCode":"                    fn #name(&mut self, node: &mut #ty) {\n                        node.visit_mut_children_with(self)\n                    }\n                ),\n            };\n\n            items.push(method);\n        }\n\n        items\n    }\n\n    /// Add fast path to a method\n    fn patch_method(&self, mut m: ImplItemFn) -> ImplItemFn {\n        let ty_arg = m\n            .sig\n            .inputs\n            .last()\n            .expect(\"method of Fold / VisitMut must accept two parameters\");\n        let ty_arg = match ty_arg {\n            FnArg::Receiver(_) => unreachable!(),\n            FnArg::Typed(ty) => ty,\n        };\n        if m.sig.ident == \"visit_mut_ident\" || m.sig.ident == \"fold_ident\" {\n            return m;\n        }\n        if m.block.stmts.is_empty() {\n            return m;\n        }\n\n        let arg = match &*ty_arg.pat {\n            Pat::Ident(i) => &i.ident,\n            _ => unimplemented!(\n                \"Fast-path injection for Fold / VisitMut where pattern is not an ident\"\n            ),\n        };\n","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_ecma_transforms_macros/src/fast.rs#L74-L110","documentation":"When `#[fast_path]` patches each method of a `Fold`/`VisitMut` impl, `patch_method` takes the last entry of `m.sig.inputs` and expects it to exist. `inputs.last()` is None only when the method signature has zero parameters, so the `expect(\"method of Fold / VisitMut must accept two parameters\")` panics during macro expansion for any associated function in the impl that declares no arguments at all. The following match also asserts the last argument is `FnArg::Typed`, not `self`.","triggerScenarios":"Adding a helper item inside the annotated impl such as `fn make() -> Self { ... }` or `fn default_config() -> Config { ... }` with no parameters and no `self`; the macro blindly patches every `ImplItem::Fn` in the block.","commonSituations":"Transform authors adding constructors or utility functions to an impl block that carries `#[fast_path]`. Note the macro only needs visitor-shaped methods like `fn fold_expr(&mut self, e: Expr) -> Expr`.","solutions":["Give every fn in the `#[fast_path]`-annotated impl the visitor shape: a receiver plus the node parameter, e.g. `fn visit_mut_expr(&mut self, n: &mut Expr)`.","Move zero-argument helpers (constructors, config fns) out of the annotated impl block into a separate `impl` or free functions.","Methods named `visit_mut_ident`/`fold_ident` and empty-bodied methods are skipped automatically; helpers cannot be marked otherwise."],"exampleFix":"// before\n#[fast_path(ArrowVisitor)]\nimpl Fold for ArrowPass {\n    fn config() -> Config { Config::default() } // zero params -> panic\n}\n\n// after\nimpl ArrowPass {\n    fn config() -> Config { Config::default() } // moved out\n}\n#[fast_path(ArrowVisitor)]\nimpl Fold for ArrowPass { /* visitor methods only */ }","handlingStrategy":"validation","validationCode":"// Every fn inside a #[fast_path] impl must have >= 1 non-self parameter.\n// Check signatures before adding helpers:\nfn has_visitor_shape(sig: &dyn std::any::Any) -> bool { false } // placeholder\n// Practical rule (manual): keep only methods like\n//   fn visit_mut_expr(&mut self, n: &mut Expr)\n//   fn fold_expr(&mut self, e: Expr) -> Expr\n// and move zero-arg helpers to a separate impl block.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep annotated impl blocks free of constructors and zero-argument helpers; put them in a separate impl.","Match the Fold/VisitMut method shape exactly: receiver + node parameter.","Run cargo check immediately after adding any item to a #[fast_path] impl."],"tags":["proc-macro","fast-path","method-signature","compile-time","swc"],"backgroundTag":"macro-method-signature-invalid","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}