From 2bf50cc91aaf92e62efc1068f9742e22fe6133f8 Mon Sep 17 00:00:00 2001 From: pyos Date: Thu, 6 Feb 2020 10:26:42 +0100 Subject: [PATCH] JVM_IR: correctly name $$forInline versions of @JvmName suspend funs Using a hack similar to $default stubs. --- .../kotlin/backend/jvm/ir/IrUtils.kt | 9 ++++- .../jvm/lower/AddContinuationLowering.kt | 1 + .../codegen/boxInline/suspend/jvmName.kt | 36 +++++++++++++++++++ .../BlackBoxInlineCodegenTestGenerated.java | 10 ++++++ ...otlinAgainstInlineKotlinTestGenerated.java | 10 ++++++ .../IrBlackBoxInlineCodegenTestGenerated.java | 5 +++ ...otlinAgainstInlineKotlinTestGenerated.java | 5 +++ 7 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/boxInline/suspend/jvmName.kt diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt index 8636945cff2..53898062685 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt @@ -7,12 +7,14 @@ package org.jetbrains.kotlin.backend.jvm.ir import org.jetbrains.kotlin.backend.common.lower.IrLoweringContext import org.jetbrains.kotlin.backend.jvm.JvmBackendContext +import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin import org.jetbrains.kotlin.backend.jvm.JvmSymbols import org.jetbrains.kotlin.backend.jvm.codegen.isInlineOnly import org.jetbrains.kotlin.backend.jvm.codegen.isJvmInterface import org.jetbrains.kotlin.backend.jvm.descriptors.JvmDeclarationFactory import org.jetbrains.kotlin.backend.jvm.lower.inlineclasses.unboxInlineClass import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.codegen.inline.coroutines.FOR_INLINE_SUFFIX import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope @@ -128,7 +130,12 @@ fun IrDeclaration.getJvmNameFromAnnotation(): String? { // TODO lower @JvmName? val const = getAnnotation(DescriptorUtils.JVM_NAME)?.getValueArgument(0) as? IrConst<*> ?: return null val value = const.value as? String ?: return null - return if (origin == IrDeclarationOrigin.FUNCTION_FOR_DEFAULT_PARAMETER) "$value\$default" else value + return when (origin) { + IrDeclarationOrigin.FUNCTION_FOR_DEFAULT_PARAMETER -> "$value\$default" + JvmLoweredDeclarationOrigin.FOR_INLINE_STATE_MACHINE_TEMPLATE, + JvmLoweredDeclarationOrigin.FOR_INLINE_STATE_MACHINE_TEMPLATE_CAPTURES_CROSSINLINE -> "$value$FOR_INLINE_SUFFIX" + else -> value + } } val IrFunction.propertyIfAccessor: IrDeclaration 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 9ec9225a3e5..030698017bd 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 @@ -618,6 +618,7 @@ private class AddContinuationLowering(private val context: JvmBackendContext) : if (view.isInline) JvmLoweredDeclarationOrigin.FOR_INLINE_STATE_MACHINE_TEMPLATE else JvmLoweredDeclarationOrigin.FOR_INLINE_STATE_MACHINE_TEMPLATE_CAPTURES_CROSSINLINE }.apply { + annotations += view.annotations.map { it.deepCopyWithSymbols(this) } copyTypeParameters(view.typeParameters) dispatchReceiverParameter = view.dispatchReceiverParameter?.copyTo(this) extensionReceiverParameter = view.extensionReceiverParameter?.copyTo(this) diff --git a/compiler/testData/codegen/boxInline/suspend/jvmName.kt b/compiler/testData/codegen/boxInline/suspend/jvmName.kt new file mode 100644 index 00000000000..cc2b0022165 --- /dev/null +++ b/compiler/testData/codegen/boxInline/suspend/jvmName.kt @@ -0,0 +1,36 @@ +// FILE: test.kt +// COMMON_COROUTINES_TEST +// WITH_RUNTIME +// WITH_COROUTINES +// NO_CHECK_LAMBDA_INLINING +// TARGET_BACKEND: JVM + +import COROUTINES_PACKAGE.* +import helpers.* + +class Result(val x: T) + +@JvmName("foo") // + foo$$forInline +suspend inline fun test(c: Result) = c.x + +@JvmName("bar") // + bar$$forInline +suspend inline fun test(c: Result) = c.x + +// FILE: box.kt +// COMMON_COROUTINES_TEST + +import COROUTINES_PACKAGE.* +import COROUTINES_PACKAGE.intrinsics.* +import helpers.* + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun box() : String { + var res = "FAIL" + builder { + res = test(Result("OK")) + } + return res +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java index 4e29a4bb0a2..62e90f40112 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxInlineCodegenTestGenerated.java @@ -3758,6 +3758,16 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo runTestWithPackageReplacement("compiler/testData/codegen/boxInline/suspend/inlineSuspendOfSuspend.kt", "kotlin.coroutines"); } + @TestMetadata("jvmName.kt") + public void testJvmName_1_2() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/boxInline/suspend/jvmName.kt", "kotlin.coroutines.experimental"); + } + + @TestMetadata("jvmName.kt") + public void testJvmName_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/boxInline/suspend/jvmName.kt", "kotlin.coroutines"); + } + @TestMetadata("kt26658.kt") public void testKt26658() throws Exception { runTest("compiler/testData/codegen/boxInline/suspend/kt26658.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java index 212a30a2b4d..016402beb3a 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java @@ -3758,6 +3758,16 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi runTestWithPackageReplacement("compiler/testData/codegen/boxInline/suspend/inlineSuspendOfSuspend.kt", "kotlin.coroutines"); } + @TestMetadata("jvmName.kt") + public void testJvmName_1_2() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/boxInline/suspend/jvmName.kt", "kotlin.coroutines.experimental"); + } + + @TestMetadata("jvmName.kt") + public void testJvmName_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/boxInline/suspend/jvmName.kt", "kotlin.coroutines"); + } + @TestMetadata("kt26658.kt") public void testKt26658() throws Exception { runTest("compiler/testData/codegen/boxInline/suspend/kt26658.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java index d1d9cb3eb66..71a15fdabdf 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxInlineCodegenTestGenerated.java @@ -3698,6 +3698,11 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli runTestWithPackageReplacement("compiler/testData/codegen/boxInline/suspend/inlineSuspendOfSuspend.kt", "kotlin.coroutines"); } + @TestMetadata("jvmName.kt") + public void testJvmName_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/boxInline/suspend/jvmName.kt", "kotlin.coroutines"); + } + @TestMetadata("kt26658.kt") public void testKt26658() throws Exception { runTest("compiler/testData/codegen/boxInline/suspend/kt26658.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java index 32502ed6f2f..98528ce5ba8 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstInlineKotlinTestGenerated.java @@ -3698,6 +3698,11 @@ public class IrCompileKotlinAgainstInlineKotlinTestGenerated extends AbstractIrC runTestWithPackageReplacement("compiler/testData/codegen/boxInline/suspend/inlineSuspendOfSuspend.kt", "kotlin.coroutines"); } + @TestMetadata("jvmName.kt") + public void testJvmName_1_3() throws Exception { + runTestWithPackageReplacement("compiler/testData/codegen/boxInline/suspend/jvmName.kt", "kotlin.coroutines"); + } + @TestMetadata("kt26658.kt") public void testKt26658() throws Exception { runTest("compiler/testData/codegen/boxInline/suspend/kt26658.kt");