[IR] Fix for https://youtrack.jetbrains.com/issue/KT-41394 + test
It is redundant to initialize the "label" field with null, since all freshly allocated objects are zeroed out
This commit is contained in:
+2
-6
@@ -48,12 +48,8 @@ internal class NativeSuspendFunctionsLowering(ctx: Context): AbstractSuspendFunc
|
||||
"${function.name}COROUTINE\$${context.coroutineCount++}".synthesizedName
|
||||
|
||||
override fun initializeStateMachine(coroutineConstructors: List<IrConstructor>, coroutineClassThis: IrValueDeclaration) {
|
||||
for (constructor in coroutineConstructors) {
|
||||
val labelField = constructor.parentAsClass.declarations.single { it is IrField && it.name.asString() == "label" } as IrField
|
||||
(constructor.body as IrBlockBody).statements += with(context.createIrBuilder(constructor.symbol, constructor.startOffset, constructor.endOffset)) {
|
||||
irSetField(irGet(coroutineClassThis), labelField, irCall(symbols.getNativeNullPtr.owner))
|
||||
}
|
||||
}
|
||||
// Nothing to do: it's redundant to initialize the "label" field with null
|
||||
// since all freshly allocated objects are zeroed out.
|
||||
}
|
||||
|
||||
override fun IrBlockBodyBuilder.generateCoroutineStart(invokeSuspendFunction: IrFunction, receiver: IrExpression) {
|
||||
|
||||
@@ -1836,6 +1836,11 @@ task coroutines_functionReference_lambdaAsSuspendLambda(type: KonanLocalTest) {
|
||||
source = "codegen/coroutines/functionReference_lambdaAsSuspendLambda.kt"
|
||||
}
|
||||
|
||||
task coroutines_kt41394(type: KonanLocalTest) {
|
||||
goldValue = ""
|
||||
source = "codegen/coroutines/kt41394.kt"
|
||||
}
|
||||
|
||||
standaloneTest('coroutines_suspendConversion') {
|
||||
goldValue = ""
|
||||
source = "codegen/coroutines/suspendConversion.kt"
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
package codegen.coroutines.kt41394
|
||||
|
||||
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() }
|
||||
}
|
||||
|
||||
suspend fun suspendHere(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
x.resume(42)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
suspend fun foo(label: String): String {
|
||||
val x = suspendHere()
|
||||
return label + x.toString()
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
c.startCoroutine(EmptyContinuation)
|
||||
}
|
||||
|
||||
@Test fun runTest() {
|
||||
var result = ""
|
||||
|
||||
builder {
|
||||
result = foo("zzz")
|
||||
}
|
||||
|
||||
assertEquals("zzz42", result)
|
||||
}
|
||||
Reference in New Issue
Block a user