[Wasm] Fix transforming setField in an inline class constructor body

IrSetField was transformed to its value to preserve side effects
This transformation produced invalid IR in cases where
IrSetField used in Unit-returning expression context.

This commit fixes it by producing Unit-typed expression

^KT-59084 Fixed
This commit is contained in:
Svyatoslav Kuzmich
2023-08-15 14:18:55 +02:00
committed by Space Team
parent 6fba6a2e3c
commit 833c8b2cd4
4 changed files with 40 additions and 1 deletions
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.backend.common.BodyLoweringPass
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.DeclarationTransformer
import org.jetbrains.kotlin.backend.common.getOrPut
import org.jetbrains.kotlin.backend.common.ir.isPure
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.builders.*
@@ -105,7 +106,15 @@ class InlineClassLowering(val context: CommonBackendContext) {
}
expression.transformChildrenVoid()
if (isMemberFieldSet) {
return expression.value
return builder.irBlock {
if (!expression.value.isPure(true)) {
// Preserving side effects
+expression.value
// Adding empty block to provide Unit value.
// This is useful when original IrSetField is the last statement in a Unit-returning block.
+builder.irBlock {}
}
}
}
return expression
}
+18
View File
@@ -0,0 +1,18 @@
// TARGET_BACKEND: WASM
inline class JsDynamic(val value: JsAny?)
val JsAny?.jsDyn: JsDynamic get() = JsDynamic(this)
fun test(): Boolean {
val jsDynamic: JsAny? = 1.toJsNumber()
val jsDyn = jsDynamic.jsDyn
return jsDyn.value == jsDynamic
}
fun box(): String {
if (!test()) {
return "Fail"
}
return "OK"
}
@@ -121,6 +121,12 @@ public class FirWasmCodegenWasmJsInteropTestGenerated extends AbstractFirWasmCod
runTest("compiler/testData/codegen/boxWasmJsInterop/kt59082.kt");
}
@Test
@TestMetadata("kt59084.kt")
public void testKt59084() throws Exception {
runTest("compiler/testData/codegen/boxWasmJsInterop/kt59084.kt");
}
@Test
@TestMetadata("lambdaAdapterNameClash.kt")
public void testLambdaAdapterNameClash() throws Exception {
@@ -121,6 +121,12 @@ public class K1WasmCodegenWasmJsInteropTestGenerated extends AbstractK1WasmCodeg
runTest("compiler/testData/codegen/boxWasmJsInterop/kt59082.kt");
}
@Test
@TestMetadata("kt59084.kt")
public void testKt59084() throws Exception {
runTest("compiler/testData/codegen/boxWasmJsInterop/kt59084.kt");
}
@Test
@TestMetadata("lambdaAdapterNameClash.kt")
public void testLambdaAdapterNameClash() throws Exception {