KT-26765: Support 'call' for constructors with inline class parameters
This commit is contained in:
@@ -34,6 +34,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassNotAny
|
||||
import org.jetbrains.kotlin.resolve.jvm.AsmTypes.*
|
||||
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin
|
||||
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
|
||||
import org.jetbrains.kotlin.resolve.jvm.shouldHideConstructorDueToInlineClassTypeValueParameters
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.typeUtil.builtIns
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions
|
||||
@@ -241,6 +242,11 @@ class PropertyReferenceCodegen(
|
||||
}
|
||||
|
||||
val accessor = when (callable) {
|
||||
is ClassConstructorDescriptor ->
|
||||
if (shouldHideConstructorDueToInlineClassTypeValueParameters(callable))
|
||||
AccessorForConstructorDescriptor(callable, callable.containingDeclaration, null, AccessorKind.NORMAL)
|
||||
else
|
||||
callable
|
||||
is FunctionDescriptor -> callable
|
||||
is VariableDescriptorWithAccessors ->
|
||||
callable.getter ?: DescriptorFactory.createDefaultGetter(callable as PropertyDescriptor, Annotations.EMPTY).apply {
|
||||
|
||||
Vendored
+27
@@ -0,0 +1,27 @@
|
||||
// IGNORE_BACKEND: JS_IR, JS, NATIVE, JVM_IR
|
||||
// WITH_REFLECT
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
inline class Z(val x: Int)
|
||||
|
||||
class Outer(val z1: Z) {
|
||||
inner class Inner(val z2: Z) {
|
||||
val test = "$z1 $z2"
|
||||
}
|
||||
}
|
||||
|
||||
inline class InlineOuter(val z1: Z) {
|
||||
inner class Inner(val z2: Z) {
|
||||
val test = "$z1 $z2"
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(Z(1), ::Outer.call(Z(1)).z1)
|
||||
assertEquals("Z(x=1) Z(x=2)", Outer::Inner.call(Outer(Z(1)), Z(2)).test)
|
||||
assertEquals("Z(x=1) Z(x=3)", Outer(Z(1))::Inner.call(Z(3)).test)
|
||||
assertEquals("Z(x=1) Z(x=2)", InlineOuter::Inner.call(InlineOuter(Z(1)), Z(2)).test)
|
||||
assertEquals("Z(x=1) Z(x=3)", InlineOuter(Z(1))::Inner.call(Z(3)).test)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
-10
@@ -13,14 +13,6 @@ class C {
|
||||
|
||||
fun topLevel(x: String, y: S): S = S(x) + y
|
||||
|
||||
/* TODO: support constructors with inline class types in the signature (KT-26765)
|
||||
class D {
|
||||
inner class Inner(x: S, y: S) {
|
||||
val result = x + y
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
fun S.extension(y: S): S = this + y
|
||||
|
||||
fun S.extension2(): String = value
|
||||
@@ -28,12 +20,10 @@ fun S.extension2(): String = value
|
||||
fun box(): String {
|
||||
assertEquals(S("ab"), C::member.call(C(), S("a"), "b"))
|
||||
assertEquals(S("cd"), ::topLevel.call("c", S("d")))
|
||||
// assertEquals(S("ef"), D::Inner.call(D(), S("e"), S("f")).result)
|
||||
assertEquals(S("gh"), S::extension.call(S("g"), S("h")))
|
||||
assertEquals("_", S::extension2.call(S("_")))
|
||||
|
||||
assertEquals(S("ij"), C()::member.call(S("i"), "j"))
|
||||
// assertEquals(S("kl"), D()::Inner.call(S("k"), S("l")).result)
|
||||
assertEquals(S("mn"), S("m")::extension.call(S("n")))
|
||||
assertEquals("_", S("_")::extension2.call())
|
||||
|
||||
+8
-3
@@ -19283,14 +19283,19 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/reflection/call/inlineClasses"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
|
||||
}
|
||||
|
||||
@TestMetadata("constructorWithInlineClassParameters.kt")
|
||||
public void testConstructorWithInlineClassParameters() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/constructorWithInlineClassParameters.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldAccessors.kt")
|
||||
public void testFieldAccessors() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/fieldAccessors.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("functionsAndConstructors.kt")
|
||||
public void testFunctionsAndConstructors() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/functionsAndConstructors.kt");
|
||||
@TestMetadata("functionsWithInlineClassParameters.kt")
|
||||
public void testFunctionsWithInlineClassParameters() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/functionsWithInlineClassParameters.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("inlineClassConstructor.kt")
|
||||
|
||||
+8
-3
@@ -19283,14 +19283,19 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/reflection/call/inlineClasses"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true);
|
||||
}
|
||||
|
||||
@TestMetadata("constructorWithInlineClassParameters.kt")
|
||||
public void testConstructorWithInlineClassParameters() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/constructorWithInlineClassParameters.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldAccessors.kt")
|
||||
public void testFieldAccessors() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/fieldAccessors.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("functionsAndConstructors.kt")
|
||||
public void testFunctionsAndConstructors() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/functionsAndConstructors.kt");
|
||||
@TestMetadata("functionsWithInlineClassParameters.kt")
|
||||
public void testFunctionsWithInlineClassParameters() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/functionsWithInlineClassParameters.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("inlineClassConstructor.kt")
|
||||
|
||||
+8
-3
@@ -19288,14 +19288,19 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/reflection/call/inlineClasses"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true);
|
||||
}
|
||||
|
||||
@TestMetadata("constructorWithInlineClassParameters.kt")
|
||||
public void testConstructorWithInlineClassParameters() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/constructorWithInlineClassParameters.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldAccessors.kt")
|
||||
public void testFieldAccessors() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/fieldAccessors.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("functionsAndConstructors.kt")
|
||||
public void testFunctionsAndConstructors() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/functionsAndConstructors.kt");
|
||||
@TestMetadata("functionsWithInlineClassParameters.kt")
|
||||
public void testFunctionsWithInlineClassParameters() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/functionsWithInlineClassParameters.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("inlineClassConstructor.kt")
|
||||
|
||||
@@ -18,6 +18,7 @@ package kotlin.reflect.jvm.internal
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.resolve.jvm.shouldHideConstructorDueToInlineClassTypeValueParameters
|
||||
import java.lang.reflect.Constructor
|
||||
import java.lang.reflect.Member
|
||||
import java.lang.reflect.Method
|
||||
@@ -76,7 +77,7 @@ internal class KFunctionImpl private constructor(
|
||||
|
||||
when (member) {
|
||||
is Constructor<*> ->
|
||||
createConstructorCaller(member)
|
||||
createConstructorCaller(member, descriptor)
|
||||
is Method -> when {
|
||||
!Modifier.isStatic(member.modifiers) ->
|
||||
createInstanceMethodCaller(member)
|
||||
@@ -112,7 +113,7 @@ internal class KFunctionImpl private constructor(
|
||||
|
||||
when (member) {
|
||||
is Constructor<*> ->
|
||||
createConstructorCaller(member)
|
||||
createConstructorCaller(member, descriptor)
|
||||
is Method -> when {
|
||||
// Note that static $default methods for @JvmStatic functions are generated differently in objects and companion objects.
|
||||
// In objects, $default's signature does _not_ contain the additional object instance parameter,
|
||||
@@ -140,8 +141,19 @@ internal class KFunctionImpl private constructor(
|
||||
private fun createInstanceMethodCaller(member: Method) =
|
||||
if (isBound) CallerImpl.Method.BoundInstance(member, boundReceiver) else CallerImpl.Method.Instance(member)
|
||||
|
||||
private fun createConstructorCaller(member: Constructor<*>) =
|
||||
if (isBound) CallerImpl.BoundConstructor(member, boundReceiver) else CallerImpl.Constructor(member)
|
||||
private fun createConstructorCaller(member: Constructor<*>, descriptor: FunctionDescriptor): CallerImpl<Constructor<*>> {
|
||||
return if (shouldHideConstructorDueToInlineClassTypeValueParameters(descriptor)) {
|
||||
if (isBound)
|
||||
CallerImpl.AccessorForHiddenBoundConstructor(member, boundReceiver)
|
||||
else
|
||||
CallerImpl.AccessorForHiddenConstructor(member)
|
||||
} else {
|
||||
if (isBound)
|
||||
CallerImpl.BoundConstructor(member, boundReceiver)
|
||||
else
|
||||
CallerImpl.Constructor(member)
|
||||
}
|
||||
}
|
||||
|
||||
override val arity: Int get() = caller.arity
|
||||
|
||||
|
||||
@@ -55,6 +55,32 @@ internal sealed class CallerImpl<out M : Member>(
|
||||
}
|
||||
}
|
||||
|
||||
class AccessorForHiddenConstructor(
|
||||
constructor: ReflectConstructor<*>
|
||||
) : CallerImpl<ReflectConstructor<*>>(
|
||||
constructor, constructor.declaringClass, null,
|
||||
constructor.genericParameterTypes.dropLast()
|
||||
) {
|
||||
override fun call(args: Array<*>): Any? {
|
||||
checkArguments(args)
|
||||
return member.newInstance(*args, null)
|
||||
}
|
||||
}
|
||||
|
||||
class AccessorForHiddenBoundConstructor(
|
||||
constructor: ReflectConstructor<*>,
|
||||
private val boundReceiver: Any?
|
||||
) : CallerImpl<ReflectConstructor<*>>(
|
||||
constructor, constructor.declaringClass,
|
||||
null,
|
||||
constructor.genericParameterTypes.dropFirstAndLast()
|
||||
), BoundCaller {
|
||||
override fun call(args: Array<*>): Any? {
|
||||
checkArguments(args)
|
||||
return member.newInstance(boundReceiver, *args, null)
|
||||
}
|
||||
}
|
||||
|
||||
sealed class Method(
|
||||
method: ReflectMethod,
|
||||
requiresInstance: Boolean = !Modifier.isStatic(method.modifiers),
|
||||
@@ -211,5 +237,13 @@ internal sealed class CallerImpl<out M : Member>(
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
inline fun <reified T> Array<out T>.dropFirst(): Array<T> =
|
||||
if (size <= 1) emptyArray() else copyOfRange(1, size) as Array<T>
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
inline fun <reified T> Array<out T>.dropLast(): Array<T> =
|
||||
if (size <= 1) emptyArray() else copyOfRange(0, size - 1) as Array<T>
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
inline fun <reified T> Array<out T>.dropFirstAndLast(): Array<T> =
|
||||
if (size <= 2) emptyArray() else copyOfRange(1, size - 1) as Array<T>
|
||||
}
|
||||
}
|
||||
|
||||
+22
-6
@@ -52,6 +52,9 @@ internal class InlineClassAwareCaller<out M : Member>(
|
||||
-1
|
||||
}
|
||||
|
||||
descriptor is ConstructorDescriptor ->
|
||||
if (caller is BoundCaller) -1 else 0
|
||||
|
||||
descriptor.dispatchReceiverParameter != null && caller !is BoundCaller -> {
|
||||
// If we have an unbound reference to the inline class member,
|
||||
// its receiver (which is passed as argument 0) should also be unboxed.
|
||||
@@ -70,7 +73,12 @@ internal class InlineClassAwareCaller<out M : Member>(
|
||||
val extensionReceiverType = descriptor.extensionReceiverParameter?.type
|
||||
if (extensionReceiverType != null) {
|
||||
kotlinParameterTypes.add(extensionReceiverType)
|
||||
} else if (descriptor !is ConstructorDescriptor) {
|
||||
} else if (descriptor is ConstructorDescriptor) {
|
||||
val constructedClass = descriptor.constructedClass
|
||||
if (constructedClass.isInner) {
|
||||
kotlinParameterTypes.add((constructedClass.containingDeclaration as ClassDescriptor).defaultType)
|
||||
}
|
||||
} else {
|
||||
val containingDeclaration = descriptor.containingDeclaration
|
||||
if (containingDeclaration is ClassDescriptor && containingDeclaration.isInline) {
|
||||
kotlinParameterTypes.add(containingDeclaration.defaultType)
|
||||
@@ -162,13 +170,21 @@ internal fun KotlinType.toInlineClass(): Class<*>? {
|
||||
return null
|
||||
}
|
||||
|
||||
private val CallableMemberDescriptor.expectedReceiverType
|
||||
get() =
|
||||
extensionReceiverParameter?.type
|
||||
?: (containingDeclaration as? ClassDescriptor)?.defaultType.takeIf { dispatchReceiverParameter != null }
|
||||
private val CallableMemberDescriptor.expectedReceiverType: KotlinType?
|
||||
get() {
|
||||
val extensionReceiver = extensionReceiverParameter
|
||||
val dispatchReceiver = dispatchReceiverParameter
|
||||
return when {
|
||||
extensionReceiver != null -> extensionReceiver.type
|
||||
dispatchReceiver == null -> null
|
||||
this is ConstructorDescriptor -> dispatchReceiver.type
|
||||
else -> (containingDeclaration as? ClassDescriptor)?.defaultType
|
||||
}
|
||||
}
|
||||
|
||||
internal fun Any?.coerceToExpectedReceiverType(descriptor: CallableMemberDescriptor): Any? {
|
||||
val unboxMethod = descriptor.expectedReceiverType?.toInlineClass()?.getUnboxMethod(descriptor) ?: return this
|
||||
val expectedReceiverType = descriptor.expectedReceiverType
|
||||
val unboxMethod = expectedReceiverType?.toInlineClass()?.getUnboxMethod(descriptor) ?: return this
|
||||
|
||||
return unboxMethod.invoke(this)
|
||||
}
|
||||
+8
-3
@@ -16813,14 +16813,19 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/reflection/call/inlineClasses"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS_IR, true);
|
||||
}
|
||||
|
||||
@TestMetadata("constructorWithInlineClassParameters.kt")
|
||||
public void testConstructorWithInlineClassParameters() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/constructorWithInlineClassParameters.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldAccessors.kt")
|
||||
public void testFieldAccessors() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/fieldAccessors.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("functionsAndConstructors.kt")
|
||||
public void testFunctionsAndConstructors() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/functionsAndConstructors.kt");
|
||||
@TestMetadata("functionsWithInlineClassParameters.kt")
|
||||
public void testFunctionsWithInlineClassParameters() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/functionsWithInlineClassParameters.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("inlineClassConstructor.kt")
|
||||
|
||||
+8
-3
@@ -17858,14 +17858,19 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/reflection/call/inlineClasses"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true);
|
||||
}
|
||||
|
||||
@TestMetadata("constructorWithInlineClassParameters.kt")
|
||||
public void testConstructorWithInlineClassParameters() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/constructorWithInlineClassParameters.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("fieldAccessors.kt")
|
||||
public void testFieldAccessors() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/fieldAccessors.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("functionsAndConstructors.kt")
|
||||
public void testFunctionsAndConstructors() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/functionsAndConstructors.kt");
|
||||
@TestMetadata("functionsWithInlineClassParameters.kt")
|
||||
public void testFunctionsWithInlineClassParameters() throws Exception {
|
||||
runTest("compiler/testData/codegen/box/reflection/call/inlineClasses/functionsWithInlineClassParameters.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("inlineClassConstructor.kt")
|
||||
|
||||
Reference in New Issue
Block a user