KT-27524 Don't box (some) inline classes in suspend fun return

If an inline class is mapped to a reference type (or an array), it's Ok
to treat JVM view on a suspend function as returning a value of
corresponding inline class (although in reality it returns 'Any?'
because of COROUTINE_SUSPENDED).
This commit is contained in:
Dmitry Petrov
2020-03-26 20:37:29 +03:00
parent d8bc29e6c6
commit 042424d599
22 changed files with 1220 additions and 7 deletions

View File

@@ -0,0 +1,20 @@
// IGNORE_BACKEND: JVM_IR
inline class ICInt(val x: Int) // unbox-impl in generated 'equals'
suspend fun suspendICInt(): ICInt = ICInt(1) // box-impl
suspend fun suspendAny(): Any = ICInt(1) // box-impl
suspend fun <T> suspendGeneric(x: T): T = x
fun useICInt(x: ICInt) {}
fun useAny(x: Any) {}
suspend fun test() {
useICInt(suspendICInt()) // unbox-impl
useICInt(suspendGeneric(ICInt(1))) // box-impl, unbox-impl
useAny(suspendAny())
useAny(suspendICInt())
}
// 3 INVOKESTATIC ICInt\.box-impl
// 3 INVOKEVIRTUAL ICInt\.unbox-impl

View File

@@ -0,0 +1,26 @@
// IGNORE_BACKEND: JVM_IR
inline class ICAny(val x: Any)
suspend fun suspendICAny(): ICAny = ICAny("")
suspend fun suspendAny(): Any = ICAny("")
suspend fun <T> suspendGeneric(x: T): T = x
fun useICAny(x: ICAny) {}
fun useAny(x: Any) {}
suspend fun test() {
useICAny(suspendICAny())
useICAny(suspendGeneric(ICAny("")))
useAny(suspendAny())
useAny(suspendICAny())
}
// -- 1 in 'suspendAny(): Any = ICAny("")'
// -- 1 in 'useAny(suspendICAny())'
// -- 1 in 'suspendGeneric(ICAny(""))'
// 3 INVOKESTATIC ICAny\.box-impl
// -- 1 in 'useICAny(suspendGeneric(ICAny("")))
// -- 1 in 'equals-impl' for ICAny
// 2 INVOKEVIRTUAL ICAny\.unbox-impl

View File

@@ -0,0 +1,24 @@
// IGNORE_BACKEND: JVM_IR
inline class IC0(val x: Any) // IC0.unbox-impl in generated 'equals'
inline class IC1(val x: IC0) // IC1.unbox-impl in generated 'equals'
suspend fun suspendIC(): IC1 = IC1(IC0(""))
suspend fun suspendAny(): Any = IC1(IC0("")) // IC1.box-impl
suspend fun <T> suspendGeneric(x: T): T = x
fun useIC(x: IC1) {}
fun useAny(x: Any) {}
suspend fun test() {
useIC(suspendIC())
useIC(suspendGeneric(IC1(IC0("")))) // IC1.box-impl, IC1.unbox-impl
useAny(suspendAny())
useAny(suspendIC()) // IC1.box-impl
}
// 0 INVOKESTATIC IC0\.box-impl
// 3 INVOKESTATIC IC1\.box-impl
// 1 INVOKEVIRTUAL IC0\.unbox-impl
// 2 INVOKEVIRTUAL IC1\.unbox-impl