diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/evaluate/ConstantExpressionEvaluator.kt b/compiler/frontend/src/org/jetbrains/jet/lang/evaluate/ConstantExpressionEvaluator.kt index ca8709f2c2b..bd5829abd2a 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/evaluate/ConstantExpressionEvaluator.kt +++ b/compiler/frontend/src/org/jetbrains/jet/lang/evaluate/ConstantExpressionEvaluator.kt @@ -190,7 +190,12 @@ public class ConstantExpressionEvaluator private (val trace: BindingTrace) : Jet val argumentForParameter = createOperationArgumentForFirstParameter(argument, parameter) if (argumentForParameter == null) return null + if (isDivisionByZero(resultingDescriptorName.asString(), argumentForParameter.value)) { + return ErrorValue.create("Division by zero") + } + val result = evaluateBinaryAndCheck(argumentForReceiver, argumentForParameter, resultingDescriptorName.asString(), callExpression) + return when(resultingDescriptorName) { OperatorConventions.COMPARE_TO -> createCompileTimeConstantForCompareTo(result, callExpression) OperatorConventions.EQUALS -> createCompileTimeConstantForEquals(result, callExpression) @@ -227,10 +232,6 @@ public class ConstantExpressionEvaluator private (val trace: BindingTrace) : Jet val functions = binaryOperations[BinaryOperationKey(receiver.ctcType, parameter.ctcType, name)] if (functions == null) return null - if (isDividingByZero(name, parameter.value)) { - return null - } - val (function, checker) = functions val actualResult = function(receiver.value, parameter.value) if (checker == emptyBinaryFun) { @@ -248,7 +249,7 @@ public class ConstantExpressionEvaluator private (val trace: BindingTrace) : Jet return actualResult } - private fun isDividingByZero(name: String, parameter: Any?): Boolean { + private fun isDivisionByZero(name: String, parameter: Any?): Boolean { if (name == OperatorConventions.BINARY_OPERATION_NAMES[JetTokens.DIV]!!.asString()) { if (isIntegerType(parameter)) { return (parameter as Number).toLong() == 0.toLong() diff --git a/compiler/testData/evaluate/constant/divideByZero.kt b/compiler/testData/evaluate/constant/divideByZero.kt index 13808d5c4a3..adc2ee5e019 100644 --- a/compiler/testData/evaluate/constant/divideByZero.kt +++ b/compiler/testData/evaluate/constant/divideByZero.kt @@ -1,16 +1,34 @@ package test -// val prop1: null +// val prop1: Division by zero val prop1 = 1 / 0 -// val prop2: null +// val prop2: Division by zero val prop2 = 1 / 0.0 -// val prop3: null +// val prop3: Division by zero val prop3 = 1.0 / 0 // val prop4: 10.0.toDouble() val prop4 = 1 / 0.1 -// val prop5: null +// val prop5: Division by zero val prop5 = 1 / 0.toLong() + +// val prop6: Division by zero +val prop6 = 1.0 / 0.toInt() + +// val prop7: Division by zero +val prop7 = 1.0 / 0.toLong() + +// val prop8: Division by zero +val prop8 = 1.0 / 0.toByte() + +// val prop9: Division by zero +val prop9 = 1.0 / 0.toShort() + +// val prop10: Division by zero +val prop10 = 1.0 / 0.toFloat() + +// val prop11: Division by zero +val prop11 = 1.0 / 0.toDouble() \ No newline at end of file