From 3849b5e72365465cc96c2a0fa6febfa33809c645 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Tue, 19 Nov 2019 10:14:53 +0300 Subject: [PATCH] FIC: add codegen tests, adapt previously tests for SAMs --- .../codegen/box/funInterface/castFromAny.kt | 18 +++++ .../box/funInterface/inlinedSamWrapper.kt | 28 ++++++++ .../codegen/box/funInterface/nullableSam.kt | 24 +++++++ .../codegen/box/funInterface/partialSam.kt | 41 +++++++++++ .../box/funInterface/receiverEvaluatedOnce.kt | 24 +++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 71 +++++++++++++------ .../LightAnalysisModeTestGenerated.java | 71 +++++++++++++------ .../ir/FirBlackBoxCodegenTestGenerated.java | 48 +++++++++++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 71 +++++++++++++------ 9 files changed, 327 insertions(+), 69 deletions(-) create mode 100644 compiler/testData/codegen/box/funInterface/castFromAny.kt create mode 100644 compiler/testData/codegen/box/funInterface/inlinedSamWrapper.kt create mode 100644 compiler/testData/codegen/box/funInterface/nullableSam.kt create mode 100644 compiler/testData/codegen/box/funInterface/partialSam.kt create mode 100644 compiler/testData/codegen/box/funInterface/receiverEvaluatedOnce.kt diff --git a/compiler/testData/codegen/box/funInterface/castFromAny.kt b/compiler/testData/codegen/box/funInterface/castFromAny.kt new file mode 100644 index 00000000000..fa48ed88b42 --- /dev/null +++ b/compiler/testData/codegen/box/funInterface/castFromAny.kt @@ -0,0 +1,18 @@ +// !LANGUAGE: +NewInference +FunctionInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions + +fun interface KRunnable { + fun invoke() +} + +fun test(a: Any?) { + a as () -> Unit + KRunnable(a).invoke() +} + +fun box(): String { + var result = "Fail" + test { + result = "OK" + } + return result +} diff --git a/compiler/testData/codegen/box/funInterface/inlinedSamWrapper.kt b/compiler/testData/codegen/box/funInterface/inlinedSamWrapper.kt new file mode 100644 index 00000000000..afae7d8c119 --- /dev/null +++ b/compiler/testData/codegen/box/funInterface/inlinedSamWrapper.kt @@ -0,0 +1,28 @@ +// !LANGUAGE: +NewInference +FunctionInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions +// WITH_RUNTIME + +fun interface MyRunnable { + fun invoke() +} + +class A { + inline fun doWork(noinline job: () -> Unit) { + MyRunnable(job).invoke() + } + + fun doNoninlineWork(job: () -> Unit) { + MyRunnable(job).invoke() + } +} + +fun box(): String { + var result = false + A().doWork { result = true } + if (!result) return "Fail 1" + + result = false + A().doNoninlineWork { result = true } + if (!result) return "Fail 2" + + return "OK" +} diff --git a/compiler/testData/codegen/box/funInterface/nullableSam.kt b/compiler/testData/codegen/box/funInterface/nullableSam.kt new file mode 100644 index 00000000000..852e9381c28 --- /dev/null +++ b/compiler/testData/codegen/box/funInterface/nullableSam.kt @@ -0,0 +1,24 @@ +// !LANGUAGE: +NewInference +FunctionInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions +// TARGET_BACKEND: JVM +// WITH_RUNTIME + +fun interface KRunnable { + fun invoke() +} + +fun isNull(r: KRunnable?): Boolean { + if (r == null) return true + r.invoke() + return false +} + +fun nullableFun(fromNull: Boolean): (() -> Unit)? = + if (fromNull) null else {{}} + +fun box(): String { + if (!isNull(nullableFun(true))) return "Fail 1" + if (isNull(nullableFun(false))) return "Fail 2" + if (!isNull(null)) return "Fail 3" + if (isNull {}) return "Fail 4" + return "OK" +} diff --git a/compiler/testData/codegen/box/funInterface/partialSam.kt b/compiler/testData/codegen/box/funInterface/partialSam.kt new file mode 100644 index 00000000000..6130c1b80be --- /dev/null +++ b/compiler/testData/codegen/box/funInterface/partialSam.kt @@ -0,0 +1,41 @@ +// !LANGUAGE: +NewInference +FunctionInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions +// WITH_RUNTIME + +fun interface Fn { + fun run(s: String, i: Int, t: T): R +} + +class J { + fun runConversion(f1: Fn, f2: Fn): Int { + return f1.run("Bar", 1, f2.run("Foo", 42, 239)) + } +} + +fun box(): String { + val j = J() + var x = "" + + val fsi = object : Fn { + override fun run(s: String, i: Int, t: String): Int { + x += "$s$i$t " + return i + } + } + + val fis = object : Fn { + override fun run(s: String, i: Int, t: Int): String { + x += "$s$i$t " + return s + } + } + + val r1 = j.runConversion(fsi) { s, i, ti -> x += "L$s$i$ti "; "L$s"} + val r2 = j.runConversion({ s, i, ts -> x += "L$s$i$ts"; i }, fis) + + if (r1 != 1) return "fail r1: $r1" + if (r2 != 1) return "fail r2: $r2" + + if (x != "LFoo42239 Bar1LFoo Foo42239 LBar1Foo") return x + + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/funInterface/receiverEvaluatedOnce.kt b/compiler/testData/codegen/box/funInterface/receiverEvaluatedOnce.kt new file mode 100644 index 00000000000..9d156c2c881 --- /dev/null +++ b/compiler/testData/codegen/box/funInterface/receiverEvaluatedOnce.kt @@ -0,0 +1,24 @@ +// !LANGUAGE: +NewInference +FunctionInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions + +// TARGET_BACKEND: JVM +// WITH_RUNTIME + +fun interface KRunnable { + fun invoke() +} + +fun runTwice(r: KRunnable) { + r.invoke() + r.invoke() +} + +class A() { + fun f() {} +} + +fun box(): String { + var x = 0 + runTwice({ x++; A() }()::f) + if (x != 1) return "Fail" + return "OK" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index f9a96bb6864..ab1bccb9aff 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -11610,6 +11610,54 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { } } + @TestMetadata("compiler/testData/codegen/box/funInterface") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class FunInterface extends AbstractBlackBoxCodegenTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath); + } + + public void testAllFilesPresentInFunInterface() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/funInterface"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); + } + + @TestMetadata("basicFunInterface.kt") + public void testBasicFunInterface() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/basicFunInterface.kt"); + } + + @TestMetadata("basicFunInterfaceConversion.kt") + public void testBasicFunInterfaceConversion() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/basicFunInterfaceConversion.kt"); + } + + @TestMetadata("castFromAny.kt") + public void testCastFromAny() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/castFromAny.kt"); + } + + @TestMetadata("inlinedSamWrapper.kt") + public void testInlinedSamWrapper() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/inlinedSamWrapper.kt"); + } + + @TestMetadata("nullableSam.kt") + public void testNullableSam() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/nullableSam.kt"); + } + + @TestMetadata("partialSam.kt") + public void testPartialSam() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/partialSam.kt"); + } + + @TestMetadata("receiverEvaluatedOnce.kt") + public void testReceiverEvaluatedOnce() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/receiverEvaluatedOnce.kt"); + } + } + @TestMetadata("compiler/testData/codegen/box/functions") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -13668,29 +13716,6 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { } } - @TestMetadata("compiler/testData/codegen/box/inlineClasses/funInterface") - @TestDataPath("$PROJECT_ROOT") - @RunWith(JUnit3RunnerWithInners.class) - public static class FunInterface extends AbstractBlackBoxCodegenTest { - private void runTest(String testDataFilePath) throws Exception { - KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath); - } - - public void testAllFilesPresentInFunInterface() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/inlineClasses/funInterface"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); - } - - @TestMetadata("basicFunInterface.kt") - public void testBasicFunInterface() throws Exception { - runTest("compiler/testData/codegen/box/inlineClasses/funInterface/basicFunInterface.kt"); - } - - @TestMetadata("basicFunInterfaceConversion.kt") - public void testBasicFunInterfaceConversion() throws Exception { - runTest("compiler/testData/codegen/box/inlineClasses/funInterface/basicFunInterfaceConversion.kt"); - } - } - @TestMetadata("compiler/testData/codegen/box/inlineClasses/functionNameMangling") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index cf17555cdf0..f33f9024ef6 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -11610,6 +11610,54 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes } } + @TestMetadata("compiler/testData/codegen/box/funInterface") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class FunInterface extends AbstractLightAnalysisModeTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath); + } + + public void testAllFilesPresentInFunInterface() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/funInterface"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); + } + + @TestMetadata("basicFunInterface.kt") + public void testBasicFunInterface() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/basicFunInterface.kt"); + } + + @TestMetadata("basicFunInterfaceConversion.kt") + public void testBasicFunInterfaceConversion() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/basicFunInterfaceConversion.kt"); + } + + @TestMetadata("castFromAny.kt") + public void testCastFromAny() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/castFromAny.kt"); + } + + @TestMetadata("inlinedSamWrapper.kt") + public void testInlinedSamWrapper() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/inlinedSamWrapper.kt"); + } + + @TestMetadata("nullableSam.kt") + public void testNullableSam() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/nullableSam.kt"); + } + + @TestMetadata("partialSam.kt") + public void testPartialSam() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/partialSam.kt"); + } + + @TestMetadata("receiverEvaluatedOnce.kt") + public void testReceiverEvaluatedOnce() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/receiverEvaluatedOnce.kt"); + } + } + @TestMetadata("compiler/testData/codegen/box/functions") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -13668,29 +13716,6 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes } } - @TestMetadata("compiler/testData/codegen/box/inlineClasses/funInterface") - @TestDataPath("$PROJECT_ROOT") - @RunWith(JUnit3RunnerWithInners.class) - public static class FunInterface extends AbstractLightAnalysisModeTest { - private void runTest(String testDataFilePath) throws Exception { - KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath); - } - - public void testAllFilesPresentInFunInterface() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/inlineClasses/funInterface"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); - } - - @TestMetadata("basicFunInterface.kt") - public void testBasicFunInterface() throws Exception { - runTest("compiler/testData/codegen/box/inlineClasses/funInterface/basicFunInterface.kt"); - } - - @TestMetadata("basicFunInterfaceConversion.kt") - public void testBasicFunInterfaceConversion() throws Exception { - runTest("compiler/testData/codegen/box/inlineClasses/funInterface/basicFunInterfaceConversion.kt"); - } - } - @TestMetadata("compiler/testData/codegen/box/inlineClasses/functionNameMangling") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index bc50570d5eb..1ed6d2d2652 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -10460,6 +10460,54 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT } } + @TestMetadata("compiler/testData/codegen/box/funInterface") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class FunInterface extends AbstractFirBlackBoxCodegenTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTestWithCustomIgnoreDirective(this::doTest, TargetBackend.JVM_IR, testDataFilePath, "// IGNORE_BACKEND_FIR: "); + } + + public void testAllFilesPresentInFunInterface() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/funInterface"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true); + } + + @TestMetadata("basicFunInterface.kt") + public void testBasicFunInterface() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/basicFunInterface.kt"); + } + + @TestMetadata("basicFunInterfaceConversion.kt") + public void testBasicFunInterfaceConversion() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/basicFunInterfaceConversion.kt"); + } + + @TestMetadata("castFromAny.kt") + public void testCastFromAny() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/castFromAny.kt"); + } + + @TestMetadata("inlinedSamWrapper.kt") + public void testInlinedSamWrapper() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/inlinedSamWrapper.kt"); + } + + @TestMetadata("nullableSam.kt") + public void testNullableSam() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/nullableSam.kt"); + } + + @TestMetadata("partialSam.kt") + public void testPartialSam() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/partialSam.kt"); + } + + @TestMetadata("receiverEvaluatedOnce.kt") + public void testReceiverEvaluatedOnce() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/receiverEvaluatedOnce.kt"); + } + } + @TestMetadata("compiler/testData/codegen/box/functions") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 145b5e82c23..986bc2e8868 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -10460,6 +10460,54 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes } } + @TestMetadata("compiler/testData/codegen/box/funInterface") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class FunInterface extends AbstractIrBlackBoxCodegenTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM_IR, testDataFilePath); + } + + public void testAllFilesPresentInFunInterface() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/funInterface"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true); + } + + @TestMetadata("basicFunInterface.kt") + public void testBasicFunInterface() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/basicFunInterface.kt"); + } + + @TestMetadata("basicFunInterfaceConversion.kt") + public void testBasicFunInterfaceConversion() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/basicFunInterfaceConversion.kt"); + } + + @TestMetadata("castFromAny.kt") + public void testCastFromAny() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/castFromAny.kt"); + } + + @TestMetadata("inlinedSamWrapper.kt") + public void testInlinedSamWrapper() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/inlinedSamWrapper.kt"); + } + + @TestMetadata("nullableSam.kt") + public void testNullableSam() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/nullableSam.kt"); + } + + @TestMetadata("partialSam.kt") + public void testPartialSam() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/partialSam.kt"); + } + + @TestMetadata("receiverEvaluatedOnce.kt") + public void testReceiverEvaluatedOnce() throws Exception { + runTest("compiler/testData/codegen/box/funInterface/receiverEvaluatedOnce.kt"); + } + } + @TestMetadata("compiler/testData/codegen/box/functions") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -12518,29 +12566,6 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes } } - @TestMetadata("compiler/testData/codegen/box/inlineClasses/funInterface") - @TestDataPath("$PROJECT_ROOT") - @RunWith(JUnit3RunnerWithInners.class) - public static class FunInterface extends AbstractIrBlackBoxCodegenTest { - private void runTest(String testDataFilePath) throws Exception { - KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM_IR, testDataFilePath); - } - - public void testAllFilesPresentInFunInterface() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/inlineClasses/funInterface"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true); - } - - @TestMetadata("basicFunInterface.kt") - public void testBasicFunInterface() throws Exception { - runTest("compiler/testData/codegen/box/inlineClasses/funInterface/basicFunInterface.kt"); - } - - @TestMetadata("basicFunInterfaceConversion.kt") - public void testBasicFunInterfaceConversion() throws Exception { - runTest("compiler/testData/codegen/box/inlineClasses/funInterface/basicFunInterfaceConversion.kt"); - } - } - @TestMetadata("compiler/testData/codegen/box/inlineClasses/functionNameMangling") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)