swc-project/swc · error

unknown binary operator: {:?}

Error message

unknown binary operator: {:?}

What it means

perform_arithmetic_op in the simplifier constant-folds bitwise/modulo arithmetic once both sides are Known values. It matches exactly `&`, `|`, `^`, `%`; any other operator reaching the final match trips this unreachable!. Like its shift sibling, the guard upstream is supposed to only route those four operators here.

Source

Thrown at crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs:1580

                let result: f64 = js_lv.pow(js_rv).into();
                return try_replace(lv, rv, result);
            }

            return Unknown;
        }
        _ => {}
    }
    let (lv, rv) = match (lv, rv) {
        (Known(lv), Known(rv)) => (lv, rv),
        _ => return Unknown,
    };

    match op {
        op!("&") => try_replace_i32(lv, rv, to_int32(lv) & to_int32(rv)),
        op!("|") => try_replace_i32(lv, rv, to_int32(lv) | to_int32(rv)),
        op!("^") => try_replace_i32(lv, rv, to_int32(lv) ^ to_int32(rv)),
        op!("%") => try_replace(lv, rv, lv % rv),
        _ => unreachable!("unknown binary operator: {:?}", op),
    }
}

/// This actually performs `<`.
///
/// https://tc39.github.io/ecma262/#sec-abstract-relational-comparison
fn perform_abstract_rel_cmp(
    expr_ctx: ExprCtx,
    left: &Expr,
    right: &Expr,
    will_negate: bool,
) -> Value<bool> {
    match (left, right) {
        // Special case: `x < x` is always false.
        (
            &Expr::Ident(
                Ident {
                    sym: ref li,

View on GitHub (pinned to 5176682b65)

Solutions

  1. Align swc crates on one swc_core version and update — the routing guards and matches are versioned together.
  2. In forks, keep the caller's operator filter consistent with this match.
  3. File an swc issue with the expression if stock swc reproduces it.
Defensive patterns

Strategy: validation

Validate before calling

fn is_arithmetic_fold_op(op: BinaryOp) -> bool {
    matches!(op, op!("&") | op!("|") | op!("^") | op!("%"))
}

Prevention

When it happens

Trigger: An swc internal bug where the simplifier routes an unsupported binary operator into perform_arithmetic_op, or a fork calling this internal function with an arbitrary op.

Common situations: Forked/patched simplifiers; version skew inside swc crates; not reachable through public config on released versions.

Related errors


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