[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
@@ -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