Use proper KotlinType in get/set methods for property reference

This commit is contained in:
Dmitry Petrov
2018-08-20 15:11:33 +03:00
parent 6cd91e43bb
commit 7d4dfc87b1
10 changed files with 201 additions and 2 deletions
@@ -0,0 +1,30 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
// WITH_RUNTIME
inline class Z(val int: Int)
inline class Str(val string: String)
inline class NStr(val string: String?)
fun fooZ(x: Z) = x
fun fooStr(x: Str) = x
fun fooNStr(x: NStr) = x
fun box(): String {
val fnZ = ::fooZ
if (fnZ.invoke(Z(42)).int != 42) throw AssertionError()
val fnStr = ::fooStr
if (fnStr.invoke(Str("str")).string != "str") throw AssertionError()
val fnNStr = ::fooNStr
if (fnNStr.invoke(NStr(null)).string != null) throw AssertionError()
if (fnNStr.invoke(NStr("nstr")).string != "nstr") throw AssertionError()
return "OK"
}
@@ -0,0 +1,12 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
// WITH_RUNTIME
inline class Foo(val z: String)
var f = Foo("zzz")
fun box(): String {
(::f).set(Foo("OK"))
return (::f).get().z
}
+25
View File
@@ -0,0 +1,25 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
// WITH_RUNTIME
import kotlin.reflect.KMutableProperty0
import kotlin.reflect.KProperty
operator fun <R> KMutableProperty0<R>.setValue(host: Any?, property: KProperty<*>, value: R) = set(value)
operator fun <R> KMutableProperty0<R>.getValue(host: Any?, property: KProperty<*>): R = get()
inline class Foo(val i: Int)
var f = Foo(4)
fun modify(ref: KMutableProperty0<Foo>) {
var a by ref
a = Foo(1)
}
fun box(): String {
modify(::f)
if (f.i != 1) throw AssertionError()
return "OK"
}
+31
View File
@@ -0,0 +1,31 @@
// WITH_UNSIGNED
// IGNORE_BACKEND: JVM_IR, JS_IR, JS
import kotlin.reflect.KProperty
import kotlin.reflect.KProperty0
class ByteDelegate(
private val position: Int,
private val uIntValue: KProperty0<UInt>
) {
operator fun getValue(any: Any?, property: KProperty<*>): UByte {
val uInt = uIntValue.get() shr (position * 8) and 0xffu
return uInt.toUByte()
}
}
class ByteDelegateTest {
val uInt = 0xA1B2C3u
val uByte by ByteDelegate(0, this::uInt)
fun test() {
val actual = uByte
if (0xC3u.toUByte() != actual) throw AssertionError()
}
}
fun box(): String {
ByteDelegateTest().test()
return "OK"
}