diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt index 14cb3cf9c63..26bc65bda48 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/NativeSuspendFunctionLowering.kt @@ -48,12 +48,8 @@ internal class NativeSuspendFunctionsLowering(ctx: Context): AbstractSuspendFunc "${function.name}COROUTINE\$${context.coroutineCount++}".synthesizedName override fun initializeStateMachine(coroutineConstructors: List, 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) { diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index bfa255c6c32..ccc7eb832c5 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -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" diff --git a/backend.native/tests/codegen/coroutines/kt41394.kt b/backend.native/tests/codegen/coroutines/kt41394.kt new file mode 100644 index 00000000000..60908e9009c --- /dev/null +++ b/backend.native/tests/codegen/coroutines/kt41394.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 { + companion object : EmptyContinuation() + override fun resumeWith(result: Result) { 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) +} \ No newline at end of file