[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
@@ -2942,6 +2942,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/numberMixedHierarchy.kt"); runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/numberMixedHierarchy.kt");
} }
@Test
@TestMetadata("overrideAbstractSetMethod.kt")
public void testOverrideAbstractSetMethod() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/overrideAbstractSetMethod.kt");
}
@Test @Test
@TestMetadata("removeAtBridgeToJavaClass.kt") @TestMetadata("removeAtBridgeToJavaClass.kt")
public void testRemoveAtBridgeToJavaClass() throws Exception { public void testRemoveAtBridgeToJavaClass() throws Exception {
@@ -2942,6 +2942,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/numberMixedHierarchy.kt"); runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/numberMixedHierarchy.kt");
} }
@Test
@TestMetadata("overrideAbstractSetMethod.kt")
public void testOverrideAbstractSetMethod() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/overrideAbstractSetMethod.kt");
}
@Test @Test
@TestMetadata("removeAtBridgeToJavaClass.kt") @TestMetadata("removeAtBridgeToJavaClass.kt")
public void testRemoveAtBridgeToJavaClass() throws Exception { public void testRemoveAtBridgeToJavaClass() throws Exception {
@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsManglerIr
import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.symbols.IrReturnableBlockSymbol import org.jetbrains.kotlin.ir.symbols.IrReturnableBlockSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.isUnit import org.jetbrains.kotlin.ir.types.isUnit
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
@@ -115,6 +116,13 @@ fun Int.toJsIdentifier(): String {
} }
} }
private fun List<IrType>.joinTypes(): String {
if (isEmpty()) {
return ""
}
return joinToString("$", "$") { superType -> superType.asString() }
}
fun calculateJsFunctionSignature(declaration: IrFunction, context: JsIrBackendContext): String { fun calculateJsFunctionSignature(declaration: IrFunction, context: JsIrBackendContext): String {
val declarationName = declaration.nameIfPropertyAccessor() ?: declaration.getJsNameOrKotlinName().asString() val declarationName = declaration.nameIfPropertyAccessor() ?: declaration.getJsNameOrKotlinName().asString()
@@ -125,20 +133,18 @@ fun calculateJsFunctionSignature(declaration: IrFunction, context: JsIrBackendCo
declaration.typeParameters.ifNotEmpty { declaration.typeParameters.ifNotEmpty {
nameBuilder.append("_\$t") nameBuilder.append("_\$t")
forEach { typeParam -> forEach { typeParam ->
nameBuilder.append("_").append(typeParam.name.asString()) nameBuilder.append("_").append(typeParam.name.asString()).append(typeParam.superTypes.joinTypes())
typeParam.superTypes.ifNotEmpty {
nameBuilder.append("$")
joinTo(nameBuilder, "") { type -> type.asString() }
}
} }
} }
declaration.extensionReceiverParameter?.let { declaration.extensionReceiverParameter?.let {
nameBuilder.append("_r$${it.type.asString()}") val superTypes = it.type.superTypes().joinTypes()
nameBuilder.append("_r$${it.type.asString()}$superTypes")
} }
declaration.valueParameters.ifNotEmpty { declaration.valueParameters.ifNotEmpty {
joinTo(nameBuilder, "") { joinTo(nameBuilder, "") {
val defaultValueSign = if (it.origin == JsLoweredDeclarationOrigin.JS_SHADOWED_DEFAULT_PARAMETER) "?" else "" val defaultValueSign = if (it.origin == JsLoweredDeclarationOrigin.JS_SHADOWED_DEFAULT_PARAMETER) "?" else ""
"_${it.type.asString()}$defaultValueSign" val superTypes = it.type.superTypes().joinTypes()
"_${it.type.asString()}$superTypes$defaultValueSign"
} }
} }
declaration.returnType.let { declaration.returnType.let {
@@ -0,0 +1,18 @@
// DONT_TARGET_EXACT_BACKEND: JS
// WITH_STDLIB
class MySet<K, V, E : Map.Entry<K, V>>: AbstractSet<E>() {
override fun contains(element: E): Boolean { return element.key !== null }
override val size: Int get() = 0
override fun isEmpty(): Boolean = false
override fun containsAll(elements: Collection<E>): Boolean = false
override fun iterator(): Iterator<E> = TODO("")
}
fun box(): String {
val h = MySet<Int, Int, Map.Entry<Int, Int>>()
val c = (object {}).let { h.contains(it as Any?) }
return if (c) "NOT OK" else "OK"
}
@@ -2786,6 +2786,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/numberMixedHierarchy.kt"); runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/numberMixedHierarchy.kt");
} }
@Test
@TestMetadata("overrideAbstractSetMethod.kt")
public void testOverrideAbstractSetMethod() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/overrideAbstractSetMethod.kt");
}
@Test @Test
@TestMetadata("removeAtBridgeToJavaClass.kt") @TestMetadata("removeAtBridgeToJavaClass.kt")
public void testRemoveAtBridgeToJavaClass() throws Exception { public void testRemoveAtBridgeToJavaClass() throws Exception {
@@ -2942,6 +2942,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/numberMixedHierarchy.kt"); runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/numberMixedHierarchy.kt");
} }
@Test
@TestMetadata("overrideAbstractSetMethod.kt")
public void testOverrideAbstractSetMethod() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/overrideAbstractSetMethod.kt");
}
@Test @Test
@TestMetadata("removeAtBridgeToJavaClass.kt") @TestMetadata("removeAtBridgeToJavaClass.kt")
public void testRemoveAtBridgeToJavaClass() throws Exception { public void testRemoveAtBridgeToJavaClass() throws Exception {
@@ -2942,6 +2942,12 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/numberMixedHierarchy.kt"); runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/numberMixedHierarchy.kt");
} }
@Test
@TestMetadata("overrideAbstractSetMethod.kt")
public void testOverrideAbstractSetMethod() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/overrideAbstractSetMethod.kt");
}
@Test @Test
@TestMetadata("removeAtBridgeToJavaClass.kt") @TestMetadata("removeAtBridgeToJavaClass.kt")
public void testRemoveAtBridgeToJavaClass() throws Exception { public void testRemoveAtBridgeToJavaClass() throws Exception {
@@ -2438,6 +2438,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/numberMixedHierarchy.kt"); runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/numberMixedHierarchy.kt");
} }
@TestMetadata("overrideAbstractSetMethod.kt")
public void testOverrideAbstractSetMethod() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/overrideAbstractSetMethod.kt");
}
@TestMetadata("removeAtBridgeToJavaClass.kt") @TestMetadata("removeAtBridgeToJavaClass.kt")
public void testRemoveAtBridgeToJavaClass() throws Exception { public void testRemoveAtBridgeToJavaClass() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/removeAtBridgeToJavaClass.kt"); runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/removeAtBridgeToJavaClass.kt");
@@ -2466,12 +2466,6 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest {
public void testSmartCastInFunction() throws Exception { public void testSmartCastInFunction() throws Exception {
runTest("js/js.translator/testData/box/expression/cast/smartCastInFunction.kt"); runTest("js/js.translator/testData/box/expression/cast/smartCastInFunction.kt");
} }
@Test
@TestMetadata("unsafeVarianceCast.kt")
public void testUnsafeVarianceCast() throws Exception {
runTest("js/js.translator/testData/box/expression/cast/unsafeVarianceCast.kt");
}
} }
@Nested @Nested
@@ -1891,6 +1891,12 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest {
public void testHashSet() throws Exception { public void testHashSet() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/hashSet.kt"); runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/hashSet.kt");
} }
@Test
@TestMetadata("overrideAbstractSetMethod.kt")
public void testOverrideAbstractSetMethod() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/overrideAbstractSetMethod.kt");
}
} }
@Nested @Nested
@@ -1891,6 +1891,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
public void testHashSet() throws Exception { public void testHashSet() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/hashSet.kt"); runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/hashSet.kt");
} }
@Test
@TestMetadata("overrideAbstractSetMethod.kt")
public void testOverrideAbstractSetMethod() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/overrideAbstractSetMethod.kt");
}
} }
@Nested @Nested
@@ -1891,6 +1891,12 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes
public void testHashSet() throws Exception { public void testHashSet() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/hashSet.kt"); runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/hashSet.kt");
} }
@Test
@TestMetadata("overrideAbstractSetMethod.kt")
public void testOverrideAbstractSetMethod() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/overrideAbstractSetMethod.kt");
}
} }
@Nested @Nested
@@ -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 fun foo(element: @UnsafeVariance E): Boolean
abstract override fun contains(element: @UnsafeVariance E): Boolean
} }
class Bar<E : C> : Foo<E>() { class Bar<E : C> : Foo<E>() {
@@ -7,15 +10,49 @@ class Bar<E : C> : Foo<E>() {
if (element !is C?) return false if (element !is C?) return false
return true 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 C
open class D : C() open class D : C()
fun box(): String { fun case1(): Int {
val a = (object{}) val a = (object{})
val foo: Foo<Any?> = Bar<D>() 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" return "OK"
} }
@@ -1,7 +1,7 @@
// EXPECTED_REACHABLE_NODES: 1285 // EXPECTED_REACHABLE_NODES: 1285
package foo 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) internal class A(val a: Int)
@@ -15,4 +15,4 @@ fun box(): String {
assertEquals(105, myMultiply(3, 5, 7)) assertEquals(105, myMultiply(3, 5, 7))
return "OK" return "OK"
} }
+7 -7
View File
@@ -115,9 +115,9 @@ private class A {
// CHECK_FUNCTION_EXISTS: get_p13_s8ev3n$ TARGET_BACKENDS=JS // CHECK_FUNCTION_EXISTS: get_p13_s8ev3n$ TARGET_BACKENDS=JS
// CHECK_NOT_CALLED_IN_SCOPE: function=get_p13_s8ev3n$ scope=box 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_CALLED_IN_SCOPE: function=set_p13_dqglrj$ scope=box TARGET_BACKENDS=JS
// CHECK_FUNCTION_EXISTS: get_p13_s8qimu_k$ IGNORED_BACKENDS=JS // CHECK_FUNCTION_EXISTS: get_p13_v9rv0h_k$ IGNORED_BACKENDS=JS
// CHECK_NOT_CALLED_IN_SCOPE: function=get_p13_s8qimu_k$ scope=box 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_8qdsq6_k$ scope=box IGNORED_BACKENDS=JS // CHECK_CALLED_IN_SCOPE: function=set_p13_7j3jda_k$ scope=box IGNORED_BACKENDS=JS
var Int.p13: Int var Int.p13: Int
inline get() = this * 100 + a + 130000 inline get() = this * 100 + a + 130000
set(v) { set(v) {
@@ -127,9 +127,9 @@ private class A {
// CHECK_CALLED_IN_SCOPE: function=get_p14_s8ev3n$ scope=box TARGET_BACKENDS=JS // CHECK_CALLED_IN_SCOPE: function=get_p14_s8ev3n$ scope=box TARGET_BACKENDS=JS
// CHECK_FUNCTION_EXISTS: set_p14_dqglrj$ 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_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_CALLED_IN_SCOPE: function=get_p14_7twbq6_k$ scope=box IGNORED_BACKENDS=JS
// CHECK_FUNCTION_EXISTS: set_p14_uaac7n_k$ IGNORED_BACKENDS=JS // CHECK_FUNCTION_EXISTS: set_p14_fdbk5p_k$ IGNORED_BACKENDS=JS
// CHECK_NOT_CALLED_IN_SCOPE: function=set_p14_uaac7n_k$ scope=box IGNORED_BACKENDS=JS // CHECK_NOT_CALLED_IN_SCOPE: function=set_p14_fdbk5p_k$ scope=box IGNORED_BACKENDS=JS
var Int.p14: Int var Int.p14: Int
get() = this * 100 + a + 140000 get() = this * 100 + a + 140000
inline set(v) { inline set(v) {
@@ -224,4 +224,4 @@ fun box(): String {
if (a.p17 != 171717) return "test17: ${a.p17}" if (a.p17 != 171717) return "test17: ${a.p17}"
return "OK" return "OK"
} }
@@ -1962,6 +1962,12 @@ public class FirNativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTe
public void testHashSet() throws Exception { public void testHashSet() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/hashSet.kt"); runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/hashSet.kt");
} }
@Test
@TestMetadata("overrideAbstractSetMethod.kt")
public void testOverrideAbstractSetMethod() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/overrideAbstractSetMethod.kt");
}
} }
@Nested @Nested
@@ -2012,6 +2012,12 @@ public class FirNativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenB
public void testHashSet() throws Exception { public void testHashSet() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/hashSet.kt"); runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/hashSet.kt");
} }
@Test
@TestMetadata("overrideAbstractSetMethod.kt")
public void testOverrideAbstractSetMethod() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/overrideAbstractSetMethod.kt");
}
} }
@Nested @Nested
@@ -1938,6 +1938,12 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
public void testHashSet() throws Exception { public void testHashSet() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/hashSet.kt"); runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/hashSet.kt");
} }
@Test
@TestMetadata("overrideAbstractSetMethod.kt")
public void testOverrideAbstractSetMethod() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/overrideAbstractSetMethod.kt");
}
} }
@Nested @Nested
@@ -1963,6 +1963,12 @@ public class NativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenBoxT
public void testHashSet() throws Exception { public void testHashSet() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/hashSet.kt"); runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/hashSet.kt");
} }
@Test
@TestMetadata("overrideAbstractSetMethod.kt")
public void testOverrideAbstractSetMethod() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/overrideAbstractSetMethod.kt");
}
} }
@Nested @Nested
@@ -1682,6 +1682,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
public void testHashSet() throws Exception { public void testHashSet() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/hashSet.kt"); runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/hashSet.kt");
} }
@TestMetadata("overrideAbstractSetMethod.kt")
public void testOverrideAbstractSetMethod() throws Exception {
runTest("compiler/testData/codegen/box/builtinStubMethods/extendJavaClasses/overrideAbstractSetMethod.kt");
}
} }
@TestMetadata("compiler/testData/codegen/box/builtinStubMethods/mapGetOrDefault") @TestMetadata("compiler/testData/codegen/box/builtinStubMethods/mapGetOrDefault")