JS IR: materialize Unit in lambdas

^KT-52010 fixed
This commit is contained in:
Anton Bannykh
2022-04-15 21:01:43 +03:00
committed by Anton Bannykh
parent bc3514feae
commit 281e381223
5 changed files with 87 additions and 0 deletions
@@ -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
@@ -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
}
@@ -2959,6 +2959,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
runTest("js/js.translator/testData/box/expression/function/lambdaOrLocalFunInsideEnumMethod.kt");
}
@Test
@TestMetadata("lambdaReturnValue.kt")
public void testLambdaReturnValue() throws Exception {
runTest("js/js.translator/testData/box/expression/function/lambdaReturnValue.kt");
}
@Test
@TestMetadata("localExtFunction.kt")
public void testLocalExtFunction() throws Exception {
@@ -8860,6 +8866,12 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
runTest("js/js.translator/testData/box/regression/kt2470.kt");
}
@Test
@TestMetadata("kt52010.kt")
public void testKt52010() throws Exception {
runTest("js/js.translator/testData/box/regression/kt52010.kt");
}
@Test
@TestMetadata("tmpInsidePrimaryConstructor.kt")
public void testTmpInsidePrimaryConstructor() throws Exception {
@@ -3355,6 +3355,12 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest {
runTest("js/js.translator/testData/box/expression/function/lambdaOrLocalFunInsideEnumMethod.kt");
}
@Test
@TestMetadata("lambdaReturnValue.kt")
public void testLambdaReturnValue() throws Exception {
runTest("js/js.translator/testData/box/expression/function/lambdaReturnValue.kt");
}
@Test
@TestMetadata("localExtFunction.kt")
public void testLocalExtFunction() throws Exception {
@@ -9484,6 +9490,12 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest {
runTest("js/js.translator/testData/box/regression/kt2470.kt");
}
@Test
@TestMetadata("kt52010.kt")
public void testKt52010() throws Exception {
runTest("js/js.translator/testData/box/regression/kt52010.kt");
}
@Test
@TestMetadata("tmpInsidePrimaryConstructor.kt")
public void testTmpInsidePrimaryConstructor() throws Exception {
@@ -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
View File
@@ -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()
}
}