Fix incorrect coroutines codegen behavior

If all the suspension calls in a suspend function were "hidden"
under the for-convention (iterator/next/hasNext) calls,
control-flow didn't find them, thus supposing that there is no
suspension points and there is no need to generate a coroutine state machine

The solution is to add relevant calls to CFG

 #KT-15824 Fixed
This commit is contained in:
Denis Zharkov
2017-01-20 15:25:21 +03:00
parent 8cbea903f4
commit 02b40326cc
22 changed files with 609 additions and 291 deletions
@@ -0,0 +1,130 @@
// WITH_RUNTIME
// WITH_COROUTINES
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
interface AsyncGenerator<in T> {
suspend fun yield(value: T)
}
interface AsyncSequence<out T> {
operator fun iterator(): AsyncIterator<T>
}
interface AsyncIterator<out T> {
operator suspend fun hasNext(): Boolean
operator suspend fun next(): T
}
fun <T> asyncGenerate(block: suspend AsyncGenerator<T>.() -> Unit): AsyncSequence<T> = object : AsyncSequence<T> {
override fun iterator(): AsyncIterator<T> {
val iterator = AsyncGeneratorIterator<T>()
iterator.nextStep = block.createCoroutine(receiver = iterator, completion = iterator)
return iterator
}
}
class AsyncGeneratorIterator<T>: AsyncIterator<T>, AsyncGenerator<T>, Continuation<Unit> {
var computedNext = false
var nextValue: T? = null
var nextStep: Continuation<Unit>? = null
// if (computesNext) computeContinuation is Continuation<T>
// if (!computesNext) computeContinuation is Continuation<Boolean>
var computesNext = false
var computeContinuation: Continuation<*>? = null
override val context = EmptyCoroutineContext
suspend fun computeHasNext(): Boolean = suspendCoroutineOrReturn { c ->
computesNext = false
computeContinuation = c
nextStep!!.resume(Unit)
SUSPENDED_MARKER
}
suspend fun computeNext(): T = suspendCoroutineOrReturn { c ->
computesNext = true
computeContinuation = c
nextStep!!.resume(Unit)
SUSPENDED_MARKER
}
@Suppress("UNCHECKED_CAST")
fun resumeIterator(exception: Throwable?) {
if (exception != null) {
done()
computeContinuation!!.resumeWithException(exception)
return
}
if (computesNext) {
computedNext = false
(computeContinuation as Continuation<T>).resume(nextValue as T)
} else {
(computeContinuation as Continuation<Boolean>).resume(nextStep != null)
}
}
override suspend fun hasNext(): Boolean {
if (!computedNext) return computeHasNext()
return nextStep != null
}
override suspend fun next(): T {
if (!computedNext) return computeNext()
computedNext = false
return nextValue as T
}
private fun done() {
computedNext = true
nextStep = null
}
// Completion continuation implementation
override fun resume(value: Unit) {
done()
resumeIterator(null)
}
override fun resumeWithException(exception: Throwable) {
done()
resumeIterator(exception)
}
// Generator implementation
override suspend fun yield(value: T): Unit = suspendCoroutineOrReturn { c ->
computedNext = true
nextValue = value
nextStep = c
resumeIterator(null)
SUSPENDED_MARKER
}
}
suspend fun <T> AsyncSequence<T>.toList(): List<T> {
val out = arrayListOf<T>()
for (e in this@toList) out += e // fails at this line
return out
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
fun box(): String {
val seq = asyncGenerate {
yield("O")
yield("K")
}
var res = listOf<String>()
builder {
res = seq.toList()
}
if (res.size > 2) return "fail 1: ${res.size}"
return res[0] + res[1]
}
@@ -0,0 +1,81 @@
@kotlin.Metadata
public interface AsyncGenerator {
public abstract @org.jetbrains.annotations.Nullable method yield(p0: java.lang.Object, @org.jetbrains.annotations.NotNull p1: kotlin.coroutines.Continuation): java.lang.Object
}
@kotlin.Metadata
public final class AsyncGeneratorIterator {
private @org.jetbrains.annotations.Nullable field computeContinuation: kotlin.coroutines.Continuation
private field computedNext: boolean
private field computesNext: boolean
private final @org.jetbrains.annotations.NotNull field context: kotlin.coroutines.EmptyCoroutineContext
private @org.jetbrains.annotations.Nullable field nextStep: kotlin.coroutines.Continuation
private @org.jetbrains.annotations.Nullable field nextValue: java.lang.Object
public method <init>(): void
public final @org.jetbrains.annotations.Nullable method computeHasNext(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object
public final @org.jetbrains.annotations.Nullable method computeNext(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object
private final method done(): void
public final @org.jetbrains.annotations.Nullable method getComputeContinuation(): kotlin.coroutines.Continuation
public final method getComputedNext(): boolean
public final method getComputesNext(): boolean
public synthetic method getContext(): kotlin.coroutines.CoroutineContext
public @org.jetbrains.annotations.NotNull method getContext(): kotlin.coroutines.EmptyCoroutineContext
public final @org.jetbrains.annotations.Nullable method getNextStep(): kotlin.coroutines.Continuation
public final @org.jetbrains.annotations.Nullable method getNextValue(): java.lang.Object
public @org.jetbrains.annotations.Nullable method hasNext(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object
public @org.jetbrains.annotations.Nullable method next(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object
public method resume(@org.jetbrains.annotations.NotNull p0: kotlin.Unit): void
public synthetic method resume(p0: java.lang.Object): void
public final @kotlin.Suppress method resumeIterator(@org.jetbrains.annotations.Nullable p0: java.lang.Throwable): void
public method resumeWithException(@org.jetbrains.annotations.NotNull p0: java.lang.Throwable): void
public final method setComputeContinuation(@org.jetbrains.annotations.Nullable p0: kotlin.coroutines.Continuation): void
public final method setComputedNext(p0: boolean): void
public final method setComputesNext(p0: boolean): void
public final method setNextStep(@org.jetbrains.annotations.Nullable p0: kotlin.coroutines.Continuation): void
public final method setNextValue(@org.jetbrains.annotations.Nullable p0: java.lang.Object): void
public @org.jetbrains.annotations.Nullable method yield(p0: java.lang.Object, @org.jetbrains.annotations.NotNull p1: kotlin.coroutines.Continuation): java.lang.Object
}
@kotlin.Metadata
public interface AsyncIterator {
public abstract @org.jetbrains.annotations.Nullable method hasNext(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object
public abstract @org.jetbrains.annotations.Nullable method next(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.Continuation): java.lang.Object
}
@kotlin.Metadata
public final class AsyncIteratorToListKt {
public final static @org.jetbrains.annotations.NotNull method asyncGenerate(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function2): AsyncSequence
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
public final static method builder(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): void
public final static @org.jetbrains.annotations.Nullable method toList(@org.jetbrains.annotations.NotNull p0: AsyncSequence, @org.jetbrains.annotations.NotNull p1: kotlin.coroutines.Continuation): java.lang.Object
}
@kotlin.Metadata
public interface AsyncSequence {
public abstract @org.jetbrains.annotations.NotNull method iterator(): AsyncIterator
}
@kotlin.Metadata
public final class CoroutineUtilKt {
public final static @org.jetbrains.annotations.NotNull method handleExceptionContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.Continuation
public final static @org.jetbrains.annotations.NotNull method handleResultContinuation(@org.jetbrains.annotations.NotNull p0: kotlin.jvm.functions.Function1): kotlin.coroutines.Continuation
}
@kotlin.Metadata
public class EmptyContinuation {
public final static field Companion: EmptyContinuation.Companion
private final @org.jetbrains.annotations.NotNull field context: kotlin.coroutines.CoroutineContext
inner class EmptyContinuation/Companion
public @synthetic.kotlin.jvm.GeneratedByJvmOverloads method <init>(): void
public method <init>(@org.jetbrains.annotations.NotNull p0: kotlin.coroutines.CoroutineContext): void
public synthetic method <init>(p0: kotlin.coroutines.CoroutineContext, p1: int, p2: kotlin.jvm.internal.DefaultConstructorMarker): void
public @org.jetbrains.annotations.NotNull method getContext(): kotlin.coroutines.CoroutineContext
public method resume(@org.jetbrains.annotations.Nullable p0: java.lang.Object): void
public method resumeWithException(@org.jetbrains.annotations.NotNull p0: java.lang.Throwable): void
}
@kotlin.Metadata
public final static class EmptyContinuation/Companion {
inner class EmptyContinuation/Companion
private method <init>(): void
}