Fix blackbox codegen tests that use coroutine inference
Note that previously we implicitly ignored JS_IR backend when directive COMMON_COROUTINES_TEST was enabled, see kotlin/generators/util/coroutines.kt#L57
This commit is contained in:
+5
-4
@@ -1,10 +1,11 @@
|
||||
// IGNORE_BACKEND: JVM_IR
|
||||
// !LANGUAGE: -ReleaseCoroutines -ExperimentalBuilderInference
|
||||
// IGNORE_BACKEND: JVM_IR, JS_IR
|
||||
// WITH_RUNTIME
|
||||
// WITH_COROUTINES
|
||||
// COMMON_COROUTINES_TEST
|
||||
|
||||
import helpers.*
|
||||
import COROUTINES_PACKAGE.*
|
||||
import COROUTINES_PACKAGE.intrinsics.*
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
interface AsyncGenerator<in T> {
|
||||
suspend fun yield(value: T)
|
||||
@@ -0,0 +1,141 @@
|
||||
// !LANGUAGE: +ReleaseCoroutines +ExperimentalBuilderInference
|
||||
// IGNORE_BACKEND: JVM_IR
|
||||
// WITH_RUNTIME
|
||||
// WITH_COROUTINES
|
||||
|
||||
import helpers.*
|
||||
import kotlin.coroutines.*
|
||||
import kotlin.coroutines.intrinsics.*
|
||||
import kotlin.experimental.ExperimentalTypeInference
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@UseExperimental(ExperimentalTypeInference::class)
|
||||
fun <T> asyncGenerate(@BuilderInference 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>, ContinuationAdapter<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 = suspendCoroutineUninterceptedOrReturn { c ->
|
||||
computesNext = false
|
||||
computeContinuation = c
|
||||
nextStep!!.resume(Unit)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
suspend fun computeNext(): T = suspendCoroutineUninterceptedOrReturn { c ->
|
||||
computesNext = true
|
||||
computeContinuation = c
|
||||
nextStep!!.resume(Unit)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
@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 = suspendCoroutineUninterceptedOrReturn { c ->
|
||||
computedNext = true
|
||||
nextValue = value
|
||||
nextStep = c
|
||||
resumeIterator(null)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun cst(a: Any?): String? = a as String?
|
||||
fun any(a: Any?): Any? = a
|
||||
|
||||
fun box(): String {
|
||||
val seq = asyncGenerate {
|
||||
yield("O")
|
||||
yield("K")
|
||||
}
|
||||
|
||||
var res = ""
|
||||
|
||||
builder {
|
||||
// type of `prev` should be j/l/Object everywhere (even in a expected type position)
|
||||
var prev: Any? = null
|
||||
for (i in seq) {
|
||||
res += i
|
||||
prev = any(res)
|
||||
// merge of NULL_VALUE and j/l/Object should result in common j/l/Object value
|
||||
// but it was NULL_VALUE and we do not spill null values, we just put
|
||||
// ACONST_NULL after suspension point instead
|
||||
}
|
||||
|
||||
res = cst(prev) ?: "fail 1"
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
+5
-4
@@ -1,10 +1,11 @@
|
||||
// IGNORE_BACKEND: JVM_IR
|
||||
// !LANGUAGE: -ReleaseCoroutines -ExperimentalBuilderInference
|
||||
// IGNORE_BACKEND: JVM_IR, JS_IR
|
||||
// WITH_RUNTIME
|
||||
// WITH_COROUTINES
|
||||
// COMMON_COROUTINES_TEST
|
||||
|
||||
import helpers.*
|
||||
import COROUTINES_PACKAGE.*
|
||||
import COROUTINES_PACKAGE.intrinsics.*
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
interface AsyncGenerator<in T> {
|
||||
suspend fun yield(value: T)
|
||||
@@ -0,0 +1,136 @@
|
||||
// !LANGUAGE: +ReleaseCoroutines +ExperimentalBuilderInference
|
||||
// IGNORE_BACKEND: JVM_IR
|
||||
// WITH_RUNTIME
|
||||
// WITH_COROUTINES
|
||||
|
||||
import helpers.*
|
||||
import kotlin.coroutines.*
|
||||
import kotlin.coroutines.intrinsics.*
|
||||
import kotlin.experimental.ExperimentalTypeInference
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@UseExperimental(ExperimentalTypeInference::class)
|
||||
fun <T> asyncGenerate(@BuilderInference 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>, ContinuationAdapter<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 = suspendCoroutineUninterceptedOrReturn { c ->
|
||||
computesNext = false
|
||||
computeContinuation = c
|
||||
nextStep!!.resume(Unit)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
suspend fun computeNext(): T = suspendCoroutineUninterceptedOrReturn { c ->
|
||||
computesNext = true
|
||||
computeContinuation = c
|
||||
nextStep!!.resume(Unit)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
@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 = suspendCoroutineUninterceptedOrReturn { c ->
|
||||
computedNext = true
|
||||
nextValue = value
|
||||
nextStep = c
|
||||
resumeIterator(null)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
}
|
||||
|
||||
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]
|
||||
}
|
||||
+5
-4
@@ -1,10 +1,11 @@
|
||||
// IGNORE_BACKEND: JVM_IR
|
||||
// !LANGUAGE: -ReleaseCoroutines -ExperimentalBuilderInference
|
||||
// IGNORE_BACKEND: JVM_IR, JS_IR
|
||||
// WITH_RUNTIME
|
||||
// WITH_COROUTINES
|
||||
// COMMON_COROUTINES_TEST
|
||||
|
||||
import helpers.*
|
||||
import COROUTINES_PACKAGE.*
|
||||
import COROUTINES_PACKAGE.intrinsics.*
|
||||
import kotlin.coroutines.experimental.*
|
||||
import kotlin.coroutines.experimental.intrinsics.*
|
||||
|
||||
interface AsyncGenerator<in T> {
|
||||
suspend fun yield(value: T)
|
||||
@@ -0,0 +1,130 @@
|
||||
// !LANGUAGE: +ReleaseCoroutines +ExperimentalBuilderInference
|
||||
// IGNORE_BACKEND: JVM_IR
|
||||
// WITH_RUNTIME
|
||||
// WITH_COROUTINES
|
||||
|
||||
import helpers.*
|
||||
import kotlin.coroutines.*
|
||||
import kotlin.coroutines.intrinsics.*
|
||||
import kotlin.experimental.ExperimentalTypeInference
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@UseExperimental(ExperimentalTypeInference::class)
|
||||
fun <T> asyncGenerate(@BuilderInference 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>, ContinuationAdapter<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 = suspendCoroutineUninterceptedOrReturn { c ->
|
||||
computesNext = false
|
||||
computeContinuation = c
|
||||
nextStep!!.resume(Unit)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
suspend fun computeNext(): T = suspendCoroutineUninterceptedOrReturn { c ->
|
||||
computesNext = true
|
||||
computeContinuation = c
|
||||
nextStep!!.resume(Unit)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
@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 = suspendCoroutineUninterceptedOrReturn { c ->
|
||||
computedNext = true
|
||||
nextValue = value
|
||||
nextStep = c
|
||||
resumeIterator(null)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val seq = asyncGenerate {
|
||||
yield("O")
|
||||
yield("K")
|
||||
}
|
||||
|
||||
var res = ""
|
||||
|
||||
builder {
|
||||
for (i in seq) {
|
||||
res += i
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
Reference in New Issue
Block a user