Fix bogus warning about numeric overflow when value is zero

#KT-17149 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2017-03-29 21:56:22 +03:00
parent 3820c7653b
commit fd6ed5aa72
4 changed files with 30 additions and 9 deletions
@@ -556,7 +556,7 @@ private class ConstantExpressionEvaluatorVisitor(
assert(isIntegerType(receiver.value)) { "Only integer constants should be checked for overflow" }
assert(name == "minus" || name == "unaryMinus") { "Only negation should be checked for overflow" }
if (receiver.value == result) {
if (receiver.value == result && !isZero(receiver.value)) {
trace.report(Errors.INTEGER_OVERFLOW.on(callExpression.getStrictParentOfType<KtExpression>() ?: callExpression))
}
return result
@@ -598,15 +598,15 @@ private class ConstantExpressionEvaluatorVisitor(
binaryOperations[BinaryOperationKey(receiver.ctcType, parameter.ctcType, name)]
private fun isDivisionByZero(name: String, parameter: Any?): Boolean {
if (name in DIVISION_OPERATION_NAMES) {
if (isIntegerType(parameter)) {
return (parameter as Number).toLong() == 0.toLong()
}
else if (parameter is Float || parameter is Double) {
return (parameter as Number).toDouble() == 0.0
}
return name in DIVISION_OPERATION_NAMES && isZero(parameter)
}
private fun isZero(value: Any?): Boolean {
return when {
isIntegerType(value) -> (value as Number).toLong() == 0L
value is Float || value is Double -> (value as Number).toDouble() == 0.0
else -> false
}
return false
}
override fun visitUnaryExpression(expression: KtUnaryExpression, expectedType: KotlinType?): CompileTimeConstant<*>? {
@@ -0,0 +1,11 @@
val zero = 0
fun test() {
-0
-0L
-0.0
-(1 - 1)
-zero
+0
}
@@ -0,0 +1,4 @@
package
public val zero: kotlin.Int = 0
public fun test(): kotlin.Unit
@@ -7561,6 +7561,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("noOverflowWithZero.kt")
public void testNoOverflowWithZero() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/evaluate/noOverflowWithZero.kt");
doTest(fileName);
}
@TestMetadata("numberBinaryOperations.kt")
public void testNumberBinaryOperations() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/evaluate/numberBinaryOperations.kt");