Files
kotlin-fork/compiler/testData/codegen/box/delegation/withDefaultParameters.kt
T
Dmitry Petrov 2f82c5b6af JVM: Fix default parameter values handling
When we generate call for 'foo', we make decision about invoking
a 'foo$default' too late, after the call arguments are generated.
If 'foo' was an override, and base class (interface) was generic,
'foo' in base class could have a different Kotlin and JVM
signature, so the arguments we generated could be generated wrong
(primitive or inline class values instead of boxes, see KT-38680).
Also, we always selected first base class in supertypes list,
which caused KT-15971.

Look into resolved call and see if we should actually call
'foo$default' instead of 'foo' when determining actual callable.

Overrides can't introduce default parameter values, and
override-equivalent inherited methods with default parameters
is an error in a child class. Thus, if we are calling a class
member function with a default parameters, there should be one
and only one overridden function that has default parameter values
and overrides nothing.
2020-05-20 07:19:29 +03:00

60 lines
989 B
Kotlin
Vendored

// IGNORE_BACKEND_FIR: JVM_IR
var log = ""
fun log(a: String) {
log += a + ";"
}
interface C {
fun foo(x: Int): Unit {
log("C.foo($x)")
}
}
interface I {
fun foo(x: Int = 1): 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"
}