KT-38337 Fix inline class value coercion in delegated members

This commit is contained in:
Dmitry Petrov
2020-04-20 18:25:25 +03:00
parent 29f19c78f2
commit 318d4d17ba
15 changed files with 586 additions and 36 deletions
@@ -0,0 +1,18 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND_FIR: JVM_IR
interface IFoo {
fun getO(): String
val k: String
val ok: String get() = getO() + k
}
inline class InlineFooImpl(val s: String): IFoo {
override fun getO(): String = s
override val k: String get() = "K"
}
class Test(s: String) : IFoo by InlineFooImpl(s)
fun box() = Test("O").ok
@@ -0,0 +1,13 @@
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
// IGNORE_BACKEND_FIR: JVM_IR
inline class Wrapper(val id: Int)
class DMap(private val map: Map<Wrapper, String>) :
Map<Wrapper, String> by map
fun box(): String {
val dmap = DMap(mutableMapOf(Wrapper(42) to "OK"))
return dmap[Wrapper(42)] ?: "Fail"
}
@@ -0,0 +1,40 @@
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
// IGNORE_BACKEND_FIR: JVM_IR
import kotlin.test.assertEquals
inline class S(val x: String)
interface IFoo {
val S.extVal: String
}
interface GFoo<T> {
val T.extVal: String
}
object FooImpl : IFoo {
override val S.extVal: String
get() = x
}
object GFooImpl : GFoo<S> {
override val S.extVal: String
get() = x
}
class TestFoo : IFoo by FooImpl
class TestGFoo : GFoo<S> by GFooImpl
fun box(): String {
with(TestFoo()) {
assertEquals("OK", S("OK").extVal)
}
with(TestGFoo()) {
assertEquals("OK", S("OK").extVal)
}
return "OK"
}
@@ -0,0 +1,47 @@
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
// IGNORE_BACKEND_FIR: JVM_IR
import kotlin.test.assertEquals
inline class S(val xs: Array<String>)
interface IFoo {
var S.extVar: String
}
interface GFoo<T> {
var T.extVar: String
}
object FooImpl : IFoo {
override var S.extVar: String
get() = xs[0]
set(value) { xs[0] = value }
}
object GFooImpl : GFoo<S> {
override var S.extVar: String
get() = xs[0]
set(value) { xs[0] = value }
}
class TestFoo : IFoo by FooImpl
class TestGFoo : GFoo<S> by GFooImpl
fun box(): String {
with(TestFoo()) {
val s = S(arrayOf("Fail 1"))
s.extVar = "OK"
assertEquals("OK", s.extVar)
}
with(TestGFoo()) {
val s = S(arrayOf("Fail 2"))
s.extVar = "OK"
assertEquals("OK", s.extVar)
}
return "OK"
}
@@ -0,0 +1,20 @@
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
// IGNORE_BACKEND_FIR: JVM_IR
import kotlin.test.assertEquals
interface IFoo {
fun foo(s: String): String
}
inline class Z(val x: Int) : IFoo {
override fun foo(s: String): String = x.toString() + s
}
class Test(x: Int) : IFoo by Z(x)
fun box(): String {
assertEquals("1OK", Test(1).foo("OK"))
return "OK"
}
@@ -0,0 +1,20 @@
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
// IGNORE_BACKEND_FIR: JVM_IR
import kotlin.test.assertEquals
interface IFoo {
fun foo(s: String): String
}
inline class Z(val x: Long) : IFoo {
override fun foo(s: String): String = x.toString() + s
}
class Test(x: Long) : IFoo by Z(x)
fun box(): String {
assertEquals("1OK", Test(1L).foo("OK"))
return "OK"
}
@@ -0,0 +1,42 @@
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
// IGNORE_BACKEND_FIR: JVM_IR
import kotlin.test.assertEquals
inline class S(val x: String)
interface IFoo<T> {
fun memberFun(s1: S, s2: String): String
fun memberFunT(x1: T, x2: String): String
fun <X> genericMemberFun(x1: T, x2: X): String
fun S.memberExtFun(s: String): String
fun T.memberExtFunT(x: String): String
fun <X> T.genericMemberExtFun(x: X): String
}
inline class FooImpl(val xs: Array<String>) : IFoo<S> {
override fun memberFun(s1: S, s2: String): String = xs[0] + s1.x + s2
override fun memberFunT(x1: S, x2: String): String = xs[0] + x1.x + x2
override fun <X> genericMemberFun(x1: S, x2: X): String = xs[0] + x1.x + x2.toString()
override fun S.memberExtFun(s: String): String = xs[0] + this.x + s
override fun S.memberExtFunT(x: String): String = xs[0] + this.x + x
override fun <X> S.genericMemberExtFun(x: X): String = xs[0] + this.x + x.toString()
}
class Test : IFoo<S> by FooImpl(arrayOf("1"))
fun box(): String {
val test = Test()
assertEquals("1OK", test.memberFun(S("O"), "K"))
assertEquals("1OK", test.memberFunT(S("O"), "K"))
assertEquals("1OK", test.genericMemberFun(S("O"), "K"))
with(test) {
assertEquals("1OK", S("O").memberExtFun("K"))
assertEquals("1OK", S("O").memberExtFunT("K"))
assertEquals("1OK", S("O").genericMemberExtFun("K"))
}
return "OK"
}
@@ -0,0 +1,42 @@
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
// IGNORE_BACKEND_FIR: JVM_IR
import kotlin.test.assertEquals
inline class S(val x: String)
interface IFoo<T> {
fun memberFun(s1: S, s2: String): String
fun memberFunT(x1: T, x2: String): String
fun <X> genericMemberFun(x1: T, x2: X): String
fun S.memberExtFun(s: String): String
fun T.memberExtFunT(x: String): String
fun <X> T.genericMemberExtFun(x: X): String
}
object FooImpl : IFoo<S> {
override fun memberFun(s1: S, s2: String): String = s1.x + s2
override fun memberFunT(x1: S, x2: String): String = x1.x + x2
override fun <X> genericMemberFun(x1: S, x2: X): String = x1.x + x2.toString()
override fun S.memberExtFun(s: String): String = this.x + s
override fun S.memberExtFunT(x: String): String = this.x + x
override fun <X> S.genericMemberExtFun(x: X): String = this.x + x.toString()
}
class Test : IFoo<S> by FooImpl
fun box(): String {
val test = Test()
assertEquals("OK", test.memberFun(S("O"), "K"))
assertEquals("OK", test.memberFunT(S("O"), "K"))
assertEquals("OK", test.genericMemberFun(S("O"), "K"))
with(test) {
assertEquals("OK", S("O").memberExtFun("K"))
assertEquals("OK", S("O").memberExtFunT("K"))
assertEquals("OK", S("O").genericMemberExtFun("K"))
}
return "OK"
}