[JS IR] Fix default arguments in suspend functions

Don't forget to remap parameter references in default arg expressions

The issue originally discovered in kotlinx.coroutines tests.

 - add test
This commit is contained in:
Roman Artemev
2021-10-25 13:36:28 +03:00
committed by Space
parent 1d58fd159a
commit 9f52326d14
10 changed files with 105 additions and 6 deletions
@@ -9894,6 +9894,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/coroutines/simpleSuspendCallableReference.kt");
}
@Test
@TestMetadata("simpleWithDefaultValue.kt")
public void testSimpleWithDefaultValue() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/simpleWithDefaultValue.kt");
}
@Test
@TestMetadata("simpleWithHandleResult.kt")
public void testSimpleWithHandleResult() throws Exception {
@@ -5,19 +5,25 @@
package org.jetbrains.kotlin.ir.backend.js.lower.coroutines
import org.jetbrains.kotlin.backend.common.*
import org.jetbrains.kotlin.backend.common.BodyLoweringPass
import org.jetbrains.kotlin.backend.common.DeclarationTransformer
import org.jetbrains.kotlin.backend.common.getOrPut
import org.jetbrains.kotlin.backend.common.ir.*
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.backend.js.JsCommonBackendContext
import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.builders.declarations.*
import org.jetbrains.kotlin.ir.builders.declarations.addValueParameter
import org.jetbrains.kotlin.ir.builders.declarations.buildFun
import org.jetbrains.kotlin.ir.builders.irReturnUnit
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.IrBlockBody
import org.jetbrains.kotlin.ir.expressions.IrBody
import org.jetbrains.kotlin.ir.expressions.IrReturn
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.isNullable
import org.jetbrains.kotlin.ir.types.typeWith
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.util.explicitParameters
import org.jetbrains.kotlin.ir.util.substitute
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
@@ -109,6 +115,11 @@ private fun IrSimpleFunction.createSuspendFunctionStub(context: JsCommonBackendC
}
function.valueParameters = valueParameters.map { it.copyTo(function) }
val mapping = mutableMapOf<IrValueSymbol, IrValueSymbol>()
valueParameters.forEach { mapping[it.symbol] = function.valueParameters[it.index].symbol }
val remapper = ValueRemapper(mapping)
function.valueParameters.forEach { it.defaultValue = it.defaultValue?.transform(remapper, null) }
function.addValueParameter(
"\$cont",
continuationType(context).substitute(substitutionMap),
@@ -0,0 +1,44 @@
// WITH_RUNTIME
// WITH_COROUTINES
import helpers.*
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
suspend fun suspendHere(a: String, b: String = a, c: String = a + b): String = suspendCoroutineUninterceptedOrReturn { x ->
x.resume("$a:$b:$c")
COROUTINE_SUSPENDED
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
fun box(): String {
var result = ""
builder {
result = suspendHere("AAA")
}
if (result != "AAA:AAA:AAAAAA") return "FAIL 1: $result"
builder {
result = suspendHere("AAA", c = "CCC")
}
if (result != "AAA:AAA:CCC") return "FAIL 2: $result"
builder {
result = suspendHere("AAA", "BBB")
}
if (result != "AAA:BBB:AAABBB") return "FAIL 3: $result"
builder {
result = suspendHere("AAA", "BBB", "CCC")
}
if (result != "AAA:BBB:CCC") return "FAIL 4: $result"
return "OK"
}
@@ -9816,6 +9816,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/coroutines/simpleSuspendCallableReference.kt");
}
@Test
@TestMetadata("simpleWithDefaultValue.kt")
public void testSimpleWithDefaultValue() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/simpleWithDefaultValue.kt");
}
@Test
@TestMetadata("simpleWithHandleResult.kt")
public void testSimpleWithHandleResult() throws Exception {
@@ -9894,6 +9894,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/coroutines/simpleSuspendCallableReference.kt");
}
@Test
@TestMetadata("simpleWithDefaultValue.kt")
public void testSimpleWithDefaultValue() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/simpleWithDefaultValue.kt");
}
@Test
@TestMetadata("simpleWithHandleResult.kt")
public void testSimpleWithHandleResult() throws Exception {
@@ -7698,6 +7698,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/coroutines/simpleSuspendCallableReference.kt");
}
@TestMetadata("simpleWithDefaultValue.kt")
public void testSimpleWithDefaultValue() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/simpleWithDefaultValue.kt");
}
@TestMetadata("simpleWithHandleResult.kt")
public void testSimpleWithHandleResult() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/simpleWithHandleResult.kt");
@@ -6857,6 +6857,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/coroutines/simpleSuspendCallableReference.kt");
}
@TestMetadata("simpleWithDefaultValue.kt")
public void testSimpleWithDefaultValue() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/simpleWithDefaultValue.kt");
}
@TestMetadata("simpleWithHandleResult.kt")
public void testSimpleWithHandleResult() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/simpleWithHandleResult.kt");
@@ -6263,6 +6263,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/coroutines/simpleSuspendCallableReference.kt");
}
@TestMetadata("simpleWithDefaultValue.kt")
public void testSimpleWithDefaultValue() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/simpleWithDefaultValue.kt");
}
@TestMetadata("simpleWithHandleResult.kt")
public void testSimpleWithHandleResult() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/simpleWithHandleResult.kt");
@@ -6086,6 +6086,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/coroutines/simpleSuspendCallableReference.kt");
}
@TestMetadata("simpleWithDefaultValue.kt")
public void testSimpleWithDefaultValue() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/simpleWithDefaultValue.kt");
}
@TestMetadata("simpleWithHandleResult.kt")
public void testSimpleWithHandleResult() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/simpleWithHandleResult.kt");
@@ -7082,6 +7082,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/coroutines/simpleSuspendCallableReference.kt");
}
@Test
@TestMetadata("simpleWithDefaultValue.kt")
public void testSimpleWithDefaultValue() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/simpleWithDefaultValue.kt");
}
@Test
@TestMetadata("simpleWithHandleResult.kt")
public void testSimpleWithHandleResult() throws Exception {