swc-project/swc · error

unknown compound assignment operator

Error message

unknown compound assignment operator

What it means

While converting functions into regenerator-style state machines, the es2015 generator pass must rewrite compound assignments (e.g. `a += b`) into read-op-write sequences and maps each compound AssignOp to its binary counterpart. The match exhaustively lists every compound operator (`%= **= <<= >>= >>>= &= |= ^= &&= ||= ??=`); the unreachable! arm fires only for an AssignOp outside that set, which the parser can never produce.

Source

Thrown at crates/swc_ecma_compat_es2015/src/generator.rs:666

                    // 4. Convert compound assignment to normal assignment
                    let bin_op = match node.op {
                        op!("+=") => op!(bin, "+"),
                        op!("-=") => op!(bin, "-"),
                        op!("*=") => op!("*"),
                        op!("/=") => op!("/"),
                        op!("%=") => op!("%"),
                        op!("**=") => op!("**"),
                        op!("<<=") => op!("<<"),
                        op!(">>=") => op!(">>"),
                        op!(">>>=") => op!(">>>"),
                        op!("&=") => op!("&"),
                        op!("|=") => op!("|"),
                        op!("^=") => op!("^"),
                        op!("&&=") => op!("&&"),
                        op!("||=") => op!("||"),
                        op!("??=") => op!("??"),
                        _ => {
                            unreachable!("unknown compound assignment operator")
                        }
                    };

                    *e = AssignExpr {
                        span: node.span,
                        op: op!("="),
                        left: target,
                        right: BinExpr {
                            span: DUMMY_SP,
                            op: bin_op,
                            left: cached_value.into(),
                            right: node.right.take(),
                        }
                        .into(),
                    }
                    .into();
                } else {
                    node.right.visit_mut_with(self);

View on GitHub (pinned to 5176682b65)

Solutions

  1. If you build ASTs programmatically or via FFI/serde, audit how AssignExpr.op is constructed and use the op!() macros so only valid operators appear.
  2. Make sure swc_ecma_ast, swc_ecma_compat_es2015 and swc_core are the same release line (cargo tree -i swc_ecma_ast).
  3. If the input is plain parsed source, report an swc bug with a minimal repro (the arm is unreachable from the parser).
Defensive patterns

Strategy: validation

Validate before calling

// If you synthesize AssignExpr nodes, validate the operator first
fn valid_compound_op(op: AssignOp) -> bool {
    matches!(op, op!("%=") | op!("**=") | op!("<<=") | op!(">>=") | op!(">>>=")
        | op!("&=") | op!("|=") | op!("^=") | op!("&&=") | op!("||=") | op!("??="))
}

Prevention

When it happens

Trigger: A corrupted or hand-constructed AST (via FFI, serde round-trip, or a custom transform) carrying an invalid/unknown AssignOp discriminant inside a generator function; or a swc_ecma_ast version that added a new assignment operator without a matching update in swc_ecma_compat_es2015::generator.

Common situations: wasm/FFI bindings deserializing an AST produced by a different swc version; unit tests synthesizing AssignExpr nodes by hand; upgrading swc_ecma_ast ahead of the compat crates.

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/a5f16211fcbd5dcc. Report an issue: GitHub.