Files
Alexander Korepanov 57f16e801f [JS IR] Materialize Unit more aggressively for IrCalls
Unit materialization for IrCall is required for operations with dynamic type.

However it produces useless Unit_getInstance() calls,
especially in return expressions. The pach also adds some heuristics
for reducing the amount of Unit_getInstance() calls from return expressions:
do not add Unit_getInstance() if return value has Unit type.

Relaited to KT-51139
^KT-23252 Fixed
2022-05-04 13:14:06 +00:00

52 lines
1.1 KiB
Kotlin
Vendored

// DONT_TARGET_EXACT_BACKEND: JS
// KJS_WITH_FULL_RUNTIME
fun demo(): Unit {}
fun testFunctionUsage() {
val u1 = demo()
val d1: dynamic = u1
assertTrue(d1 == u1)
assertEquals(u1.toString(), "kotlin.Unit")
}
fun testFunctionCall() {
fun test(u1: Unit): Boolean {
val d1: dynamic = u1
assertEquals(u1.toString(), "kotlin.Unit")
return d1 == u1
}
assertTrue(test(demo()))
}
fun testObjectField() {
val o = object { val u1 = demo() }
val d1: dynamic = o.u1
assertTrue(d1 == o.u1)
assertEquals(o.u1.toString(), "kotlin.Unit")
}
fun testClassField() {
class Test(val u1: Unit)
val o = Test(demo())
val d1: dynamic = o.u1
assertTrue(d1 == o.u1)
assertEquals(o.u1.toString(), "kotlin.Unit")
}
fun testContainer() {
val l = listOf<Unit>(demo())
val d1: dynamic = l.last()
assertTrue(d1 == l.last())
assertEquals(l.last().toString(), "kotlin.Unit")
}
fun box(): String {
testFunctionUsage()
testFunctionCall()
testObjectField()
testClassField()
testContainer()
return "OK"
}