From dad10e94aa10ccee950579a069ea098e515fbd62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Sch=C3=A4fer?= Date: Thu, 7 Jan 2021 16:17:48 +0100 Subject: [PATCH] JVM IR: Mangle names of $$forInline functions --- .../codegen/ir/FirBytecodeTextTestGenerated.java | 5 +++++ .../kotlin/backend/jvm/codegen/CoroutineCodegen.kt | 6 ++++-- .../backend/jvm/lower/AddContinuationLowering.kt | 2 +- .../bytecodeText/coroutines/internalInlineSuspend.kt | 12 ++++++++++++ .../kotlin/codegen/BytecodeTextTestGenerated.java | 5 +++++ .../codegen/ir/IrBytecodeTextTestGenerated.java | 5 +++++ 6 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/codegen/bytecodeText/coroutines/internalInlineSuspend.kt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/codegen/ir/FirBytecodeTextTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/codegen/ir/FirBytecodeTextTestGenerated.java index 1f4d0f08c27..d15c74a552d 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/codegen/ir/FirBytecodeTextTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/codegen/ir/FirBytecodeTextTestGenerated.java @@ -1453,6 +1453,11 @@ public class FirBytecodeTextTestGenerated extends AbstractFirBytecodeTextTest { runTest("compiler/testData/codegen/bytecodeText/coroutines/effectivelyInlineOnly.kt"); } + @TestMetadata("internalInlineSuspend.kt") + public void testInternalInlineSuspend() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/coroutines/internalInlineSuspend.kt"); + } + @TestMetadata("mergeLvt.kt") public void testMergeLvt() throws Exception { runTest("compiler/testData/codegen/bytecodeText/coroutines/mergeLvt.kt"); diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/CoroutineCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/CoroutineCodegen.kt index 4a7e23e4a72..5af003f4f5c 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/CoroutineCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/CoroutineCodegen.kt @@ -98,8 +98,10 @@ internal fun IrFunction.suspendForInlineToOriginal(): IrSimpleFunction? { origin != JvmLoweredDeclarationOrigin.FOR_INLINE_STATE_MACHINE_TEMPLATE_CAPTURES_CROSSINLINE ) return null return parentAsClass.declarations.find { - it is IrSimpleFunction && it.attributeOwnerId == (this as IrSimpleFunction).attributeOwnerId && - it.name.asString() + FOR_INLINE_SUFFIX == name.asString() + // The function may not be named `it.name.asString() + FOR_INLINE_SUFFIX` due to name mangling, + // e.g., for internal declarations. We check for a function with the same `attributeOwnerId` instead. + // This is copied in `AddContinuationLowering`. + it is IrSimpleFunction && it.attributeOwnerId == (this as IrSimpleFunction).attributeOwnerId } as IrSimpleFunction? } 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 d410b75f43b..b0ef3d63134 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 @@ -285,7 +285,7 @@ private class AddContinuationLowering(context: JvmBackendContext) : SuspendLower if (flag.capturesCrossinline || function.isInline) { result += context.irFactory.buildFun { containerSource = view.containerSource - name = Name.identifier(view.name.asString() + FOR_INLINE_SUFFIX) + name = Name.identifier(context.methodSignatureMapper.mapFunctionName(view) + FOR_INLINE_SUFFIX) returnType = view.returnType modality = view.modality isSuspend = view.isSuspend diff --git a/compiler/testData/codegen/bytecodeText/coroutines/internalInlineSuspend.kt b/compiler/testData/codegen/bytecodeText/coroutines/internalInlineSuspend.kt new file mode 100644 index 00000000000..e29e5537329 --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/coroutines/internalInlineSuspend.kt @@ -0,0 +1,12 @@ +class A { + suspend fun f() {} + + internal inline suspend fun g() { + f() + f() + } +} + +// 1 public final g\$test_module\(Lkotlin/coroutines/Continuation;\)Ljava/lang/Object; +// 1 private final g\$test_module\$\$forInline\(Lkotlin/coroutines/Continuation;\)Ljava/lang/Object; +// 0 g\$\$forInline \ No newline at end of file diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index 61a164a3198..6fcef959ef7 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -1443,6 +1443,11 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest { runTest("compiler/testData/codegen/bytecodeText/coroutines/effectivelyInlineOnly.kt"); } + @TestMetadata("internalInlineSuspend.kt") + public void testInternalInlineSuspend() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/coroutines/internalInlineSuspend.kt"); + } + @TestMetadata("mergeLvt.kt") public void testMergeLvt() throws Exception { runTest("compiler/testData/codegen/bytecodeText/coroutines/mergeLvt.kt"); diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java index 6ce2511b1e4..f8976f8f180 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java @@ -1453,6 +1453,11 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest { runTest("compiler/testData/codegen/bytecodeText/coroutines/effectivelyInlineOnly.kt"); } + @TestMetadata("internalInlineSuspend.kt") + public void testInternalInlineSuspend() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/coroutines/internalInlineSuspend.kt"); + } + @TestMetadata("mergeLvt.kt") public void testMergeLvt() throws Exception { runTest("compiler/testData/codegen/bytecodeText/coroutines/mergeLvt.kt");