diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ReturnsInsertionLowering.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ReturnsInsertionLowering.kt index 1adab6017c9..ba1f8750270 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ReturnsInsertionLowering.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ReturnsInsertionLowering.kt @@ -16,6 +16,7 @@ import org.jetbrains.kotlin.ir.declarations.IrFile import org.jetbrains.kotlin.ir.declarations.IrFunction import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.IrBlockBodyImpl +import org.jetbrains.kotlin.ir.types.isNullableNothing import org.jetbrains.kotlin.ir.util.isSimpleTypeWithQuestionMark import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid @@ -46,7 +47,8 @@ internal class ReturnsInsertionLowering(val context: Context) : FileLoweringPass } else if (declaration.returnType.isSimpleTypeWithQuestionMark) { // this is a workaround for KT-42832 val typeOperatorCall = body.statements.lastOrNull() as? IrTypeOperatorCall - if (typeOperatorCall?.operator == IrTypeOperator.IMPLICIT_COERCION_TO_UNIT) { + if (typeOperatorCall?.operator == IrTypeOperator.IMPLICIT_COERCION_TO_UNIT + && typeOperatorCall.argument.type.isNullableNothing()) { body.statements[body.statements.lastIndex] = irReturn(typeOperatorCall.argument) } } diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index d141c161b1f..969d5f1a439 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -533,6 +533,12 @@ standaloneTest("function_nothingN_returning_safe_call") { source = "codegen/function/nothingNReturningSafeCall.kt" } +standaloneTest("unreachable_statement_after_return") { + flags = ["-g", "-entry", "codegen.function.unreachable_statement_after_return.main"] + source = "codegen/function/unreachableStatementAfterReturn.kt" + goldValue = "1\n2\n3\n4\n" +} + task codegen_controlflow_for_loops(type: KonanLocalTest) { source = "codegen/controlflow/for_loops.kt" goldValue = "01234\n0123\n43210\n\n024\n02\n420\n\n036\n03\n630\n\n024\n02\n420\n\n" diff --git a/kotlin-native/backend.native/tests/codegen/function/unreachableStatementAfterReturn.kt b/kotlin-native/backend.native/tests/codegen/function/unreachableStatementAfterReturn.kt new file mode 100644 index 00000000000..b98bf249146 --- /dev/null +++ b/kotlin-native/backend.native/tests/codegen/function/unreachableStatementAfterReturn.kt @@ -0,0 +1,34 @@ +/* + * Copyright 2010-2021 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.function.unreachable_statement_after_return + +fun test1(): Any? { + return 1 + 42 +} + +fun test2(): Int? { + return 2 + 42 +} + +fun test3(): Any { + return 3 + 42 +} + +fun test4(): Int { + return 4 + 42 +} + +fun main() { + println(test1()) + println(test2()) + println(test3()) + println(test4()) +} +