[K/N] Fix handling of legacy suspend-function superclass

^KT-50737
This commit is contained in:
Pavel Kunyavskiy
2022-01-12 15:23:56 +03:00
committed by Space
parent b2b5f4a63a
commit 541f6b0c88
4 changed files with 63 additions and 1 deletions
@@ -71,8 +71,14 @@ abstract class AbstractSuspendFunctionsLowering<C : CommonBackendContext>(val co
declaration.acceptChildrenVoid(this)
}
private fun addMissingSupertypes(clazz: IrClass) {
val suspendFunctionTypes = getAllSubstitutedSupertypes(clazz).filter { it.isSuspendFunction() }.toSet()
val suspendFunctionTypes = getAllSubstitutedSupertypes(clazz).filter {
// SuspendFunction class is some hack in old Kotlin/Native compiler versions.
// It's not used now, but is considered as SuspendFunction-like class in isSuspendFunction util,
// if found in old klib. We need just to ignore it.
it.isSuspendFunction() && it.classOrNull?.owner?.name?.toString() != "SuspendFunction"
}.toSet()
for (suspendFunctionType in suspendFunctionTypes) {
val suspendFunctionClassSymbol = suspendFunctionType.classOrNull ?: continue
@@ -2204,6 +2204,11 @@ task coroutines_kt41394(type: KonanLocalTest) {
source = "codegen/coroutines/kt41394.kt"
}
task coroutines_inheritance(type: KonanLocalTest) {
useGoldenData = true
source = "codegen/coroutines/inheritance.kt"
}
standaloneTest('coroutines_suspendConversion') {
useGoldenData = true
source = "codegen/coroutines/suspendConversion.kt"
@@ -0,0 +1,49 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package codegen.coroutines.inheritance
import kotlin.test.*
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
companion object : EmptyContinuation()
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
}
class SuspendHere(): suspend () -> Int {
override suspend fun invoke() : Int = suspendCoroutineUninterceptedOrReturn { x ->
x.resume(42)
COROUTINE_SUSPENDED
}
}
// in old compiler versions all suspend functions were implementing this interface
// now they are not, but let's test it works correctly
class SuspendHereLegacy(): suspend () -> Int, SuspendFunction<Int> {
override suspend fun invoke() : Int = suspendCoroutineUninterceptedOrReturn { x ->
x.resume(43)
COROUTINE_SUSPENDED
}
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
@Test fun runTest() {
var result = 0
builder {
result = SuspendHere()()
}
println(result)
builder {
result = SuspendHereLegacy()()
}
println(result)
}
@@ -0,0 +1,2 @@
42
43