Fix wrong state-machine generation if inner object is retransformed

during inlining.
 #KT-29492 Fixed
This commit is contained in:
Ilmir Usmanov
2019-03-12 17:49:31 +03:00
parent 7b97c2a42a
commit 1c2b8e6fad
19 changed files with 481 additions and 55 deletions
@@ -0,0 +1,79 @@
// IGNORE_BACKEND: JVM_IR
// COMMON_COROUTINES_TEST
// WITH_RUNTIME
// WITH_COROUTINES
// CHECK_STATE_MACHINE
// In this test the following transformation are occuring:
// flow$1 -> flowWith$$inlined$flow$1
// flow$1 -> check$$inlined$flow$1
// flow$1 -> flowWith$$inlined$flow$2
// flowWith$$inlined$flow$2 -> check$$inlined$flowWith$1
// All thansformations, except the third, shall generate state-machine.
// The third shall not generate state-machine, since it is retransformed.
// FILE: inline.kt
package flow
interface FlowCollector<T> {
suspend fun emit(value: T)
}
interface Flow<T : Any> {
suspend fun collect(collector: FlowCollector<T>)
}
public inline fun <T : Any> flow(crossinline block: suspend FlowCollector<T>.() -> Unit) = object : Flow<T> {
override suspend fun collect(collector: FlowCollector<T>) {
collector.block()
collector.block()
}
}
suspend inline fun <T : Any> Flow<T>.collect(crossinline action: suspend (T) -> Unit) {
collect(object : FlowCollector<T> {
override suspend fun emit(value: T) {
action(value)
action(value)
}
})
}
inline fun <T : Any, R : Any> Flow<T>.flowWith(crossinline builderBlock: suspend Flow<T>.() -> Flow<R>): Flow<T> =
flow {
builderBlock()
builderBlock()
}
// FILE: box.kt
// COMMON_COROUTINES_TEST
import flow.*
import helpers.*
import COROUTINES_PACKAGE.*
fun builder(c: suspend () -> Unit) {
c.startCoroutine(CheckStateMachineContinuation)
}
suspend fun check() {
val f: Unit = flow<Int> {
emit(1)
}.flowWith {
StateMachineChecker.suspendHere()
StateMachineChecker.suspendHere()
this
}.collect {
// In this test collect is just terminating operation, which just runs the lazy computations
}
}
fun box(): String {
builder {
check()
}
StateMachineChecker.check(numberOfSuspensions = 8)
return "OK"
}
@@ -0,0 +1,48 @@
// IGNORE_BACKEND: JVM_IR
// COMMON_COROUTINES_TEST
// WITH_RUNTIME
// WITH_COROUTINES
// CHECK_STATE_MACHINE
// FILE: inline.kt
import helpers.*
interface SuspendRunnable {
suspend fun run()
}
class R : SuspendRunnable {
override suspend fun run() {
val sr: SuspendRunnable = inlineMe2 {
StateMachineChecker.suspendHere()
StateMachineChecker.suspendHere()
}
sr.run()
}
inline fun inlineMe2(crossinline c: suspend () -> Unit) = object : SuspendRunnable {
override suspend fun run() {
c()
c()
}
}
}
// FILE: box.kt
// COMMON_COROUTINES_TEST
import helpers.*
import COROUTINES_PACKAGE.*
fun builder(c: suspend () -> Unit) {
c.startCoroutine(CheckStateMachineContinuation)
}
fun box(): String {
builder {
R().run()
}
StateMachineChecker.check(numberOfSuspensions = 4)
return "OK"
}
@@ -0,0 +1,44 @@
// IGNORE_BACKEND: JVM_IR
// COMMON_COROUTINES_TEST
// WITH_RUNTIME
// WITH_COROUTINES
// CHECK_STATE_MACHINE
// FILE: inline.kt
interface SuspendRunnable {
suspend fun run()
}
inline fun inlineMe(crossinline c: suspend () -> Unit) = {
{
val sr: SuspendRunnable = object : SuspendRunnable {
override suspend fun run() {
c()
c()
}
}
sr
}()
}()
// FILE: box.kt
// COMMON_COROUTINES_TEST
import helpers.*
import COROUTINES_PACKAGE.*
fun builder(c: suspend () -> Unit) {
c.startCoroutine(CheckStateMachineContinuation)
}
fun box(): String {
builder {
inlineMe {
StateMachineChecker.suspendHere()
StateMachineChecker.suspendHere()
}.run()
}
StateMachineChecker.check(numberOfSuspensions = 4)
return "OK"
}
@@ -0,0 +1,57 @@
// IGNORE_BACKEND: JVM_IR
// COMMON_COROUTINES_TEST
// WITH_RUNTIME
// WITH_COROUTINES
// CHECK_STATE_MACHINE
// FILE: inline.kt
interface SuspendRunnable {
suspend fun run()
suspend fun run2()
}
inline fun inlineMe(crossinline c: suspend () -> Unit, crossinline c2: suspend () -> Unit) =
object : SuspendRunnable {
override suspend fun run() {
c()
c()
}
override suspend fun run2() {
c2()
c2()
}
}
// FILE: box.kt
// COMMON_COROUTINES_TEST
import helpers.*
import COROUTINES_PACKAGE.*
fun builder(c: suspend () -> Unit) {
c.startCoroutine(CheckStateMachineContinuation)
}
fun box(): String {
val r = inlineMe(
{
StateMachineChecker.suspendHere()
StateMachineChecker.suspendHere()
}
) {
StateMachineChecker.suspendHere()
StateMachineChecker.suspendHere()
}
builder {
r.run()
}
StateMachineChecker.check(numberOfSuspensions = 4)
StateMachineChecker.reset()
builder {
r.run2()
}
StateMachineChecker.check(numberOfSuspensions = 4)
return "OK"
}