Support suspending extension functions

This commit is contained in:
Denis Zharkov
2016-07-05 18:25:09 +03:00
parent 2cc09f928e
commit 3a59e1d222
9 changed files with 167 additions and 11 deletions
@@ -0,0 +1,36 @@
@AllowSuspendExtensions
class Controller {
suspend fun String.suspendHere(x: Continuation<String>) {
x.resume(this)
}
inline suspend fun String.inlineSuspendHere(x: Continuation<String>) {
suspendHere(x)
}
}
suspend fun Controller.suspendExtension(v: String, x: Continuation<String>) {
v.suspendHere(x)
}
inline suspend fun Controller.inlineSuspendExtension(v: String, x: Continuation<String>) {
v.inlineSuspendHere(x)
}
fun builder(coroutine c: Controller.() -> Continuation<Unit>) {
c(Controller()).resume(Unit)
}
fun box(): String {
var result = ""
builder {
if ("56".suspendHere() != "56") throw java.lang.RuntimeException("fail 1")
if ("28".inlineSuspendHere() != "28") throw java.lang.RuntimeException("fail 2")
if (suspendExtension("123") != "123") throw java.lang.RuntimeException("fail 3")
result = inlineSuspendExtension("OK")
}
return result
}
@@ -0,0 +1,60 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
// !CHECK_TYPE
@AllowSuspendExtensions
class Controller
suspend fun Controller.noParams(c: Continuation<Unit>) {
}
suspend fun Controller.yieldString(value: String, c: Continuation<Unit>) {
}
suspend fun <V> Controller.await(f: () -> V, machine: Continuation<V>) {
}
suspend fun <V> Controller.await(f: Int, machine: Continuation<V>) {
}
suspend fun Controller.severalParams(x: String, y: Int, machine: Continuation<Double>) {
}
// These two must be prohibited because String and Any are not properly annotated
<!INAPPLICABLE_MODIFIER!>suspend<!> fun String.wrongReceiver(y: Int, machine: Continuation<Double>) {
}
<!INAPPLICABLE_MODIFIER!>suspend<!> fun Any.anyReceiver(y: Int, machine: Continuation<Double>) {
}
fun builder(coroutine c: Controller.() -> Continuation<Unit>) {}
fun test() {
builder {
noParams()
yieldString("abc") checkType { _<Unit>() }
yieldString(<!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>) checkType { _<Unit>() }
await<String> { "123" } checkType { _<String>() }
// Inference from lambda return type
await { 123 } checkType { _<Int>() }
// Inference from expected type
checkSubtype<String>(await(567))
await<Double>(123) checkType { _<Double>() }
severalParams("", 89) checkType { _<Double>() }
// TODO: prohibit such calls
severalParams("", 89, <!CONSTANT_EXPECTED_TYPE_MISMATCH!>6.9<!>) checkType { _<Unit>() }
severalParams("", 89, this <!CAST_NEVER_SUCCEEDS!>as<!> Continuation<Double>) checkType { _<Unit>() }
<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>wrongReceiver<!>(1)
with("") {
wrongReceiver(2<!NO_VALUE_FOR_PARAMETER!>)<!>
}
// Though such calls are allowed declarations with not-annotated receiver should be prohibited
anyReceiver(3)
}
}
@@ -0,0 +1,18 @@
package
public fun builder(/*0*/ coroutine c: Controller.() -> kotlin.coroutines.Continuation<kotlin.Unit>): kotlin.Unit
public fun test(): kotlin.Unit
public suspend fun kotlin.Any.anyReceiver(/*0*/ y: kotlin.Int, /*1*/ machine: kotlin.coroutines.Continuation<kotlin.Double>): kotlin.Unit
public suspend fun </*0*/ V> Controller.await(/*0*/ f: () -> V, /*1*/ machine: kotlin.coroutines.Continuation<V>): kotlin.Unit
public suspend fun </*0*/ V> Controller.await(/*0*/ f: kotlin.Int, /*1*/ machine: kotlin.coroutines.Continuation<V>): kotlin.Unit
public suspend fun Controller.noParams(/*0*/ c: kotlin.coroutines.Continuation<kotlin.Unit>): kotlin.Unit
public suspend fun Controller.severalParams(/*0*/ x: kotlin.String, /*1*/ y: kotlin.Int, /*2*/ machine: kotlin.coroutines.Continuation<kotlin.Double>): kotlin.Unit
public suspend fun kotlin.String.wrongReceiver(/*0*/ y: kotlin.Int, /*1*/ machine: kotlin.coroutines.Continuation<kotlin.Double>): kotlin.Unit
public suspend fun Controller.yieldString(/*0*/ value: kotlin.String, /*1*/ c: kotlin.coroutines.Continuation<kotlin.Unit>): kotlin.Unit
@kotlin.coroutines.AllowSuspendExtensions() public final class Controller {
public constructor Controller()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}