From f34224eceec036c580a74b868286a43a74781afb Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Wed, 27 Jan 2021 16:33:16 +0300 Subject: [PATCH] JVM_IR indy-SAM conversions: tests for function references equality (SAM conversion using LambdaMetafactory is not used for function refs). KT-44278 KT-26060 KT-42621 --- .../FirBlackBoxCodegenTestGenerated.java | 12 ++++ .../sam/boundFunctionReferenceEquality.kt | 57 +++++++++++++++++++ .../sam/unboundFunctionReferenceEquality.kt | 48 ++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 12 ++++ .../IrBlackBoxCodegenTestGenerated.java | 12 ++++ .../LightAnalysisModeTestGenerated.java | 10 ++++ 6 files changed, 151 insertions(+) create mode 100644 compiler/testData/codegen/box/invokedynamic/sam/boundFunctionReferenceEquality.kt create mode 100644 compiler/testData/codegen/box/invokedynamic/sam/unboundFunctionReferenceEquality.kt diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 713269121e5..2a336c39aa8 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -18397,6 +18397,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/invokedynamic/sam"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); } + @Test + @TestMetadata("boundFunctionReferenceEquality.kt") + public void testBoundFunctionReferenceEquality() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/boundFunctionReferenceEquality.kt"); + } + @Test @TestMetadata("boundReference.kt") public void testBoundReference() throws Exception { @@ -18505,6 +18511,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/invokedynamic/sam/suspendFunInterface.kt"); } + @Test + @TestMetadata("unboundFunctionReferenceEquality.kt") + public void testUnboundFunctionReferenceEquality() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/unboundFunctionReferenceEquality.kt"); + } + @Nested @TestMetadata("compiler/testData/codegen/box/invokedynamic/sam/inline") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/testData/codegen/box/invokedynamic/sam/boundFunctionReferenceEquality.kt b/compiler/testData/codegen/box/invokedynamic/sam/boundFunctionReferenceEquality.kt new file mode 100644 index 00000000000..6cec5948027 --- /dev/null +++ b/compiler/testData/codegen/box/invokedynamic/sam/boundFunctionReferenceEquality.kt @@ -0,0 +1,57 @@ +// TARGET_BACKEND: JVM +// JVM_TARGET: 1.8 +// SAM_CONVERSIONS: INDY +// WITH_RUNTIME +// FILE: boundAdaptedFunctionReference.kt +fun checkEqual(x: Any, y: Any) { + if (x != y || y != x) throw AssertionError("$x and $y should be equal") + if (x.hashCode() != y.hashCode()) throw AssertionError("$x and $y should have the same hash code") +} + +fun checkNotEqual(x: Any, y: Any) { + if (x == y || y == x) throw AssertionError("$x and $y should NOT be equal") +} + +fun interface FunInterface { + fun invoke() +} + +private fun id(f: FunInterface): Any = f + +class C { + fun target1() {} + fun target2() {} + + fun adapted1(s: String? = null): String = s!! + fun adapted2(vararg s: String): String = s[0] +} + +fun box(): String { + val c0 = C() + + checkEqual(id(c0::target1), id(c0::target1)) + checkEqual(id(c0::target1), target1FromOtherFile(c0)) + + checkNotEqual(id(c0::target1), id(c0::target2)) + + checkEqual(id(c0::adapted1), id(c0::adapted1)) + checkEqual(id(c0::adapted1), adapted1FromOtherFile(c0)) + checkEqual(id(c0::adapted2), id(c0::adapted2)) + checkEqual(id(c0::adapted2), adapted2FromOtherFile(c0)) + checkNotEqual(id(c0::adapted1), id(c0::adapted2)) + + val c1 = C() + checkNotEqual(id(c0::target1), id(c1::target1)) + checkNotEqual(id(c0::target1), id(c1::target2)) + checkNotEqual(id(c0::adapted1), id(c1::adapted1)) + + return "OK" +} + +// FILE: fromOtherFile.kt + +private fun id(f: FunInterface): Any = f + +fun target1FromOtherFile(c0: C): Any = id(c0::target1) +fun adapted1FromOtherFile(c0: C): Any = id(c0::adapted1) +fun adapted2FromOtherFile(c0: C): Any = id(c0::adapted2) diff --git a/compiler/testData/codegen/box/invokedynamic/sam/unboundFunctionReferenceEquality.kt b/compiler/testData/codegen/box/invokedynamic/sam/unboundFunctionReferenceEquality.kt new file mode 100644 index 00000000000..2058123e499 --- /dev/null +++ b/compiler/testData/codegen/box/invokedynamic/sam/unboundFunctionReferenceEquality.kt @@ -0,0 +1,48 @@ +// TARGET_BACKEND: JVM +// JVM_TARGET: 1.8 +// SAM_CONVERSIONS: INDY +// WITH_RUNTIME +// FILE: unboundAdaptedFunctionReference.kt +fun checkEqual(x: Any, y: Any) { + if (x != y || y != x) throw AssertionError("$x and $y should be equal") + if (x.hashCode() != y.hashCode()) throw AssertionError("$x and $y should have the same hash code") +} + +fun checkNotEqual(x: Any, y: Any) { + if (x == y || y == x) throw AssertionError("$x and $y should NOT be equal") +} + +fun interface FunInterface { + fun invoke() +} + +private fun id(f: FunInterface): Any = f + +fun target1() {} +fun target2() {} + +fun adapted1(s: String? = null): String = s!! +fun adapted2(vararg s: String): String = s[0] + +fun box(): String { + checkEqual(id(::target1), id(::target1)) + checkEqual(id(::target1), target1FromOtherFile()) + + checkNotEqual(id(::target1), id(::target2)) + + checkEqual(id(::adapted1), id(::adapted1)) + checkEqual(id(::adapted1), adapted1FromOtherFile()) + checkEqual(id(::adapted2), id(::adapted2)) + checkEqual(id(::adapted2), adapted2FromOtherFile()) + checkNotEqual(id(::adapted1), id(::adapted2)) + + return "OK" +} + +// FILE: fromOtherFile.kt + +private fun id(f: FunInterface): Any = f + +fun target1FromOtherFile(): Any = id(::target1) +fun adapted1FromOtherFile(): Any = id(::adapted1) +fun adapted2FromOtherFile(): Any = id(::adapted2) diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index 2bd48b10080..46135a7b9eb 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -18397,6 +18397,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/invokedynamic/sam"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true); } + @Test + @TestMetadata("boundFunctionReferenceEquality.kt") + public void testBoundFunctionReferenceEquality() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/boundFunctionReferenceEquality.kt"); + } + @Test @TestMetadata("boundReference.kt") public void testBoundReference() throws Exception { @@ -18505,6 +18511,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/invokedynamic/sam/suspendFunInterface.kt"); } + @Test + @TestMetadata("unboundFunctionReferenceEquality.kt") + public void testUnboundFunctionReferenceEquality() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/unboundFunctionReferenceEquality.kt"); + } + @Nested @TestMetadata("compiler/testData/codegen/box/invokedynamic/sam/inline") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index a31d49d27ae..796a43c4270 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -18397,6 +18397,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/invokedynamic/sam"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); } + @Test + @TestMetadata("boundFunctionReferenceEquality.kt") + public void testBoundFunctionReferenceEquality() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/boundFunctionReferenceEquality.kt"); + } + @Test @TestMetadata("boundReference.kt") public void testBoundReference() throws Exception { @@ -18505,6 +18511,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/invokedynamic/sam/suspendFunInterface.kt"); } + @Test + @TestMetadata("unboundFunctionReferenceEquality.kt") + public void testUnboundFunctionReferenceEquality() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/unboundFunctionReferenceEquality.kt"); + } + @Nested @TestMetadata("compiler/testData/codegen/box/invokedynamic/sam/inline") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 1253e91d528..6b69b50e143 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -16133,6 +16133,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/invokedynamic/sam"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true); } + @TestMetadata("boundFunctionReferenceEquality.kt") + public void testBoundFunctionReferenceEquality() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/boundFunctionReferenceEquality.kt"); + } + @TestMetadata("boundReference.kt") public void testBoundReference() throws Exception { runTest("compiler/testData/codegen/box/invokedynamic/sam/boundReference.kt"); @@ -16223,6 +16228,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/invokedynamic/sam/suspendFunInterface.kt"); } + @TestMetadata("unboundFunctionReferenceEquality.kt") + public void testUnboundFunctionReferenceEquality() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/unboundFunctionReferenceEquality.kt"); + } + @TestMetadata("compiler/testData/codegen/box/invokedynamic/sam/inline") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)