Support non-trivial default argument values in expected functions on JVM

#KT-22818 Fixed
This commit is contained in:
Alexander Udalov
2019-02-20 19:30:54 +01:00
parent b2cc661783
commit b8bc79e17c
16 changed files with 297 additions and 7 deletions
@@ -100,7 +100,8 @@ import static org.jetbrains.kotlin.codegen.JvmCodegenUtil.*;
import static org.jetbrains.kotlin.codegen.binding.CodegenBinding.*;
import static org.jetbrains.kotlin.codegen.inline.InlineCodegenUtilsKt.*;
import static org.jetbrains.kotlin.resolve.BindingContext.*;
import static org.jetbrains.kotlin.resolve.BindingContextUtils.*;
import static org.jetbrains.kotlin.resolve.BindingContextUtils.getDelegationConstructorCall;
import static org.jetbrains.kotlin.resolve.BindingContextUtils.isBoxedLocalCapturedInClosure;
import static org.jetbrains.kotlin.resolve.DescriptorUtils.*;
import static org.jetbrains.kotlin.resolve.jvm.AsmTypes.*;
import static org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils.isFunctionExpression;
@@ -2900,7 +2901,9 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
while (cur != null) {
ClassDescriptor thisDescriptor = cur.getThisDescriptor();
if (!isSuper && thisDescriptor == thisOrOuterClass) {
// We use equals on type constructors (instead of reference equality on classes) to support the case where default parameter
// values of actual functions loaded from the expected function refer to the expected class.
if (!isSuper && thisDescriptor.getTypeConstructor().equals(thisOrOuterClass.getTypeConstructor())) {
return result;
}
@@ -21,10 +21,9 @@ import com.intellij.openapi.util.Trinity
import gnu.trove.TObjectIntHashMap
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.org.objectweb.asm.Type
import java.util.*
import java.util.ArrayList
class FrameMap : FrameMapBase<DeclarationDescriptor>()
open class FrameMap : FrameMapBase<DeclarationDescriptor>()
open class FrameMapBase<T : Any> {
private val myVarIndex = TObjectIntHashMap<T>()
@@ -61,7 +60,7 @@ open class FrameMapBase<T : Any> {
currentSize -= type.size
}
fun getIndex(descriptor: T): Int {
open fun getIndex(descriptor: T): Int {
return if (myVarIndex.contains(descriptor)) myVarIndex.get(descriptor) else -1
}
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.codegen
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.resolve.multiplatform.ExpectedActualResolver
import org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver
/**
* This [FrameMap] subclass substitutes values declared in the expected declaration with the corresponding value in the actual declaration,
* which is needed for the case when expected function declares parameters with default values, which refer to other parameters.
*/
class FrameMapWithExpectActualSupport(private val module: ModuleDescriptor) : FrameMap() {
override fun getIndex(descriptor: DeclarationDescriptor): Int {
val tmp = if (descriptor is ParameterDescriptor) findActualParameter(descriptor) ?: descriptor else descriptor
return super.getIndex(tmp)
}
private fun findActualParameter(parameter: ParameterDescriptor): ParameterDescriptor? {
val container = parameter.containingDeclaration
if (container !is CallableMemberDescriptor || !container.isExpect) return null
// Generation of value parameters is supported by the fact that FunctionCodegen.generateDefaultImplBody substitutes value parameters
// of the generated actual function with the parameters of the expected declaration in the first place.
// Generation of dispatch receiver parameters (this and outer receiver values) is supported
// in ExpressionCodegen.generateThisOrOuterFromContext by comparing classes by type constructor equality.
if (parameter !is ReceiverParameterDescriptor || parameter.value !is ExtensionReceiver) return null
val actual = with(ExpectedActualResolver) {
container.findCompatibleActualForExpected(module).firstOrNull()
}
return (actual as? CallableDescriptor)?.extensionReceiverParameter
}
}
@@ -1360,7 +1360,7 @@ public class FunctionCodegen {
@NotNull List<ValueParameterDescriptor> valueParameters,
boolean isStatic
) {
FrameMap frameMap = new FrameMap();
FrameMap frameMap = new FrameMapWithExpectActualSupport(state.getModule());
if (!isStatic) {
frameMap.enterTemp(OBJECT_TYPE);
}
@@ -0,0 +1,18 @@
// !LANGUAGE: +MultiPlatformProjects
// IGNORE_BACKEND: JVM_IR
// FILE: common.kt
expect class C {
val value: String
fun test(result: String = value): String
}
// FILE: platform.kt
actual class C(actual val value: String) {
actual fun test(result: String): String = result
}
fun box() = C("Fail").test("OK")
@@ -0,0 +1,16 @@
// !LANGUAGE: +MultiPlatformProjects
// IGNORE_BACKEND: JVM_IR
// FILE: common.kt
class Receiver(val value: String)
expect fun Receiver.test(result: String = value): String
// FILE: platform.kt
actual fun Receiver.test(result: String): String {
return result
}
fun box() = Receiver("Fail").test("OK")
@@ -0,0 +1,17 @@
// !LANGUAGE: +MultiPlatformProjects
// IGNORE_BACKEND: JVM_IR
// FILE: common.kt
class B(val value: Int)
expect fun test(a: Int = 2, b: Int = B(a * 2).value, c: String = "${b}$a"): String
// FILE: platform.kt
actual fun test(a: Int, b: Int, c: String): String = c
fun box(): String {
val result = test()
return if (result == "42") "OK" else "Fail: $result"
}
@@ -0,0 +1,31 @@
// !LANGUAGE: +MultiPlatformProjects
// NO_CHECK_LAMBDA_INLINING
// IGNORE_BACKEND: JVM_IR
// FILE: 1.kt
class C(val s1: String, val s2: String)
expect fun C.test(r1: () -> String = { s1 }, r2: () -> String = this::s2): String
actual inline fun C.test(r1: () -> String, r2: () -> String): String = r1() + r2()
expect class D {
val s1: String
val s2: String
fun test(r1: () -> String = { s1 }, r2: () -> String = this::s2): String
}
actual class D(actual val s1: String, actual val s2: String) {
actual inline fun test(r1: () -> String, r2: () -> String): String = r1() + r2()
}
// FILE: 2.kt
fun box(): String {
if (C("O", "K").test() != "OK") return "Fail extension receiver"
if (D("O", "K").test() != "OK") return "Fail dispatch receiver"
return "OK"
}
@@ -15875,6 +15875,16 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/delegatedExpectedInterface.kt");
}
@TestMetadata("dispatchReceiverValue.kt")
public void testDispatchReceiverValue() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/dispatchReceiverValue.kt");
}
@TestMetadata("extensionReceiverValue.kt")
public void testExtensionReceiverValue() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/extensionReceiverValue.kt");
}
@TestMetadata("function.kt")
public void testFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/function.kt");
@@ -15930,6 +15940,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/kt23739.kt");
}
@TestMetadata("parametersInArgumentValues.kt")
public void testParametersInArgumentValues() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/parametersInArgumentValues.kt");
}
@TestMetadata("superCall.kt")
public void testSuperCall() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/superCall.kt");
@@ -1890,6 +1890,37 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo
}
}
@TestMetadata("compiler/testData/codegen/boxInline/multiplatform")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Multiplatform extends AbstractBlackBoxInlineCodegenTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInMultiplatform() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/multiplatform"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("compiler/testData/codegen/boxInline/multiplatform/defaultArguments")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class DefaultArguments extends AbstractBlackBoxInlineCodegenTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInDefaultArguments() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/multiplatform/defaultArguments"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("receiversAndParametersInLambda.kt")
public void testReceiversAndParametersInLambda() throws Exception {
runTest("compiler/testData/codegen/boxInline/multiplatform/defaultArguments/receiversAndParametersInLambda.kt");
}
}
}
@TestMetadata("compiler/testData/codegen/boxInline/noInline")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -1890,6 +1890,37 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi
}
}
@TestMetadata("compiler/testData/codegen/boxInline/multiplatform")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Multiplatform extends AbstractCompileKotlinAgainstInlineKotlinTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInMultiplatform() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/multiplatform"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("compiler/testData/codegen/boxInline/multiplatform/defaultArguments")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class DefaultArguments extends AbstractCompileKotlinAgainstInlineKotlinTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInDefaultArguments() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/multiplatform/defaultArguments"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("receiversAndParametersInLambda.kt")
public void testReceiversAndParametersInLambda() throws Exception {
runTest("compiler/testData/codegen/boxInline/multiplatform/defaultArguments/receiversAndParametersInLambda.kt");
}
}
}
@TestMetadata("compiler/testData/codegen/boxInline/noInline")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -15880,6 +15880,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/constructor.kt");
}
@TestMetadata("dispatchReceiverValue.kt")
public void testDispatchReceiverValue() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/dispatchReceiverValue.kt");
}
@TestMetadata("extensionReceiverValue.kt")
public void testExtensionReceiverValue() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/extensionReceiverValue.kt");
}
@TestMetadata("function.kt")
public void testFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/function.kt");
@@ -15934,6 +15944,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
public void testKt23739() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/kt23739.kt");
}
@TestMetadata("parametersInArgumentValues.kt")
public void testParametersInArgumentValues() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/parametersInArgumentValues.kt");
}
}
}
@@ -15880,6 +15880,16 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/delegatedExpectedInterface.kt");
}
@TestMetadata("dispatchReceiverValue.kt")
public void testDispatchReceiverValue() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/dispatchReceiverValue.kt");
}
@TestMetadata("extensionReceiverValue.kt")
public void testExtensionReceiverValue() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/extensionReceiverValue.kt");
}
@TestMetadata("function.kt")
public void testFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/function.kt");
@@ -15935,6 +15945,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/kt23739.kt");
}
@TestMetadata("parametersInArgumentValues.kt")
public void testParametersInArgumentValues() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/parametersInArgumentValues.kt");
}
@TestMetadata("superCall.kt")
public void testSuperCall() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/superCall.kt");
@@ -1890,6 +1890,37 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli
}
}
@TestMetadata("compiler/testData/codegen/boxInline/multiplatform")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Multiplatform extends AbstractIrBlackBoxInlineCodegenTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM_IR, testDataFilePath);
}
public void testAllFilesPresentInMultiplatform() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/multiplatform"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true);
}
@TestMetadata("compiler/testData/codegen/boxInline/multiplatform/defaultArguments")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class DefaultArguments extends AbstractIrBlackBoxInlineCodegenTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM_IR, testDataFilePath);
}
public void testAllFilesPresentInDefaultArguments() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/boxInline/multiplatform/defaultArguments"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true);
}
@TestMetadata("receiversAndParametersInLambda.kt")
public void testReceiversAndParametersInLambda() throws Exception {
runTest("compiler/testData/codegen/boxInline/multiplatform/defaultArguments/receiversAndParametersInLambda.kt");
}
}
}
@TestMetadata("compiler/testData/codegen/boxInline/noInline")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -12265,6 +12265,16 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/delegatedExpectedInterface.kt");
}
@TestMetadata("dispatchReceiverValue.kt")
public void testDispatchReceiverValue() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/dispatchReceiverValue.kt");
}
@TestMetadata("extensionReceiverValue.kt")
public void testExtensionReceiverValue() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/extensionReceiverValue.kt");
}
@TestMetadata("function.kt")
public void testFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/function.kt");
@@ -12315,6 +12325,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/kt23739.kt");
}
@TestMetadata("parametersInArgumentValues.kt")
public void testParametersInArgumentValues() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/parametersInArgumentValues.kt");
}
@TestMetadata("superCall.kt")
public void testSuperCall() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/superCall.kt");
@@ -13360,6 +13360,16 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/delegatedExpectedInterface.kt");
}
@TestMetadata("dispatchReceiverValue.kt")
public void testDispatchReceiverValue() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/dispatchReceiverValue.kt");
}
@TestMetadata("extensionReceiverValue.kt")
public void testExtensionReceiverValue() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/extensionReceiverValue.kt");
}
@TestMetadata("function.kt")
public void testFunction() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/function.kt");
@@ -13410,6 +13420,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/kt23739.kt");
}
@TestMetadata("parametersInArgumentValues.kt")
public void testParametersInArgumentValues() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/parametersInArgumentValues.kt");
}
@TestMetadata("superCall.kt")
public void testSuperCall() throws Exception {
runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/superCall.kt");