{"record":{"id":"539a7c06f4c81df8","repo":"swc-project/swc","slug":"rest-pattern-should-handled-by-array-pattern-handl","errorCode":null,"errorMessage":"rest pattern should handled by array pattern handler: {:?}","messagePattern":"rest pattern should handled by array pattern handler: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_ecma_compat_es2015/src/destructuring.rs","lineNumber":156,"sourceCode":"                },\n                body => BlockStmt {\n                    stmts: vec![stmt, body.take()],\n                    ..Default::default()\n                },\n            }));\n        }\n    };\n}\n\nfn make_ref_ident_for_for_stmt() -> Ident {\n    private_ident!(\"ref\")\n}\n\nimpl AssignFolder {\n    fn visit_mut_var_decl(&mut self, decls: &mut Vec<VarDeclarator>, decl: VarDeclarator) {\n        match decl.name {\n            Pat::Ident(..) => decls.push(decl),\n            Pat::Rest(..) => unreachable!(\n                \"rest pattern should handled by array pattern handler: {:?}\",\n                decl.name\n            ),\n            Pat::Array(ArrayPat { elems, .. }) => {\n                assert!(\n                    decl.init.is_some(),\n                    \"destructuring pattern binding requires initializer\"\n                );\n\n                let init = decl.init.unwrap();\n\n                if is_literal(&init) {\n                    match *init {\n                        Expr::Array(arr)\n                            if !elems.is_empty()\n                                && (elems.len() == arr.elems.len()\n                                    || (elems.len() < arr.elems.len() && has_rest_pat(&elems))) =>\n                        {","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_ecma_compat_es2015/src/destructuring.rs#L138-L174","documentation":"While lowering destructuring declarations, a VarDeclarator whose name pattern is a top-level Rest (`const ...rest = arr`) is invalid — rest patterns only legally occur inside array patterns and are consumed by that handler. Seeing one means an earlier transform or hand-built AST produced a malformed pattern tree.","triggerScenarios":"Pipelines where a previous pass rewrites array/object patterns and accidentally hoists a Rest to the declarator level; AST constructed manually with Pat::Rest as a VarDeclarator name; bugs in sibling destructuring-related passes.","commonSituations":"Custom or out-of-order compat passes (spread, object_rest_spread, destructuring) mutating patterns; plugin authors moving Pat nodes between positions.","solutions":["Restore the official pass ordering and retest — most hoisting bugs come from reordered transforms","Validate AST invariants before compat: walk VarDeclarators and assert name is not Pat::Rest","Reduce the input to the statement that produces the pattern and report upstream with your pass list"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// Assert no top-level Rest pattern in declarators before compat passes\nfn no_top_level_rest(m: &swc_ecma_ast::Module) -> bool {\n    struct V(bool);\n    impl Visit for V {\n        fn visit_var_declarator(&mut self, d: &VarDeclarator) {\n            if matches!(d.name, Pat::Rest(_)) { self.0 = false; }\n            d.visit_children_with(self);\n        }\n    }\n    let mut v = V(true);\n    m.visit_with(&mut v);\n    v.0\n}","tryCatchPattern":"if let Err(p) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| destructuring(&mut program))) {\n    if panic_message(&p).contains(\"rest pattern\") {\n        // an earlier pass produced a malformed pattern; disable that pass or reorder\n    } else { std::panic::resume_unwind(p); }\n}","preventionTips":["Keep transforms that move Pat nodes from hoisting Rest out of ArrayPat","Validate AST invariants (no top-level Pat::Rest in VarDeclarator) between passes in debug builds","Reorder passes back to the official sequence before debugging deeper"],"tags":["swc","compat","es2015","destructuring","rest-pattern","ast"],"backgroundTag":"malformed-destructuring-pattern","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"}