diff --git a/codeOptimizers/src/prog8/optimizer/ConstantFoldingOptimizer.kt b/codeOptimizers/src/prog8/optimizer/ConstantFoldingOptimizer.kt index 055d5dc8c..4e78bdf40 100644 --- a/codeOptimizers/src/prog8/optimizer/ConstantFoldingOptimizer.kt +++ b/codeOptimizers/src/prog8/optimizer/ConstantFoldingOptimizer.kt @@ -233,10 +233,14 @@ class ConstantFoldingOptimizer(private val program: Program, private val errors: } else if(leftBinExpr.operator=="*" && rightBinExpr.operator=="*"){ if (c1 != null && c2 != null && c1==c2) { - //(X * C) (Y * C) => (X Y) * C - val xwithy = BinaryExpression(leftBinExpr.left, expr.operator, rightBinExpr.left, expr.position) - val newExpr = BinaryExpression(xwithy, "*", c1, expr.position) - modifications += IAstModification.ReplaceNode(expr, newExpr, parent) + //(X * C) (Y * C) => (X Y) * C (only if types of X and Y are the same!) + val xDt = leftBinExpr.left.inferType(program) + val yDt = rightBinExpr.left.inferType(program) + if(xDt==yDt) { + val xwithy = BinaryExpression(leftBinExpr.left, expr.operator, rightBinExpr.left, expr.position) + val newExpr = BinaryExpression(xwithy, "*", c1, expr.position) + modifications += IAstModification.ReplaceNode(expr, newExpr, parent) + } } } }