Box inline class return value in lambdas (including suspend lambdas)

This commit is contained in:
Dmitry Petrov
2020-04-08 12:28:17 +03:00
parent 59b72b3156
commit 9615b20e5d
22 changed files with 691 additions and 18 deletions
@@ -0,0 +1,27 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JS_IR
// WITH_RUNTIME
// WITH_COROUTINES
import helpers.*
import kotlin.coroutines.*
inline class R(val x: Any)
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
suspend fun <T> call(fn: suspend () -> T) = fn()
fun useR(r: R) = if (r.x == "OK") "OK" else "fail: $r"
suspend fun ok() = R("OK")
fun box(): String {
var res: String = "fail"
builder {
res = useR(call(::ok))
}
return res
}
@@ -0,0 +1,25 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JS_IR
// WITH_RUNTIME
// WITH_COROUTINES
import helpers.*
import kotlin.coroutines.*
inline class R(val x: Any)
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
suspend fun <T> call(fn: suspend () -> T) = fn()
fun useR(r: R) = if (r.x == "OK") "OK" else "fail: $r"
fun box(): String {
var res: String = "fail"
builder {
res = useR(call { R("OK") })
}
return res
}
@@ -0,0 +1,10 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JVM_IR
inline class X(val x: Any)
fun useX(x: X): String = x.x as String
fun <T> call(fn: () -> T) = fn()
fun box() = useX(call { X("OK") })
@@ -0,0 +1,10 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JVM_IR
inline class X(val x: Any)
fun useX(x: X): String = x.x as String
fun <T> call(fn: () -> T) = fn()
fun box() = useX(call(fun(): X { return X("OK") }))
@@ -0,0 +1,9 @@
// IGNORE_BACKEND: JVM_IR
inline class X(val x: Int)
fun useX(x: X): String = if (x.x == 42) "OK" else "fail: $x"
fun <T> call(fn: () -> T) = fn()
fun box() = useX(call { X(42) })
@@ -0,0 +1,10 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JVM_IR
inline class X(val x: Any?)
fun useX(x: X): String = x.x as String
fun <T> call(fn: () -> T) = fn()
fun box() = useX(call { X("OK") })
@@ -0,0 +1,10 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JVM_IR
inline class X(val x: Any?)
fun useX(x: X): String = if (x.x == null) "OK" else "fail: $x"
fun <T> call(fn: () -> T) = fn()
fun box() = useX(call { X(null) })
@@ -0,0 +1,9 @@
// IGNORE_BACKEND: JVM_IR
inline class X(val x: Int?)
fun useX(x: X): String = if (x.x == 42) "OK" else "fail: $x"
fun <T> call(fn: () -> T) = fn()
fun box() = useX(call { X(42) })
@@ -0,0 +1,9 @@
// IGNORE_BACKEND: JVM_IR
inline class X(val x: Int?)
fun useX(x: X): String = if (x.x == null) "OK" else "fail: $x"
fun <T> call(fn: () -> T) = fn()
fun box() = useX(call { X(null) })
@@ -0,0 +1,9 @@
// IGNORE_BACKEND: JVM_IR
inline class X(val x: String?)
fun useX(x: X): String = x.x ?: "fail: $x"
fun <T> call(fn: () -> T) = fn()
fun box() = useX(call { X("OK") })
@@ -0,0 +1,9 @@
// IGNORE_BACKEND: JVM_IR
inline class X(val x: String?)
fun useX(x: X): String = if (x.x == null) "OK" else "fail: $x"
fun <T> call(fn: () -> T) = fn()
fun box() = useX(call { X(null) })
@@ -0,0 +1,9 @@
// IGNORE_BACKEND: JVM_IR
inline class X(val x: String)
fun useX(x: X): String = x.x
fun <T> call(fn: () -> T) = fn()
fun box() = useX(call { X("OK") })
@@ -0,0 +1,16 @@
// WITH_RUNTIME
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JVM_IR
fun f1(): () -> Result<String> {
return {
runCatching {
"OK"
}
}
}
fun box(): String {
val r = f1()()
return r.getOrNull() ?: "fail: $r"
}
@@ -0,0 +1,14 @@
// WITH_RUNTIME
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JVM_IR
fun f1() = lazy {
runCatching {
"OK"
}
}
fun box(): String {
val r = f1().value
return r.getOrNull() ?: "fail: $r"
}