diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ArgumentsUtils.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ArgumentsUtils.kt index a076ce1e263..0ac38a4b11a 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ArgumentsUtils.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ArgumentsUtils.kt @@ -69,7 +69,7 @@ fun ValueParameterDescriptor.hasDefaultValue(): Boolean { return DFS.ifAny( listOf(this), { current -> current.overriddenDescriptors.map(ValueParameterDescriptor::getOriginal) }, - { it.declaresDefaultValue() || it.isActualParameterWithAnyExpectedDefault } + { it.declaresDefaultValue() || it.isActualParameterWithCorrespondingExpectedDefault } ) } diff --git a/compiler/testData/codegen/box/defaultArguments/constructor/checkIfConstructorIsSynthetic.kt b/compiler/testData/codegen/box/defaultArguments/constructor/checkIfConstructorIsSynthetic.kt index cfd12c58f64..562c46fb3fe 100644 --- a/compiler/testData/codegen/box/defaultArguments/constructor/checkIfConstructorIsSynthetic.kt +++ b/compiler/testData/codegen/box/defaultArguments/constructor/checkIfConstructorIsSynthetic.kt @@ -1,6 +1,4 @@ -// TODO: muted automatically, investigate should it be ran for JS or not -// IGNORE_BACKEND: JS, NATIVE - +// TARGET_BACKEND: JVM // WITH_RUNTIME class A(value: Int = 1) diff --git a/compiler/testData/codegen/box/defaultArguments/constructor/manyArgs.kt b/compiler/testData/codegen/box/defaultArguments/constructor/manyArgs.kt index ac3eab5136f..9591a15c803 100644 --- a/compiler/testData/codegen/box/defaultArguments/constructor/manyArgs.kt +++ b/compiler/testData/codegen/box/defaultArguments/constructor/manyArgs.kt @@ -1,6 +1,4 @@ -// TODO: muted automatically, investigate should it be ran for JS or not -// IGNORE_BACKEND: JS, NATIVE - +// TARGET_BACKEND: JVM // WITH_RUNTIME class A(val a: Int = 1, diff --git a/compiler/testData/codegen/box/defaultArguments/function/complexInheritance.kt b/compiler/testData/codegen/box/defaultArguments/function/complexInheritance.kt new file mode 100644 index 00000000000..5a8df66d795 --- /dev/null +++ b/compiler/testData/codegen/box/defaultArguments/function/complexInheritance.kt @@ -0,0 +1,15 @@ +// See KT-21968 + +interface A { + fun f(cause: Int? = null): Boolean +} + +open class B : A { + override fun f(cause: Int?): Boolean = true +} + +class D : B(), A + +fun box(): String { + return if (D().f()) "OK" else "fail" +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/defaultArguments/function/funInTraitChain.kt b/compiler/testData/codegen/box/defaultArguments/function/funInTraitChain.kt new file mode 100644 index 00000000000..50c0b9df7ae --- /dev/null +++ b/compiler/testData/codegen/box/defaultArguments/function/funInTraitChain.kt @@ -0,0 +1,20 @@ +// IGNORE_BACKEND: JVM +// See KT-15971 + +interface Foo { + fun foo(a: Double = 1.0): Double +} + +interface FooChain: Foo + +open class Impl { + fun foo(a: Double) = a +} + +class FooImpl : FooChain, Impl() + +fun box(): String { + if (FooImpl().foo() != 1.0) return "fail" + if (FooImpl().foo(2.0) != 2.0) return "fail" + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/defaultArguments/implementedByFake2.kt b/compiler/testData/codegen/box/defaultArguments/implementedByFake2.kt index e4f4625658f..2f944ef8ab1 100644 --- a/compiler/testData/codegen/box/defaultArguments/implementedByFake2.kt +++ b/compiler/testData/codegen/box/defaultArguments/implementedByFake2.kt @@ -1,4 +1,4 @@ -// IGNORE_BACKEND: JVM, JS +// IGNORE_BACKEND: JVM interface I { val prop: T diff --git a/compiler/testData/codegen/box/defaultArguments/implementedByFake3.kt b/compiler/testData/codegen/box/defaultArguments/implementedByFake3.kt index 1d3a4ee8cbf..a7e13ed0678 100644 --- a/compiler/testData/codegen/box/defaultArguments/implementedByFake3.kt +++ b/compiler/testData/codegen/box/defaultArguments/implementedByFake3.kt @@ -1,4 +1,4 @@ -// IGNORE_BACKEND: JVM, JS +// IGNORE_BACKEND: JVM interface I { val prop: T diff --git a/compiler/testData/codegen/box/defaultArguments/superCallCheck.kt b/compiler/testData/codegen/box/defaultArguments/superCallCheck.kt index 918a68ed0d8..318c26e6331 100644 --- a/compiler/testData/codegen/box/defaultArguments/superCallCheck.kt +++ b/compiler/testData/codegen/box/defaultArguments/superCallCheck.kt @@ -1,6 +1,4 @@ -// TODO: muted automatically, investigate should it be ran for JS or not -// IGNORE_BACKEND: JS, NATIVE - +// TARGET_BACKEND: JVM // WITH_RUNTIME open class MyClass { diff --git a/compiler/testData/codegen/box/multiplatform/defaultArguments/delegatedExpectedInterface.kt b/compiler/testData/codegen/box/multiplatform/defaultArguments/delegatedExpectedInterface.kt new file mode 100644 index 00000000000..9671b495a97 --- /dev/null +++ b/compiler/testData/codegen/box/multiplatform/defaultArguments/delegatedExpectedInterface.kt @@ -0,0 +1,69 @@ +// !LANGUAGE: +MultiPlatformProjects +// IGNORE_BACKEND: JVM + +// FILE: lib.kt + +expect interface I { + fun foo(x: Int = 1): Unit +} + +// FILE: main.kt + +var log = "" +fun log(a: String) { + log += a + ";" +} + +interface C { + fun foo(x: Int): Unit { + log("C.foo($x)") + } +} + +actual interface I { + actual fun foo(x: Int): Unit +} + +class G(c: C) : C by c, I +class H(c: C) : I, C by c + +fun test1() { + log = "" + + val g1 = G(object: C {}) + g1.foo(2) + g1.foo() + val g2 = G(object: C { + override fun foo(x: Int) { + log("[2] object:C.foo($x)") + } + }) + g2.foo(2) + g2.foo() +} + +fun test2() { + log = "" + + val h1 = H(object: C {}) + h1.foo(2) + h1.foo() + val h2 = H(object: C { + override fun foo(x: Int) { + log("[2] object:C.foo($x)") + } + }) + h2.foo(2) + h2.foo() +} + + +fun box(): String { + test1() + if (log != "C.foo(2);C.foo(1);[2] object:C.foo(2);[2] object:C.foo(1);") return "fail1: $log" + + test2() + if (log != "C.foo(2);C.foo(1);[2] object:C.foo(2);[2] object:C.foo(1);") return "fail2: $log" + + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedFromExpectedInterface.kt b/compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedFromExpectedInterface.kt new file mode 100644 index 00000000000..97f349ebc2f --- /dev/null +++ b/compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedFromExpectedInterface.kt @@ -0,0 +1,17 @@ +// !LANGUAGE: +MultiPlatformProjects + +// FILE: lib.kt +expect interface I { + fun f(p: String = "OK"): String +} + +// FILE: main.kt +actual interface I { + actual fun f(p: String): String +} + +class Impl : I { + override fun f(p: String) = p +} + +fun box() = Impl().f() diff --git a/compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedViaAnotherInterfaceIndirectly.kt b/compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedViaAnotherInterfaceIndirectly.kt new file mode 100644 index 00000000000..ccb1059ca15 --- /dev/null +++ b/compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedViaAnotherInterfaceIndirectly.kt @@ -0,0 +1,48 @@ +// !LANGUAGE: +MultiPlatformProjects + +// FILE: lib.kt + +package foo + +expect interface H { + fun foo(x: String = "default"): String +} + +// FILE: main.kt +package foo + +actual interface H { + actual fun foo(x: String): String +} + +interface I: H { + override fun foo(x: String): String = "I.foo($x)" +} + +interface J : I { + override fun foo(x: String): String +} + +interface K : J { + override fun foo(x: String): String = "K.foo($x)" +} + +class A : I + +class B : K + +fun box(): String { + val a = A() + var r = a.foo() + if (r != "I.foo(default)") return "fail: A.foo()" + r = a.foo("Q") + if (r != "I.foo(Q)") return "fail A.foo(Q): $r" + + val b = B() + r = b.foo() + if (r != "K.foo(default)") return "fail B.foo(): $r" + r = b.foo("W") + if (r != "K.foo(W)") return "fail B.foo(W): $r" + + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/multiplatform/defaultArguments/superCall.kt b/compiler/testData/codegen/box/multiplatform/defaultArguments/superCall.kt new file mode 100644 index 00000000000..0a43e1942bc --- /dev/null +++ b/compiler/testData/codegen/box/multiplatform/defaultArguments/superCall.kt @@ -0,0 +1,40 @@ +// !LANGUAGE: +MultiPlatformProjects +// IGNORE_BACKEND: JVM + +// FILE: lib.kt + +package foo + +expect open class A { + open fun foo(x: Int = 20, y: Int = 3): Int +} + +// FILE: main.kt +package foo + +actual open class A { + actual open fun foo(x: Int, y: Int) = x + y +} + +open class B : A() { + override fun foo(x: Int, y: Int) = 0 + + fun bar1() = super.foo() + + fun bar2() = super.foo(30) + + fun bar3() = super.foo(y = 4) +} + +fun box(): String { + val v1 = B().bar1() + if (v1 != 23) return "fail1: $v1" + + val v2 = B().bar2() + if (v2 != 33) return "fail2: $v2" + + val v3 = B().bar3() + if (v3 != 24) return "fail3: $v3" + + return "OK" +} \ No newline at end of file diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 5b07cc4ca65..b2f67cfc8f2 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -8300,6 +8300,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/defaultArguments/function"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("complexInheritance.kt") + public void testComplexInheritance() throws Exception { + runTest("compiler/testData/codegen/box/defaultArguments/function/complexInheritance.kt"); + } + @TestMetadata("covariantOverride.kt") public void testCovariantOverride() throws Exception { runTest("compiler/testData/codegen/box/defaultArguments/function/covariantOverride.kt"); @@ -8350,6 +8355,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/defaultArguments/function/funInTrait.kt"); } + @TestMetadata("funInTraitChain.kt") + public void testFunInTraitChain() throws Exception { + runTest("compiler/testData/codegen/box/defaultArguments/function/funInTraitChain.kt"); + } + @TestMetadata("innerExtentionFunction.kt") public void testInnerExtentionFunction() throws Exception { runTest("compiler/testData/codegen/box/defaultArguments/function/innerExtentionFunction.kt"); @@ -13472,6 +13482,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/constructor.kt"); } + @TestMetadata("delegatedExpectedInterface.kt") + public void testDelegatedExpectedInterface() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/delegatedExpectedInterface.kt"); + } + @TestMetadata("function.kt") public void testFunction() throws Exception { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/function.kt"); @@ -13487,6 +13502,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedFromExpectedClass.kt"); } + @TestMetadata("inheritedFromExpectedInterface.kt") + public void testInheritedFromExpectedInterface() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedFromExpectedInterface.kt"); + } + @TestMetadata("inheritedFromExpectedMethod.kt") public void testInheritedFromExpectedMethod() throws Exception { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedFromExpectedMethod.kt"); @@ -13497,6 +13517,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedInExpectedDeclarations.kt"); } + @TestMetadata("inheritedViaAnotherInterfaceIndirectly.kt") + public void testInheritedViaAnotherInterfaceIndirectly() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedViaAnotherInterfaceIndirectly.kt"); + } + @TestMetadata("inlineFunctionWithDefaultLambda.kt") public void testInlineFunctionWithDefaultLambda() throws Exception { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inlineFunctionWithDefaultLambda.kt"); @@ -13511,6 +13536,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes public void testKt23239() throws Exception { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/kt23239.kt"); } + + @TestMetadata("superCall.kt") + public void testSuperCall() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/superCall.kt"); + } } } diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 829205a2b34..80669e9772a 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -8300,6 +8300,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/defaultArguments/function"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("complexInheritance.kt") + public void testComplexInheritance() throws Exception { + runTest("compiler/testData/codegen/box/defaultArguments/function/complexInheritance.kt"); + } + @TestMetadata("covariantOverride.kt") public void testCovariantOverride() throws Exception { runTest("compiler/testData/codegen/box/defaultArguments/function/covariantOverride.kt"); @@ -8350,6 +8355,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/defaultArguments/function/funInTrait.kt"); } + @TestMetadata("funInTraitChain.kt") + public void testFunInTraitChain() throws Exception { + runTest("compiler/testData/codegen/box/defaultArguments/function/funInTraitChain.kt"); + } + @TestMetadata("innerExtentionFunction.kt") public void testInnerExtentionFunction() throws Exception { runTest("compiler/testData/codegen/box/defaultArguments/function/innerExtentionFunction.kt"); @@ -13472,6 +13482,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/constructor.kt"); } + @TestMetadata("delegatedExpectedInterface.kt") + public void testDelegatedExpectedInterface() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/delegatedExpectedInterface.kt"); + } + @TestMetadata("function.kt") public void testFunction() throws Exception { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/function.kt"); @@ -13487,6 +13502,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedFromExpectedClass.kt"); } + @TestMetadata("inheritedFromExpectedInterface.kt") + public void testInheritedFromExpectedInterface() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedFromExpectedInterface.kt"); + } + @TestMetadata("inheritedFromExpectedMethod.kt") public void testInheritedFromExpectedMethod() throws Exception { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedFromExpectedMethod.kt"); @@ -13497,6 +13517,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedInExpectedDeclarations.kt"); } + @TestMetadata("inheritedViaAnotherInterfaceIndirectly.kt") + public void testInheritedViaAnotherInterfaceIndirectly() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedViaAnotherInterfaceIndirectly.kt"); + } + @TestMetadata("inlineFunctionWithDefaultLambda.kt") public void testInlineFunctionWithDefaultLambda() throws Exception { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inlineFunctionWithDefaultLambda.kt"); @@ -13511,6 +13536,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { public void testKt23239() throws Exception { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/kt23239.kt"); } + + @TestMetadata("superCall.kt") + public void testSuperCall() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/superCall.kt"); + } } } diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index a0dd54a65a1..8db8a0bbd15 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -8287,6 +8287,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) public static class Function extends AbstractLightAnalysisModeTest { + @TestMetadata("funInTraitChain.kt") + public void ignoreFunInTraitChain() throws Exception { + runTest("compiler/testData/codegen/box/defaultArguments/function/funInTraitChain.kt"); + } + private void runTest(String testDataFilePath) throws Exception { KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath); } @@ -8300,6 +8305,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/defaultArguments/function"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("complexInheritance.kt") + public void testComplexInheritance() throws Exception { + runTest("compiler/testData/codegen/box/defaultArguments/function/complexInheritance.kt"); + } + @TestMetadata("covariantOverride.kt") public void testCovariantOverride() throws Exception { runTest("compiler/testData/codegen/box/defaultArguments/function/covariantOverride.kt"); @@ -13454,6 +13464,16 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) public static class DefaultArguments extends AbstractLightAnalysisModeTest { + @TestMetadata("delegatedExpectedInterface.kt") + public void ignoreDelegatedExpectedInterface() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/delegatedExpectedInterface.kt"); + } + + @TestMetadata("superCall.kt") + public void ignoreSuperCall() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/superCall.kt"); + } + private void runTest(String testDataFilePath) throws Exception { KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath); } @@ -13487,6 +13507,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedFromExpectedClass.kt"); } + @TestMetadata("inheritedFromExpectedInterface.kt") + public void testInheritedFromExpectedInterface() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedFromExpectedInterface.kt"); + } + @TestMetadata("inheritedFromExpectedMethod.kt") public void testInheritedFromExpectedMethod() throws Exception { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedFromExpectedMethod.kt"); @@ -13497,6 +13522,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedInExpectedDeclarations.kt"); } + @TestMetadata("inheritedViaAnotherInterfaceIndirectly.kt") + public void testInheritedViaAnotherInterfaceIndirectly() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedViaAnotherInterfaceIndirectly.kt"); + } + @TestMetadata("inlineFunctionWithDefaultLambda.kt") public void testInlineFunctionWithDefaultLambda() throws Exception { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inlineFunctionWithDefaultLambda.kt"); diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt index b00d57375ba..6d3a1dd62d8 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt @@ -197,14 +197,6 @@ fun ValueParameterDescriptor.declaresOrInheritsDefaultValue(): Boolean { ) } -fun FunctionDescriptor.hasOrInheritsParametersWithDefaultValue(): Boolean = DFS.ifAny( - listOf(this), - { current -> current.overriddenDescriptors.map { it.original } }, - { it.hasOwnParametersWithDefaultValue() } -) - -fun FunctionDescriptor.hasOwnParametersWithDefaultValue() = original.valueParameters.any { it.declaresDefaultValue() } - fun Annotated.isRepeatableAnnotation(): Boolean = annotations.findAnnotation(KotlinBuiltIns.FQ_NAMES.repeatable) != null diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 02322bcc1f1..852489a89f0 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -6743,11 +6743,6 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/defaultArguments/simpleFromOtherFile.kt"); } - @TestMetadata("superCallCheck.kt") - public void testSuperCallCheck() throws Exception { - runTest("compiler/testData/codegen/box/defaultArguments/superCallCheck.kt"); - } - @TestMetadata("compiler/testData/codegen/box/defaultArguments/constructor") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -6770,11 +6765,6 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/defaultArguments/constructor/annotationWithEmptyArray.kt"); } - @TestMetadata("checkIfConstructorIsSynthetic.kt") - public void testCheckIfConstructorIsSynthetic() throws Exception { - runTest("compiler/testData/codegen/box/defaultArguments/constructor/checkIfConstructorIsSynthetic.kt"); - } - @TestMetadata("defArgs1.kt") public void testDefArgs1() throws Exception { runTest("compiler/testData/codegen/box/defaultArguments/constructor/defArgs1.kt"); @@ -6824,11 +6814,6 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { public void testKt3060() throws Exception { runTest("compiler/testData/codegen/box/defaultArguments/constructor/kt3060.kt"); } - - @TestMetadata("manyArgs.kt") - public void testManyArgs() throws Exception { - runTest("compiler/testData/codegen/box/defaultArguments/constructor/manyArgs.kt"); - } } @TestMetadata("compiler/testData/codegen/box/defaultArguments/convention") @@ -6876,6 +6861,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/defaultArguments/function"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true); } + @TestMetadata("complexInheritance.kt") + public void testComplexInheritance() throws Exception { + runTest("compiler/testData/codegen/box/defaultArguments/function/complexInheritance.kt"); + } + @TestMetadata("covariantOverride.kt") public void testCovariantOverride() throws Exception { runTest("compiler/testData/codegen/box/defaultArguments/function/covariantOverride.kt"); @@ -6926,6 +6916,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/defaultArguments/function/funInTrait.kt"); } + @TestMetadata("funInTraitChain.kt") + public void testFunInTraitChain() throws Exception { + runTest("compiler/testData/codegen/box/defaultArguments/function/funInTraitChain.kt"); + } + @TestMetadata("innerExtentionFunction.kt") public void testInnerExtentionFunction() throws Exception { runTest("compiler/testData/codegen/box/defaultArguments/function/innerExtentionFunction.kt"); @@ -11713,6 +11708,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/constructor.kt"); } + @TestMetadata("delegatedExpectedInterface.kt") + public void testDelegatedExpectedInterface() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/delegatedExpectedInterface.kt"); + } + @TestMetadata("function.kt") public void testFunction() throws Exception { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/function.kt"); @@ -11728,6 +11728,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedFromExpectedClass.kt"); } + @TestMetadata("inheritedFromExpectedInterface.kt") + public void testInheritedFromExpectedInterface() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedFromExpectedInterface.kt"); + } + @TestMetadata("inheritedFromExpectedMethod.kt") public void testInheritedFromExpectedMethod() throws Exception { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedFromExpectedMethod.kt"); @@ -11738,6 +11743,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedInExpectedDeclarations.kt"); } + @TestMetadata("inheritedViaAnotherInterfaceIndirectly.kt") + public void testInheritedViaAnotherInterfaceIndirectly() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inheritedViaAnotherInterfaceIndirectly.kt"); + } + @TestMetadata("inlineFunctionWithDefaultLambda.kt") public void testInlineFunctionWithDefaultLambda() throws Exception { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/inlineFunctionWithDefaultLambda.kt"); @@ -11747,6 +11757,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { public void testKt23239() throws Exception { runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/kt23239.kt"); } + + @TestMetadata("superCall.kt") + public void testSuperCall() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/defaultArguments/superCall.kt"); + } } } diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/ClassModelGenerator.kt b/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/ClassModelGenerator.kt index 24ebcbc892f..acddf2e7a20 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/ClassModelGenerator.kt +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/ClassModelGenerator.kt @@ -24,12 +24,9 @@ import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.js.backend.ast.* import org.jetbrains.kotlin.js.translate.context.Namer import org.jetbrains.kotlin.js.translate.context.TranslationContext -import org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils -import org.jetbrains.kotlin.js.translate.utils.JsAstUtils +import org.jetbrains.kotlin.js.translate.utils.* import org.jetbrains.kotlin.js.translate.utils.JsAstUtils.prototypeOf import org.jetbrains.kotlin.js.translate.utils.JsAstUtils.pureFqn -import org.jetbrains.kotlin.js.translate.utils.TranslationUtils -import org.jetbrains.kotlin.js.translate.utils.generateDelegateCall import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.descriptorUtil.* import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter @@ -85,9 +82,10 @@ class ClassModelGenerator(val context: TranslationContext) { // However, D inherits `foo` without suffix (i.e. it corresponds to I's dispatcher function). // We must copy B.foo to D.foo$default and then I.foo to D.foo private fun tryCopyWhenImplementingInterfaceWithDefaultArgs(member: FunctionDescriptor, model: JsClassModel): Boolean { - val fromInterface = member.overriddenDescriptors.firstOrNull { it.hasOwnParametersWithDefaultValue() } ?: return false + val fromInterface = member.overriddenDescriptors.firstOrNull { it.hasOrInheritsParametersWithDefaultValue() } ?: return false if (!DescriptorUtils.isInterface(fromInterface.containingDeclaration)) return false val fromClass = member.overriddenDescriptors.firstOrNull { !DescriptorUtils.isInterface(it.containingDeclaration) } ?: return false + if (fromClass.hasOrInheritsParametersWithDefaultValue()) return false val targetClass = member.containingDeclaration as ClassDescriptor val fromInterfaceName = context.getNameForDescriptor(fromInterface).ident diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/DeclarationBodyVisitor.kt b/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/DeclarationBodyVisitor.kt index 01a3cb59b04..d059efbd7c0 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/DeclarationBodyVisitor.kt +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/DeclarationBodyVisitor.kt @@ -28,9 +28,6 @@ import org.jetbrains.kotlin.js.translate.utils.BindingUtils.getClassDescriptor import org.jetbrains.kotlin.js.translate.utils.JsAstUtils.pureFqn import org.jetbrains.kotlin.js.translate.utils.JsDescriptorUtils.getSupertypesWithoutFakes import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.resolve.calls.components.hasDefaultValue -import org.jetbrains.kotlin.resolve.calls.components.isActualParameterWithCorrespondingExpectedDefault -import org.jetbrains.kotlin.resolve.descriptorUtil.hasOwnParametersWithDefaultValue class DeclarationBodyVisitor( private val containingClass: ClassDescriptor, @@ -111,12 +108,8 @@ class DeclarationBodyVisitor( initializerStatements.add(statement) } - private fun hasParametersWithDefaultValue(descriptor: FunctionDescriptor) = descriptor.valueParameters.any { it.hasDefaultValue() } - private fun hasCorrespondingExpectParametersWithDefaultValue(descriptor: FunctionDescriptor) = - descriptor.valueParameters.any { it.isActualParameterWithCorrespondingExpectedDefault } - override fun addFunction(descriptor: FunctionDescriptor, expression: JsExpression?, psi: KtElement?) { - if (!hasParametersWithDefaultValue(descriptor) || !descriptor.isOverridableOrOverrides) { + if (!descriptor.hasOrInheritsParametersWithDefaultValue() || !descriptor.isOverridableOrOverrides) { if (expression != null) { context.addDeclarationStatement(context.addFunctionToPrototype(containingClass, descriptor, expression)) } @@ -130,7 +123,7 @@ class DeclarationBodyVisitor( context.addDeclarationStatement(JsAstUtils.assignment(functionRef, expression).makeStmt()) } - if (descriptor.hasOwnParametersWithDefaultValue() || hasCorrespondingExpectParametersWithDefaultValue(descriptor)) { + if (descriptor.hasOwnParametersWithDefaultValue()) { val caller = JsFunction(context.getScopeForDescriptor(containingClass), JsBlock(), "") caller.source = psi?.finalElement val callerContext = context diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/TranslationUtils.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/TranslationUtils.java index 37233de1a0a..c855e000bbe 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/TranslationUtils.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/TranslationUtils.java @@ -48,6 +48,7 @@ import java.util.stream.Collectors; import static org.jetbrains.kotlin.js.backend.ast.JsBinaryOperator.*; import static org.jetbrains.kotlin.js.translate.utils.BindingUtils.getCallableDescriptorForOperationExpression; import static org.jetbrains.kotlin.js.translate.utils.JsAstUtils.*; +import static org.jetbrains.kotlin.js.translate.utils.UtilsKt.hasOrInheritsParametersWithDefaultValue; public final class TranslationUtils { private static final Set CLASSES_WITH_NON_BOXED_CHARS = new HashSet<>(Arrays.asList( @@ -405,10 +406,10 @@ public final class TranslationUtils { } public static boolean isOverridableFunctionWithDefaultParameters(@NotNull FunctionDescriptor descriptor) { - return DescriptorUtilsKt.hasOrInheritsParametersWithDefaultValue(descriptor) && - !(descriptor instanceof ConstructorDescriptor) && - descriptor.getContainingDeclaration() instanceof ClassDescriptor && - ModalityKt.isOverridable(descriptor); + return hasOrInheritsParametersWithDefaultValue(descriptor) && + !(descriptor instanceof ConstructorDescriptor) && + descriptor.getContainingDeclaration() instanceof ClassDescriptor && + ModalityKt.isOverridable(descriptor); } @NotNull diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/utils.kt b/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/utils.kt index 592ef1274e9..eb6a6e70de0 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/utils.kt +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/utils.kt @@ -28,11 +28,12 @@ import org.jetbrains.kotlin.js.translate.utils.TranslationUtils.simpleReturnFunc import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.DescriptorUtils +import org.jetbrains.kotlin.resolve.calls.components.isActualParameterWithCorrespondingExpectedDefault import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall -import org.jetbrains.kotlin.resolve.descriptorUtil.hasOrInheritsParametersWithDefaultValue import org.jetbrains.kotlin.resolve.source.getPsi import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.TypeUtils +import org.jetbrains.kotlin.utils.DFS fun generateDelegateCall( classDescriptor: ClassDescriptor, @@ -270,4 +271,13 @@ fun TranslationContext.getPrimitiveNumericComparisonInfo(expression: KtExpressio } else { null } -} \ No newline at end of file +} + +fun FunctionDescriptor.hasOrInheritsParametersWithDefaultValue(): Boolean = DFS.ifAny( + listOf(this), + { current -> current.overriddenDescriptors.map { it.original } }, + { it.hasOwnParametersWithDefaultValue() } +) + +fun FunctionDescriptor.hasOwnParametersWithDefaultValue() = + original.valueParameters.any { it.declaresDefaultValue() || it.isActualParameterWithCorrespondingExpectedDefault } \ No newline at end of file