From 516692008fcce7629c6062914787f0418389f01b Mon Sep 17 00:00:00 2001 From: pyos Date: Fri, 3 Apr 2020 15:04:05 +0200 Subject: [PATCH] JVM: add tests fixed by the last two commits One was broken on JVM, the other on JVM_IR. --- .../withCapturedInlineLambda.kt | 55 +++++++++++++++++++ .../withCapturedInlineLambda2.kt | 37 +++++++++++++ .../BlackBoxInlineCodegenTestGenerated.java | 10 ++++ ...otlinAgainstInlineKotlinTestGenerated.java | 10 ++++ .../IrBlackBoxInlineCodegenTestGenerated.java | 10 ++++ ...otlinAgainstInlineKotlinTestGenerated.java | 10 ++++ 6 files changed, 132 insertions(+) create mode 100644 compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/withCapturedInlineLambda.kt create mode 100644 compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/withCapturedInlineLambda2.kt diff --git a/compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/withCapturedInlineLambda.kt b/compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/withCapturedInlineLambda.kt new file mode 100644 index 00000000000..35b5a2c9331 --- /dev/null +++ b/compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/withCapturedInlineLambda.kt @@ -0,0 +1,55 @@ +// WITH_RUNTIME +// WITH_REFLECT +// WITH_COROUTINES +// TARGET_BACKEND: JVM +// NO_CHECK_LAMBDA_INLINING +// FILE: inlined.kt +package flow + +interface Flow { + suspend fun collect(collector: FlowCollector) +} + +interface FlowCollector { + suspend fun emit(value: T) +} + +inline fun flow(crossinline body: suspend FlowCollector.() -> Unit): Flow = + object : Flow { + override suspend fun collect(collector: FlowCollector) = collector.body() + } + +suspend inline fun Flow.collect(crossinline body: suspend (T) -> Unit) = + collect(object : FlowCollector { + override suspend fun emit(value: T) = body(value) + }) + +inline fun Flow.transform(crossinline transformer: suspend FlowCollector.(value: T) -> Unit): Flow = + flow { collect { value -> transformer(value) } } + +inline fun Flow.map(crossinline transformer: suspend (value: T) -> R): Flow = + transform { value -> emit(transformer(value)) } + +// FILE: box.kt +import flow.* +import helpers.* +import kotlin.reflect.* +import kotlin.reflect.jvm.* +import kotlin.coroutines.* +import kotlin.coroutines.intrinsics.* + +suspend fun addK(x: String): String = suspendCoroutineUninterceptedOrReturn { cont -> + cont.resume(x + "K") + COROUTINE_SUSPENDED +} + +var result = "" + +fun box(): String { + suspend { + val source = flow { emit("O") } + val reference: (suspend (String) -> String) -> Flow = source::map + ((reference as KFunction<*>).javaMethod!!.invoke(null, source, ::addK) as Flow).collect { result = it } + }.startCoroutine(EmptyContinuation) + return result +} diff --git a/compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/withCapturedInlineLambda2.kt b/compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/withCapturedInlineLambda2.kt new file mode 100644 index 00000000000..97424c4661e --- /dev/null +++ b/compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/withCapturedInlineLambda2.kt @@ -0,0 +1,37 @@ +// WITH_RUNTIME +// WITH_REFLECT +// WITH_COROUTINES +// TARGET_BACKEND: JVM +// NO_CHECK_LAMBDA_INLINING +// FILE: inlined.kt +package test + +interface SuspendRunnable { + suspend fun run(): R +} + +inline fun makeKAdder(crossinline x: suspend () -> String) = object : SuspendRunnable { + override suspend fun run() = run { x() } + "K" +} + +// FILE: box.kt +import test.* +import helpers.* +import kotlin.reflect.* +import kotlin.reflect.jvm.* +import kotlin.coroutines.* +import kotlin.coroutines.intrinsics.* + +suspend fun getO(): String = suspendCoroutineUninterceptedOrReturn { cont -> + cont.resume("O") + COROUTINE_SUSPENDED +} + +var result = "" + +fun box(): String { + suspend { + result = ((::makeKAdder as KFunction<*>).javaMethod!!.invoke(null, ::getO) as SuspendRunnable).run() + }.startCoroutine(EmptyContinuation) + return result +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java index 703104ce9cf..137df5bfcaf 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java @@ -4044,6 +4044,16 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo public void testSimpleNamed() throws Exception { runTest("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/simpleNamed.kt"); } + + @TestMetadata("withCapturedInlineLambda.kt") + public void testWithCapturedInlineLambda() throws Exception { + runTest("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/withCapturedInlineLambda.kt"); + } + + @TestMetadata("withCapturedInlineLambda2.kt") + public void testWithCapturedInlineLambda2() throws Exception { + runTest("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/withCapturedInlineLambda2.kt"); + } } @TestMetadata("compiler/testData/codegen/boxInline/suspend/receiver") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java index 5f1c11a5701..6ba3b2923d0 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java @@ -4044,6 +4044,16 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi public void testSimpleNamed() throws Exception { runTest("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/simpleNamed.kt"); } + + @TestMetadata("withCapturedInlineLambda.kt") + public void testWithCapturedInlineLambda() throws Exception { + runTest("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/withCapturedInlineLambda.kt"); + } + + @TestMetadata("withCapturedInlineLambda2.kt") + public void testWithCapturedInlineLambda2() throws Exception { + runTest("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/withCapturedInlineLambda2.kt"); + } } @TestMetadata("compiler/testData/codegen/boxInline/suspend/receiver") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java index 9bbb2b12f7c..968c653a00b 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java @@ -3924,6 +3924,16 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli public void testSimpleNamed() throws Exception { runTest("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/simpleNamed.kt"); } + + @TestMetadata("withCapturedInlineLambda.kt") + public void testWithCapturedInlineLambda() throws Exception { + runTest("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/withCapturedInlineLambda.kt"); + } + + @TestMetadata("withCapturedInlineLambda2.kt") + public void testWithCapturedInlineLambda2() throws Exception { + runTest("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/withCapturedInlineLambda2.kt"); + } } @TestMetadata("compiler/testData/codegen/boxInline/suspend/receiver") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java index 9d5a93d36a9..14fbea4485d 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java @@ -3924,6 +3924,16 @@ public class IrCompileKotlinAgainstInlineKotlinTestGenerated extends AbstractIrC public void testSimpleNamed() throws Exception { runTest("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/simpleNamed.kt"); } + + @TestMetadata("withCapturedInlineLambda.kt") + public void testWithCapturedInlineLambda() throws Exception { + runTest("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/withCapturedInlineLambda.kt"); + } + + @TestMetadata("withCapturedInlineLambda2.kt") + public void testWithCapturedInlineLambda2() throws Exception { + runTest("compiler/testData/codegen/boxInline/suspend/inlineUsedAsNoinline/withCapturedInlineLambda2.kt"); + } } @TestMetadata("compiler/testData/codegen/boxInline/suspend/receiver")