From d5ff1047a5573a63e15855cf3ce65470dc0e3e19 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Tue, 14 Jan 2020 10:12:50 +0300 Subject: [PATCH] JVM: Fix IEEE 754 comparison generation for boxed numbers --- .../kotlin/codegen/ExpressionCodegen.java | 3 ++- .../binaryOp/compareWithBoxedNotNullDouble.kt | 19 +++++++++++++++++++ .../binaryOp/compareWithBoxedNotNullLong.kt | 17 +++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 10 ++++++++++ .../LightAnalysisModeTestGenerated.java | 10 ++++++++++ .../ir/FirBlackBoxCodegenTestGenerated.java | 10 ++++++++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 10 ++++++++++ 7 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/box/binaryOp/compareWithBoxedNotNullDouble.kt create mode 100644 compiler/testData/codegen/box/binaryOp/compareWithBoxedNotNullLong.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index 832004ef253..b4586ce0270 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -4044,7 +4044,8 @@ public class ExpressionCodegen extends KtVisitor impleme boolean properIeee754Comparisons = shouldUseProperIeee754Comparisons(); if (properIeee754Comparisons && left754Type != null && right754Type != null) { - type = comparisonOperandType(leftType, rightType); + type = comparisonOperandType(left754Type.type, right754Type.type); + //type = comparisonOperandType(leftType, rightType); leftValue = gen(left); rightValue = gen(right); } diff --git a/compiler/testData/codegen/box/binaryOp/compareWithBoxedNotNullDouble.kt b/compiler/testData/codegen/box/binaryOp/compareWithBoxedNotNullDouble.kt new file mode 100644 index 00000000000..d8521182e13 --- /dev/null +++ b/compiler/testData/codegen/box/binaryOp/compareWithBoxedNotNullDouble.kt @@ -0,0 +1,19 @@ +// IGNORE_BACKEND_FIR: JVM_IR +// TARGET_BACKEND: JVM +// reason - multifile tests are not supported in JS tests +//FILE: Holder.java +import org.jetbrains.annotations.*; + +class Holder { + public @NotNull Double value; + public Holder(Double value) { this.value = value; } +} + +//FILE: test.kt + +import Holder + +fun box(): String { + val j = Holder(0.99) + return if (j.value > 0) "OK" else "fail" +} diff --git a/compiler/testData/codegen/box/binaryOp/compareWithBoxedNotNullLong.kt b/compiler/testData/codegen/box/binaryOp/compareWithBoxedNotNullLong.kt new file mode 100644 index 00000000000..31d08f85a8d --- /dev/null +++ b/compiler/testData/codegen/box/binaryOp/compareWithBoxedNotNullLong.kt @@ -0,0 +1,17 @@ +// IGNORE_BACKEND_FIR: JVM_IR +// TARGET_BACKEND: JVM +// reason - multifile tests are not supported in JS tests +//FILE: JavaClass.java +import org.jetbrains.annotations.*; + +class JavaClass { + public static @NotNull Long get() { return 2364137526064485012L; } +} + +//FILE: test.kt + +import JavaClass + +fun box(): String { + return if (JavaClass.get() > 0) "OK" else "fail" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 688de975dbb..f32757a5852 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -1101,6 +1101,16 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/binaryOp/compareWithBoxedLong.kt"); } + @TestMetadata("compareWithBoxedNotNullDouble.kt") + public void testCompareWithBoxedNotNullDouble() throws Exception { + runTest("compiler/testData/codegen/box/binaryOp/compareWithBoxedNotNullDouble.kt"); + } + + @TestMetadata("compareWithBoxedNotNullLong.kt") + public void testCompareWithBoxedNotNullLong() throws Exception { + runTest("compiler/testData/codegen/box/binaryOp/compareWithBoxedNotNullLong.kt"); + } + @TestMetadata("divisionByZero.kt") public void testDivisionByZero() throws Exception { runTest("compiler/testData/codegen/box/binaryOp/divisionByZero.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index e73244c73a4..4e3ba51e808 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -1101,6 +1101,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/binaryOp/compareWithBoxedLong.kt"); } + @TestMetadata("compareWithBoxedNotNullDouble.kt") + public void testCompareWithBoxedNotNullDouble() throws Exception { + runTest("compiler/testData/codegen/box/binaryOp/compareWithBoxedNotNullDouble.kt"); + } + + @TestMetadata("compareWithBoxedNotNullLong.kt") + public void testCompareWithBoxedNotNullLong() throws Exception { + runTest("compiler/testData/codegen/box/binaryOp/compareWithBoxedNotNullLong.kt"); + } + @TestMetadata("divisionByZero.kt") public void testDivisionByZero() throws Exception { runTest("compiler/testData/codegen/box/binaryOp/divisionByZero.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index c94a189cddc..30b1e615443 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -1081,6 +1081,16 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/binaryOp/compareWithBoxedLong.kt"); } + @TestMetadata("compareWithBoxedNotNullDouble.kt") + public void testCompareWithBoxedNotNullDouble() throws Exception { + runTest("compiler/testData/codegen/box/binaryOp/compareWithBoxedNotNullDouble.kt"); + } + + @TestMetadata("compareWithBoxedNotNullLong.kt") + public void testCompareWithBoxedNotNullLong() throws Exception { + runTest("compiler/testData/codegen/box/binaryOp/compareWithBoxedNotNullLong.kt"); + } + @TestMetadata("divisionByZero.kt") public void testDivisionByZero() throws Exception { runTest("compiler/testData/codegen/box/binaryOp/divisionByZero.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index b6d30a8b6cc..135dcb57b38 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -1081,6 +1081,16 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/binaryOp/compareWithBoxedLong.kt"); } + @TestMetadata("compareWithBoxedNotNullDouble.kt") + public void testCompareWithBoxedNotNullDouble() throws Exception { + runTest("compiler/testData/codegen/box/binaryOp/compareWithBoxedNotNullDouble.kt"); + } + + @TestMetadata("compareWithBoxedNotNullLong.kt") + public void testCompareWithBoxedNotNullLong() throws Exception { + runTest("compiler/testData/codegen/box/binaryOp/compareWithBoxedNotNullLong.kt"); + } + @TestMetadata("divisionByZero.kt") public void testDivisionByZero() throws Exception { runTest("compiler/testData/codegen/box/binaryOp/divisionByZero.kt");