NI: add fallback strategy to get lexical scope to checking coroutine call legality

^KT-40247 Fixed
^KT-40337 Fixed
This commit is contained in:
Victor Petukhov
2020-07-16 11:08:12 +03:00
parent 5951e7500a
commit e754585e38
8 changed files with 83 additions and 2 deletions
@@ -0,0 +1,13 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNCHECKED_CAST
class Foo {
suspend operator fun <T> invoke(body: () -> T) = null as T
}
suspend fun main() {
val retry = Foo()
try {
retry { 1 } // Before the fix: "Suspend function 'invoke' should be called only from a coroutine or another suspend function"
} catch (e: Exception) { }
}
@@ -0,0 +1,11 @@
package
public suspend fun main(): kotlin.Unit
public final class Foo {
public constructor Foo()
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 final suspend operator fun </*0*/ T> invoke(/*0*/ body: () -> T): T
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,16 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNCHECKED_CAST
internal class Demo() {
suspend operator fun <T> invoke(name: String, block: suspend () -> T): T {
TODO()
}
}
suspend fun demo(callback: suspend () -> Unit) = when {
true -> {
val demo = Demo()
demo("test") { callback() } // Before the fix: "Suspend function 'invoke' should be called only from a coroutine or another suspend function"
}
else -> TODO()
}
@@ -0,0 +1,11 @@
package
public suspend fun demo(/*0*/ callback: suspend () -> kotlin.Unit): kotlin.Unit
internal final class Demo {
public constructor Demo()
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 final suspend operator fun </*0*/ T> invoke(/*0*/ name: kotlin.String, /*1*/ block: suspend () -> T): T
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}