From 19660f11a7cb385469c07103c2e4f3a4cac79abb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Sch=C3=A4fer?= Date: Wed, 18 Aug 2021 15:25:41 +0200 Subject: [PATCH] jvm-abi-gen: Remove WhenMapping classes from ABI if possible --- .../jvm/abi/JvmAbiClassBuilderInterceptor.kt | 6 ++++- .../CompileAgainstJvmAbiTestGenerated.java | 5 +++++ .../IrCompileAgainstJvmAbiTestGenerated.java | 5 +++++ .../jvm/abi/IrJvmAbiContentTestGenerated.java | 10 +++++++++ .../jvm/abi/JvmAbiContentTestGenerated.java | 10 +++++++++ ...gacyCompileAgainstJvmAbiTestGenerated.java | 5 +++++ .../abi/LegacyJvmAbiContentTestGenerated.java | 10 +++++++++ .../compile/inlineWhenMappings/app/app.kt | 5 +++++ .../compile/inlineWhenMappings/directives.txt | 1 + .../compile/inlineWhenMappings/lib/lib.kt | 10 +++++++++ .../anonymousWhenMapping/signatures.txt | 14 ++++++++++++ .../content/anonymousWhenMapping/test.kt | 10 +++++++++ .../content/whenMapping/directives.txt | 2 ++ .../content/whenMapping/signatures.txt | 22 +++++++++++++++++++ .../testData/content/whenMapping/test.kt | 10 +++++++++ 15 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 plugins/jvm-abi-gen/testData/compile/inlineWhenMappings/app/app.kt create mode 100644 plugins/jvm-abi-gen/testData/compile/inlineWhenMappings/directives.txt create mode 100644 plugins/jvm-abi-gen/testData/compile/inlineWhenMappings/lib/lib.kt create mode 100644 plugins/jvm-abi-gen/testData/content/anonymousWhenMapping/signatures.txt create mode 100644 plugins/jvm-abi-gen/testData/content/anonymousWhenMapping/test.kt create mode 100644 plugins/jvm-abi-gen/testData/content/whenMapping/directives.txt create mode 100644 plugins/jvm-abi-gen/testData/content/whenMapping/signatures.txt create mode 100644 plugins/jvm-abi-gen/testData/content/whenMapping/test.kt diff --git a/plugins/jvm-abi-gen/src/org/jetbrains/kotlin/jvm/abi/JvmAbiClassBuilderInterceptor.kt b/plugins/jvm-abi-gen/src/org/jetbrains/kotlin/jvm/abi/JvmAbiClassBuilderInterceptor.kt index 2f079b0276b..78124b9d8de 100644 --- a/plugins/jvm-abi-gen/src/org/jetbrains/kotlin/jvm/abi/JvmAbiClassBuilderInterceptor.kt +++ b/plugins/jvm-abi-gen/src/org/jetbrains/kotlin/jvm/abi/JvmAbiClassBuilderInterceptor.kt @@ -10,6 +10,7 @@ import org.jetbrains.kotlin.codegen.ClassBuilder import org.jetbrains.kotlin.codegen.ClassBuilderFactory import org.jetbrains.kotlin.codegen.DelegatingClassBuilder import org.jetbrains.kotlin.codegen.DelegatingClassBuilderFactory +import org.jetbrains.kotlin.codegen.`when`.WhenByEnumsMapping import org.jetbrains.kotlin.codegen.extensions.ClassBuilderInterceptorExtension import org.jetbrains.kotlin.codegen.inline.coroutines.FOR_INLINE_SUFFIX import org.jetbrains.kotlin.descriptors.ClassDescriptor @@ -156,7 +157,7 @@ class JvmAbiClassBuilderInterceptor : ClassBuilderInterceptorExtension { when { publicAbi -> abiClassInfo[internalName] = AbiClassInfo.Public - !localOrAnonymousClass -> { + !localOrAnonymousClass && !isWhenMappingClass -> { for (method in maskedMethods) { methodInfos.replace(method, AbiMethodInfo.STRIP) } @@ -165,5 +166,8 @@ class JvmAbiClassBuilderInterceptor : ClassBuilderInterceptorExtension { } super.done() } + + private val isWhenMappingClass: Boolean + get() = internalName.endsWith(WhenByEnumsMapping.MAPPINGS_CLASS_NAME_POSTFIX) } } diff --git a/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/CompileAgainstJvmAbiTestGenerated.java b/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/CompileAgainstJvmAbiTestGenerated.java index 42d210c04b7..766693321ec 100644 --- a/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/CompileAgainstJvmAbiTestGenerated.java +++ b/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/CompileAgainstJvmAbiTestGenerated.java @@ -65,6 +65,11 @@ public class CompileAgainstJvmAbiTestGenerated extends AbstractCompileAgainstJvm runTest("plugins/jvm-abi-gen/testData/compile/inlineReifiedFunction/"); } + @TestMetadata("inlineWhenMappings") + public void testInlineWhenMappings() throws Exception { + runTest("plugins/jvm-abi-gen/testData/compile/inlineWhenMappings/"); + } + @TestMetadata("innerObjectRegeneration") public void testInnerObjectRegeneration() throws Exception { runTest("plugins/jvm-abi-gen/testData/compile/innerObjectRegeneration/"); diff --git a/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/IrCompileAgainstJvmAbiTestGenerated.java b/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/IrCompileAgainstJvmAbiTestGenerated.java index 8645c7cc996..137f0319624 100644 --- a/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/IrCompileAgainstJvmAbiTestGenerated.java +++ b/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/IrCompileAgainstJvmAbiTestGenerated.java @@ -65,6 +65,11 @@ public class IrCompileAgainstJvmAbiTestGenerated extends AbstractIrCompileAgains runTest("plugins/jvm-abi-gen/testData/compile/inlineReifiedFunction/"); } + @TestMetadata("inlineWhenMappings") + public void testInlineWhenMappings() throws Exception { + runTest("plugins/jvm-abi-gen/testData/compile/inlineWhenMappings/"); + } + @TestMetadata("innerObjectRegeneration") public void testInnerObjectRegeneration() throws Exception { runTest("plugins/jvm-abi-gen/testData/compile/innerObjectRegeneration/"); diff --git a/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/IrJvmAbiContentTestGenerated.java b/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/IrJvmAbiContentTestGenerated.java index be4cade3dc3..78d2edd683f 100644 --- a/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/IrJvmAbiContentTestGenerated.java +++ b/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/IrJvmAbiContentTestGenerated.java @@ -35,8 +35,18 @@ public class IrJvmAbiContentTestGenerated extends AbstractIrJvmAbiContentTest { runTest("plugins/jvm-abi-gen/testData/content/annotation/"); } + @TestMetadata("anonymousWhenMapping") + public void testAnonymousWhenMapping() throws Exception { + runTest("plugins/jvm-abi-gen/testData/content/anonymousWhenMapping/"); + } + @TestMetadata("class") public void testClass() throws Exception { runTest("plugins/jvm-abi-gen/testData/content/class/"); } + + @TestMetadata("whenMapping") + public void testWhenMapping() throws Exception { + runTest("plugins/jvm-abi-gen/testData/content/whenMapping/"); + } } diff --git a/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/JvmAbiContentTestGenerated.java b/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/JvmAbiContentTestGenerated.java index df6e09403a0..c5c19bc8791 100644 --- a/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/JvmAbiContentTestGenerated.java +++ b/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/JvmAbiContentTestGenerated.java @@ -35,8 +35,18 @@ public class JvmAbiContentTestGenerated extends AbstractJvmAbiContentTest { runTest("plugins/jvm-abi-gen/testData/content/annotation/"); } + @TestMetadata("anonymousWhenMapping") + public void testAnonymousWhenMapping() throws Exception { + runTest("plugins/jvm-abi-gen/testData/content/anonymousWhenMapping/"); + } + @TestMetadata("class") public void testClass() throws Exception { runTest("plugins/jvm-abi-gen/testData/content/class/"); } + + @TestMetadata("whenMapping") + public void testWhenMapping() throws Exception { + runTest("plugins/jvm-abi-gen/testData/content/whenMapping/"); + } } diff --git a/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/LegacyCompileAgainstJvmAbiTestGenerated.java b/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/LegacyCompileAgainstJvmAbiTestGenerated.java index 2abd2b3fb86..afc3ccce6ee 100644 --- a/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/LegacyCompileAgainstJvmAbiTestGenerated.java +++ b/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/LegacyCompileAgainstJvmAbiTestGenerated.java @@ -65,6 +65,11 @@ public class LegacyCompileAgainstJvmAbiTestGenerated extends AbstractLegacyCompi runTest("plugins/jvm-abi-gen/testData/compile/inlineReifiedFunction/"); } + @TestMetadata("inlineWhenMappings") + public void testInlineWhenMappings() throws Exception { + runTest("plugins/jvm-abi-gen/testData/compile/inlineWhenMappings/"); + } + @TestMetadata("innerObjectRegeneration") public void testInnerObjectRegeneration() throws Exception { runTest("plugins/jvm-abi-gen/testData/compile/innerObjectRegeneration/"); diff --git a/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/LegacyJvmAbiContentTestGenerated.java b/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/LegacyJvmAbiContentTestGenerated.java index 3749be04ccd..401a3f514ac 100644 --- a/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/LegacyJvmAbiContentTestGenerated.java +++ b/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/LegacyJvmAbiContentTestGenerated.java @@ -35,8 +35,18 @@ public class LegacyJvmAbiContentTestGenerated extends AbstractLegacyJvmAbiConten runTest("plugins/jvm-abi-gen/testData/content/annotation/"); } + @TestMetadata("anonymousWhenMapping") + public void testAnonymousWhenMapping() throws Exception { + runTest("plugins/jvm-abi-gen/testData/content/anonymousWhenMapping/"); + } + @TestMetadata("class") public void testClass() throws Exception { runTest("plugins/jvm-abi-gen/testData/content/class/"); } + + @TestMetadata("whenMapping") + public void testWhenMapping() throws Exception { + runTest("plugins/jvm-abi-gen/testData/content/whenMapping/"); + } } diff --git a/plugins/jvm-abi-gen/testData/compile/inlineWhenMappings/app/app.kt b/plugins/jvm-abi-gen/testData/compile/inlineWhenMappings/app/app.kt new file mode 100644 index 00000000000..ed5a53b51f9 --- /dev/null +++ b/plugins/jvm-abi-gen/testData/compile/inlineWhenMappings/app/app.kt @@ -0,0 +1,5 @@ +package app + +import lib.* + +fun runAppAndReturnOk(): String = value(E.A) diff --git a/plugins/jvm-abi-gen/testData/compile/inlineWhenMappings/directives.txt b/plugins/jvm-abi-gen/testData/compile/inlineWhenMappings/directives.txt new file mode 100644 index 00000000000..3f7eb8b3500 --- /dev/null +++ b/plugins/jvm-abi-gen/testData/compile/inlineWhenMappings/directives.txt @@ -0,0 +1 @@ +// IGNORE_BACKEND_LEGACY: JVM \ No newline at end of file diff --git a/plugins/jvm-abi-gen/testData/compile/inlineWhenMappings/lib/lib.kt b/plugins/jvm-abi-gen/testData/compile/inlineWhenMappings/lib/lib.kt new file mode 100644 index 00000000000..170ec9d5dc3 --- /dev/null +++ b/plugins/jvm-abi-gen/testData/compile/inlineWhenMappings/lib/lib.kt @@ -0,0 +1,10 @@ +package lib + +enum class E { + A, B +} + +inline fun value(x: E) = when (x) { + E.A -> "OK" + E.B -> "Fail" +} diff --git a/plugins/jvm-abi-gen/testData/content/anonymousWhenMapping/signatures.txt b/plugins/jvm-abi-gen/testData/content/anonymousWhenMapping/signatures.txt new file mode 100644 index 00000000000..eb01d402de0 --- /dev/null +++ b/plugins/jvm-abi-gen/testData/content/anonymousWhenMapping/signatures.txt @@ -0,0 +1,14 @@ +@kotlin.Metadata +public final enum class test/E { + // source: 'test.kt' + public final enum static field A: test.E + public final enum static field B: test.E + public static method valueOf(p0: java.lang.String): test.E + public static method values(): test.E[] +} +@kotlin.Metadata +public final class test/Test { + // source: 'test.kt' + public method (): void + public final @org.jetbrains.annotations.NotNull method anonymousWhenMapping(@org.jetbrains.annotations.NotNull p0: test.E): java.lang.String +} \ No newline at end of file diff --git a/plugins/jvm-abi-gen/testData/content/anonymousWhenMapping/test.kt b/plugins/jvm-abi-gen/testData/content/anonymousWhenMapping/test.kt new file mode 100644 index 00000000000..ff8a2fdbe22 --- /dev/null +++ b/plugins/jvm-abi-gen/testData/content/anonymousWhenMapping/test.kt @@ -0,0 +1,10 @@ +package test + +enum class E { A, B } + +class Test { + fun anonymousWhenMapping(e: E) = when (e) { + E.A -> "OK" + else -> "Fail" + } +} diff --git a/plugins/jvm-abi-gen/testData/content/whenMapping/directives.txt b/plugins/jvm-abi-gen/testData/content/whenMapping/directives.txt new file mode 100644 index 00000000000..e59cf931fc8 --- /dev/null +++ b/plugins/jvm-abi-gen/testData/content/whenMapping/directives.txt @@ -0,0 +1,2 @@ +// IGNORE_BACKEND_LEGACY: JVM +// IGNORE_BACKEND: JVM \ No newline at end of file diff --git a/plugins/jvm-abi-gen/testData/content/whenMapping/signatures.txt b/plugins/jvm-abi-gen/testData/content/whenMapping/signatures.txt new file mode 100644 index 00000000000..e5f81893c8d --- /dev/null +++ b/plugins/jvm-abi-gen/testData/content/whenMapping/signatures.txt @@ -0,0 +1,22 @@ +@kotlin.Metadata +public final enum class test/E { + // source: 'test.kt' + public final enum static field A: test.E + public final enum static field B: test.E + public static method valueOf(p0: java.lang.String): test.E + public static method values(): test.E[] +} +@kotlin.Metadata +public synthetic final class test/Test$WhenMappings { + // source: 'test.kt' + public synthetic final static field $EnumSwitchMapping$0: int[] + static method (): void + public synthetic inner class test/Test$WhenMappings +} +@kotlin.Metadata +public final class test/Test { + // source: 'test.kt' + public method (): void + public final @org.jetbrains.annotations.NotNull method visibleWhenMapping(@org.jetbrains.annotations.NotNull p0: test.E): java.lang.String + public synthetic inner class test/Test$WhenMappings +} \ No newline at end of file diff --git a/plugins/jvm-abi-gen/testData/content/whenMapping/test.kt b/plugins/jvm-abi-gen/testData/content/whenMapping/test.kt new file mode 100644 index 00000000000..c1b2a449873 --- /dev/null +++ b/plugins/jvm-abi-gen/testData/content/whenMapping/test.kt @@ -0,0 +1,10 @@ +package test + +enum class E { A, B } + +class Test { + inline fun visibleWhenMapping(e: E) = when (e) { + E.A -> "OK" + else -> "Fail" + } +}