From 3080b4c435b997a89c47c509786568e3ce15ab54 Mon Sep 17 00:00:00 2001 From: pyos Date: Tue, 18 Feb 2020 10:59:09 +0100 Subject: [PATCH] JVM_IR: remove a suspend-related hack from BridgeLowering `invoke` in suspend lambdas overrides FunctionN.invoke, so the refactored BridgeLowering already generates correct bridges there. All the hack does is break overrides of interface suspend methods. --- .../backend/jvm/lower/BridgeLowering.kt | 7 +----- .../bridges/interfaceSpecialization.kt | 23 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 10 ++++++++ .../LightAnalysisModeTestGenerated.java | 10 ++++++++ .../ir/FirBlackBoxCodegenTestGenerated.java | 5 ++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 5 ++++ .../IrJsCodegenBoxTestGenerated.java | 5 ++++ .../semantics/JsCodegenBoxTestGenerated.java | 5 ++++ 8 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 compiler/testData/codegen/box/coroutines/bridges/interfaceSpecialization.kt diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/BridgeLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/BridgeLowering.kt index e4e8fa1585e..e8cc21fc174 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/BridgeLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/BridgeLowering.kt @@ -463,12 +463,7 @@ private class BridgeLowering(val context: JvmBackendContext) : FileLoweringPass, private fun IrValueParameter.copyWithTypeErasure(target: IrSimpleFunction, forceBoxing: Boolean = false): IrValueParameter = copyTo( target, IrDeclarationOrigin.BRIDGE, - type = - // SuspendFunction{N} is Function{N+1} at runtime, thus, when we generate a bridge for suspend callable references, - // we need to replace the type of its continuation parameter with Any? - if (target.isSuspend && type.eraseTypeParameters().getClass() - ?.fqNameWhenAvailable == DescriptorUtils.CONTINUATION_INTERFACE_FQ_NAME_RELEASE - ) context.irBuiltIns.anyNType else type.eraseTypeParameters().let { if (forceBoxing) it.makeNullable() else it }, + type = type.eraseTypeParameters().let { if (forceBoxing) it.makeNullable() else it }, varargElementType = varargElementType?.eraseTypeParameters() ) diff --git a/compiler/testData/codegen/box/coroutines/bridges/interfaceSpecialization.kt b/compiler/testData/codegen/box/coroutines/bridges/interfaceSpecialization.kt new file mode 100644 index 00000000000..065199121fd --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/bridges/interfaceSpecialization.kt @@ -0,0 +1,23 @@ +// IGNORE_BACKEND_FIR: JVM_IR +// WITH_RUNTIME +// WITH_COROUTINES +// COMMON_COROUTINES_TEST + +import helpers.* +import COROUTINES_PACKAGE.* + +interface I1 { + suspend fun f(a: A, b: B): String +} + +class C : I1 { + override suspend fun f(a: A, b: String): String = b +} + +fun box(): String { + var result = "fail" + suspend { + result = (C() as I1).f(Unit, "OK") + }.startCoroutine(EmptyContinuation) + return result +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 4c2d4def357..e9819e4bf1d 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -6908,6 +6908,16 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/coroutines/bridges"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true); } + @TestMetadata("interfaceSpecialization.kt") + public void testInterfaceSpecialization_1_2() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/bridges/interfaceSpecialization.kt", "kotlin.coroutines.experimental"); + } + + @TestMetadata("interfaceSpecialization.kt") + public void testInterfaceSpecialization_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/bridges/interfaceSpecialization.kt", "kotlin.coroutines"); + } + @TestMetadata("lambdaWithLongReceiver.kt") public void testLambdaWithLongReceiver_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/bridges/lambdaWithLongReceiver.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 04209e36173..cc0bbe25bdc 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -6908,6 +6908,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/coroutines/bridges"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true); } + @TestMetadata("interfaceSpecialization.kt") + public void testInterfaceSpecialization_1_2() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/bridges/interfaceSpecialization.kt", "kotlin.coroutines.experimental"); + } + + @TestMetadata("interfaceSpecialization.kt") + public void testInterfaceSpecialization_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/bridges/interfaceSpecialization.kt", "kotlin.coroutines"); + } + @TestMetadata("lambdaWithLongReceiver.kt") public void testLambdaWithLongReceiver_1_2() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/bridges/lambdaWithLongReceiver.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 17070056777..23a21ae4719 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -6453,6 +6453,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/coroutines/bridges"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); } + @TestMetadata("interfaceSpecialization.kt") + public void testInterfaceSpecialization_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/bridges/interfaceSpecialization.kt", "kotlin.coroutines"); + } + @TestMetadata("lambdaWithLongReceiver.kt") public void testLambdaWithLongReceiver_1_3() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/bridges/lambdaWithLongReceiver.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 461d8c39137..a002681b3f2 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -6453,6 +6453,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/coroutines/bridges"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); } + @TestMetadata("interfaceSpecialization.kt") + public void testInterfaceSpecialization_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/bridges/interfaceSpecialization.kt", "kotlin.coroutines"); + } + @TestMetadata("lambdaWithLongReceiver.kt") public void testLambdaWithLongReceiver_1_3() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/bridges/lambdaWithLongReceiver.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 52eaa2b8201..05c418313de 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 @@ -5483,6 +5483,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/coroutines/bridges"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true); } + @TestMetadata("interfaceSpecialization.kt") + public void testInterfaceSpecialization_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/bridges/interfaceSpecialization.kt", "kotlin.coroutines"); + } + @TestMetadata("lambdaWithLongReceiver.kt") public void testLambdaWithLongReceiver_1_3() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/bridges/lambdaWithLongReceiver.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 fe47cb4358e..6de7b000bea 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 @@ -5483,6 +5483,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/coroutines/bridges"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS, true); } + @TestMetadata("interfaceSpecialization.kt") + public void testInterfaceSpecialization_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/bridges/interfaceSpecialization.kt", "kotlin.coroutines"); + } + @TestMetadata("lambdaWithLongReceiver.kt") public void testLambdaWithLongReceiver_1_3() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/bridges/lambdaWithLongReceiver.kt", "kotlin.coroutines");