[K/N] Fix default arguments in suspend functions

AddContinuationToFunctionsLowering was rewritten in way the order
of this lowering and defaults lowering doesn't matter.

^KT-58214
This commit is contained in:
Pavel Kunyavskiy
2023-04-28 16:08:06 +02:00
committed by Space Team
parent fa1b22cf31
commit af318fdfb0
17 changed files with 117 additions and 94 deletions
@@ -10245,6 +10245,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
runTest("compiler/testData/codegen/box/coroutines/crossInlineWithCapturedOuterReceiver.kt");
}
@Test
@TestMetadata("defaultParameterLambdaInSuspend.kt")
public void testDefaultParameterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParameterLambdaInSuspend.kt");
}
@Test
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
@@ -10257,12 +10263,6 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspendWithJvmOverloads.kt");
}
@Test
@TestMetadata("defaultParamterLambdaInSuspend.kt")
public void testDefaultParamterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParamterLambdaInSuspend.kt");
}
@Test
@TestMetadata("delegatedSuspendMember.kt")
public void testDelegatedSuspendMember() throws Exception {
@@ -10245,6 +10245,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
runTest("compiler/testData/codegen/box/coroutines/crossInlineWithCapturedOuterReceiver.kt");
}
@Test
@TestMetadata("defaultParameterLambdaInSuspend.kt")
public void testDefaultParameterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParameterLambdaInSuspend.kt");
}
@Test
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
@@ -10257,12 +10263,6 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspendWithJvmOverloads.kt");
}
@Test
@TestMetadata("defaultParamterLambdaInSuspend.kt")
public void testDefaultParamterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParamterLambdaInSuspend.kt");
}
@Test
@TestMetadata("delegatedSuspendMember.kt")
public void testDelegatedSuspendMember() throws Exception {
@@ -10,6 +10,7 @@ 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.*
import org.jetbrains.kotlin.backend.common.lower.VariableRemapper
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.ir.IrBuiltIns
import org.jetbrains.kotlin.ir.IrStatement
@@ -71,8 +72,11 @@ class AddContinuationToLocalSuspendFunctionsLowering(val context: CommonBackendC
private fun transformSuspendFunction(context: CommonBackendContext, function: IrSimpleFunction): IrSimpleFunction {
val newFunctionWithContinuation = function.getOrCreateFunctionWithContinuationStub(context)
// Using custom mapping because number of parameters doesn't match
val parameterMapping = function.explicitParameters.zip(newFunctionWithContinuation.explicitParameters).toMap()
val parameterMapping : Map<IrValueParameter, IrValueParameter> = function.explicitParameters.zip(newFunctionWithContinuation.explicitParameters).toMap()
val newBody = function.moveBodyTo(newFunctionWithContinuation, parameterMapping)
for ((old, new) in parameterMapping.entries) {
new.defaultValue = old.defaultValue?.transform(VariableRemapper(parameterMapping), null)
}
// Since we are changing return type to Any, function can no longer return unit implicitly.
if (
@@ -129,11 +133,6 @@ private fun IrSimpleFunction.createSuspendFunctionStub(context: CommonBackendCon
}
function.valueParameters = valueParameters.memoryOptimizedMap { 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 {
startOffset = function.startOffset
endOffset = function.endOffset
@@ -14,6 +14,14 @@ class Controller1 {
x.resume(block())
COROUTINE_SUSPENDED
}
suspend fun getDef() = "DEF2"
suspend fun suspendHere2(block : suspend () -> String = { getDef() }): String {
val result = block()
return suspendCoroutineUninterceptedOrReturn { x ->
x.resume(result)
COROUTINE_SUSPENDED
}
}
}
fun builder1(c: suspend Controller1.() -> Unit) {
@@ -34,6 +42,10 @@ fun box(): String {
result = "FAIL"
return@builder1
}
if (suspendHere2() != "DEF2") {
result = "FAIL"
return@builder1
}
result = suspendHere { "OK" }
}
if (result != "OK") return result
@@ -44,6 +56,10 @@ fun box(): String {
result = "FAIL"
return@builder2
}
if (suspendHere2() != "DEF2") {
result = "FAIL"
return@builder2
}
result = suspendHere { "OK" }
}
@@ -62,6 +78,14 @@ class Controller2 {
x.resume(block())
COROUTINE_SUSPENDED
}
suspend fun getDef() = "DEF2"
suspend fun suspendHere2(block : suspend () -> String = { getDef() }): String {
val result = block()
return suspendCoroutineUninterceptedOrReturn { x ->
x.resume(result)
COROUTINE_SUSPENDED
}
}
}
fun builder2(c: suspend Controller2.() -> Unit) {
@@ -9987,6 +9987,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/coroutines/crossInlineWithCapturedOuterReceiver.kt");
}
@Test
@TestMetadata("defaultParameterLambdaInSuspend.kt")
public void testDefaultParameterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParameterLambdaInSuspend.kt");
}
@Test
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
@@ -9999,12 +10005,6 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspendWithJvmOverloads.kt");
}
@Test
@TestMetadata("defaultParamterLambdaInSuspend.kt")
public void testDefaultParamterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParamterLambdaInSuspend.kt");
}
@Test
@TestMetadata("delegatedSuspendMember.kt")
public void testDelegatedSuspendMember() throws Exception {
@@ -10245,6 +10245,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/coroutines/crossInlineWithCapturedOuterReceiver.kt");
}
@Test
@TestMetadata("defaultParameterLambdaInSuspend.kt")
public void testDefaultParameterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParameterLambdaInSuspend.kt");
}
@Test
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
@@ -10257,12 +10263,6 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspendWithJvmOverloads.kt");
}
@Test
@TestMetadata("defaultParamterLambdaInSuspend.kt")
public void testDefaultParamterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParamterLambdaInSuspend.kt");
}
@Test
@TestMetadata("delegatedSuspendMember.kt")
public void testDelegatedSuspendMember() throws Exception {
@@ -10245,6 +10245,12 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack
runTest("compiler/testData/codegen/box/coroutines/crossInlineWithCapturedOuterReceiver.kt");
}
@Test
@TestMetadata("defaultParameterLambdaInSuspend.kt")
public void testDefaultParameterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParameterLambdaInSuspend.kt");
}
@Test
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
@@ -10257,12 +10263,6 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspendWithJvmOverloads.kt");
}
@Test
@TestMetadata("defaultParamterLambdaInSuspend.kt")
public void testDefaultParamterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParamterLambdaInSuspend.kt");
}
@Test
@TestMetadata("delegatedSuspendMember.kt")
public void testDelegatedSuspendMember() throws Exception {
@@ -7882,6 +7882,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/coroutines/crossInlineWithCapturedOuterReceiver.kt");
}
@TestMetadata("defaultParameterLambdaInSuspend.kt")
public void testDefaultParameterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParameterLambdaInSuspend.kt");
}
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspend.kt");
@@ -7892,11 +7897,6 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspendWithJvmOverloads.kt");
}
@TestMetadata("defaultParamterLambdaInSuspend.kt")
public void testDefaultParamterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParamterLambdaInSuspend.kt");
}
@TestMetadata("delegatedSuspendMember.kt")
public void testDelegatedSuspendMember() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/delegatedSuspendMember.kt");
@@ -7036,15 +7036,15 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
}
@Test
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspend.kt");
@TestMetadata("defaultParameterLambdaInSuspend.kt")
public void testDefaultParameterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParameterLambdaInSuspend.kt");
}
@Test
@TestMetadata("defaultParamterLambdaInSuspend.kt")
public void testDefaultParamterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParamterLambdaInSuspend.kt");
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspend.kt");
}
@Test
@@ -7132,15 +7132,15 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest {
}
@Test
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspend.kt");
@TestMetadata("defaultParameterLambdaInSuspend.kt")
public void testDefaultParameterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParameterLambdaInSuspend.kt");
}
@Test
@TestMetadata("defaultParamterLambdaInSuspend.kt")
public void testDefaultParamterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParamterLambdaInSuspend.kt");
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspend.kt");
}
@Test
@@ -7132,15 +7132,15 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
}
@Test
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspend.kt");
@TestMetadata("defaultParameterLambdaInSuspend.kt")
public void testDefaultParameterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParameterLambdaInSuspend.kt");
}
@Test
@TestMetadata("defaultParamterLambdaInSuspend.kt")
public void testDefaultParamterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParamterLambdaInSuspend.kt");
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspend.kt");
}
@Test
@@ -7132,15 +7132,15 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes
}
@Test
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspend.kt");
@TestMetadata("defaultParameterLambdaInSuspend.kt")
public void testDefaultParameterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParameterLambdaInSuspend.kt");
}
@Test
@TestMetadata("defaultParamterLambdaInSuspend.kt")
public void testDefaultParamterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParamterLambdaInSuspend.kt");
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspend.kt");
}
@Test
@@ -8145,15 +8145,15 @@ public class FirNativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTe
}
@Test
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspend.kt");
@TestMetadata("defaultParameterLambdaInSuspend.kt")
public void testDefaultParameterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParameterLambdaInSuspend.kt");
}
@Test
@TestMetadata("defaultParamterLambdaInSuspend.kt")
public void testDefaultParamterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParamterLambdaInSuspend.kt");
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspend.kt");
}
@Test
@@ -8321,15 +8321,15 @@ public class FirNativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenB
}
@Test
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspend.kt");
@TestMetadata("defaultParameterLambdaInSuspend.kt")
public void testDefaultParameterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParameterLambdaInSuspend.kt");
}
@Test
@TestMetadata("defaultParamterLambdaInSuspend.kt")
public void testDefaultParamterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParamterLambdaInSuspend.kt");
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspend.kt");
}
@Test
@@ -7971,15 +7971,15 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
}
@Test
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspend.kt");
@TestMetadata("defaultParameterLambdaInSuspend.kt")
public void testDefaultParameterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParameterLambdaInSuspend.kt");
}
@Test
@TestMetadata("defaultParamterLambdaInSuspend.kt")
public void testDefaultParamterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParamterLambdaInSuspend.kt");
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspend.kt");
}
@Test
@@ -8059,15 +8059,15 @@ public class NativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenBoxT
}
@Test
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspend.kt");
@TestMetadata("defaultParameterLambdaInSuspend.kt")
public void testDefaultParameterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParameterLambdaInSuspend.kt");
}
@Test
@TestMetadata("defaultParamterLambdaInSuspend.kt")
public void testDefaultParamterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParamterLambdaInSuspend.kt");
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspend.kt");
}
@Test
@@ -6277,16 +6277,16 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/coroutines/crossInlineWithCapturedOuterReceiver.kt");
}
@TestMetadata("defaultParameterLambdaInSuspend.kt")
public void testDefaultParameterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParameterLambdaInSuspend.kt");
}
@TestMetadata("defaultParametersInSuspend.kt")
public void testDefaultParametersInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParametersInSuspend.kt");
}
@TestMetadata("defaultParamterLambdaInSuspend.kt")
public void testDefaultParamterLambdaInSuspend() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/defaultParamterLambdaInSuspend.kt");
}
@TestMetadata("delegatedSuspendMember.kt")
public void testDelegatedSuspendMember() throws Exception {
runTest("compiler/testData/codegen/box/coroutines/delegatedSuspendMember.kt");