From d61e7db937499b0a26efacb8c10c5f92ec512aa2 Mon Sep 17 00:00:00 2001 From: pyos Date: Wed, 6 Apr 2022 13:10:12 +0200 Subject: [PATCH] JVM_IR: add a test for when(enum) binary compatibility --- .../changedEnumsInLibrary/new/enum.kt | 4 ++++ .../changedEnumsInLibrary/old/enum.kt | 4 ++++ .../changedEnumsInLibrary/output.txt | 1 + .../changedEnumsInLibrary/source.kt | 18 ++++++++++++++++++ .../CompileKotlinAgainstCustomBinariesTest.kt | 11 +++++++++++ 5 files changed, 38 insertions(+) create mode 100644 compiler/testData/compileKotlinAgainstCustomBinaries/changedEnumsInLibrary/new/enum.kt create mode 100644 compiler/testData/compileKotlinAgainstCustomBinaries/changedEnumsInLibrary/old/enum.kt create mode 100644 compiler/testData/compileKotlinAgainstCustomBinaries/changedEnumsInLibrary/output.txt create mode 100644 compiler/testData/compileKotlinAgainstCustomBinaries/changedEnumsInLibrary/source.kt diff --git a/compiler/testData/compileKotlinAgainstCustomBinaries/changedEnumsInLibrary/new/enum.kt b/compiler/testData/compileKotlinAgainstCustomBinaries/changedEnumsInLibrary/new/enum.kt new file mode 100644 index 00000000000..5f413d9ed79 --- /dev/null +++ b/compiler/testData/compileKotlinAgainstCustomBinaries/changedEnumsInLibrary/new/enum.kt @@ -0,0 +1,4 @@ +package test + +enum class E1 { A, C, B, D } +enum class E2 { A, B } diff --git a/compiler/testData/compileKotlinAgainstCustomBinaries/changedEnumsInLibrary/old/enum.kt b/compiler/testData/compileKotlinAgainstCustomBinaries/changedEnumsInLibrary/old/enum.kt new file mode 100644 index 00000000000..2848d4e3abd --- /dev/null +++ b/compiler/testData/compileKotlinAgainstCustomBinaries/changedEnumsInLibrary/old/enum.kt @@ -0,0 +1,4 @@ +package test + +enum class E1 { A, B, C } +enum class E2 { A, B, C } diff --git a/compiler/testData/compileKotlinAgainstCustomBinaries/changedEnumsInLibrary/output.txt b/compiler/testData/compileKotlinAgainstCustomBinaries/changedEnumsInLibrary/output.txt new file mode 100644 index 00000000000..a0aba9318ad --- /dev/null +++ b/compiler/testData/compileKotlinAgainstCustomBinaries/changedEnumsInLibrary/output.txt @@ -0,0 +1 @@ +OK \ No newline at end of file diff --git a/compiler/testData/compileKotlinAgainstCustomBinaries/changedEnumsInLibrary/source.kt b/compiler/testData/compileKotlinAgainstCustomBinaries/changedEnumsInLibrary/source.kt new file mode 100644 index 00000000000..0828132994c --- /dev/null +++ b/compiler/testData/compileKotlinAgainstCustomBinaries/changedEnumsInLibrary/source.kt @@ -0,0 +1,18 @@ +import test.* + +fun f1(x: E1) = when (x) { + E1.A -> "A" + E1.B -> "B" + E1.C -> "C" +} + +fun f2(x: E2) = when (x) { + E2.A -> "A" + E2.B -> "B" + E2.C -> "C" +} + +fun run(): String { + val c2 = try { f2(E2.C) } catch (e: java.lang.NoSuchFieldError) { "" } + return f1(E1.A) + f1(E1.B) + f1(E1.C) + f2(E2.A) + f2(E2.B) + c2 +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.kt b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.kt index 68c6309d978..2b68c3d3e76 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.kt @@ -519,6 +519,17 @@ class CompileKotlinAgainstCustomBinariesTest : AbstractKotlinCompilerIntegration .loadClass("SourceKt").getDeclaredMethod("run").invoke(null) } + fun testChangedEnumsInLibrary() { + val oldLibrary = compileLibrary("old", checkKotlinOutput = {}) + val newLibrary = compileLibrary("new", checkKotlinOutput = {}) + compileKotlin("source.kt", tmpdir, listOf(oldLibrary)) + + val result = + URLClassLoader(arrayOf(newLibrary.toURI().toURL(), tmpdir.toURI().toURL()), ForTestCompileRuntime.runtimeJarClassLoader()) + .loadClass("SourceKt").getDeclaredMethod("run").invoke(null) as String + assertEquals("ABCAB", result) + } + fun testClassFromJdkInLibrary() { val library = compileLibrary("library") compileKotlin("source.kt", tmpdir, listOf(library))