From a644cbb5ed2da2e6417d6385bfc25b6117305875 Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Wed, 20 Jun 2018 20:09:09 +0300 Subject: [PATCH] Fix deserialization of kotlin.suspend when LV=1.3 is used Before this change, kotlin.suspend was being loaded as having a common function type instead of suspend function type. With LV=1.3, we expect that suspend function types should have new Continuation interface as a last type argument, while kotlin.suspend is built with LV=1.2 and has old Continuation. This change might be reverted once stdlib will be rebuilt with LV=1.3 NB: kotlin.suspend doesn't need to be intrinsified since it only returns its parameter with checkcast to kotlinin.jvm.functions.Function1 (i.e., it doesn't refer the coroutines package) #KT-24861 Fixed --- .../suspendFunctionTypeCall/suspendModifier.kt | 6 +++--- .../testsWithStdLib/coroutines/suspendLambda.kt | 5 ++--- .../codegen/ir/IrBlackBoxCodegenTestGenerated.java | 11 +++++++++-- .../DiagnosticsTestWithStdLibGenerated.java | 11 +++++++++-- ...iagnosticsTestWithStdLibUsingJavacGenerated.java | 11 +++++++++-- .../codegen/BlackBoxCodegenTestGenerated.java | 11 +++++++++-- .../codegen/LightAnalysisModeTestGenerated.java | 11 +++++++++-- .../deserialization/TypeDeserializer.kt | 13 ++++++++++++- .../test/semantics/IrJsCodegenBoxTestGenerated.java | 11 +++++++++-- .../test/semantics/JsCodegenBoxTestGenerated.java | 5 +++-- 10 files changed, 74 insertions(+), 21 deletions(-) diff --git a/compiler/testData/codegen/box/coroutines/suspendFunctionTypeCall/suspendModifier.kt b/compiler/testData/codegen/box/coroutines/suspendFunctionTypeCall/suspendModifier.kt index 658ae3b66b9..b630d11b723 100644 --- a/compiler/testData/codegen/box/coroutines/suspendFunctionTypeCall/suspendModifier.kt +++ b/compiler/testData/codegen/box/coroutines/suspendFunctionTypeCall/suspendModifier.kt @@ -1,10 +1,10 @@ -// !API_VERSION: 1.2 // IGNORE_BACKEND: JS_IR // WITH_RUNTIME +// COMMON_COROUTINES_TEST // WITH_COROUTINES import helpers.* -import kotlin.coroutines.experimental.* -import kotlin.coroutines.experimental.intrinsics.* +import COROUTINES_PACKAGE.* +import COROUTINES_PACKAGE.intrinsics.* suspend fun suspendHere(v: String): String = suspendCoroutineOrReturn { x -> x.resume(v) diff --git a/compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendLambda.kt b/compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendLambda.kt index ba70c569ef2..139ab2e3dfc 100644 --- a/compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendLambda.kt +++ b/compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendLambda.kt @@ -1,6 +1,5 @@ -import kotlin.coroutines.experimental.Continuation -import kotlin.coroutines.experimental.CoroutineContext -import kotlin.coroutines.experimental.startCoroutine +// COMMON_COROUTINES_TEST +import COROUTINES_PACKAGE.* fun bar() { suspend { diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 545c8ec7781..ed6bcb6e5de 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -7493,8 +7493,15 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes } @TestMetadata("suspendModifier.kt") - public void testSuspendModifier() throws Exception { - runTest("compiler/testData/codegen/box/coroutines/suspendFunctionTypeCall/suspendModifier.kt"); + public void testSuspendModifier_1_2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendFunctionTypeCall/suspendModifier.kt"); + doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines.experimental"); + } + + @TestMetadata("suspendModifier.kt") + public void testSuspendModifier_1_3() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendFunctionTypeCall/suspendModifier.kt"); + doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines"); } } diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java index 4ee0b14c282..af6abe2ef17 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestWithStdLibGenerated.java @@ -1520,8 +1520,15 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW } @TestMetadata("suspendLambda.kt") - public void testSuspendLambda() throws Exception { - runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendLambda.kt"); + public void testSuspendLambda_1_2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendLambda.kt"); + doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines.experimental"); + } + + @TestMetadata("suspendLambda.kt") + public void testSuspendLambda_1_3() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendLambda.kt"); + doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines"); } @TestMetadata("suspendOverridability.kt") diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java index e6739dc9a73..2d7351444ff 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/javac/DiagnosticsTestWithStdLibUsingJavacGenerated.java @@ -1520,8 +1520,15 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno } @TestMetadata("suspendLambda.kt") - public void testSuspendLambda() throws Exception { - runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendLambda.kt"); + public void testSuspendLambda_1_2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendLambda.kt"); + doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines.experimental"); + } + + @TestMetadata("suspendLambda.kt") + public void testSuspendLambda_1_3() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/coroutines/suspendLambda.kt"); + doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines"); } @TestMetadata("suspendOverridability.kt") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 1f8a30a79f8..d410401b35f 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -7493,8 +7493,15 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { } @TestMetadata("suspendModifier.kt") - public void testSuspendModifier() throws Exception { - runTest("compiler/testData/codegen/box/coroutines/suspendFunctionTypeCall/suspendModifier.kt"); + public void testSuspendModifier_1_2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendFunctionTypeCall/suspendModifier.kt"); + doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines.experimental"); + } + + @TestMetadata("suspendModifier.kt") + public void testSuspendModifier_1_3() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendFunctionTypeCall/suspendModifier.kt"); + doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines"); } } diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index d9fd1f2ec3b..c5fb6b61811 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -7493,8 +7493,15 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes } @TestMetadata("suspendModifier.kt") - public void testSuspendModifier() throws Exception { - runTest("compiler/testData/codegen/box/coroutines/suspendFunctionTypeCall/suspendModifier.kt"); + public void testSuspendModifier_1_2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendFunctionTypeCall/suspendModifier.kt"); + doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines.experimental"); + } + + @TestMetadata("suspendModifier.kt") + public void testSuspendModifier_1_3() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendFunctionTypeCall/suspendModifier.kt"); + doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines"); } } diff --git a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt index e0c2a0ad37b..240da5c58a1 100644 --- a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt +++ b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt @@ -13,9 +13,11 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.metadata.ProtoBuf import org.jetbrains.kotlin.metadata.deserialization.* import org.jetbrains.kotlin.name.ClassId +import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedAnnotationsWithPossibleTargets import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedTypeParameterDescriptor import org.jetbrains.kotlin.types.* +import org.jetbrains.kotlin.utils.addToStdlib.safeAs import java.util.* class TypeDeserializer( @@ -164,7 +166,16 @@ class TypeDeserializer( ): SimpleType? { val functionType = KotlinTypeFactory.simpleType(annotations, functionTypeConstructor, arguments, nullable) if (!functionType.isFunctionType) return null - return transformRuntimeFunctionTypeToSuspendFunction(functionType, isReleaseCoroutines) + + transformRuntimeFunctionTypeToSuspendFunction(functionType, isReleaseCoroutines)?.let { return it } + + // kotlin.suspend is still built with LV=1.2, thus it references old Continuation + // And otherwise, once stdlib is compiled with 1.3 one may want to stay at LV=1.2 + if (c.containingDeclaration.safeAs()?.fqNameOrNull() == KOTLIN_SUSPEND_BUILT_IN_FUNCTION_FQ_NAME) { + transformRuntimeFunctionTypeToSuspendFunction(functionType, !isReleaseCoroutines)?.let { return it } + } + + return null } private fun typeParameterTypeConstructor(typeParameterId: Int): TypeConstructor? = diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java index 57fd9fc98ec..bad78857d20 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/IrJsCodegenBoxTestGenerated.java @@ -7203,8 +7203,15 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { } @TestMetadata("suspendModifier.kt") - public void testSuspendModifier() throws Exception { - runTest("compiler/testData/codegen/box/coroutines/suspendFunctionTypeCall/suspendModifier.kt"); + public void testSuspendModifier_1_2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendFunctionTypeCall/suspendModifier.kt"); + try { + doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines.experimental"); + } + catch (Throwable ignore) { + return; + } + throw new AssertionError("Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that."); } } 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 1578a51d109..e3cd59048c6 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 @@ -6183,8 +6183,9 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { } @TestMetadata("suspendModifier.kt") - public void testSuspendModifier() throws Exception { - runTest("compiler/testData/codegen/box/coroutines/suspendFunctionTypeCall/suspendModifier.kt"); + public void testSuspendModifier_1_2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/coroutines/suspendFunctionTypeCall/suspendModifier.kt"); + doTestWithCoroutinesPackageReplacement(fileName, "kotlin.coroutines.experimental"); } }