From afd710292a873ebbeecf45e535b9a13a827afbb9 Mon Sep 17 00:00:00 2001 From: Mads Ager Date: Thu, 8 Oct 2020 14:29:46 +0200 Subject: [PATCH] [JVM_IR] Fix mangling of default argument stubs for internal methods. The MethodSignatureMapper expected to be able to look at the body of the default argument stub. That is of course not possible when it is from an external dependency. Instead, we go through the attribute owner to get to the method the stub is a default argument adapter for. --- ...mpileKotlinAgainstKotlinTestGenerated.java | 5 ++++ .../jvm/codegen/MethodSignatureMapper.kt | 29 +++++++++++-------- .../internalWithDefaultArgs.kt | 15 ++++++++++ ...mpileKotlinAgainstKotlinTestGenerated.java | 5 ++++ ...mpileKotlinAgainstKotlinTestGenerated.java | 5 ++++ .../ir/JvmIrAgainstOldBoxTestGenerated.java | 5 ++++ .../ir/JvmOldAgainstIrBoxTestGenerated.java | 5 ++++ 7 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 compiler/testData/compileKotlinAgainstKotlin/internalWithDefaultArgs.kt diff --git a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/FirCompileKotlinAgainstKotlinTestGenerated.java b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/FirCompileKotlinAgainstKotlinTestGenerated.java index ff6351c8d34..a70fd85ae8c 100644 --- a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/FirCompileKotlinAgainstKotlinTestGenerated.java +++ b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/FirCompileKotlinAgainstKotlinTestGenerated.java @@ -178,6 +178,11 @@ public class FirCompileKotlinAgainstKotlinTestGenerated extends AbstractFirCompi runTest("compiler/testData/compileKotlinAgainstKotlin/internalSetterOverridden.kt"); } + @TestMetadata("internalWithDefaultArgs.kt") + public void testInternalWithDefaultArgs() throws Exception { + runTest("compiler/testData/compileKotlinAgainstKotlin/internalWithDefaultArgs.kt"); + } + @TestMetadata("internalWithOtherModuleName.kt") public void testInternalWithOtherModuleName() throws Exception { runTest("compiler/testData/compileKotlinAgainstKotlin/internalWithOtherModuleName.kt"); diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/MethodSignatureMapper.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/MethodSignatureMapper.kt index 5e44d7baca2..a9c36b9988b 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/MethodSignatureMapper.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/MethodSignatureMapper.kt @@ -105,12 +105,12 @@ class MethodSignatureMapper(private val context: JvmBackendContext) { private fun mangleMemberNameIfRequired(name: String, function: IrSimpleFunction): String { val newName = JvmCodegenUtil.sanitizeNameIfNeeded(name, context.state.languageVersionSettings) - val suffix = when { - function.isTopLevel -> - if (function.isInvisibleInMultifilePart()) function.parentAsClass.name.asString() else null - function.shouldMangleAsInternal() -> - NameUtils.sanitizeAsJavaIdentifier(getModuleName(function)) - else -> null + val suffix = if (function.isTopLevel) { + if (function.isInvisibleInMultifilePart()) function.parentAsClass.name.asString() else null + } else { + function.getInternalFunctionForManglingIfNeeded()?.let { + NameUtils.sanitizeAsJavaIdentifier(getModuleName(it)) + } } ?: return newName if (function.origin == IrDeclarationOrigin.FUNCTION_FOR_DEFAULT_PARAMETER) { @@ -127,15 +127,20 @@ class MethodSignatureMapper(private val context: JvmBackendContext) { (DescriptorVisibilities.isPrivate(suspendFunctionOriginal().visibility) || originalForDefaultAdapter?.isInvisibleInMultifilePart() == true) - private fun IrSimpleFunction.shouldMangleAsInternal(): Boolean = - (origin != JvmLoweredDeclarationOrigin.STATIC_INLINE_CLASS_CONSTRUCTOR && - visibility == DescriptorVisibilities.INTERNAL && - !isPublishedApi()) - || originalForDefaultAdapter?.shouldMangleAsInternal() == true + private fun IrSimpleFunction.getInternalFunctionForManglingIfNeeded(): IrSimpleFunction? { + if (origin != JvmLoweredDeclarationOrigin.STATIC_INLINE_CLASS_CONSTRUCTOR && + visibility == DescriptorVisibilities.INTERNAL && + !isPublishedApi() + ) { + return this + } + originalForDefaultAdapter?.getInternalFunctionForManglingIfNeeded()?.let { return it } + return null + } private val IrSimpleFunction.originalForDefaultAdapter: IrSimpleFunction? get() = if (origin == IrDeclarationOrigin.FUNCTION_FOR_DEFAULT_PARAMETER) { - ((body?.statements?.lastOrNull() as? IrReturn)?.value as? IrCall)?.symbol?.owner + (attributeOwnerId as IrFunction).symbol.owner as IrSimpleFunction } else null private fun getModuleName(function: IrSimpleFunction): String = diff --git a/compiler/testData/compileKotlinAgainstKotlin/internalWithDefaultArgs.kt b/compiler/testData/compileKotlinAgainstKotlin/internalWithDefaultArgs.kt new file mode 100644 index 00000000000..224667f963f --- /dev/null +++ b/compiler/testData/compileKotlinAgainstKotlin/internalWithDefaultArgs.kt @@ -0,0 +1,15 @@ +// IGNORE_BACKEND_FIR: JVM_IR +// IGNORE_BACKEND: NATIVE +// FILE: A.kt + +package a + +class Box() { + internal fun result(value: String = "OK"): String = value +} + +// FILE: B.kt + +fun box(): String { + return a.Box().result() +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstKotlinTestGenerated.java index e937dc2640c..ca782f86282 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/CompileKotlinAgainstKotlinTestGenerated.java @@ -183,6 +183,11 @@ public class CompileKotlinAgainstKotlinTestGenerated extends AbstractCompileKotl runTest("compiler/testData/compileKotlinAgainstKotlin/internalSetterOverridden.kt"); } + @TestMetadata("internalWithDefaultArgs.kt") + public void testInternalWithDefaultArgs() throws Exception { + runTest("compiler/testData/compileKotlinAgainstKotlin/internalWithDefaultArgs.kt"); + } + @TestMetadata("internalWithOtherModuleName.kt") public void testInternalWithOtherModuleName() throws Exception { runTest("compiler/testData/compileKotlinAgainstKotlin/internalWithOtherModuleName.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstKotlinTestGenerated.java index 8b8fbfc8ce6..934a3e50494 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrCompileKotlinAgainstKotlinTestGenerated.java @@ -178,6 +178,11 @@ public class IrCompileKotlinAgainstKotlinTestGenerated extends AbstractIrCompile runTest("compiler/testData/compileKotlinAgainstKotlin/internalSetterOverridden.kt"); } + @TestMetadata("internalWithDefaultArgs.kt") + public void testInternalWithDefaultArgs() throws Exception { + runTest("compiler/testData/compileKotlinAgainstKotlin/internalWithDefaultArgs.kt"); + } + @TestMetadata("internalWithOtherModuleName.kt") public void testInternalWithOtherModuleName() throws Exception { runTest("compiler/testData/compileKotlinAgainstKotlin/internalWithOtherModuleName.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/JvmIrAgainstOldBoxTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/JvmIrAgainstOldBoxTestGenerated.java index a9883877546..2a67be36af2 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/JvmIrAgainstOldBoxTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/JvmIrAgainstOldBoxTestGenerated.java @@ -178,6 +178,11 @@ public class JvmIrAgainstOldBoxTestGenerated extends AbstractJvmIrAgainstOldBoxT runTest("compiler/testData/compileKotlinAgainstKotlin/internalSetterOverridden.kt"); } + @TestMetadata("internalWithDefaultArgs.kt") + public void testInternalWithDefaultArgs() throws Exception { + runTest("compiler/testData/compileKotlinAgainstKotlin/internalWithDefaultArgs.kt"); + } + @TestMetadata("internalWithOtherModuleName.kt") public void testInternalWithOtherModuleName() throws Exception { runTest("compiler/testData/compileKotlinAgainstKotlin/internalWithOtherModuleName.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/JvmOldAgainstIrBoxTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/JvmOldAgainstIrBoxTestGenerated.java index 501027b170e..5004645338e 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/JvmOldAgainstIrBoxTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/JvmOldAgainstIrBoxTestGenerated.java @@ -178,6 +178,11 @@ public class JvmOldAgainstIrBoxTestGenerated extends AbstractJvmOldAgainstIrBoxT runTest("compiler/testData/compileKotlinAgainstKotlin/internalSetterOverridden.kt"); } + @TestMetadata("internalWithDefaultArgs.kt") + public void testInternalWithDefaultArgs() throws Exception { + runTest("compiler/testData/compileKotlinAgainstKotlin/internalWithDefaultArgs.kt"); + } + @TestMetadata("internalWithOtherModuleName.kt") public void testInternalWithOtherModuleName() throws Exception { runTest("compiler/testData/compileKotlinAgainstKotlin/internalWithOtherModuleName.kt");