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
var offset = 0 valueParameters = buildList {
val dispatchReceiver = oldFunction.dispatchReceiverParameter?.copyTo( var offset = 0
this,
name = Name.identifier("\$this"), addIfNotNull(
index = offset++, oldFunction.dispatchReceiverParameter?.copyTo(
type = remap(dispatchReceiverType!!), this@apply,
origin = IrDeclarationOrigin.MOVED_DISPATCH_RECEIVER name = Name.identifier("\$this"),
) index = offset++,
val extensionReceiver = oldFunction.extensionReceiverParameter?.copyTo( type = remap(dispatchReceiverType!!),
this, origin = IrDeclarationOrigin.MOVED_DISPATCH_RECEIVER
name = Name.identifier("\$receiver"), )
index = offset++, )
origin = IrDeclarationOrigin.MOVED_EXTENSION_RECEIVER,
remapTypeMap = typeParameterMap addAll(
) oldFunction.valueParameters
valueParameters = listOfNotNull(dispatchReceiver, extensionReceiver) + .asSequence()
oldFunction.valueParameters.map { .take(oldFunction.contextReceiverParametersCount)
it.copyTo( .map {
this, it.copyTo(
index = it.index + offset, this@apply,
remapTypeMap = typeParameterMap 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 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 {