From c8a4ba1ce30370f364bcb89d3aa0ec55f6f11fb6 Mon Sep 17 00:00:00 2001 From: Alexander Korepanov Date: Mon, 2 Jan 2023 16:47:07 +0100 Subject: [PATCH] [JS IR] Don't check an interface method default impl during JS translation We do not need to check a default implementation of the interface during the translation to JS because it must be checked before. Moreover, this check breaks the produced JS code if IR is partial loaded, e.g. during the incremental rebuild. ^KT-55716 Fixed --- .../js/transformers/irToJs/jsAstUtils.kt | 5 ++- .../InvalidationTestGenerated.java | 5 +++ .../interfaceSuperUsage/lib1/ClassA.0.kt | 7 ++++ .../interfaceSuperUsage/lib1/ClassA.8.kt | 12 ++++++ .../interfaceSuperUsage/lib1/ClassB.0.kt | 9 +++++ .../interfaceSuperUsage/lib1/ClassB.3.kt | 3 ++ .../interfaceSuperUsage/lib1/ClassB.6.kt | 6 +++ .../interfaceSuperUsage/lib1/ClassB.7.kt | 8 ++++ .../interfaceSuperUsage/lib1/Interface.0.kt | 9 +++++ .../interfaceSuperUsage/lib1/Interface.2.kt | 15 +++++++ .../interfaceSuperUsage/lib1/Interface.4.kt | 20 ++++++++++ .../interfaceSuperUsage/lib1/module.info | 38 ++++++++++++++++++ .../interfaceSuperUsage/main/m.kt | 7 ++++ .../interfaceSuperUsage/main/module.info | 39 +++++++++++++++++++ .../interfaceSuperUsage/main/test.0.kt | 9 +++++ .../interfaceSuperUsage/main/test.1.kt | 17 ++++++++ .../interfaceSuperUsage/main/test.10.kt | 17 ++++++++ .../interfaceSuperUsage/main/test.5.kt | 17 ++++++++ .../interfaceSuperUsage/main/test.9.kt | 17 ++++++++ .../interfaceSuperUsage/project.info | 23 +++++++++++ 20 files changed, 281 insertions(+), 2 deletions(-) create mode 100644 js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassA.0.kt create mode 100644 js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassA.8.kt create mode 100644 js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassB.0.kt create mode 100644 js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassB.3.kt create mode 100644 js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassB.6.kt create mode 100644 js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassB.7.kt create mode 100644 js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/Interface.0.kt create mode 100644 js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/Interface.2.kt create mode 100644 js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/Interface.4.kt create mode 100644 js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/module.info create mode 100644 js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/m.kt create mode 100644 js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/module.info create mode 100644 js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.0.kt create mode 100644 js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.1.kt create mode 100644 js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.10.kt create mode 100644 js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.5.kt create mode 100644 js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.9.kt create mode 100644 js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/project.info diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/jsAstUtils.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/jsAstUtils.kt index ff158d58c2464..8431d4b2a91a1 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/jsAstUtils.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/jsAstUtils.kt @@ -162,8 +162,9 @@ fun translateCall( Pair(function, superQualifier.owner) } - val callRef = if (klass.isInterface && target.body != null) { - JsNameRef(Namer.CALL_FUNCTION, JsNameRef(context.getNameForStaticDeclaration(target))) + val callRef = if (klass.isInterface) { + val nameForStaticDeclaration = context.getNameForStaticDeclaration(target) + JsNameRef(Namer.CALL_FUNCTION, JsNameRef(nameForStaticDeclaration)) } else { val qualifierName = context.getNameForClass(klass).makeRef() val targetName = context.getNameForMemberFunction(target) diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/incremental/InvalidationTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/incremental/InvalidationTestGenerated.java index bbee88fff0fa9..9ba6fc02a62af 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/incremental/InvalidationTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/incremental/InvalidationTestGenerated.java @@ -195,6 +195,11 @@ public void testInlineFunctionDefaultParams() throws Exception { runTest("js/js.translator/testData/incremental/invalidation/inlineFunctionDefaultParams/"); } + @TestMetadata("interfaceSuperUsage") + public void testInterfaceSuperUsage() throws Exception { + runTest("js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/"); + } + @TestMetadata("interfaceWithDefaultParams") public void testInterfaceWithDefaultParams() throws Exception { runTest("js/js.translator/testData/incremental/invalidation/interfaceWithDefaultParams/"); diff --git a/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassA.0.kt b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassA.0.kt new file mode 100644 index 0000000000000..2603c74f41376 --- /dev/null +++ b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassA.0.kt @@ -0,0 +1,7 @@ +class ClassA : Interface { + override var someVar: Int? + get() = super.someVar + set(value) { + super.someVar = value + } +} diff --git a/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassA.8.kt b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassA.8.kt new file mode 100644 index 0000000000000..e6c310e04eba1 --- /dev/null +++ b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassA.8.kt @@ -0,0 +1,12 @@ +class ClassA : Interface { + override var someVar: Int? + get() = 1 + set(value) { + super.someVar = value + } + + override val someValue: Int + get() = super.someValue + + override fun someFunction(): Int = super.someFunction() +} diff --git a/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassB.0.kt b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassB.0.kt new file mode 100644 index 0000000000000..da9c864b77ada --- /dev/null +++ b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassB.0.kt @@ -0,0 +1,9 @@ +class ClassB : Interface { + override var someVar: Int? + get() = super.someVar + set(value) { + super.someVar = value + } + + val x = 1 +} diff --git a/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassB.3.kt b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassB.3.kt new file mode 100644 index 0000000000000..f6a37f178a45f --- /dev/null +++ b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassB.3.kt @@ -0,0 +1,3 @@ +class ClassB : Interface { + val x = 3 +} diff --git a/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassB.6.kt b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassB.6.kt new file mode 100644 index 0000000000000..eca177d52ec12 --- /dev/null +++ b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassB.6.kt @@ -0,0 +1,6 @@ +class ClassB : Interface { + val x = 3 + + override val someValue: Int + get() = super.someValue + 1 +} diff --git a/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassB.7.kt b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassB.7.kt new file mode 100644 index 0000000000000..27fdee506ab1c --- /dev/null +++ b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/ClassB.7.kt @@ -0,0 +1,8 @@ +class ClassB : Interface { + val x = 3 + + override val someValue: Int + get() = super.someValue + 1 + + override fun someFunction(): Int = super.someFunction() + 1 +} diff --git a/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/Interface.0.kt b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/Interface.0.kt new file mode 100644 index 0000000000000..a0ae4205533cc --- /dev/null +++ b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/Interface.0.kt @@ -0,0 +1,9 @@ +private var myProperty: Int? = null + +interface Interface { + var someVar: Int? + get() = myProperty + set(value) { + myProperty = value + } +} diff --git a/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/Interface.2.kt b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/Interface.2.kt new file mode 100644 index 0000000000000..93db9407a74ed --- /dev/null +++ b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/Interface.2.kt @@ -0,0 +1,15 @@ +private var myProperty: Int? = null + +interface Interface { + var someVar: Int? + get() = myProperty?.let { + if (it == 1) { + it + 1 + } else { + it + } + } + set(value) { + myProperty = value + } +} diff --git a/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/Interface.4.kt b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/Interface.4.kt new file mode 100644 index 0000000000000..2c062acb8cdbc --- /dev/null +++ b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/Interface.4.kt @@ -0,0 +1,20 @@ +private var myProperty: Int? = null + +interface Interface { + var someVar: Int? + get() = myProperty?.let { + if (it == 1 || it == 3) { + it + 1 + } else { + it + } + } + set(value) { + myProperty = value + } + + val someValue: Int + get() = 1 + + fun someFunction(): Int = someValue +} diff --git a/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/module.info b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/module.info new file mode 100644 index 0000000000000..93e2a7b5b7598 --- /dev/null +++ b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/lib1/module.info @@ -0,0 +1,38 @@ +STEP 0: + modifications: + U : ClassA.0.kt -> ClassA.kt + U : ClassB.0.kt -> ClassB.kt + U : Interface.0.kt -> Interface.kt + added file: ClassA.kt, ClassB.kt, Interface.kt +STEP 1: + updated exports: ClassB.kt +STEP 2: + modifications: + U : Interface.2.kt -> Interface.kt + modified ir: Interface.kt +STEP 3: + modifications: + U : ClassB.3.kt -> ClassB.kt + modified ir: ClassB.kt + updated exports: ClassB.kt +STEP 4: + modifications: + U : Interface.4.kt -> Interface.kt + modified ir: Interface.kt +STEP 5: + updated exports: Interface.kt, ClassB.kt +STEP 6: + modifications: + U : ClassB.6.kt -> ClassB.kt + modified ir: ClassB.kt + updated exports: Interface.kt +STEP 7: + modifications: + U : ClassB.7.kt -> ClassB.kt + modified ir: ClassB.kt +STEP 8: + modifications: + U : ClassA.8.kt -> ClassA.kt + modified ir: ClassA.kt +STEP 9..10: + updated exports: ClassA.kt diff --git a/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/m.kt b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/m.kt new file mode 100644 index 0000000000000..9b36f2c569628 --- /dev/null +++ b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/m.kt @@ -0,0 +1,7 @@ +fun box(stepId: Int): String { + val x = test() + if (x != stepId) { + return "Fail: $x != $stepId" + } + return "OK" +} diff --git a/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/module.info b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/module.info new file mode 100644 index 0000000000000..7b6aa2dae0793 --- /dev/null +++ b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/module.info @@ -0,0 +1,39 @@ +STEP 0: + dependencies: lib1 + modifications: + U : test.0.kt -> test.kt + added file: m.kt, test.kt +STEP 1: + dependencies: lib1 + modifications: + U : test.1.kt -> test.kt + modified ir: test.kt +STEP 2: + dependencies: lib1 +STEP 3: + dependencies: lib1 + updated imports: test.kt +STEP 4: + dependencies: lib1 +STEP 5: + dependencies: lib1 + modifications: + U : test.5.kt -> test.kt + modified ir: test.kt +STEP 6: + dependencies: lib1 +STEP 7: + dependencies: lib1 + updated imports: test.kt +STEP 8: + dependencies: lib1 +STEP 9: + dependencies: lib1 + modifications: + U : test.9.kt -> test.kt + modified ir: test.kt +STEP 10: + dependencies: lib1 + modifications: + U : test.10.kt -> test.kt + modified ir: test.kt diff --git a/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.0.kt b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.0.kt new file mode 100644 index 0000000000000..23beb52b161ad --- /dev/null +++ b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.0.kt @@ -0,0 +1,9 @@ +private fun testClassA(): Int { + val a = ClassA() + a.someVar = 0 + return a.someVar!! +} + +fun test(): Int { + return testClassA() +} diff --git a/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.1.kt b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.1.kt new file mode 100644 index 0000000000000..2e91c3149446a --- /dev/null +++ b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.1.kt @@ -0,0 +1,17 @@ +private fun testClassA(): Int { + val a = ClassA() + a.someVar = 0 + return a.someVar!! +} + +private fun testClassB(): Int { + val b = ClassB() + b.someVar = b.x + return b.someVar!! +} + +fun test(): Int { + val b = testClassB() + val a = testClassA() + return b + a +} diff --git a/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.10.kt b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.10.kt new file mode 100644 index 0000000000000..5c33c18fe70dc --- /dev/null +++ b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.10.kt @@ -0,0 +1,17 @@ +private fun testClassA(): Int { + val a = ClassA() + a.someVar = 0 + return a.someVar!! + a.someFunction() + a.someValue +} + +private fun testClassB(): Int { + val b = ClassB() + b.someVar = b.x + return b.someVar!! + b.someFunction() +} + +fun test(): Int { + val b = testClassB() + val a = testClassA() + return b + a +} diff --git a/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.5.kt b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.5.kt new file mode 100644 index 0000000000000..61276ac82164d --- /dev/null +++ b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.5.kt @@ -0,0 +1,17 @@ +private fun testClassA(): Int { + val a = ClassA() + a.someVar = 0 + return a.someVar!! +} + +private fun testClassB(): Int { + val b = ClassB() + b.someVar = b.x + return b.someVar!! + b.someFunction() +} + +fun test(): Int { + val b = testClassB() + val a = testClassA() + return b + a +} diff --git a/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.9.kt b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.9.kt new file mode 100644 index 0000000000000..8b4a630d11274 --- /dev/null +++ b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/main/test.9.kt @@ -0,0 +1,17 @@ +private fun testClassA(): Int { + val a = ClassA() + a.someVar = 0 + return a.someVar!! + a.someFunction() +} + +private fun testClassB(): Int { + val b = ClassB() + b.someVar = b.x + return b.someVar!! + b.someFunction() +} + +fun test(): Int { + val b = testClassB() + val a = testClassA() + return b + a +} diff --git a/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/project.info b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/project.info new file mode 100644 index 0000000000000..e1f2243bc04c4 --- /dev/null +++ b/js/js.translator/testData/incremental/invalidation/interfaceSuperUsage/project.info @@ -0,0 +1,23 @@ +MODULES: lib1, main + +STEP 0..3: + libs: lib1, main + dirty js: lib1, main +STEP 4: + libs: lib1, main + dirty js: lib1 +STEP 5: + libs: lib1, main + dirty js: lib1, main +STEP 6: + libs: lib1, main + dirty js: lib1 +STEP 7: + libs: lib1, main + dirty js: lib1, main +STEP 8: + libs: lib1, main + dirty js: lib1 +STEP 9..10: + libs: lib1, main + dirty js: lib1, main