{"record":{"id":"7c3fa3b2204e052f","repo":"swc-project/swc","slug":"illegal-conversion-cannot-convert-to-pattern","errorCode":null,"errorMessage":"illegal conversion: Cannot convert {:?} to Pattern","messagePattern":"illegal conversion: Cannot convert (.+?) to Pattern","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_estree_compat/src/babelify/pat.rs","lineNumber":52,"sourceCode":"            Pat::Assign(a) => PatOutput::Assign(a.babelify(ctx)),\n            Pat::Expr(e) => PatOutput::Expr(Box::alloc().init(e.babelify(ctx).into())),\n            Pat::Invalid(_) => panic!(\n                \"illegal conversion: Cannot convert {:?} to PatOutput\",\n                &self\n            ),\n            #[cfg(swc_ast_unknown)]\n            _ => panic!(\"unable to access unknown nodes\"),\n        }\n    }\n}\n\nimpl From<PatOutput> for Pattern {\n    fn from(pat: PatOutput) -> Self {\n        match pat {\n            PatOutput::Assign(a) => Pattern::Assignment(a),\n            PatOutput::Array(a) => Pattern::Array(a),\n            PatOutput::Object(o) => Pattern::Object(o),\n            _ => panic!(\"illegal conversion: Cannot convert {:?} to Pattern\", &pat),\n        }\n    }\n}\n\nimpl From<PatOutput> for ObjectPropVal {\n    fn from(pat: PatOutput) -> Self {\n        match pat {\n            PatOutput::Expr(e) => ObjectPropVal::Expr(e),\n            PatOutput::Id(p) => ObjectPropVal::Pattern(PatternLike::Id(p)),\n            PatOutput::Array(p) => ObjectPropVal::Pattern(PatternLike::ArrayPat(p)),\n            PatOutput::Rest(p) => ObjectPropVal::Pattern(PatternLike::RestEl(p)),\n            PatOutput::Object(p) => ObjectPropVal::Pattern(PatternLike::ObjectPat(p)),\n            PatOutput::Assign(p) => ObjectPropVal::Pattern(PatternLike::AssignmentPat(p)),\n        }\n    }\n}\n\nimpl From<PatOutput> for LVal {","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_estree_compat/src/babelify/pat.rs#L34-L70","documentation":"The `From<PatOutput> for Pattern` impl converts babelify's intermediate pattern into an estree `Pattern`, which models only destructuring forms: Assignment, Array, Object. Every other PatOutput variant — Id, Rest, and Expr included — falls into `_` and panics. It is a type-shape mismatch: the value is a legitimate SWC pattern but Babel's Pattern type cannot represent it.","triggerScenarios":"Call `.into()` / `Pattern::from` on a PatOutput that is an identifier, a rest element (`...r`), or a wrapped expression, in any slot typed `swc_estree_ast::Pattern` — usually a recovered parse or a transform that placed a non-destructuring pattern into a pattern-only position.","commonSituations":"Codemods/transforms rewriting parameters or defaults and leaving Ident/Rest where a destructuring pattern is required; babelifying error-recovered ASTs; direct users of the crate's public From<PatOutput> impls.","solutions":["Route only Array/Object/Assignment patterns into Pattern-typed slots; keep identifiers in Id-typed slots (PatternLike/Param/LVal)","Validate patterns before conversion and reject non-destructuring ones with a diagnostic","Fix the transform or input that produced the misplaced pattern","Maintainer: change call sites to use PatternLike/Param targets instead of Pattern where Ids are legal"],"exampleFix":"// before\nlet pat: Pattern = pat_output.into(); // panics for Id/Rest/Expr\n\n// after\nlet pat: Pattern = match pat_output {\n    PatOutput::Array(a) => Pattern::Array(a),\n    PatOutput::Object(o) => Pattern::Object(o),\n    PatOutput::Assign(a) => Pattern::Assignment(a),\n    other => return Err(anyhow!(\"not a destructuring pattern: {other:?}\")),\n};","handlingStrategy":"validation","validationCode":"use swc_ecma_ast::Pat;\n\nfn is_destructuring_only(p: &Pat) -> bool {\n    matches!(p, Pat::Array(_) | Pat::Object(_) | Pat::Assign(_))\n}","typeGuard":"fn fits_estree_pattern(p: &Pat) -> bool {\n    matches!(p, Pat::Array(_) | Pat::Object(_) | Pat::Assign(_))\n}","tryCatchPattern":"use std::panic::{catch_unwind, AssertUnwindSafe};\n\ncatch_unwind(AssertUnwindSafe(|| pat_output.into_pattern()))\n    .map_err(|p| anyhow!(\"cannot convert pattern to estree Pattern: {:?}\", p))?;","preventionTips":["Route identifiers to Id-typed slots (PatternLike/Param/LVal), never to Pattern-typed slots","Unit-test transforms with Id, Rest and Expr patterns to catch misplacement early","Treat estree Pattern as destructuring-only by convention in your code"],"tags":["rust","swc","babelify","destructuring","type-conversion","panic"],"backgroundTag":"unsupported-ast-conversion","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-08-22T14:17:55.899Z"}