JS: more default arguments fixes (KT-24413, KT-21968 fixed)

MPP-related:
* inherited from interfaces
* inherited body from interface
* default arguments in an interface, implemented by a class delegate
* super call of a method with default argument

Also:
* inheritance from an interface and another interface descendant (KT-21968)
* inheritance through an intermediate interface
This commit is contained in:
Anton Bannykh
2018-05-15 14:55:59 +03:00
parent da7b59984f
commit 03e46ce0ca
21 changed files with 357 additions and 55 deletions
@@ -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 }
)
}
@@ -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)
@@ -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,
@@ -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"
}
@@ -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"
}
@@ -1,4 +1,4 @@
// IGNORE_BACKEND: JVM, JS
// IGNORE_BACKEND: JVM
interface I<T> {
val prop: T
@@ -1,4 +1,4 @@
// IGNORE_BACKEND: JVM, JS
// IGNORE_BACKEND: JVM
interface I<T> {
val prop: T
@@ -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 {
@@ -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"
}
@@ -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()
@@ -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"
}
@@ -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"
}
@@ -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");
}
}
}
@@ -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");
}
}
}
@@ -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");
@@ -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
@@ -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");
}
}
}
@@ -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
@@ -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
@@ -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<FqNameUnsafe> 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
@@ -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
}
}
}
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 }