diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt index 2c94e8958dc..69d4274b6fa 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt @@ -702,9 +702,10 @@ private class ConstantExpressionEvaluatorVisitor( } } - private fun createOperationArgument(expression: KtExpression, expressionType: KotlinType, compileTimeType: CompileTimeType<*>): OperationArgument? { - val compileTimeConstant = constantExpressionEvaluator.evaluateExpression(expression, trace, expressionType) ?: return null - val evaluationResult = compileTimeConstant.getValue(expressionType) ?: return null + private fun createOperationArgument(expression: KtExpression, parameterType: KotlinType, compileTimeType: CompileTimeType<*>): OperationArgument? { + val compileTimeConstant = constantExpressionEvaluator.evaluateExpression(expression, trace, parameterType) ?: return null + if (compileTimeConstant is TypedCompileTimeConstant && !compileTimeConstant.type.isSubtypeOf(parameterType)) return null + val evaluationResult = compileTimeConstant.getValue(parameterType) ?: return null return OperationArgument(evaluationResult, compileTimeType, expression) } @@ -866,18 +867,20 @@ private fun getReceiverExpressionType(resolvedCall: ResolvedCall<*>): KotlinType } } -internal class CompileTimeType +internal class CompileTimeType(val name: String) { + override fun toString() = name +} -internal val BYTE = CompileTimeType() -internal val SHORT = CompileTimeType() -internal val INT = CompileTimeType() -internal val LONG = CompileTimeType() -internal val DOUBLE = CompileTimeType() -internal val FLOAT = CompileTimeType() -internal val CHAR = CompileTimeType() -internal val BOOLEAN = CompileTimeType() -internal val STRING = CompileTimeType() -internal val ANY = CompileTimeType() +internal val BYTE = CompileTimeType("Byte") +internal val SHORT = CompileTimeType("Short") +internal val INT = CompileTimeType("Int") +internal val LONG = CompileTimeType("Long") +internal val DOUBLE = CompileTimeType("Double") +internal val FLOAT = CompileTimeType("Float") +internal val CHAR = CompileTimeType("Char") +internal val BOOLEAN = CompileTimeType("Boolean") +internal val STRING = CompileTimeType("String") +internal val ANY = CompileTimeType("Any") @Suppress("UNCHECKED_CAST") internal fun binaryOperation( diff --git a/compiler/testData/diagnostics/tests/evaluate/logicWithNumber.kt b/compiler/testData/diagnostics/tests/evaluate/logicWithNumber.kt new file mode 100644 index 00000000000..0ca70e913b1 --- /dev/null +++ b/compiler/testData/diagnostics/tests/evaluate/logicWithNumber.kt @@ -0,0 +1,8 @@ +fun bar() { + false and false +} + +// See exception in KT-13421 +fun foo() { + 42 and false +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/evaluate/logicWithNumber.txt b/compiler/testData/diagnostics/tests/evaluate/logicWithNumber.txt new file mode 100644 index 00000000000..a34eb4a98fa --- /dev/null +++ b/compiler/testData/diagnostics/tests/evaluate/logicWithNumber.txt @@ -0,0 +1,4 @@ +package + +public fun bar(): kotlin.Unit +public fun foo(): kotlin.Unit diff --git a/compiler/testData/evaluate/constant/integerOperations.kt b/compiler/testData/evaluate/constant/integerOperations.kt new file mode 100644 index 00000000000..12451a6fc5e --- /dev/null +++ b/compiler/testData/evaluate/constant/integerOperations.kt @@ -0,0 +1,23 @@ +package test + +// val x1: 3 +val x1 = 1 + 2 + +// val x2: 3.toLong() +val x2 = 1 + 2L + +// val x3: 3 +val x3 = 1.toShort() + 2.toByte() + +// val x4: 3 +val x4 = 1.toByte() + 2.toByte() + +// val x5: 4656 +val x5 = 0x1234 and 0x5678 + +// Strange result, see KT-13517 +// val x6: null +val x6 = 0x1234 and 0x5678L + +// val x7: 4656.toLong() +val x7 = 0x1234L and 0x5678 \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 5da9bc89e14..3d59db00e50 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -6840,6 +6840,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("logicWithNumber.kt") + public void testLogicWithNumber() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/evaluate/logicWithNumber.kt"); + doTest(fileName); + } + @TestMetadata("longOverflow.kt") public void testLongOverflow() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/evaluate/longOverflow.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/resolve/constants/evaluate/CompileTimeConstantEvaluatorTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/resolve/constants/evaluate/CompileTimeConstantEvaluatorTestGenerated.java index 51f803ab530..e5810585979 100644 --- a/compiler/tests/org/jetbrains/kotlin/resolve/constants/evaluate/CompileTimeConstantEvaluatorTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/resolve/constants/evaluate/CompileTimeConstantEvaluatorTestGenerated.java @@ -97,6 +97,12 @@ public class CompileTimeConstantEvaluatorTestGenerated extends AbstractCompileTi doConstantTest(fileName); } + @TestMetadata("integerOperations.kt") + public void testIntegerOperations() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/evaluate/constant/integerOperations.kt"); + doConstantTest(fileName); + } + @TestMetadata("integers.kt") public void testIntegers() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/evaluate/constant/integers.kt");