From 75d2e415e15d30078d82513646be197d83fd07c1 Mon Sep 17 00:00:00 2001 From: Dmitriy Dolovov Date: Tue, 20 Apr 2021 16:57:17 +0300 Subject: [PATCH] [K/N][IR] Generate missing return statement, p. 2 ^KT-42832 --- .../konan/lower/ReturnsInsertionLowering.kt | 8 ++-- .../backend.native/tests/build.gradle | 11 ++--- .../function/nothingNReturningSafeCall.kt | 46 +++++++++++++++++++ .../function/nothingNReturningSafeCall1.kt | 18 -------- .../function/nothingNReturningSafeCall2.kt | 23 ---------- 5 files changed, 54 insertions(+), 52 deletions(-) create mode 100644 kotlin-native/backend.native/tests/codegen/function/nothingNReturningSafeCall.kt delete mode 100644 kotlin-native/backend.native/tests/codegen/function/nothingNReturningSafeCall1.kt delete mode 100644 kotlin-native/backend.native/tests/codegen/function/nothingNReturningSafeCall2.kt 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 51c845647ed..1adab6017c9 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.util.isSimpleTypeWithQuestionMark import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid import org.jetbrains.kotlin.ir.visitors.acceptVoid @@ -42,10 +43,11 @@ internal class ReturnsInsertionLowering(val context: Context) : FileLoweringPass is IrBlockBody -> { if (declaration is IrConstructor || declaration.returnType == context.irBuiltIns.unitType) { body.statements += irReturn(irGetObject(symbols.unit)) - } else if (declaration.returnType == context.irBuiltIns.nothingNType) { - val typeOperatorCall = body.statements.singleOrNull() as? IrTypeOperatorCall + } 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) { - body.statements[0] = irReturn(typeOperatorCall.argument) + 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 133c73bff19..5f65a2ac657 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -528,14 +528,9 @@ task sum_3const(type: KonanLocalTest) { source = "codegen/function/sum_3const.kt" } -standaloneTest("function_nothingN_returning_safe_call1") { - flags = ["-g", "-entry", "codegen.function.nothingN_returning_safe_call1.main"] - source = "codegen/function/nothingNReturningSafeCall1.kt" -} - -standaloneTest("function_nothingN_returning_safe_call2") { - flags = ["-g", "-entry", "codegen.function.nothingN_returning_safe_call2.main"] - source = "codegen/function/nothingNReturningSafeCall2.kt" +standaloneTest("function_nothingN_returning_safe_call") { + flags = ["-g", "-entry", "codegen.function.nothingN_returning_safe_call.main"] + source = "codegen/function/nothingNReturningSafeCall.kt" } task codegen_controlflow_for_loops(type: KonanLocalTest) { diff --git a/kotlin-native/backend.native/tests/codegen/function/nothingNReturningSafeCall.kt b/kotlin-native/backend.native/tests/codegen/function/nothingNReturningSafeCall.kt new file mode 100644 index 00000000000..bf2636c34df --- /dev/null +++ b/kotlin-native/backend.native/tests/codegen/function/nothingNReturningSafeCall.kt @@ -0,0 +1,46 @@ +/* + * 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.nothingN_returning_safe_call + +import kotlin.test.* + +fun Any.nothing(): Nothing { + while (true) {} +} + +class Anything + +fun testFunction1(obj: Any?): Nothing? = obj?.nothing() +fun testFunction2(obj: Any?): Any? = obj?.nothing() +fun testFunction3(obj: Any?): String? = obj?.nothing() +fun testFunction4(obj: Any?): Unit? = obj?.nothing() +fun testFunction5(obj: Any?): Anything? = obj?.nothing() + +fun testLambda1() { + val block: (Any?) -> Nothing? = { + it?.nothing() + } + block(null) +} + +fun testLambda2() { + val block: (Any?) -> Nothing? = { + println() // more than one statement inside of the body + it?.nothing() + } + block(null) +} + +fun main() { + testFunction1(null) + testFunction2(null) + testFunction3(null) + testFunction4(null) + testFunction5(null) + + testLambda1() + testLambda2() +} diff --git a/kotlin-native/backend.native/tests/codegen/function/nothingNReturningSafeCall1.kt b/kotlin-native/backend.native/tests/codegen/function/nothingNReturningSafeCall1.kt deleted file mode 100644 index 7bcaebc6396..00000000000 --- a/kotlin-native/backend.native/tests/codegen/function/nothingNReturningSafeCall1.kt +++ /dev/null @@ -1,18 +0,0 @@ -/* - * 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.nothingN_returning_safe_call1 - -import kotlin.test.* - -fun Any.nothing(): Nothing { - while (true) {} -} - -fun foo(obj: Any?): Nothing? = obj?.nothing() - -fun main() { - foo(null) -} diff --git a/kotlin-native/backend.native/tests/codegen/function/nothingNReturningSafeCall2.kt b/kotlin-native/backend.native/tests/codegen/function/nothingNReturningSafeCall2.kt deleted file mode 100644 index 4d2adac4d08..00000000000 --- a/kotlin-native/backend.native/tests/codegen/function/nothingNReturningSafeCall2.kt +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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.nothingN_returning_safe_call2 - -import kotlin.test.* - -fun Any.nothing(): Nothing { - while (true) {} -} - -fun foo() { - val block: (Any?) -> Nothing? = { - it?.nothing() - } - block(null) -} - -fun main() { - foo() -}