{"record":{"id":"718b67c2057acfe7","repo":"swc-project/swc","slug":"using-declaration-must-be-removed-by-previous-pass-718b67","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/generator.rs","lineNumber":1918,"sourceCode":"\n            // Assign to user's variable\n            let variable = match node.left {\n                ForHead::VarDecl(initializer) => {\n                    for variable in initializer.decls.iter() {\n                        self.hoist_variable_declaration(&Ident::from(\n                            variable.name.as_ident().unwrap(),\n                        ));\n                    }\n\n                    initializer.decls[0].name.clone()\n                }\n                ForHead::Pat(mut initializer) => {\n                    initializer.visit_mut_with(self);\n                    *initializer\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            self.emit_assignment(variable.try_into().unwrap(), Box::new(key.into()), None);\n            self.transform_and_emit_embedded_stmt(*node.body);\n\n            self.mark_label(increment_label);\n            self.emit_stmt(\n                ExprStmt {\n                    span: DUMMY_SP,\n                    expr: UpdateExpr {\n                        span: DUMMY_SP,\n                        prefix: false,\n                        op: op!(\"++\"),\n                        arg: Box::new(keys_index.clone().into()),\n                    }","sourceCodeStart":1900,"sourceCodeEnd":1936,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_ecma_compat_es2015/src/generator.rs#L1900-L1936","documentation":"The es2015 generator pass also rewrites for-of loops that appear inside converted functions: it extracts the loop head to emit the assignment `head = _key`. It handles VarDecl and Pat heads but panics on ForHead::UsingDecl because using-declarations (`for (using x of xs)`) must be lowered by swc_ecma_transforms_proposal::explicit_resource_management before generator conversion.","triggerScenarios":"A for-of loop with a `using` head inside a generator or async function, compiled by a chain that includes es2015::generator but not explicit_resource_management.","commonSituations":"TS 5.2+ `using` syntax compiled to ES5 through custom swc_core pipelines; bundler plugins assembling compat passes by hand; swc_core version mismatch omitting the proposal pass.","solutions":["Put explicit_resource_management() before generator()/for_of() in your chain.","Use preset_env/CompatEnv (correct ordering guaranteed).","Unify swc versions via a single swc_core release.","Avoid `using` in for-of heads until wired."],"exampleFix":"// before\nlet pass = Chain::new(swc_ecma_compat_es2015::generator());\n// after\nlet pass = Chain::new(\n    swc_ecma_transforms_proposal::explicit_resource_management(),\n    swc_ecma_compat_es2015::generator(),\n);","handlingStrategy":"validation","validationCode":"// Reuse the ForHead scan before generator conversion\nuse swc_ecma_visit::{Visit, VisitWith};\nstruct UsingHead(bool); impl Visit for UsingHead {\n    fn visit_for_head(&mut self, h: &ForHead) {\n        self.0 |= matches!(h, ForHead::UsingDecl(_)); h.visit_children_with(self);\n    }\n}\n// if UsingHead found anything, prepend explicit_resource_management()","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Gate `using` syntax in the parser on the presence of its lowering pass.","Include generator-function fixtures containing for-of in CI.","Use CompatEnv/preset_env pipelines for ES5 targets."],"tags":["swc","compat-es2015","generator","for-of","using-declarations","pass-ordering","panic"],"backgroundTag":"compiler-pass-ordering","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}