{"record":{"id":"7223c766d64ca461","repo":"swc-project/swc","slug":"cannot-transform-sum-children-into-calcproduct","errorCode":null,"errorMessage":"Cannot transform sum children into CalcProduct","messagePattern":"Cannot transform sum children into CalcProduct","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_css_minifier/src/compressor/calc_sum.rs","lineNumber":890,"sourceCode":"                            }));\n                            expr.push(serialize_calc_node_into_calc_product(calc_node));\n                        }\n                        CalcNode::OperatorNode(op) => match &**op {\n                            CalcOperatorNode::Product(_) => {\n                                expr.push(CalcProductOrOperator::Operator(CalcOperator {\n                                    value: CalcOperatorType::Add,\n                                    span: Span::dummy_with_cmt(),\n                                }));\n                                expr.push(serialize_calc_node_into_calc_product(calc_node));\n                            }\n                            CalcOperatorNode::Negate(calc_node) => {\n                                expr.push(CalcProductOrOperator::Operator(CalcOperator {\n                                    value: CalcOperatorType::Sub,\n                                    span: Span::dummy_with_cmt(),\n                                }));\n                                expr.push(serialize_calc_node_into_calc_product(calc_node));\n                            }\n                            _ => unreachable!(\"Cannot transform sum children into CalcProduct\"),\n                        },\n                    }\n                }\n                CalcSum {\n                    expressions: expr,\n                    span: Span::dummy_with_cmt(),\n                }\n            }\n            _ => CalcSum {\n                expressions: vec![serialize_calc_node_into_calc_product(calc_node)],\n                span: Span::dummy_with_cmt(),\n            },\n        },\n    }\n}\n\nfn serialize_calc_node_into_calc_product(calc_node: &CalcNode) -> CalcProductOrOperator {\n    match calc_node {","sourceCodeStart":872,"sourceCodeEnd":908,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_css_minifier/src/compressor/calc_sum.rs#L872-L908","documentation":"While minifying, swc_css_minifier re-serializes folded calc() trees. Converting a CalcSum's children back into CalcProductOrOperator it expects each CalcOperatorNode to be Add or Negate; any other operator shape hits unreachable!(), flagging a calc AST the serializer cannot express — normally produced by an earlier folding bug or by externally constructed calc nodes.","triggerScenarios":"Minifying stylesheets containing calc() expressions with mixed/nested operators where the internal re-serialization invariant breaks; custom CSS transforms that emit CalcOperatorNode trees directly into the minifier.","commonSituations":"Aggressive CSS minification on complex calc() like `calc(100% - (10px + 2em) * 3)`; version drift between swc_css_parser and swc_css_minifier; machine-generated stylesheets.","solutions":["Update swc_css_minifier and swc_css_parser together to the same release","Bisect the stylesheet to the failing calc() expression, simplify it (pre-resolve arithmetic manually), and attach the repro to a swc issue","If you generate calc ASTs programmatically, round-trip them through the parser's own serialization once to normalize the tree"],"exampleFix":"/* before */\nwidth: calc(100% - (10px + 2em) * 3);\n\n/* after */\nwidth: calc(100% - 30px - 6em);","handlingStrategy":"fallback","validationCode":"// Flag complex calc() expressions so they can be excluded/simplified before minification\nfn risky_calc(css: &str) -> bool {\n    css.matches(\"calc(\").count() > 4 || css.contains(\"calc(\") && css.contains(\"* 3\")\n}","typeGuard":null,"tryCatchPattern":"let min = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| minify(&stylesheet)));\nmatch min {\n    Ok(v) => v,\n    Err(p) if panic_message(&p).contains(\"CalcProduct\") => {\n        // fallback: emit the stylesheet unminified (or skip calc folding) for this file\n        stylesheet\n    }\n    Err(p) => std::panic::resume_unwind(p),\n}","preventionTips":["Keep swc_css_minifier and swc_css_parser on the same release","Pre-resolve arithmetic in generated calc() where possible","Ship a build fallback that skips CSS minification on panic instead of failing the release","Bisect reported failures to a single calc() expression and pin/simplify it"],"tags":["swc","css","minifier","calc","invariant"],"backgroundTag":"calc-minification-unsupported","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}