From 3f47725eb9cadc61288855184e8e5f53922eb089 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 26 Nov 2021 00:07:14 +0100 Subject: [PATCH] Add regression test for coroutines issue KT-49294 --- .../FirBlackBoxCodegenTestGenerated.java | 6 ++ .../codegen/box/coroutines/kt49294.kt | 95 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 ++ .../IrBlackBoxCodegenTestGenerated.java | 6 ++ .../LightAnalysisModeTestGenerated.java | 5 + .../js/test/JsCodegenBoxTestGenerated.java | 6 ++ .../test/ir/IrJsCodegenBoxTestGenerated.java | 6 ++ .../IrCodegenBoxWasmTestGenerated.java | 5 + .../NativeExtBlackBoxTestGenerated.java | 6 ++ 9 files changed, 141 insertions(+) create mode 100644 compiler/testData/codegen/box/coroutines/kt49294.kt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 03c7afd57ff..a4fdc5c448a 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -9828,6 +9828,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/coroutines/kt49168.kt"); } + @Test + @TestMetadata("kt49294.kt") + public void testKt49294() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/kt49294.kt"); + } + @Test @TestMetadata("lastExpressionIsLoop.kt") public void testLastExpressionIsLoop() throws Exception { diff --git a/compiler/testData/codegen/box/coroutines/kt49294.kt b/compiler/testData/codegen/box/coroutines/kt49294.kt new file mode 100644 index 00000000000..7072f9432c7 --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/kt49294.kt @@ -0,0 +1,95 @@ +// WITH_STDLIB +// IGNORE_BACKEND: JVM +// IGNORE_LIGHT_ANALYSIS +// FILE: 1.kt + +@file:OptIn(ExperimentalTypeInference::class) + +import kotlin.experimental.* + +fun interface FlowCollector { + suspend fun emit(value: T) +} + +interface SendChannel { + suspend fun send(element: E) +} + +suspend fun Flow.toList(): List { + val destination = ArrayList() + collect { value -> + destination.add(value) + } + return destination +} + +fun flow(@BuilderInference block: suspend FlowCollector.() -> Unit): Flow = SafeFlow(block) + +private class SafeFlow(private val block: suspend FlowCollector.() -> Unit) : Flow { + override suspend fun collect(collector: FlowCollector) { + collector.block() + } +} + +fun channelFlow(@BuilderInference block: suspend SendChannel.() -> Unit): Flow = + ChannelFlowBuilder(block) + +private open class ChannelFlowBuilder( + private val block: suspend SendChannel.() -> Unit +) : ChannelFlow() { + override suspend fun collectTo(scope: SendChannel) = + block(scope) +} + +abstract class ChannelFlow : Flow { + protected abstract suspend fun collectTo(scope: SendChannel) + + override suspend fun collect(collector: FlowCollector): Unit { + collectTo(object : SendChannel { + override suspend fun send(element: T) {} + }) + } +} + +interface Flow { + suspend fun collect(collector: FlowCollector) +} + +inline fun Flow.map(crossinline transform: suspend (value: T) -> R): Flow = flow { + collect { value -> + emit(transform(value)) + } +} + +fun Flow.flatMapMerge(transform: suspend (value: T) -> Flow): Flow = + map(transform).flattenMerge() + +fun Flow>.flattenMerge(): Flow = + ChannelFlowMerge(this) + +// FILE: 2.kt + +class ChannelFlowMerge(val flow: Flow>) : ChannelFlow() { + override suspend fun collectTo(scope: SendChannel) { + flow.collect {} + } +} + +// FILE: 3.kt + +import kotlin.coroutines.* + +fun box(): String { + val l: suspend Any.() -> Unit = { + flow { emit(1) }.flatMapMerge { + channelFlow { + val value = channelFlow { send(1) } + send(value) + } + }.toList() + } + l.startCoroutine(Any(), Continuation(EmptyCoroutineContext) { + it.getOrThrow() + }) + return "OK" +} 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 fee7cc2c6d4..8ec267cae2a 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 @@ -9750,6 +9750,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/coroutines/kt49168.kt"); } + @Test + @TestMetadata("kt49294.kt") + public void testKt49294() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/kt49294.kt"); + } + @Test @TestMetadata("lastExpressionIsLoop.kt") public void testLastExpressionIsLoop() throws Exception { 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 812d9df9bfd..2f95c95b414 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 @@ -9828,6 +9828,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/coroutines/kt49168.kt"); } + @Test + @TestMetadata("kt49294.kt") + public void testKt49294() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/kt49294.kt"); + } + @Test @TestMetadata("lastExpressionIsLoop.kt") public void testLastExpressionIsLoop() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index f1b821ba8dc..b05176d2686 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -7280,6 +7280,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/coroutines/kt49168.kt"); } + @TestMetadata("kt49294.kt") + public void ignoreKt49294() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/kt49294.kt"); + } + @TestMetadata("suspendFunctionAsSupertype.kt") public void ignoreSuspendFunctionAsSupertype() throws Exception { runTest("compiler/testData/codegen/box/coroutines/suspendFunctionAsSupertype.kt"); diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java index 7f5ea9e7dce..5def178de5c 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenBoxTestGenerated.java @@ -6968,6 +6968,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/coroutines/kt49168.kt"); } + @Test + @TestMetadata("kt49294.kt") + public void testKt49294() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/kt49294.kt"); + } + @Test @TestMetadata("lastExpressionIsLoop.kt") public void testLastExpressionIsLoop() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java index 6cca9905548..6bb62c951a3 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenBoxTestGenerated.java @@ -7010,6 +7010,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/coroutines/kt49168.kt"); } + @Test + @TestMetadata("kt49294.kt") + public void testKt49294() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/kt49294.kt"); + } + @Test @TestMetadata("lastExpressionIsLoop.kt") public void testLastExpressionIsLoop() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java index 5ff09516cff..686438c4526 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/testOld/wasm/semantics/IrCodegenBoxWasmTestGenerated.java @@ -6060,6 +6060,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/coroutines/kt49168.kt"); } + @TestMetadata("kt49294.kt") + public void testKt49294() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/kt49294.kt"); + } + @TestMetadata("lastExpressionIsLoop.kt") public void testLastExpressionIsLoop() throws Exception { runTest("compiler/testData/codegen/box/coroutines/lastExpressionIsLoop.kt"); diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeExtBlackBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeExtBlackBoxTestGenerated.java index ae4aef197ae..8a7aa527d2d 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeExtBlackBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeExtBlackBoxTestGenerated.java @@ -9931,6 +9931,12 @@ public class NativeExtBlackBoxTestGenerated extends AbstractNativeBlackBoxTest { runTest("compiler/testData/codegen/box/coroutines/kt49168.kt"); } + @Test + @TestMetadata("kt49294.kt") + public void testKt49294() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/kt49294.kt"); + } + @Test @TestMetadata("lastExpressionIsLoop.kt") public void testLastExpressionIsLoop() throws Exception {