diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/inlineclasses/InlineClassAbi.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/inlineclasses/InlineClassAbi.kt index 83d283dbe96..20f22382cfd 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/inlineclasses/InlineClassAbi.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/inlineclasses/InlineClassAbi.kt @@ -11,7 +11,9 @@ import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.load.java.JvmAbi +import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.resolve.DescriptorUtils /** * Replace inline classes by their underlying types. @@ -52,6 +54,13 @@ object InlineClassAbi { * to avoid clashes between overloaded methods. */ fun mangledNameFor(irFunction: IrFunction): Name { + val suffix = when { + irFunction.fullValueParameterList.any { it.type.requiresMangling } -> + hashSuffix(irFunction) + (irFunction.parent as? IrClass)?.isInline == true -> "impl" + else -> return irFunction.name + } + val base = when { irFunction is IrConstructor -> "constructor" @@ -65,13 +74,6 @@ object InlineClassAbi { irFunction.name.asString() } - val suffix = when { - irFunction.fullValueParameterList.any { it.type.erasedUpperBound.isInline } -> - hashSuffix(irFunction) - (irFunction.parent as? IrClass)?.isInline == true -> "impl" - else -> return irFunction.name - } - return Name.identifier("$base-$suffix") } @@ -89,6 +91,12 @@ object InlineClassAbi { } } +internal val IrType.requiresMangling: Boolean + get() { + val irClass = erasedUpperBound + return irClass.isInline && irClass.fqNameWhenAvailable != DescriptorUtils.RESULT_FQ_NAME + } + internal val IrFunction.fullValueParameterList: List get() = listOfNotNull(extensionReceiverParameter) + valueParameters diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/inlineclasses/MemoizedInlineClassReplacements.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/inlineclasses/MemoizedInlineClassReplacements.kt index 7d15e0d70f1..0bb714740ba 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/inlineclasses/MemoizedInlineClassReplacements.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/inlineclasses/MemoizedInlineClassReplacements.kt @@ -46,18 +46,17 @@ class MemoizedInlineClassReplacements { val getReplacementFunction: (IrFunction) -> IrReplacementFunction? = storageManager.createMemoizedFunctionWithNullableValues { when { - !it.hasInlineClassParameters || it.isSyntheticInlineClassMember || it.origin == IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA -> null + !it.hasMangledParameters || it.isSyntheticInlineClassMember || it.origin == IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA -> null it.hasMethodReplacement -> createMethodReplacement(it) it.hasStaticReplacement -> createStaticReplacement(it) else -> null } } - private val IrFunction.hasInlineClassParameters: Boolean - get() = explicitParameters.any { it.type.erasedUpperBound.isInline && !it.type.isDontMangleType() } - || (this is IrConstructor && constructedClass.isInline) - - private fun IrType.isDontMangleType() = getClass()?.fqNameWhenAvailable == DescriptorUtils.RESULT_FQ_NAME + private val IrFunction.hasMangledParameters: Boolean + get() = dispatchReceiverParameter?.type?.getClass()?.isInline == true || + fullValueParameterList.any { it.type.requiresMangling } || + (this is IrConstructor && constructedClass.isInline) private val IrFunction.hasStaticReplacement: Boolean get() = origin != IrDeclarationOrigin.FAKE_OVERRIDE && diff --git a/compiler/testData/codegen/box/coroutines/beginWithException.kt b/compiler/testData/codegen/box/coroutines/beginWithException.kt index 5c085b6ed0f..d259f4b6e45 100644 --- a/compiler/testData/codegen/box/coroutines/beginWithException.kt +++ b/compiler/testData/codegen/box/coroutines/beginWithException.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR // WITH_RUNTIME // WITH_COROUTINES // COMMON_COROUTINES_TEST diff --git a/compiler/testData/codegen/box/coroutines/createCoroutinesOnManualInstances.kt b/compiler/testData/codegen/box/coroutines/createCoroutinesOnManualInstances.kt index 232c322dd04..def0dc67381 100644 --- a/compiler/testData/codegen/box/coroutines/createCoroutinesOnManualInstances.kt +++ b/compiler/testData/codegen/box/coroutines/createCoroutinesOnManualInstances.kt @@ -1,5 +1,4 @@ // IGNORE_BACKEND: JS_IR -// IGNORE_BACKEND: JVM_IR // WITH_RUNTIME // WITH_COROUTINES import helpers.ContinuationAdapter diff --git a/compiler/testData/codegen/box/coroutines/intrinsicSemantics/startCoroutine.kt b/compiler/testData/codegen/box/coroutines/intrinsicSemantics/startCoroutine.kt index 9a9f375f437..65a477c0cc5 100644 --- a/compiler/testData/codegen/box/coroutines/intrinsicSemantics/startCoroutine.kt +++ b/compiler/testData/codegen/box/coroutines/intrinsicSemantics/startCoroutine.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR // WITH_RUNTIME // WITH_COROUTINES // COMMON_COROUTINES_TEST diff --git a/compiler/testData/codegen/box/coroutines/localFunctions/named/stateMachine.kt b/compiler/testData/codegen/box/coroutines/localFunctions/named/stateMachine.kt index 8ab99d236a4..a65c502cb60 100644 --- a/compiler/testData/codegen/box/coroutines/localFunctions/named/stateMachine.kt +++ b/compiler/testData/codegen/box/coroutines/localFunctions/named/stateMachine.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR // WITH_RUNTIME // WITH_COROUTINES // COMMON_COROUTINES_TEST diff --git a/compiler/testData/codegen/box/coroutines/simpleWithHandleResult.kt b/compiler/testData/codegen/box/coroutines/simpleWithHandleResult.kt index 11ad9308f24..25ca2d4562c 100644 --- a/compiler/testData/codegen/box/coroutines/simpleWithHandleResult.kt +++ b/compiler/testData/codegen/box/coroutines/simpleWithHandleResult.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR // WITH_RUNTIME // WITH_COROUTINES // COMMON_COROUTINES_TEST diff --git a/compiler/testData/codegen/box/coroutines/unitTypeReturn/coroutineReturn.kt b/compiler/testData/codegen/box/coroutines/unitTypeReturn/coroutineReturn.kt index ca5c26d8532..e444505b7b1 100644 --- a/compiler/testData/codegen/box/coroutines/unitTypeReturn/coroutineReturn.kt +++ b/compiler/testData/codegen/box/coroutines/unitTypeReturn/coroutineReturn.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR // WITH_RUNTIME // WITH_COROUTINES // COMMON_COROUTINES_TEST diff --git a/compiler/testData/codegen/box/inlineClasses/resultInlining.kt b/compiler/testData/codegen/box/inlineClasses/resultInlining.kt new file mode 100644 index 00000000000..aeaf19a1b85 --- /dev/null +++ b/compiler/testData/codegen/box/inlineClasses/resultInlining.kt @@ -0,0 +1,6 @@ +// WITH_RUNTIME + +fun box(): String { + val ok = Result.success("OK") + return ok.getOrNull()!! +} diff --git a/compiler/testData/codegen/bytecodeText/inlineClasses/resultMangling.kt b/compiler/testData/codegen/bytecodeText/inlineClasses/resultMangling.kt new file mode 100644 index 00000000000..556896bbbe9 --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/inlineClasses/resultMangling.kt @@ -0,0 +1,14 @@ +// !LANGUAGE: +InlineClasses +// WITH_RUNTIME +// FILE: test.kt +inline class A(val s: String) { + fun fromResult(x: Result) = + x.getOrNull() ?: s +} + +fun box(): String { + return A("Fail").fromResult(Result.success("OK")) +} + +// @TestKt.class: +// 1 INVOKESTATIC A.fromResult \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 1e8b6772dfc..110adcc0e24 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -12615,6 +12615,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/inlineClasses/referToUnderlyingPropertyOfInlineClass.kt"); } + @TestMetadata("resultInlining.kt") + public void testResultInlining() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/resultInlining.kt"); + } + @TestMetadata("secondaryConstructorWithVararg.kt") public void testSecondaryConstructorWithVararg() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/secondaryConstructorWithVararg.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index 040027605fb..1eb67448762 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -2525,6 +2525,11 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest { runTest("compiler/testData/codegen/bytecodeText/inlineClasses/resultApiDoesntUseBox.kt"); } + @TestMetadata("resultMangling.kt") + public void testResultMangling() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/inlineClasses/resultMangling.kt"); + } + @TestMetadata("skipCallToUnderlyingValueOfInlineClass.kt") public void testSkipCallToUnderlyingValueOfInlineClass() throws Exception { runTest("compiler/testData/codegen/bytecodeText/inlineClasses/skipCallToUnderlyingValueOfInlineClass.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index fd348b896c2..b1d872d32e2 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -12620,6 +12620,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/inlineClasses/referToUnderlyingPropertyOfInlineClass.kt"); } + @TestMetadata("resultInlining.kt") + public void testResultInlining() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/resultInlining.kt"); + } + @TestMetadata("secondaryConstructorWithVararg.kt") public void testSecondaryConstructorWithVararg() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/secondaryConstructorWithVararg.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 5ba2c17ea71..d93c0f477a0 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -11505,6 +11505,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/inlineClasses/referToUnderlyingPropertyOfInlineClass.kt"); } + @TestMetadata("resultInlining.kt") + public void testResultInlining() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/resultInlining.kt"); + } + @TestMetadata("secondaryConstructorWithVararg.kt") public void testSecondaryConstructorWithVararg() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/secondaryConstructorWithVararg.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java index 8d924eb9676..beab979a176 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java @@ -2480,6 +2480,11 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest { runTest("compiler/testData/codegen/bytecodeText/inlineClasses/resultApiDoesntUseBox.kt"); } + @TestMetadata("resultMangling.kt") + public void testResultMangling() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/inlineClasses/resultMangling.kt"); + } + @TestMetadata("skipCallToUnderlyingValueOfInlineClass.kt") public void testSkipCallToUnderlyingValueOfInlineClass() throws Exception { runTest("compiler/testData/codegen/bytecodeText/inlineClasses/skipCallToUnderlyingValueOfInlineClass.kt"); 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 8fd302a195e..5fb6c4a31c7 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 @@ -9960,6 +9960,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/inlineClasses/referToUnderlyingPropertyOfInlineClass.kt"); } + @TestMetadata("resultInlining.kt") + public void testResultInlining() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/resultInlining.kt"); + } + @TestMetadata("secondaryConstructorWithVararg.kt") public void testSecondaryConstructorWithVararg() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/secondaryConstructorWithVararg.kt"); 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 04dad9de18f..e5dd77fab3b 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 @@ -11115,6 +11115,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/inlineClasses/referToUnderlyingPropertyOfInlineClass.kt"); } + @TestMetadata("resultInlining.kt") + public void testResultInlining() throws Exception { + runTest("compiler/testData/codegen/box/inlineClasses/resultInlining.kt"); + } + @TestMetadata("secondaryConstructorWithVararg.kt") public void testSecondaryConstructorWithVararg() throws Exception { runTest("compiler/testData/codegen/box/inlineClasses/secondaryConstructorWithVararg.kt");