[Reflection] Support callBy for inline class interface functions with default parameters

#KT-57972
This commit is contained in:
Evgeniy.Zhelenskiy
2024-01-29 04:04:59 +00:00
committed by Space Team
parent 1e7cc00dcb
commit b0367d9399
18 changed files with 691 additions and 13 deletions
@@ -0,0 +1,60 @@
// TARGET_BACKEND: JVM_IR
// WITH_REFLECT
// LANGUAGE: +ValueClasses
// !JVM_DEFAULT_MODE: disable
import kotlin.test.assertEquals
import kotlin.reflect.full.instanceParameter
interface I1 {
fun f(i1: Int = 1, i2: Int): Int
}
interface I2 {
fun f(i1: Int, i2: Int = 2): Int
}
data class DC(val x: Int, val y: Int) : I1, I2 {
override fun f(i1: Int, i2: Int) = x + y + i1
}
fun dataClass() {
val unbounded = DC::f
assertEquals(111, unbounded.callBy(mapOf(unbounded.instanceParameter!! to DC(10, 100))))
val bounded = DC(10, 100)::f
assertEquals(111, bounded.callBy(mapOf()))
}
@JvmInline
value class VC(val x: Int, val y: Int) : I1, I2 {
override fun f(i1: Int, i2: Int) = x + y + i1
}
fun valueClass() {
val unbounded = VC::f
assertEquals(111, unbounded.callBy(mapOf(unbounded.instanceParameter!! to VC(10, 100))))
val bounded = VC(10, 100)::f
assertEquals(111, bounded.callBy(mapOf()))
}
@JvmInline
value class IC(val x: Int) : I1, I2 {
override fun f(i1: Int, i2: Int) = x + i1
}
fun inlineClass() {
val unbounded = IC::f
assertEquals(11, unbounded.callBy(mapOf(unbounded.instanceParameter!! to IC(10))))
val bounded = IC(10)::f
assertEquals(11, bounded.callBy(mapOf()))
}
fun box(): String {
dataClass()
inlineClass()
valueClass()
return "OK"
}
@@ -0,0 +1,60 @@
// TARGET_BACKEND: JVM_IR
// WITH_REFLECT
// LANGUAGE: +ValueClasses
// !JVM_DEFAULT_MODE: all
import kotlin.test.assertEquals
import kotlin.reflect.full.instanceParameter
interface I1 {
fun f(i1: Int = 1, i2: Int): Int
}
interface I2 {
fun f(i1: Int, i2: Int = 2): Int
}
data class DC(val x: Int, val y: Int) : I1, I2 {
override fun f(i1: Int, i2: Int) = x + y + i1
}
fun dataClass() {
val unbounded = DC::f
assertEquals(111, unbounded.callBy(mapOf(unbounded.instanceParameter!! to DC(10, 100))))
val bounded = DC(10, 100)::f
assertEquals(111, bounded.callBy(mapOf()))
}
@JvmInline
value class VC(val x: Int, val y: Int) : I1, I2 {
override fun f(i1: Int, i2: Int) = x + y + i1
}
fun valueClass() {
val unbounded = VC::f
assertEquals(111, unbounded.callBy(mapOf(unbounded.instanceParameter!! to VC(10, 100))))
val bounded = VC(10, 100)::f
assertEquals(111, bounded.callBy(mapOf()))
}
@JvmInline
value class IC(val x: Int) : I1, I2 {
override fun f(i1: Int, i2: Int) = x + i1
}
fun inlineClass() {
val unbounded = IC::f
assertEquals(11, unbounded.callBy(mapOf(unbounded.instanceParameter!! to IC(10))))
val bounded = IC(10)::f
assertEquals(11, bounded.callBy(mapOf()))
}
fun box(): String {
dataClass()
inlineClass()
valueClass()
return "OK"
}
@@ -0,0 +1,44 @@
// TARGET_BACKEND: JVM_IR
// WITH_REFLECT
// !JVM_DEFAULT_MODE: disable
import kotlin.test.assertEquals
import kotlin.reflect.full.instanceParameter
interface IIC {
fun f(i1: Int = 1): Int
}
inline class IC(val x: Int) : IIC {
override fun f(i1: Int) = x + i1
}
interface Outer {
@JvmInline
value class DefaultImpls(val x: Int) {
fun f(i1: Int = 1) = x + i1
}
}
fun box(): String {
val unbounded1 = IC::f
assertEquals(3, unbounded1.callBy(mapOf(unbounded1.instanceParameter!! to IC(2))))
assertEquals(7, unbounded1.callBy(mapOf(unbounded1.instanceParameter!! to IC(2), unbounded1.parameters[1] to 5)))
val bounded1 = IC(2)::f
assertEquals(3, bounded1.callBy(mapOf()))
assertEquals(7, bounded1.callBy(mapOf(bounded1.parameters.first() to 5)))
val unbounded2 = Outer.DefaultImpls::f
assertEquals(3, unbounded2.callBy(mapOf(unbounded2.instanceParameter!! to Outer.DefaultImpls(2))))
assertEquals(7, unbounded2.callBy(mapOf(unbounded2.instanceParameter!! to Outer.DefaultImpls(2), unbounded2.parameters[1] to 5)))
val bounded2 = Outer.DefaultImpls(2)::f
assertEquals(3, bounded2.callBy(mapOf()))
assertEquals(7, bounded2.callBy(mapOf(bounded2.parameters.first() to 5)))
return "OK"
}
@@ -0,0 +1,45 @@
// TARGET_BACKEND: JVM_IR
// WITH_REFLECT
// !JVM_DEFAULT_MODE: all
// IGNORE_BACKEND: ANDROID
import kotlin.test.assertEquals
import kotlin.reflect.full.instanceParameter
interface IIC {
fun f(i1: Int = 1): Int
}
inline class IC(val x: Int) : IIC {
override fun f(i1: Int) = x + i1
}
interface Outer {
@JvmInline
value class DefaultImpls(val x: Int) {
fun f(i1: Int = 1) = x + i1
}
}
fun box(): String {
val unbounded1 = IC::f
assertEquals(3, unbounded1.callBy(mapOf(unbounded1.instanceParameter!! to IC(2))))
assertEquals(7, unbounded1.callBy(mapOf(unbounded1.instanceParameter!! to IC(2), unbounded1.parameters[1] to 5)))
val bounded1 = IC(2)::f
assertEquals(3, bounded1.callBy(mapOf()))
assertEquals(7, bounded1.callBy(mapOf(bounded1.parameters.first() to 5)))
val unbounded2 = Outer.DefaultImpls::f
assertEquals(3, unbounded2.callBy(mapOf(unbounded2.instanceParameter!! to Outer.DefaultImpls(2))))
assertEquals(7, unbounded2.callBy(mapOf(unbounded2.instanceParameter!! to Outer.DefaultImpls(2), unbounded2.parameters[1] to 5)))
val bounded2 = Outer.DefaultImpls(2)::f
assertEquals(3, bounded2.callBy(mapOf()))
assertEquals(7, bounded2.callBy(mapOf(bounded2.parameters.first() to 5)))
return "OK"
}
@@ -0,0 +1,46 @@
// TARGET_BACKEND: JVM_IR
// WITH_REFLECT
// LANGUAGE: +ValueClasses
// !JVM_DEFAULT_MODE: disable
import kotlin.test.assertEquals
import kotlin.reflect.full.instanceParameter
interface IVC {
fun f(i1: Int = 1): Int
}
@JvmInline
value class VC(val x: Int, val y: Int) : IVC {
override fun f(i1: Int) = x + y + i1
}
interface Outer {
@JvmInline
value class DefaultImpls(val x: Int, val y: Int) {
fun f(i1: Int = 1) = x + y + i1
}
}
fun box(): String {
val unbounded1 = VC::f
assertEquals(8, unbounded1.callBy(mapOf(unbounded1.instanceParameter!! to VC(2, 5))))
assertEquals(12, unbounded1.callBy(mapOf(unbounded1.instanceParameter!! to VC(2, 5), unbounded1.parameters[1] to 5)))
val bounded1 = VC(2, 5)::f
assertEquals(8, bounded1.callBy(mapOf()))
assertEquals(12, bounded1.callBy(mapOf(bounded1.parameters.first() to 5)))
val unbounded2 = Outer.DefaultImpls::f
assertEquals(8, unbounded2.callBy(mapOf(unbounded2.instanceParameter!! to Outer.DefaultImpls(2, 5))))
assertEquals(12, unbounded2.callBy(mapOf(unbounded2.instanceParameter!! to Outer.DefaultImpls(2, 5), unbounded2.parameters[1] to 5)))
val bounded2 = Outer.DefaultImpls(2, 5)::f
assertEquals(8, bounded2.callBy(mapOf()))
assertEquals(12, bounded2.callBy(mapOf(bounded2.parameters.first() to 5)))
return "OK"
}
@@ -0,0 +1,46 @@
// TARGET_BACKEND: JVM_IR
// WITH_REFLECT
// LANGUAGE: +ValueClasses
// !JVM_DEFAULT_MODE: all
import kotlin.test.assertEquals
import kotlin.reflect.full.instanceParameter
interface IVC {
fun f(i1: Int = 1): Int
}
@JvmInline
value class VC(val x: Int, val y: Int) : IVC {
override fun f(i1: Int) = x + y + i1
}
interface Outer {
@JvmInline
value class DefaultImpls(val x: Int, val y: Int) {
fun f(i1: Int = 1) = x + y + i1
}
}
fun box(): String {
val unbounded1 = VC::f
assertEquals(8, unbounded1.callBy(mapOf(unbounded1.instanceParameter!! to VC(2, 5))))
assertEquals(12, unbounded1.callBy(mapOf(unbounded1.instanceParameter!! to VC(2, 5), unbounded1.parameters[1] to 5)))
val bounded1 = VC(2, 5)::f
assertEquals(8, bounded1.callBy(mapOf()))
assertEquals(12, bounded1.callBy(mapOf(bounded1.parameters.first() to 5)))
val unbounded2 = Outer.DefaultImpls::f
assertEquals(8, unbounded2.callBy(mapOf(unbounded2.instanceParameter!! to Outer.DefaultImpls(2, 5))))
assertEquals(12, unbounded2.callBy(mapOf(unbounded2.instanceParameter!! to Outer.DefaultImpls(2, 5), unbounded2.parameters[1] to 5)))
val bounded2 = Outer.DefaultImpls(2, 5)::f
assertEquals(8, bounded2.callBy(mapOf()))
assertEquals(12, bounded2.callBy(mapOf(bounded2.parameters.first() to 5)))
return "OK"
}