{"record":{"id":"d8659d17a4c1d3a8","repo":"swc-project/swc","slug":"illegal-conversion-cannot-convert-to-lval","errorCode":null,"errorMessage":"illegal conversion: Cannot convert {:?} to LVal","messagePattern":"illegal conversion: Cannot convert (.+?) to LVal","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_estree_compat/src/babelify/pat.rs","lineNumber":80,"sourceCode":"            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 {\n    fn from(pat: PatOutput) -> Self {\n        match pat {\n            PatOutput::Id(i) => LVal::Id(i),\n            PatOutput::Array(a) => LVal::ArrayPat(a),\n            PatOutput::Rest(r) => LVal::RestEl(r),\n            PatOutput::Object(o) => LVal::ObjectPat(o),\n            PatOutput::Assign(a) => LVal::AssignmentPat(a),\n            PatOutput::Expr(expr) => match *expr {\n                Expression::Member(e) => LVal::MemberExpr(e),\n                _ => panic!(\"illegal conversion: Cannot convert {:?} to LVal\", &expr),\n            },\n        }\n    }\n}\n\nimpl From<PatOutput> for PatternLike {\n    fn from(pat: PatOutput) -> Self {\n        match pat {\n            PatOutput::Id(i) => PatternLike::Id(i),\n            PatOutput::Array(a) => PatternLike::ArrayPat(a),\n            PatOutput::Rest(r) => PatternLike::RestEl(r),\n            PatOutput::Object(o) => PatternLike::ObjectPat(o),\n            PatOutput::Assign(a) => PatternLike::AssignmentPat(a),\n            PatOutput::Expr(_) => panic!(\"illegal conversion: Cannot convert {:?} to LVal\", &pat),\n        }\n    }\n}\n","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_estree_compat/src/babelify/pat.rs#L62-L98","documentation":"Babel's LVal accepts identifiers, destructuring patterns, rest elements and member expressions as assignment targets. When converting `PatOutput::Expr`, this From impl maps only `Expression::Member`; any other expression (binary, call, literal, ...) is not a legal assignment target in ESTree, so the conversion panics with 'illegal conversion: Cannot convert ... to LVal'.","triggerScenarios":"A for-in/for-of head (`ForHead::Pat` -> ForStmtLeft::LVal at stmt.rs:327) or other LVal slot receives `Pat::Expr` wrapping a non-member expression — recovered code like `for ((a + b) of xs);` or `this() = 1;`, or a transform that put an arbitrary expression on the left of an assignment.","commonSituations":"Parsing invalid assignment targets with recovery and converting anyway; codemods building AssignExpr with unvalidated left sides; ASTs assembled with swc_ecma_quote without target validation.","solutions":["Validate before babelify that every `Pat::Expr` in LVal position wraps a `MemberExpr`","Reject or fix the source: assignment to a non-member expression is a syntax error anyway","In transforms, construct assignment targets only from valid shapes (Ident patterns or member expressions)","Wrap babelify in catch_unwind at FFI boundaries so this surfaces as an error, not an abort"],"exampleFix":"// before\nlet estree = module.babelify(); // panics on `for ((a + b) of xs);`\n\n// after - reject invalid targets first\nif module.has_invalid_lvals() { return Err(anyhow!(\"invalid assignment target\")); }\nlet estree = module.babelify();","handlingStrategy":"type-guard","validationCode":"use swc_ecma_ast::{Expr, Pat};\n\nfn is_valid_lval(p: &Pat) -> bool {\n    match p {\n        Pat::Ident(_) | Pat::Array(_) | Pat::Rest(_) | Pat::Object(_) | Pat::Assign(_) => true,\n        Pat::Expr(e) => matches!(&**e, Expr::Member(_)),\n        Pat::Invalid(_) => false,\n    }\n}\n\n// run over for-heads and assignment targets before babelify\nfn lvals_ok(m: &swc_ecma_ast::Module) -> bool { /* visitor using is_valid_lval */ true }","typeGuard":"fn expr_is_member(e: &Expr) -> bool {\n    matches!(e, Expr::Member(_))\n}","tryCatchPattern":"use std::panic::{catch_unwind, AssertUnwindSafe};\n\ncatch_unwind(AssertUnwindSafe(|| program.babelify()))\n    .map_err(|_| anyhow!(\"invalid assignment target in input\"))?;","preventionTips":["Validate for-in/for-of heads and assignment LHS before estree conversion","In transforms, build AssignExpr left sides only from valid target shapes","Reject recovered parses instead of babelifying them"],"tags":["rust","swc","babelify","assignment-target","lval","parser-error-recovery","panic"],"backgroundTag":"unsupported-ast-conversion","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}