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"); 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 @Test
@TestMetadata("overload.kt") @TestMetadata("overload.kt")
public void testOverload() throws Exception { 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 { when {
receiversAsArguments && srcFunction.extensionReceiverParameter != null -> { receiversAsArguments && srcFunction.extensionReceiverParameter != null -> {
putValueArgument(destValueArgumentIndex++, src.extensionReceiver) putValueArgument(destValueArgumentIndex++, src.extensionReceiver)
@@ -1188,29 +1192,55 @@ fun IrFactory.createStaticFunctionWithReceivers(
annotations = oldFunction.annotations annotations = oldFunction.annotations
valueParameters = buildList {
var offset = 0 var offset = 0
val dispatchReceiver = oldFunction.dispatchReceiverParameter?.copyTo(
this, addIfNotNull(
oldFunction.dispatchReceiverParameter?.copyTo(
this@apply,
name = Name.identifier("\$this"), name = Name.identifier("\$this"),
index = offset++, index = offset++,
type = remap(dispatchReceiverType!!), type = remap(dispatchReceiverType!!),
origin = IrDeclarationOrigin.MOVED_DISPATCH_RECEIVER origin = IrDeclarationOrigin.MOVED_DISPATCH_RECEIVER
) )
val extensionReceiver = oldFunction.extensionReceiverParameter?.copyTo( )
this,
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"), name = Name.identifier("\$receiver"),
index = offset++, index = offset++,
origin = IrDeclarationOrigin.MOVED_EXTENSION_RECEIVER, origin = IrDeclarationOrigin.MOVED_EXTENSION_RECEIVER,
remapTypeMap = typeParameterMap remapTypeMap = typeParameterMap
) )
valueParameters = listOfNotNull(dispatchReceiver, extensionReceiver) + )
oldFunction.valueParameters.map {
addAll(
oldFunction.valueParameters
.asSequence()
.drop(oldFunction.contextReceiverParametersCount)
.map {
it.copyTo( it.copyTo(
this, this@apply,
index = it.index + offset, index = offset++,
remapTypeMap = typeParameterMap remapTypeMap = typeParameterMap
) )
} }
)
}
if (copyMetadata) metadata = oldFunction.metadata 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"); 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 @Test
@TestMetadata("overload.kt") @TestMetadata("overload.kt")
public void testOverload() throws Exception { public void testOverload() throws Exception {