From d861373c6d14b8cd5a18d229404240eacf70cc82 Mon Sep 17 00:00:00 2001 From: Ilmir Usmanov Date: Tue, 11 Aug 2020 00:10:17 +0200 Subject: [PATCH] Hack attributes for continuation of suspend function in SAM-adapter --- .../ir/FirBlackBoxCodegenTestGenerated.java | 5 ++++ .../jvm/lower/AddContinuationLowering.kt | 11 ++++++++ .../featureIntersection/funInterface.kt | 27 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 5 ++++ .../LightAnalysisModeTestGenerated.java | 5 ++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 5 ++++ 6 files changed, 58 insertions(+) create mode 100644 compiler/testData/codegen/box/coroutines/featureIntersection/funInterface.kt diff --git a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index c3a75646d61..a3aeab570b6 100644 --- a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -7073,6 +7073,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/featureIntersection/destructuringInLambdas.kt", "kotlin.coroutines"); } + @TestMetadata("funInterface.kt") + public void testFunInterface() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/featureIntersection/funInterface.kt"); + } + @TestMetadata("inlineSuspendFinally.kt") public void testInlineSuspendFinally_1_3() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/featureIntersection/inlineSuspendFinally.kt", "kotlin.coroutines"); 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 ab09789acaa..36c9118dc6e 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 @@ -48,6 +48,7 @@ import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.util.OperatorNameConventions import org.jetbrains.kotlin.utils.DFS +import org.jetbrains.org.objectweb.asm.Type internal val addContinuationPhase = makeIrFilePhase( ::AddContinuationLowering, @@ -597,6 +598,16 @@ private class AddContinuationLowering(private val context: JvmBackendContext) : val result = mutableListOf(view) if (function.body == null || !function.hasContinuation()) return result + // This is a suspend function inside of SAM adapter. + // The attribute of function reference is used for the SAM adapter. + // So, we hack new attributes for continuation class. + if (function.parentAsClass.origin == JvmLoweredDeclarationOrigin.LAMBDA_IMPL) { + context.putLocalClassType( + function.attributeOwnerId, + Type.getObjectType("${context.getLocalClassType(function.parentAsClass)!!.internalName}$${function.name}$1") + ) + } + if (flag.capturesCrossinline || function.isInline) { result += context.irFactory.buildFun { containerSource = view.containerSource diff --git a/compiler/testData/codegen/box/coroutines/featureIntersection/funInterface.kt b/compiler/testData/codegen/box/coroutines/featureIntersection/funInterface.kt new file mode 100644 index 00000000000..b5ad568d3f9 --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/featureIntersection/funInterface.kt @@ -0,0 +1,27 @@ +// WITH_RUNTIME +// WITH_COROUTINES +// TARGET_BACKEND: JVM +// IGNORE_BACKEND: JVM +// IGNORE_BACKEND_FIR: JVM_IR + +import helpers.* +import kotlin.coroutines.* + +fun interface Action { + suspend fun run() +} +suspend fun runAction(a: Action) { + a.run() +} +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} +fun box(): String { + var res = "FAIL" + builder { + runAction { + res = "OK" + } + } + return res +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 8cdf9d2208a..49d75b8e107 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -7673,6 +7673,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/featureIntersection/destructuringInLambdas.kt", "kotlin.coroutines"); } + @TestMetadata("funInterface.kt") + public void testFunInterface() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/featureIntersection/funInterface.kt"); + } + @TestMetadata("inlineSuspendFinally.kt") public void testInlineSuspendFinally_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/featureIntersection/inlineSuspendFinally.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 f2e9906e4d4..17b12c6a9e7 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -7631,6 +7631,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) public static class FeatureIntersection extends AbstractLightAnalysisModeTest { + @TestMetadata("funInterface.kt") + public void ignoreFunInterface() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/featureIntersection/funInterface.kt"); + } + private void runTest(String testDataFilePath) throws Exception { KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath); } diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index e16f70f66cd..643beaf3084 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -7073,6 +7073,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/featureIntersection/destructuringInLambdas.kt", "kotlin.coroutines"); } + @TestMetadata("funInterface.kt") + public void testFunInterface() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/featureIntersection/funInterface.kt"); + } + @TestMetadata("inlineSuspendFinally.kt") public void testInlineSuspendFinally_1_3() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/featureIntersection/inlineSuspendFinally.kt", "kotlin.coroutines");