{"record":{"id":"7561b3721ee3c1a8","repo":"swc-project/swc","slug":"using-declaration-must-be-removed-by-previous-pass-7561b3","errorCode":null,"errorMessage":"using declaration must be removed by previous pass","messagePattern":"using declaration must be removed by previous pass","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_ecma_compat_es2015/src/for_of.rs","lineNumber":184,"sourceCode":"                            ..Default::default()\n                        }\n                        .into(),\n                    )\n                }\n\n                ForHead::Pat(pat) => prepend_stmt(\n                    &mut body.stmts,\n                    AssignExpr {\n                        span: DUMMY_SP,\n                        left: pat.try_into().unwrap(),\n                        op: op!(\"=\"),\n                        right: arr.computed_member(i).into(),\n                    }\n                    .into_stmt(),\n                ),\n\n                ForHead::UsingDecl(..) => {\n                    unreachable!(\"using declaration must be removed by previous pass\")\n                }\n\n                #[cfg(swc_ast_unknown)]\n                _ => panic!(\"unable to access unknown nodes\"),\n            }\n\n            let stmt = ForStmt {\n                span,\n                init: Some(\n                    VarDecl {\n                        span: DUMMY_SP,\n                        kind: VarDeclKind::Let,\n                        declare: false,\n                        decls,\n                        ..Default::default()\n                    }\n                    .into(),\n                ),","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_ecma_compat_es2015/src/for_of.rs#L166-L202","documentation":"The es2015 for-of lowering (swc_ecma_compat_es2015::for_of, array-iteration strategy) rewrites `for (... of ...)` heads into index-based ForStmt loops. It only knows how to emit the element assignment for VarDecl and Pat heads; ForHead::UsingDecl (`for (using x of xs)`, the explicit-resource-management proposal) must already have been rewritten by swc_ecma_transforms_proposal::explicit_resource_management, so seeing it is an invariant violation and panics.","triggerScenarios":"Compiling `for (using x of xs) { ... }` (or `for await (using x of xs)`) through es2015 for-of lowering when the explicit_resource_management pass has not run first — typically a hand-built compat chain, or a driver that enables `using` parsing without wiring its lowering pass.","commonSituations":"Adopting TypeScript 5.2+ `using` declarations while targeting ES5/ES2015 via custom swc integrations; codemods/bundlers embedding swc_core that assemble compat passes manually; swc_core version mismatch dropping the proposal pass.","solutions":["Run swc_ecma_transforms_proposal::explicit_resource_management() before any swc_ecma_compat_es2015 for-of/generator pass in your chain.","Use the standard preset_env/CompatEnv pipeline, which schedules the using-declaration lowering before compat passes.","Align all swc crates on a single swc_core version.","As a stopgap, avoid `using` bindings in for-of heads until your pipeline lowers them."],"exampleFix":"// before: for-of lowering panics on `for (using res of resources)`\nlet pass = Chain::new(swc_ecma_compat_es2015::for_of());\n\n// after: lower using-declarations first\nlet pass = Chain::new(\n    swc_ecma_transforms_proposal::explicit_resource_management(),\n    swc_ecma_compat_es2015::for_of(),\n);","handlingStrategy":"validation","validationCode":"use swc_ecma_ast::{ForHead, Program}; use swc_ecma_visit::{Visit, VisitWith};\nstruct UsingInForOf; impl Visit for UsingInForOf {\n    fn visit_for_of_stmt(&mut self, n: &ForOfStmt) {\n        if let ForHead::UsingDecl(_) = n.left {\n            panic!(\"explicit_resource_management pass must run before compat es2015\");\n        }\n        n.visit_children_with(self);\n    }\n}\n// program.visit_with(&mut UsingInForOf);","typeGuard":null,"tryCatchPattern":"let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| for_of_pass.apply(cm, &mut program)));\nr.map_err(|p| map_payload_to_config_error(p, \"run explicit_resource_management first\"))?;","preventionTips":["Always pair `using` syntax support with the explicit_resource_management pass.","Run a pre-flight AST scan for ForHead::UsingDecl before es2015 lowering.","Test your pipeline with TS 5.2 explicit-resource-management fixtures."],"tags":["swc","compat-es2015","for-of","using-declarations","explicit-resource-management","pass-ordering","panic"],"backgroundTag":"compiler-pass-ordering","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}