Fix bogus integer overflow warning for 'mod' operator

#KT-15875 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2017-01-24 17:13:42 +03:00
parent f9e552e3c2
commit 68b223211c
5 changed files with 31 additions and 3 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -510,7 +510,7 @@ private class ConstantExpressionEvaluatorVisitor(
}
private fun evaluateBinaryAndCheck(receiver: OperationArgument, parameter: OperationArgument, name: String, callExpression: KtExpression): Any? {
val functions = binaryOperations[BinaryOperationKey(receiver.ctcType, parameter.ctcType, name)] ?: return null
val functions = getBinaryOperation(receiver, parameter, name) ?: return null
val (function, checker) = functions
val actualResult = try {
@@ -526,7 +526,14 @@ private class ConstantExpressionEvaluatorVisitor(
fun toBigInteger(value: Any?) = BigInteger.valueOf((value as Number).toLong())
val resultInBigIntegers = checker(toBigInteger(receiver.value), toBigInteger(parameter.value))
val refinedChecker = if (name == OperatorNameConventions.MOD.asString()) {
getBinaryOperation(receiver, parameter, OperatorNameConventions.REM.asString())?.second ?: return null
}
else {
checker
}
val resultInBigIntegers = refinedChecker(toBigInteger(receiver.value), toBigInteger(parameter.value))
if (toBigInteger(actualResult) != resultInBigIntegers) {
trace.report(Errors.INTEGER_OVERFLOW.on(callExpression.getStrictParentOfType<KtExpression>() ?: callExpression))
@@ -534,6 +541,9 @@ private class ConstantExpressionEvaluatorVisitor(
return actualResult
}
private fun getBinaryOperation(receiver: OperationArgument, parameter: OperationArgument, name: String) =
binaryOperations[BinaryOperationKey(receiver.ctcType, parameter.ctcType, name)]
private fun isDivisionByZero(name: String, parameter: Any?): Boolean {
if (name == OperatorConventions.BINARY_OPERATION_NAMES[KtTokens.DIV]!!.asString()) {
if (isIntegerType(parameter)) {