KT-52373 Fix generation of synthetic functions with context receivers and default parameters

This commit is contained in:
Pavel Mikhailovskii
2022-12-07 16:40:07 +01:00
committed by Space Team
parent 4fa701f798
commit 76997edebe
4 changed files with 83 additions and 23 deletions
@@ -17474,6 +17474,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/extensionFunctions/contextReceivers/kt52207.kt");
}
@Test
@TestMetadata("kt52373.kt")
public void testKt52373() throws Exception {
runTest("compiler/testData/codegen/box/extensionFunctions/contextReceivers/kt52373.kt");
}
@Test
@TestMetadata("overload.kt")
public void testOverload() throws Exception {
@@ -496,6 +496,10 @@ fun IrMemberAccessExpression<IrFunctionSymbol>.copyValueArgumentsFrom(
}
}
while (srcValueArgumentIndex < src.symbol.owner.contextReceiverParametersCount) {
putValueArgument(destValueArgumentIndex++, src.getValueArgument(srcValueArgumentIndex++))
}
when {
receiversAsArguments && srcFunction.extensionReceiverParameter != null -> {
putValueArgument(destValueArgumentIndex++, src.extensionReceiver)
@@ -1188,29 +1192,55 @@ fun IrFactory.createStaticFunctionWithReceivers(
annotations = oldFunction.annotations
var offset = 0
val dispatchReceiver = oldFunction.dispatchReceiverParameter?.copyTo(
this,
name = Name.identifier("\$this"),
index = offset++,
type = remap(dispatchReceiverType!!),
origin = IrDeclarationOrigin.MOVED_DISPATCH_RECEIVER
)
val extensionReceiver = oldFunction.extensionReceiverParameter?.copyTo(
this,
name = Name.identifier("\$receiver"),
index = offset++,
origin = IrDeclarationOrigin.MOVED_EXTENSION_RECEIVER,
remapTypeMap = typeParameterMap
)
valueParameters = listOfNotNull(dispatchReceiver, extensionReceiver) +
oldFunction.valueParameters.map {
it.copyTo(
this,
index = it.index + offset,
remapTypeMap = typeParameterMap
)
}
valueParameters = buildList {
var offset = 0
addIfNotNull(
oldFunction.dispatchReceiverParameter?.copyTo(
this@apply,
name = Name.identifier("\$this"),
index = offset++,
type = remap(dispatchReceiverType!!),
origin = IrDeclarationOrigin.MOVED_DISPATCH_RECEIVER
)
)
addAll(
oldFunction.valueParameters
.asSequence()
.take(oldFunction.contextReceiverParametersCount)
.map {
it.copyTo(
this@apply,
index = offset++,
remapTypeMap = typeParameterMap
)
}
)
addIfNotNull(
oldFunction.extensionReceiverParameter?.copyTo(
this@apply,
name = Name.identifier("\$receiver"),
index = offset++,
origin = IrDeclarationOrigin.MOVED_EXTENSION_RECEIVER,
remapTypeMap = typeParameterMap
)
)
addAll(
oldFunction.valueParameters
.asSequence()
.drop(oldFunction.contextReceiverParametersCount)
.map {
it.copyTo(
this@apply,
index = offset++,
remapTypeMap = typeParameterMap
)
}
)
}
if (copyMetadata) metadata = oldFunction.metadata
@@ -0,0 +1,18 @@
// !LANGUAGE: +ContextReceivers
// TARGET_BACKEND: JVM_IR
class Context
class Extended
class Containing {
context(Context) fun Extended.foo(obj: Any? = null) {}
}
fun box(): String {
with (Containing()) {
with (Context()) {
Extended().foo()
}
}
return "OK"
}
@@ -17474,6 +17474,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/extensionFunctions/contextReceivers/kt52207.kt");
}
@Test
@TestMetadata("kt52373.kt")
public void testKt52373() throws Exception {
runTest("compiler/testData/codegen/box/extensionFunctions/contextReceivers/kt52373.kt");
}
@Test
@TestMetadata("overload.kt")
public void testOverload() throws Exception {