diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt index 8677eda294f..860e8c8e7c5 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt @@ -133,6 +133,9 @@ internal class KonanLower(val context: Context) { validateIrFile(context, irFile) Autoboxing(context).lower(irFile) } + phaser.phase(KonanPhase.RETURNS_INSERTION) { + ReturnsInsertionLowering(context).lower(irFile) + } } } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanPhases.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanPhases.kt index afb7f1fd079..1ad9e7d71bc 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanPhases.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanPhases.kt @@ -55,6 +55,7 @@ enum class KonanPhase(val description: String, /* ... ... */ LOWER_STRING_CONCAT("String concatenation lowering"), /* ... ... */ LOWER_DATA_CLASSES("Data classes lowering"), /* ... ... */ AUTOBOX("Autoboxing of primitive types", BRIDGES_BUILDING, LOWER_COROUTINES), + /* ... ... */ RETURNS_INSERTION("Returns insertion for Unit functions", AUTOBOX, LOWER_COROUTINES, LOWER_ENUMS), /* ... */ BITCODE("LLVM BitCode Generation"), /* ... ... */ RTTI("RTTI Generation"), /* ... ... */ ESCAPE_ANALYSIS("Escape analysis"), diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt index 00a90a4fa31..8a7ede5912d 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt @@ -78,12 +78,8 @@ internal inline fun generateFunction(codegen: CodeGenerator, function: LLVMVa inline private fun generateFunctionBody(functionGenerationContext: FunctionGenerationContext, code: FunctionGenerationContext.(FunctionGenerationContext) -> R) { functionGenerationContext.prologue() functionGenerationContext.code(functionGenerationContext) - if (!functionGenerationContext.isAfterTerminator()) { - if (functionGenerationContext.returnType == voidType) - functionGenerationContext.ret(null) - else - functionGenerationContext.unreachable() - } + if (!functionGenerationContext.isAfterTerminator()) + functionGenerationContext.unreachable() functionGenerationContext.epilogue() functionGenerationContext.resetDebugLocation() } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt index dcb801b401d..3102d642468 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt @@ -338,6 +338,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1() { + println("s1") +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + builder { + s1() + } +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/degenerate2.kt b/backend.native/tests/codegen/coroutines/degenerate2.kt new file mode 100644 index 00000000000..b5f4d4f91b9 --- /dev/null +++ b/backend.native/tests/codegen/coroutines/degenerate2.kt @@ -0,0 +1,29 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Unit = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(Unit) + COROUTINE_SUSPENDED +} + +suspend fun s2() { + println("s2") + s1() +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + builder { + s2() + } +} \ No newline at end of file