[JS IR] Use type upper bounds for calculating function signatures

^KT-59239 Fixed
This commit is contained in:
Alexander Korepanov
2023-06-12 13:41:15 +02:00
committed by Space Team
parent a4d40498c7
commit fc898c7620
20 changed files with 163 additions and 26 deletions
@@ -1,5 +1,8 @@
abstract class Foo<out E> {
// DONT_TARGET_EXACT_BACKEND: JS
abstract class Foo<out E>: Collection<E> {
abstract fun foo(element: @UnsafeVariance E): Boolean
abstract override fun contains(element: @UnsafeVariance E): Boolean
}
class Bar<E : C> : Foo<E>() {
@@ -7,15 +10,49 @@ class Bar<E : C> : Foo<E>() {
if (element !is C?) return false
return true
}
override fun contains(element: E): Boolean {
if (element !is C?) return false
return true
}
override val size: Int get() = -1
override fun isEmpty() = true
override fun containsAll(elements: Collection<E>) = false
override fun iterator(): Iterator<E> = TODO("Not yet implemented")
}
open class C
open class D : C()
fun box(): String {
fun case1(): Int {
val a = (object{})
val foo: Foo<Any?> = Bar<D>()
if (foo.foo(a as Any?)) return "fail"
try {
if (foo.foo(a as Any?))
return 1
return 2
} catch (e: ClassCastException) {
return 0
}
}
fun case2(): Int {
val a = (object{})
val foo: Collection<Any?> = Bar<D>()
// compiler knows about this "special" method and it adds an implicit type check which prevents class cast exception
if (foo.contains(a as Any?))
return 1
return 0
}
fun box(): String {
var r = case1()
if (r != 0) return "Fail case 1, got $r"
r = case2()
if (r != 0) return "Fail case 2, got $r"
return "OK"
}
}
@@ -1,7 +1,7 @@
// EXPECTED_REACHABLE_NODES: 1285
package foo
// CHECK_CONTAINS_NO_CALLS: myMultiply except=A;imul;new_foo_A_16tm4z_k$
// CHECK_CONTAINS_NO_CALLS: myMultiply except=A;imul;new_foo_A_rthawl_k$
internal class A(val a: Int)
@@ -15,4 +15,4 @@ fun box(): String {
assertEquals(105, myMultiply(3, 5, 7))
return "OK"
}
}
+7 -7
View File
@@ -115,9 +115,9 @@ private class A {
// CHECK_FUNCTION_EXISTS: get_p13_s8ev3n$ TARGET_BACKENDS=JS
// CHECK_NOT_CALLED_IN_SCOPE: function=get_p13_s8ev3n$ scope=box TARGET_BACKENDS=JS
// CHECK_CALLED_IN_SCOPE: function=set_p13_dqglrj$ scope=box TARGET_BACKENDS=JS
// CHECK_FUNCTION_EXISTS: get_p13_s8qimu_k$ IGNORED_BACKENDS=JS
// CHECK_NOT_CALLED_IN_SCOPE: function=get_p13_s8qimu_k$ scope=box IGNORED_BACKENDS=JS
// CHECK_CALLED_IN_SCOPE: function=set_p13_8qdsq6_k$ scope=box IGNORED_BACKENDS=JS
// CHECK_FUNCTION_EXISTS: get_p13_v9rv0h_k$ IGNORED_BACKENDS=JS
// CHECK_NOT_CALLED_IN_SCOPE: function=get_p13_v9rv0h_k$ scope=box IGNORED_BACKENDS=JS
// CHECK_CALLED_IN_SCOPE: function=set_p13_7j3jda_k$ scope=box IGNORED_BACKENDS=JS
var Int.p13: Int
inline get() = this * 100 + a + 130000
set(v) {
@@ -127,9 +127,9 @@ private class A {
// CHECK_CALLED_IN_SCOPE: function=get_p14_s8ev3n$ scope=box TARGET_BACKENDS=JS
// CHECK_FUNCTION_EXISTS: set_p14_dqglrj$ TARGET_BACKENDS=JS
// CHECK_NOT_CALLED_IN_SCOPE: function=set_p14_dqglrj$ scope=box TARGET_BACKENDS=JS
// CHECK_CALLED_IN_SCOPE: function=get_p14_yfdnt5_k$ scope=box IGNORED_BACKENDS=JS
// CHECK_FUNCTION_EXISTS: set_p14_uaac7n_k$ IGNORED_BACKENDS=JS
// CHECK_NOT_CALLED_IN_SCOPE: function=set_p14_uaac7n_k$ scope=box IGNORED_BACKENDS=JS
// CHECK_CALLED_IN_SCOPE: function=get_p14_7twbq6_k$ scope=box IGNORED_BACKENDS=JS
// CHECK_FUNCTION_EXISTS: set_p14_fdbk5p_k$ IGNORED_BACKENDS=JS
// CHECK_NOT_CALLED_IN_SCOPE: function=set_p14_fdbk5p_k$ scope=box IGNORED_BACKENDS=JS
var Int.p14: Int
get() = this * 100 + a + 140000
inline set(v) {
@@ -224,4 +224,4 @@ fun box(): String {
if (a.p17 != 171717) return "test17: ${a.p17}"
return "OK"
}
}