Skip to content

Commit

Permalink
JS IR: materialize Unit in lambdas
Browse files Browse the repository at this point in the history
^KT-52010 fixed
  • Loading branch information
anton-bannykh committed Apr 16, 2022
1 parent bc3514f commit 281e381
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classifierOrNull
import org.jetbrains.kotlin.ir.types.isUnit
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
Expand Down Expand Up @@ -185,6 +186,11 @@ class InteropCallableReferenceLowering(val context: JsIrBackendContext) : BodyLo
// Fix parents of declarations inside body
body.patchDeclarationParents(lambdaDeclaration)

if (invokeFun.returnType.isUnit()) {
val unitValue = JsIrBuilder.buildGetObjectValue(context.irBuiltIns.unitType, context.irBuiltIns.unitClass)
(body as IrBlockBody).statements.add(IrReturnImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, context.irBuiltIns.nothingType, lambdaDeclaration.symbol, unitValue))
}

return body as IrBlockBody
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// EXPECTED_REACHABLE_NODES: 1285

fun <T> rawReturnValue(fn: () -> T): Any {
return fn() as Any
}

fun unitFun() {}

fun charFun(): Char = 'a'

value class VC(val v: Int)

fun vcFun(): VC = VC(1)



fun box(): String {
if (rawReturnValue { unitFun() } != Unit) return "fail1.1"
if (rawReturnValue<Unit> { unitFun() } != Unit) return "fail1.2"
if (rawReturnValue<Any> { unitFun() } != Unit) return "fail1.3"

val boxedA: Any = 'a'

if (rawReturnValue { charFun() } != boxedA) return "fail2.1"
if (rawReturnValue<Char> { charFun() } != boxedA) return "fail2.2"
if (rawReturnValue<Any> { charFun() } != boxedA) return "fail2.3"

val boxed1: Any = VC(1)

if (rawReturnValue { vcFun() } != boxed1) return "fail3.1"
if (rawReturnValue<VC> { vcFun() } != boxed1) return "fail3.2"
if (rawReturnValue<Any> { vcFun() } != boxed1) return "fail3.3"

return "OK"
}
22 changes: 22 additions & 0 deletions js/js.translator/testData/box/regression/kt52010.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// EXPECTED_REACHABLE_NODES: 1274

fun box(): String {
instance = Holder()
instance?.applyAndRet<Unit> { sideEffect("left") } ?: sideEffect("right")

if (log == "left") return "OK" else return "fail: $log"
}

var log = ""

fun sideEffect(msg: String) {
log += msg
}

var instance: Holder? = null

class Holder() {
fun <T> applyAndRet(block: () -> T): T {
return block()
}
}

0 comments on commit 281e381

Please sign in to comment.