Fix bogus warning about numeric overflow when value is zero
#KT-17149 Fixed
This commit is contained in:
+9
-9
@@ -556,7 +556,7 @@ private class ConstantExpressionEvaluatorVisitor(
|
|||||||
assert(isIntegerType(receiver.value)) { "Only integer constants should be checked for overflow" }
|
assert(isIntegerType(receiver.value)) { "Only integer constants should be checked for overflow" }
|
||||||
assert(name == "minus" || name == "unaryMinus") { "Only negation 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))
|
trace.report(Errors.INTEGER_OVERFLOW.on(callExpression.getStrictParentOfType<KtExpression>() ?: callExpression))
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
@@ -598,15 +598,15 @@ private class ConstantExpressionEvaluatorVisitor(
|
|||||||
binaryOperations[BinaryOperationKey(receiver.ctcType, parameter.ctcType, name)]
|
binaryOperations[BinaryOperationKey(receiver.ctcType, parameter.ctcType, name)]
|
||||||
|
|
||||||
private fun isDivisionByZero(name: String, parameter: Any?): Boolean {
|
private fun isDivisionByZero(name: String, parameter: Any?): Boolean {
|
||||||
if (name in DIVISION_OPERATION_NAMES) {
|
return name in DIVISION_OPERATION_NAMES && isZero(parameter)
|
||||||
if (isIntegerType(parameter)) {
|
}
|
||||||
return (parameter as Number).toLong() == 0.toLong()
|
|
||||||
}
|
private fun isZero(value: Any?): Boolean {
|
||||||
else if (parameter is Float || parameter is Double) {
|
return when {
|
||||||
return (parameter as Number).toDouble() == 0.0
|
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<*>? {
|
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);
|
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")
|
@TestMetadata("numberBinaryOperations.kt")
|
||||||
public void testNumberBinaryOperations() throws Exception {
|
public void testNumberBinaryOperations() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/evaluate/numberBinaryOperations.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/evaluate/numberBinaryOperations.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user