This commit is contained in:
+1
-5
@@ -1818,11 +1818,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
|
||||
val bbExit = returnableBlockScope.bbExit
|
||||
if (bbExit != null) {
|
||||
if (!functionGenerationContext.isAfterTerminator()) { // TODO should we solve this problem once and for all
|
||||
if (returnableBlockScope.resultPhi != null) {
|
||||
functionGenerationContext.unreachable()
|
||||
} else {
|
||||
functionGenerationContext.br(bbExit)
|
||||
}
|
||||
functionGenerationContext.unreachable()
|
||||
}
|
||||
functionGenerationContext.positionAtEnd(bbExit)
|
||||
}
|
||||
|
||||
+14
-4
@@ -14,8 +14,7 @@ import org.jetbrains.kotlin.ir.builders.irReturn
|
||||
import org.jetbrains.kotlin.ir.declarations.IrConstructor
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||
import org.jetbrains.kotlin.ir.expressions.IrBlockBody
|
||||
import org.jetbrains.kotlin.ir.types.classifierOrNull
|
||||
import org.jetbrains.kotlin.ir.expressions.*
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptVoid
|
||||
@@ -32,14 +31,25 @@ internal class ReturnsInsertionLowering(val context: Context) : FileLoweringPass
|
||||
override fun visitFunction(declaration: IrFunction) {
|
||||
declaration.acceptChildrenVoid(this)
|
||||
|
||||
val body = declaration.body
|
||||
if ((declaration is IrConstructor || declaration.returnType.classifierOrNull == symbols.unit) && body != null) {
|
||||
val body = declaration.body ?: return
|
||||
if (declaration is IrConstructor || declaration.returnType == context.irBuiltIns.unitType) {
|
||||
val irBuilder = context.createIrBuilder(declaration.symbol, declaration.endOffset, declaration.endOffset)
|
||||
irBuilder.run {
|
||||
(body as IrBlockBody).statements += irReturn(irGetObject(symbols.unit))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitBlock(expression: IrBlock) {
|
||||
expression.acceptChildrenVoid(this)
|
||||
if (expression !is IrReturnableBlock) return
|
||||
if (expression.inlineFunctionSymbol?.owner?.returnType == context.irBuiltIns.unitType) {
|
||||
val irBuilder = context.createIrBuilder(expression.symbol, expression.endOffset, expression.endOffset)
|
||||
irBuilder.run {
|
||||
expression.statements += irReturn(irGetObject(symbols.unit))
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -2994,6 +2994,11 @@ task inline_correctOrderFunctionReference(type: KonanLocalTest) {
|
||||
source = "codegen/inline/correctOrderFunctionReference.kt"
|
||||
}
|
||||
|
||||
task inline_coercionToUnit(type: KonanLocalTest) {
|
||||
goldValue = "kotlin.Unit\n"
|
||||
source = "codegen/inline/coercionToUnit.kt"
|
||||
}
|
||||
|
||||
task classDeclarationInsideInline(type: KonanLocalTest) {
|
||||
source = "codegen/inline/classDeclarationInsideInline.kt"
|
||||
goldValue = "test1: 1.0\n1\ntest2\n"
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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.inline.coercionToUnit
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
fun <T> myRun(action: () -> T): T = action()
|
||||
|
||||
fun foo(n: Number, b: Boolean) {
|
||||
n.let {
|
||||
if (b) return@let
|
||||
|
||||
myRun() { 42 }
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun runTest() {
|
||||
println(foo(42, false))
|
||||
}
|
||||
Reference in New Issue
Block a user