diff --git a/compiler/testData/codegen/box/reflection/call/inlineClasses/inlineClassConstructor.kt b/compiler/testData/codegen/box/reflection/call/inlineClasses/inlineClassConstructor.kt new file mode 100644 index 00000000000..ecc39d49f75 --- /dev/null +++ b/compiler/testData/codegen/box/reflection/call/inlineClasses/inlineClassConstructor.kt @@ -0,0 +1,46 @@ +// IGNORE_BACKEND: JS_IR, JS, NATIVE, JVM_IR +// WITH_REFLECT +import kotlin.reflect.KCallable +import kotlin.test.assertEquals + +inline class Z(val x: Int) { + constructor(a: Int, b: Int) : this(a + b) +} + +inline class L(val x: Long) { + constructor(a: Long, b: Long) : this(a + b) +} + +inline class S(val x: String) { + constructor(a: String, b: String) : this(a + b) +} + +inline class A(val x: Any) { + constructor(a: String, b: String) : this(a + b) +} + +inline class Z2(val z: Z) + +fun box(): String { + val ctorZ1: (Int) -> Z = ::Z + val ctorZ2: (Int, Int) -> Z = ::Z + val ctorL1: (Long) -> L = ::L + val ctorL2: (Long, Long) -> L = ::L + val ctorS1: (String) -> S = ::S + val ctorS2: (String, String) -> S = ::S + val ctorA1: (Any) -> A = ::A + val ctorA2: (String, String) -> A = ::A + + assertEquals(Z(42), (ctorZ1 as KCallable).call(42)) + assertEquals(Z(123), (ctorZ2 as KCallable).call(100, 23)) + assertEquals(L(1L), (ctorL1 as KCallable).call(1L)) + assertEquals(L(123L), (ctorL2 as KCallable).call(100L, 23L)) + assertEquals(S("abc"), (ctorS1 as KCallable).call("abc")) + assertEquals(S("abc"), (ctorS2 as KCallable).call("ab", "c")) + assertEquals(A("abc"), (ctorA1 as KCallable).call("abc")) + assertEquals(A("abc"), (ctorA2 as KCallable).call("a", "bc")) + + assertEquals(Z2(Z(42)), ::Z2.call(Z(42))) + + return "OK" +} \ 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 ea00c7c7487..cedb78a1aaf 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -18535,6 +18535,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/functionsAndConstructors.kt"); } + @TestMetadata("inlineClassConstructor.kt") + public void testInlineClassConstructor() throws Exception { + runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/inlineClassConstructor.kt"); + } + @TestMetadata("jvmStaticFieldInObject.kt") public void testJvmStaticFieldInObject() throws Exception { runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/jvmStaticFieldInObject.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 62138ce6307..bfb7be96f47 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -18535,6 +18535,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/functionsAndConstructors.kt"); } + @TestMetadata("inlineClassConstructor.kt") + public void testInlineClassConstructor() throws Exception { + runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/inlineClassConstructor.kt"); + } + @TestMetadata("jvmStaticFieldInObject.kt") public void testJvmStaticFieldInObject() throws Exception { runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/jvmStaticFieldInObject.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index bcfbdf2fe9e..0593bf38216 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -18540,6 +18540,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/functionsAndConstructors.kt"); } + @TestMetadata("inlineClassConstructor.kt") + public void testInlineClassConstructor() throws Exception { + runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/inlineClassConstructor.kt"); + } + @TestMetadata("jvmStaticFieldInObject.kt") public void testJvmStaticFieldInObject() throws Exception { runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/jvmStaticFieldInObject.kt"); diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/RuntimeTypeMapper.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/RuntimeTypeMapper.kt index 6bc472d4d93..5fff48cd4d7 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/RuntimeTypeMapper.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/RuntimeTypeMapper.kt @@ -41,6 +41,7 @@ import org.jetbrains.kotlin.name.NameUtils import org.jetbrains.kotlin.resolve.DescriptorFactory import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.descriptorUtil.propertyIfAccessor +import org.jetbrains.kotlin.resolve.isInlineClass import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor @@ -107,7 +108,7 @@ internal sealed class JvmPropertySignature { } else { val (name, desc) = JvmProtoBufUtil.getJvmFieldSignature(proto, nameResolver, typeTable) - ?: throw KotlinReflectionInternalError("No field signature for property: $descriptor") + ?: throw KotlinReflectionInternalError("No field signature for property: $descriptor") JvmAbi.getterName(name) + getManglingSuffix() + "()" + desc } @@ -116,7 +117,7 @@ internal sealed class JvmPropertySignature { if (descriptor.visibility == Visibilities.INTERNAL && containingDeclaration is DeserializedClassDescriptor) { val classProto = containingDeclaration.classProto val moduleName = classProto.getExtensionOrNull(JvmProtoBuf.classModuleName)?.let(nameResolver::getString) - ?: JvmAbi.DEFAULT_MODULE_NAME + ?: JvmAbi.DEFAULT_MODULE_NAME return "$" + NameUtils.sanitizeAsJavaIdentifier(moduleName) } if (descriptor.visibility == Visibilities.PRIVATE && containingDeclaration is PackageFragmentDescriptor) { @@ -172,14 +173,17 @@ internal object RuntimeTypeMapper { } if (proto is ProtoBuf.Constructor) { JvmProtoBufUtil.getJvmConstructorSignature(proto, function.nameResolver, function.typeTable)?.let { signature -> - return JvmFunctionSignature.KotlinConstructor(signature) + return if (possiblySubstitutedFunction.containingDeclaration.isInlineClass()) + JvmFunctionSignature.KotlinFunction(signature) + else + JvmFunctionSignature.KotlinConstructor(signature) } } return mapJvmFunctionSignature(function) } is JavaMethodDescriptor -> { val method = ((function.source as? JavaSourceElement)?.javaElement as? ReflectJavaMethod)?.member - ?: throw KotlinReflectionInternalError("Incorrect resolution sequence for Java method $function") + ?: throw KotlinReflectionInternalError("Incorrect resolution sequence for Java method $function") return JvmFunctionSignature.JavaMethod(method) } diff --git a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/calls/InlineClassAwareCaller.kt b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/calls/InlineClassAwareCaller.kt index abf1142a0cc..6971c6d54e8 100644 --- a/core/reflection.jvm/src/kotlin/reflect/jvm/internal/calls/InlineClassAwareCaller.kt +++ b/core/reflection.jvm/src/kotlin/reflect/jvm/internal/calls/InlineClassAwareCaller.kt @@ -7,6 +7,7 @@ package kotlin.reflect.jvm.internal.calls import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.ConstructorDescriptor import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor import org.jetbrains.kotlin.load.java.JvmAbi import org.jetbrains.kotlin.resolve.descriptorUtil.classId @@ -69,7 +70,7 @@ internal class InlineClassAwareCaller( val extensionReceiverType = descriptor.extensionReceiverParameter?.type if (extensionReceiverType != null) { kotlinParameterTypes.add(extensionReceiverType) - } else { + } else if (descriptor !is ConstructorDescriptor) { val containingDeclaration = descriptor.containingDeclaration if (containingDeclaration is ClassDescriptor && containingDeclaration.isInline) { kotlinParameterTypes.add(containingDeclaration.defaultType) 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 ba8d220f200..8fdbc0ed0f4 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 @@ -16640,6 +16640,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/functionsAndConstructors.kt"); } + @TestMetadata("inlineClassConstructor.kt") + public void testInlineClassConstructor() throws Exception { + runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/inlineClassConstructor.kt"); + } + @TestMetadata("jvmStaticFieldInObject.kt") public void testJvmStaticFieldInObject() throws Exception { runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/jvmStaticFieldInObject.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 fd6a77fc7fd..a37cc2010b8 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 @@ -17685,6 +17685,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/functionsAndConstructors.kt"); } + @TestMetadata("inlineClassConstructor.kt") + public void testInlineClassConstructor() throws Exception { + runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/inlineClassConstructor.kt"); + } + @TestMetadata("jvmStaticFieldInObject.kt") public void testJvmStaticFieldInObject() throws Exception { runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/jvmStaticFieldInObject.kt");