From 4d9d62ad1284e7c412cd27032bf90de03297dfa3 Mon Sep 17 00:00:00 2001 From: Ilmir Usmanov Date: Fri, 27 Dec 2019 16:04:36 +0100 Subject: [PATCH] JVM_IR: Also check attributes for suspendImpls when generating continuation Otherwise, it would not generate continuations for overloads. --- .../backend/jvm/codegen/FunctionCodegen.kt | 8 ++-- .../jvm/lower/AddContinuationLowering.kt | 1 + .../superCallOverload.kt | 43 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 10 +++++ .../LightAnalysisModeTestGenerated.java | 10 +++++ .../ir/FirBlackBoxCodegenTestGenerated.java | 5 +++ .../ir/IrBlackBoxCodegenTestGenerated.java | 5 +++ .../IrJsCodegenBoxTestGenerated.java | 5 +++ .../semantics/JsCodegenBoxTestGenerated.java | 10 +++++ 9 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallOverload.kt diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/FunctionCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/FunctionCodegen.kt index fcd454683cd..4da35f03929 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/FunctionCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/FunctionCodegen.kt @@ -134,12 +134,14 @@ open class FunctionCodegen( // This is just a template for inliner origin != JvmLoweredDeclarationOrigin.FOR_INLINE_STATE_MACHINE_TEMPLATE_CAPTURES_CROSSINLINE && // Continuations are generated for suspendImpls - parentAsClass.functions.none { it.name.asString() == name.asString() + SUSPEND_IMPL_NAME_SUFFIX } && + parentAsClass.functions.none { + it.name.asString() == name.asString() + SUSPEND_IMPL_NAME_SUFFIX && + it.attributeOwnerId == (this as? IrAttributeContainer)?.attributeOwnerId + } && // $$forInline functions never have a continuation origin != JvmLoweredDeclarationOrigin.FOR_INLINE_STATE_MACHINE_TEMPLATE - private fun continuationClass(): IrClass = - irFunction.body!!.statements.firstIsInstance() ?: error("No continuation class generated for ${irFunction.name}") + private fun continuationClass() = irFunction.body!!.statements.firstIsInstance() private fun IrFunction.getVisibilityForDefaultArgumentStub(): Int = when (visibility) { diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt index 728938896b2..bbcb07bbe70 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt @@ -524,6 +524,7 @@ private class AddContinuationLowering(private val context: JvmBackendContext) : } }) } + static.copyAttributes(irFunction) return static } diff --git a/compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallOverload.kt b/compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallOverload.kt new file mode 100644 index 00000000000..2f79ace444f --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallOverload.kt @@ -0,0 +1,43 @@ +// IGNORE_BACKEND_FIR: JVM_IR +// WITH_RUNTIME +// WITH_COROUTINES +// COMMON_COROUTINES_TEST +import helpers.* +import COROUTINES_PACKAGE.* +import COROUTINES_PACKAGE.intrinsics.* + +open class A(val v: String) { + suspend fun suspendThere(v: String): String = suspendCoroutineUninterceptedOrReturn { x -> + x.resume(v) + COROUTINE_SUSPENDED + } + + open suspend fun suspendHere(): String = suspendThere("O") + suspendThere(v) +} + +class B(v: String) : A(v) { + override suspend fun suspendHere(): String = super.suspendHere() + suspendThere("56") + suspend fun suspendHere(s: String): String = super.suspendHere() + suspendThere(s) +} + +fun builder(c: suspend A.() -> Unit) { + c.startCoroutine(B("K"), EmptyContinuation) +} + +fun box(): String { + var result = "" + + builder { + result = suspendHere() + } + + if (result != "OK56") return "fail 1: $result" + + builder { + result = (this as B).suspendHere("OK") + } + + if (result != "OKOK") return "fail 2: $result" + + return "OK" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 7a3026aaa17..67564b99932 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -8458,6 +8458,16 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallInterface.kt", "kotlin.coroutines"); } + @TestMetadata("superCallOverload.kt") + public void testSuperCallOverload_1_2() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallOverload.kt", "kotlin.coroutines.experimental"); + } + + @TestMetadata("superCallOverload.kt") + public void testSuperCallOverload_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallOverload.kt", "kotlin.coroutines"); + } + @TestMetadata("superCall.kt") public void testSuperCall_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCall.kt", "kotlin.coroutines.experimental"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 1b6f8edb22e..9fc94a96ef3 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -8458,6 +8458,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallInterface.kt", "kotlin.coroutines"); } + @TestMetadata("superCallOverload.kt") + public void testSuperCallOverload_1_2() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallOverload.kt", "kotlin.coroutines.experimental"); + } + + @TestMetadata("superCallOverload.kt") + public void testSuperCallOverload_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallOverload.kt", "kotlin.coroutines"); + } + @TestMetadata("superCall.kt") public void testSuperCall_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCall.kt", "kotlin.coroutines.experimental"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index e850f558e79..7fa08947b49 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -7418,6 +7418,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallInterface.kt", "kotlin.coroutines"); } + @TestMetadata("superCallOverload.kt") + public void testSuperCallOverload_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallOverload.kt", "kotlin.coroutines"); + } + @TestMetadata("superCall.kt") public void testSuperCall_1_3() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCall.kt", "kotlin.coroutines"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index c847d58b116..6e07f3cf147 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -7418,6 +7418,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallInterface.kt", "kotlin.coroutines"); } + @TestMetadata("superCallOverload.kt") + public void testSuperCallOverload_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallOverload.kt", "kotlin.coroutines"); + } + @TestMetadata("superCall.kt") public void testSuperCall_1_3() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCall.kt", "kotlin.coroutines"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index f5f345eaa6a..84d559eb6b5 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -6348,6 +6348,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallInterface.kt", "kotlin.coroutines"); } + @TestMetadata("superCallOverload.kt") + public void testSuperCallOverload_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallOverload.kt", "kotlin.coroutines"); + } + @TestMetadata("superCall.kt") public void testSuperCall_1_3() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCall.kt", "kotlin.coroutines"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 17ecf3239d9..8aa1715b94f 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -7313,6 +7313,16 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallInterface.kt", "kotlin.coroutines"); } + @TestMetadata("superCallOverload.kt") + public void testSuperCallOverload_1_2() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallOverload.kt", "kotlin.coroutines.experimental"); + } + + @TestMetadata("superCallOverload.kt") + public void testSuperCallOverload_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCallOverload.kt", "kotlin.coroutines"); + } + @TestMetadata("superCall.kt") public void testSuperCall_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunctionAsCoroutine/superCall.kt", "kotlin.coroutines.experimental");