[IR][Native] Fix invalid IR return statement generation

^KT-46836
This commit is contained in:
Dmitriy Dolovov
2021-05-28 15:45:34 +03:00
committed by Space
parent 15b6a3c88c
commit e927764aaf
3 changed files with 43 additions and 1 deletions
@@ -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)
}
}
@@ -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"
@@ -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())
}