diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/TailCallOptimization.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/TailCallOptimization.kt index b88190874b2..b74946ebdc9 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/TailCallOptimization.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/coroutines/TailCallOptimization.kt @@ -76,11 +76,18 @@ internal fun MethodNode.addCoroutineSuspendedChecks(suspensionPoints: List null - opcode == Opcodes.NOP || !isMeaningful -> next.skipUntilMeaningful() - opcode == Opcodes.GOTO -> (this as JumpInsnNode).label.skipUntilMeaningful() - else -> this +private fun AbstractInsnNode?.skipUntilMeaningful(): AbstractInsnNode? { + var cursor: AbstractInsnNode? = this ?: return null + val visited = mutableSetOf() + while (cursor != null) { + if (!visited.add(cursor)) return null + when { + cursor.opcode == Opcodes.NOP || !cursor.isMeaningful -> cursor = cursor.next + cursor.opcode == Opcodes.GOTO -> cursor = (cursor as JumpInsnNode).label + else -> return cursor + } + } + return null } private val AbstractInsnNode.nextMeaningful: AbstractInsnNode? 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 4511ea283ad..646500d1462 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 @@ -10291,6 +10291,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/coroutines/indirectInlineUsedAsNonInline.kt"); } + @Test + @TestMetadata("infiniteLoopInNextMeaningful.kt") + public void testInfiniteLoopInNextMeaningful() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java index 0bd3bcdf66d..5fb97c0b8a7 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java @@ -10291,6 +10291,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo runTest("compiler/testData/codegen/box/coroutines/indirectInlineUsedAsNonInline.kt"); } + @Test + @TestMetadata("infiniteLoopInNextMeaningful.kt") + public void testInfiniteLoopInNextMeaningful() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { diff --git a/compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt b/compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt new file mode 100644 index 00000000000..09252506702 --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt @@ -0,0 +1,27 @@ +// WITH_STDLIB + +import kotlin.coroutines.* + +suspend fun test() { + Nexus.initialize1() + while (true) { } +} + +object Nexus { + suspend fun initialize1() { + } +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(Continuation(EmptyCoroutineContext) { + it.getOrThrow() + }) +} + +fun box(): String { + builder { + suspendCoroutine {} + test() + } + return "OK" +} \ No newline at end of file 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 207ab888ce9..6ee6bd45875 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 @@ -10069,6 +10069,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/coroutines/indirectInlineUsedAsNonInline.kt"); } + @Test + @TestMetadata("infiniteLoopInNextMeaningful.kt") + public void testInfiniteLoopInNextMeaningful() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() 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 3801c387292..64527147348 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 @@ -10291,6 +10291,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/coroutines/indirectInlineUsedAsNonInline.kt"); } + @Test + @TestMetadata("infiniteLoopInNextMeaningful.kt") + public void testInfiniteLoopInNextMeaningful() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() 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 ded7628916b..3724f4edf31 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -7927,6 +7927,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/coroutines/indirectInlineUsedAsNonInline.kt"); } + @TestMetadata("infiniteLoopInNextMeaningful.kt") + public void testInfiniteLoopInNextMeaningful() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); + } + @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { runTest("compiler/testData/codegen/box/coroutines/inlineFunInGenericClass.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 283ad995b09..babf02186fe 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 @@ -7083,6 +7083,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/coroutines/indirectInlineUsedAsNonInline.kt"); } + @Test + @TestMetadata("infiniteLoopInNextMeaningful.kt") + public void testInfiniteLoopInNextMeaningful() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java index 1245fe29c3c..e1130e96b08 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirJsCodegenBoxTestGenerated.java @@ -7179,6 +7179,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest { runTest("compiler/testData/codegen/box/coroutines/indirectInlineUsedAsNonInline.kt"); } + @Test + @TestMetadata("infiniteLoopInNextMeaningful.kt") + public void testInfiniteLoopInNextMeaningful() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() 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 411e1830592..058e24a5b6d 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 @@ -7179,6 +7179,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/coroutines/indirectInlineUsedAsNonInline.kt"); } + @Test + @TestMetadata("infiniteLoopInNextMeaningful.kt") + public void testInfiniteLoopInNextMeaningful() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java index 7f172fbaecb..48485cfd3e4 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsES6CodegenBoxTestGenerated.java @@ -7179,6 +7179,12 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes runTest("compiler/testData/codegen/box/coroutines/indirectInlineUsedAsNonInline.kt"); } + @Test + @TestMetadata("infiniteLoopInNextMeaningful.kt") + public void testInfiniteLoopInNextMeaningful() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/K2NativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/K2NativeCodegenBoxTestGenerated.java index 51d9d06f1f1..9a46937476b 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/K2NativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/K2NativeCodegenBoxTestGenerated.java @@ -8136,6 +8136,12 @@ public class K2NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTes runTest("compiler/testData/codegen/box/coroutines/indirectInlineUsedAsNonInline.kt"); } + @Test + @TestMetadata("infiniteLoopInNextMeaningful.kt") + public void testInfiniteLoopInNextMeaningful() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java index da6a10b22d7..91e7e5d307c 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/NativeCodegenBoxTestGenerated.java @@ -8047,6 +8047,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest runTest("compiler/testData/codegen/box/coroutines/indirectInlineUsedAsNonInline.kt"); } + @Test + @TestMetadata("infiniteLoopInNextMeaningful.kt") + public void testInfiniteLoopInNextMeaningful() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); + } + @Test @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { diff --git a/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/IrCodegenBoxWasmTestGenerated.java b/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/IrCodegenBoxWasmTestGenerated.java index b884a3a90fd..b565c1ee17b 100644 --- a/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/IrCodegenBoxWasmTestGenerated.java +++ b/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/IrCodegenBoxWasmTestGenerated.java @@ -6317,6 +6317,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest runTest("compiler/testData/codegen/box/coroutines/indirectInlineUsedAsNonInline.kt"); } + @TestMetadata("infiniteLoopInNextMeaningful.kt") + public void testInfiniteLoopInNextMeaningful() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/infiniteLoopInNextMeaningful.kt"); + } + @TestMetadata("inlineFunInGenericClass.kt") public void testInlineFunInGenericClass() throws Exception { runTest("compiler/testData/codegen/box/coroutines/inlineFunInGenericClass.kt");