From 64aa50a9212c7340062a955915ff1f35be570289 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Sch=C3=A4fer?= Date: Mon, 16 Sep 2019 13:43:23 +0200 Subject: [PATCH] Add more tests for SAM wrappers --- .../codegen/box/sam/inlinedSamWrapper.kt | 76 +++++++++++++++++++ .../testData/codegen/box/sam/nullableSam.kt | 26 +++++++ .../codegen/box/sam/predicateSamWrapper.kt | 38 ++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 15 ++++ .../LightAnalysisModeTestGenerated.java | 15 ++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 15 ++++ 6 files changed, 185 insertions(+) create mode 100644 compiler/testData/codegen/box/sam/inlinedSamWrapper.kt create mode 100644 compiler/testData/codegen/box/sam/nullableSam.kt create mode 100644 compiler/testData/codegen/box/sam/predicateSamWrapper.kt diff --git a/compiler/testData/codegen/box/sam/inlinedSamWrapper.kt b/compiler/testData/codegen/box/sam/inlinedSamWrapper.kt new file mode 100644 index 00000000000..6b88883410a --- /dev/null +++ b/compiler/testData/codegen/box/sam/inlinedSamWrapper.kt @@ -0,0 +1,76 @@ +// TARGET_BACKEND: JVM +// WITH_RUNTIME +// FILE: MyRunnable.java +public interface MyRunnable { + public void run(); +} + +// FILE: Foo.kt +package foo +import MyRunnable + +class A { + inline fun doWork(noinline job: () -> Unit) { + Runnable(job).run() + } + + fun doNoninlineWork(job: () -> Unit) { + Runnable(job).run() + } +} + +class B { + inline fun doWork(noinline job: () -> Unit) { + Runnable(job).run() + } + + fun doNonInlineWork(job: () -> Unit) { + MyRunnable(job).run() + } +} + +// FILE: test.kt +import foo.A +import foo.B + +fun classForName(name: String): Class<*>? = + try { + java.lang.Class.forName(name) + } catch (e: Throwable) { + null + } + +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" + + result = false + B().doWork { result = true } + if (!result) return "Fail 3" + + result = false + B().doNonInlineWork { result = true } + if (!result) return "Fail 4" + + val inlineWrapperA = classForName("foo.A\$sam\$i\$java_lang_Runnable$0") + if (inlineWrapperA == null) return "Fail 5: Can't find sam wrapper" + if (inlineWrapperA.modifiers and 1 == 0) return "Fail 6: inline sam wrapper is non-public" + + val wrapperA = classForName("foo.A\$sam\$java_lang_Runnable$0") + if (wrapperA == null) return "Fail 7: Can't find sam wrapper" + if (wrapperA.modifiers and 1 != 0) return "Fail 8: non-inline sam wrapper is public" + + val inlineWrapperB = classForName("foo.B\$sam\$i\$java_lang_Runnable$0") + if (inlineWrapperB != null) return "Fail 9: sam wrapper not cached" + + val wrapperB = classForName("foo.B\$sam\$MyRunnable$0") + if (wrapperB == null) return "Fail 10: Can't find sam wrapper" + if (wrapperB.modifiers and 1 != 0) return "Fail 11: non-inline sam wrapper is public" + + return "OK" +} diff --git a/compiler/testData/codegen/box/sam/nullableSam.kt b/compiler/testData/codegen/box/sam/nullableSam.kt new file mode 100644 index 00000000000..17c5143052e --- /dev/null +++ b/compiler/testData/codegen/box/sam/nullableSam.kt @@ -0,0 +1,26 @@ +// TARGET_BACKEND: JVM +// WITH_RUNTIME + +// FILE: Test.java + +public class Test { + public static boolean isNull(Runnable r) { + if (r == null) + return true; + r.run(); + return false; + } +} + +// FILE: test.kt + +fun nullableFun(fromNull: Boolean): (() -> Unit)? = + if (fromNull) null else {{}} + +fun box(): String { + if (!Test.isNull(nullableFun(true))) return "Fail 1" + if (Test.isNull(nullableFun(false))) return "Fail 2" + if (!Test.isNull(null)) return "Fail 3" + if (Test.isNull {}) return "Fail 4" + return "OK" +} diff --git a/compiler/testData/codegen/box/sam/predicateSamWrapper.kt b/compiler/testData/codegen/box/sam/predicateSamWrapper.kt new file mode 100644 index 00000000000..7a96c279156 --- /dev/null +++ b/compiler/testData/codegen/box/sam/predicateSamWrapper.kt @@ -0,0 +1,38 @@ +// TARGET_BACKEND: JVM +// WITH_RUNTIME +// FULL_JDK +// FILE: test.kt +// Test that SAM wrappers with type parameters are cached properly. +class A { + fun stringPredicate(string: String, p: (String) -> Boolean): Boolean { + return java.util.function.Predicate(p).test(string) + } + + fun intPredicate(int: Int, p: (Int) -> Boolean): Boolean { + return java.util.function.Predicate(p).test(int) + } +} + +fun wrapStringPredicate(p: (String) -> Boolean): java.util.function.Predicate = + java.util.function.Predicate(p) + +fun wrapIntPredicate(p: (Int) -> Boolean): java.util.function.Predicate = + java.util.function.Predicate(p) + +fun box(): String { + if (!A().stringPredicate("OK") { it == "OK"}) return "Fail 1" + if (!A().intPredicate(0) { it == 0 }) return "Fail 2" + + try { + java.lang.Class.forName("TestKt\$sam\$java_util_function_Predicate$0") + } catch (e: Throwable) { + return "Fail 3: sam wrapper not found" + } + + val stringPredicateWrapperClass = wrapStringPredicate { true }::class.java + val intPredicateWrapperClass = wrapIntPredicate { false }::class.java + if (stringPredicateWrapperClass !== intPredicateWrapperClass) + return "Fail 4: sam wrapper not cached" + + return "OK" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index b31abaafc49..d5ec76559ef 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -23690,6 +23690,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/sam/castFromAny.kt"); } + @TestMetadata("inlinedSamWrapper.kt") + public void testInlinedSamWrapper() throws Exception { + runTest("compiler/testData/codegen/box/sam/inlinedSamWrapper.kt"); + } + @TestMetadata("kt17091.kt") public void testKt17091() throws Exception { runTest("compiler/testData/codegen/box/sam/kt17091.kt"); @@ -23725,6 +23730,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/sam/kt24825.kt"); } + @TestMetadata("nullableSam.kt") + public void testNullableSam() throws Exception { + runTest("compiler/testData/codegen/box/sam/nullableSam.kt"); + } + @TestMetadata("partialSam.kt") public void testPartialSam() throws Exception { runTest("compiler/testData/codegen/box/sam/partialSam.kt"); @@ -23735,6 +23745,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/sam/partialSamKT.kt"); } + @TestMetadata("predicateSamWrapper.kt") + public void testPredicateSamWrapper() throws Exception { + runTest("compiler/testData/codegen/box/sam/predicateSamWrapper.kt"); + } + @TestMetadata("receiverEvaluatedOnce.kt") public void testReceiverEvaluatedOnce() throws Exception { runTest("compiler/testData/codegen/box/sam/receiverEvaluatedOnce.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index b34ebd0ffb0..dd7492acc94 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -23690,6 +23690,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/sam/castFromAny.kt"); } + @TestMetadata("inlinedSamWrapper.kt") + public void testInlinedSamWrapper() throws Exception { + runTest("compiler/testData/codegen/box/sam/inlinedSamWrapper.kt"); + } + @TestMetadata("kt17091.kt") public void testKt17091() throws Exception { runTest("compiler/testData/codegen/box/sam/kt17091.kt"); @@ -23725,6 +23730,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/sam/kt24825.kt"); } + @TestMetadata("nullableSam.kt") + public void testNullableSam() throws Exception { + runTest("compiler/testData/codegen/box/sam/nullableSam.kt"); + } + @TestMetadata("partialSam.kt") public void testPartialSam() throws Exception { runTest("compiler/testData/codegen/box/sam/partialSam.kt"); @@ -23735,6 +23745,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/sam/partialSamKT.kt"); } + @TestMetadata("predicateSamWrapper.kt") + public void testPredicateSamWrapper() throws Exception { + runTest("compiler/testData/codegen/box/sam/predicateSamWrapper.kt"); + } + @TestMetadata("receiverEvaluatedOnce.kt") public void testReceiverEvaluatedOnce() throws Exception { runTest("compiler/testData/codegen/box/sam/receiverEvaluatedOnce.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index eec290e7d07..0aedc693784 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -22590,6 +22590,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/sam/castFromAny.kt"); } + @TestMetadata("inlinedSamWrapper.kt") + public void testInlinedSamWrapper() throws Exception { + runTest("compiler/testData/codegen/box/sam/inlinedSamWrapper.kt"); + } + @TestMetadata("kt17091.kt") public void testKt17091() throws Exception { runTest("compiler/testData/codegen/box/sam/kt17091.kt"); @@ -22625,6 +22630,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/sam/kt24825.kt"); } + @TestMetadata("nullableSam.kt") + public void testNullableSam() throws Exception { + runTest("compiler/testData/codegen/box/sam/nullableSam.kt"); + } + @TestMetadata("partialSam.kt") public void testPartialSam() throws Exception { runTest("compiler/testData/codegen/box/sam/partialSam.kt"); @@ -22635,6 +22645,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/sam/partialSamKT.kt"); } + @TestMetadata("predicateSamWrapper.kt") + public void testPredicateSamWrapper() throws Exception { + runTest("compiler/testData/codegen/box/sam/predicateSamWrapper.kt"); + } + @TestMetadata("receiverEvaluatedOnce.kt") public void testReceiverEvaluatedOnce() throws Exception { runTest("compiler/testData/codegen/box/sam/receiverEvaluatedOnce.kt");