swc-project/swc · error

Cannot transform product children into CalcProduct

Error message

Cannot transform product children into CalcProduct

What it means

Companion guard to the CalcSum one in the same serializer: when re-serializing a CalcProduct's children into CalcValueOrOperator, only Value and operator nodes (Product/Sum get an explicit '*' inserted) are expected; any other child kind reaches unreachable!(). Like its sibling it marks a calc tree the minifier cannot round-trip — from a folding bug or hand-built AST.

Source

Thrown at crates/swc_css_minifier/src/compressor/calc_sum.rs:997

                            CalcOperatorNode::Invert(calc_node) => {
                                expr.push(CalcValueOrOperator::Operator(CalcOperator {
                                    value: CalcOperatorType::Div,
                                    span: Span::dummy_with_cmt(),
                                }));
                                expr.push(CalcValueOrOperator::Value(
                                    serialize_calc_node_into_calc_value(calc_node),
                                ));
                            }
                            CalcOperatorNode::Product(_) | CalcOperatorNode::Sum(_) => {
                                expr.push(CalcValueOrOperator::Operator(CalcOperator {
                                    value: CalcOperatorType::Mul,
                                    span: Span::dummy_with_cmt(),
                                }));
                                expr.push(CalcValueOrOperator::Value(
                                    serialize_calc_node_into_calc_value(calc_node),
                                ));
                            }
                            _ => unreachable!("Cannot transform product children into CalcProduct"),
                        },
                    }
                }
                CalcProductOrOperator::Product(CalcProduct {
                    expressions: expr,
                    span: Span::dummy_with_cmt(),
                })
            }
            CalcOperatorNode::Sum(_) => CalcProductOrOperator::Product(CalcProduct {
                expressions: vec![CalcValueOrOperator::Value(
                    serialize_calc_node_into_calc_value(calc_node),
                )],
                span: Span::dummy_with_cmt(),
            }),
        },
    }
}

View on GitHub (pinned to 5176682b65)

Solutions

  1. Update the swc_css_* crates together to one version
  2. Reduce the failing stylesheet to a single calc() declaration, simplify the expression, and report the repro upstream
  3. Normalize externally built calc ASTs through the parser before minifying
Defensive patterns

Strategy: fallback

Validate before calling

// Count calc() usages to route high-risk files to a no-minify path
let calc_heavy = css.matches("calc(").count() > 8;
let mode = if calc_heavy { Minify::Off } else { Minify::On };

Try / catch

match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| minify(&stylesheet))) {
    Ok(v) => v,
    Err(p) if panic_message(&p).contains("Cannot transform product children") => stylesheet, // unminified fallback
    Err(p) => std::panic::resume_unwind(p),
}

Prevention

When it happens

Trigger: CSS minification where the product-level serialization encounters an unexpected CalcValueOrOperator child — deeply nested calc(), division/modulus combinations, or calc trees injected by custom transforms.

Common situations: Complex utility-CSS with chained calc(); minifier/parser version skew; plugins that rewrite calc subtrees.

Related errors


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