From d204fa91cc81721012e13456909e3a690cd1fd43 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Tue, 6 Dec 2016 19:54:36 +0300 Subject: [PATCH] Use "-Xskip-metadata-version-check" to load pre-release classes --- .../common/arguments/K2JVMCompilerArguments.java | 2 +- .../org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt | 5 ++++- compiler/testData/cli/jvm/extraHelp.out | 2 +- .../library/a.kt | 9 +++++++++ .../output.txt | 1 + .../source.kt | 15 +++++++++++++++ .../CompileKotlinAgainstCustomBinariesTest.java | 13 +++++++++++-- 7 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 compiler/testData/compileKotlinAgainstCustomBinaries/releaseCompilerAgainstPreReleaseLibrarySkipVersionCheck/library/a.kt create mode 100644 compiler/testData/compileKotlinAgainstCustomBinaries/releaseCompilerAgainstPreReleaseLibrarySkipVersionCheck/output.txt create mode 100644 compiler/testData/compileKotlinAgainstCustomBinaries/releaseCompilerAgainstPreReleaseLibrarySkipVersionCheck/source.kt diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.java b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.java index b8a266b9645..06a4de6374a 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.java +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.java @@ -95,7 +95,7 @@ public class K2JVMCompilerArguments extends CommonCompilerArguments { @Argument(value = "Xallow-kotlin-package", description = "Allow compiling code in package 'kotlin'") public boolean allowKotlinPackage; - @Argument(value = "Xskip-metadata-version-check", description = "Try loading binary incompatible classes, may cause crashes") + @Argument(value = "Xskip-metadata-version-check", description = "Load classes with bad metadata version anyway (incl. pre-release classes)") public boolean skipMetadataVersionCheck; @Argument(value = "Xdump-declarations-to", description = "Path to JSON file to dump Java to Kotlin declaration mappings") diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt index 52e1929c31f..3fcd8d48b4e 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt @@ -39,6 +39,7 @@ import org.jetbrains.kotlin.compiler.plugin.cliPluginUsageString import org.jetbrains.kotlin.config.* import org.jetbrains.kotlin.incremental.components.SourceRetentionAnnotationHandler import org.jetbrains.kotlin.load.java.JvmAbi +import org.jetbrains.kotlin.load.kotlin.DeserializedDescriptorResolver import org.jetbrains.kotlin.load.kotlin.JvmMetadataVersion import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCompilationComponents import org.jetbrains.kotlin.script.KotlinScriptDefinitionFromAnnotatedTemplate @@ -115,8 +116,10 @@ class K2JVMCompiler : CLICompiler() { return ExitCode.OK } - if (arguments.skipMetadataVersionCheck) { + if (arguments.skipMetadataVersionCheck) { JvmMetadataVersion.skipCheck = true + @Suppress("DEPRECATION") + DeserializedDescriptorResolver.IS_PRE_RELEASE = true } if (arguments.includeRuntime) { diff --git a/compiler/testData/cli/jvm/extraHelp.out b/compiler/testData/cli/jvm/extraHelp.out index 290ad6e4a33..981dd94328f 100644 --- a/compiler/testData/cli/jvm/extraHelp.out +++ b/compiler/testData/cli/jvm/extraHelp.out @@ -6,7 +6,7 @@ where advanced options include: -Xreport-perf Report detailed performance statistics -Xmultifile-parts-inherit Compile multifile classes as a hierarchy of parts and facade -Xallow-kotlin-package Allow compiling code in package 'kotlin' - -Xskip-metadata-version-check Try loading binary incompatible classes, may cause crashes + -Xskip-metadata-version-check Load classes with bad metadata version anyway (incl. pre-release classes) -Xdump-declarations-to Path to JSON file to dump Java to Kotlin declaration mappings -Xsingle-module Combine modules for source files and binary dependencies into a single module -Xadd-compiler-builtins Add definitions of built-in declarations to the compilation classpath (useful with -no-stdlib) diff --git a/compiler/testData/compileKotlinAgainstCustomBinaries/releaseCompilerAgainstPreReleaseLibrarySkipVersionCheck/library/a.kt b/compiler/testData/compileKotlinAgainstCustomBinaries/releaseCompilerAgainstPreReleaseLibrarySkipVersionCheck/library/a.kt new file mode 100644 index 00000000000..a311bb96854 --- /dev/null +++ b/compiler/testData/compileKotlinAgainstCustomBinaries/releaseCompilerAgainstPreReleaseLibrarySkipVersionCheck/library/a.kt @@ -0,0 +1,9 @@ +package a + +open class A { + class Nested +} + +fun foo() {} +var bar = 42 +typealias TA = String diff --git a/compiler/testData/compileKotlinAgainstCustomBinaries/releaseCompilerAgainstPreReleaseLibrarySkipVersionCheck/output.txt b/compiler/testData/compileKotlinAgainstCustomBinaries/releaseCompilerAgainstPreReleaseLibrarySkipVersionCheck/output.txt new file mode 100644 index 00000000000..a0aba9318ad --- /dev/null +++ b/compiler/testData/compileKotlinAgainstCustomBinaries/releaseCompilerAgainstPreReleaseLibrarySkipVersionCheck/output.txt @@ -0,0 +1 @@ +OK \ No newline at end of file diff --git a/compiler/testData/compileKotlinAgainstCustomBinaries/releaseCompilerAgainstPreReleaseLibrarySkipVersionCheck/source.kt b/compiler/testData/compileKotlinAgainstCustomBinaries/releaseCompilerAgainstPreReleaseLibrarySkipVersionCheck/source.kt new file mode 100644 index 00000000000..6f6e9ef8209 --- /dev/null +++ b/compiler/testData/compileKotlinAgainstCustomBinaries/releaseCompilerAgainstPreReleaseLibrarySkipVersionCheck/source.kt @@ -0,0 +1,15 @@ +@file:Suppress("UNUSED_VARIABLE") +package usage + +import a.* + +fun baz(param: A) { + val constructor = A() + val methodCall = param.hashCode() + val supertype = object : A() {} + + val x = foo() + val y = bar + bar = 239 + val z: TA = "" +} diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.java b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.java index f6b1ef76bf6..9daedb1e48e 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.java +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstCustomBinariesTest.java @@ -229,7 +229,7 @@ public class CompileKotlinAgainstCustomBinariesTest extends TestCaseWithTmpdir { } @SuppressWarnings("deprecation") - private void doTestPreReleaseKotlinLibrary(@NotNull String libraryName) throws Exception { + private void doTestPreReleaseKotlinLibrary(@NotNull String libraryName, @NotNull String... additionalOptions) throws Exception { // Compiles the library with the "pre-release" flag, then compiles a usage of this library in the release mode File library; @@ -244,7 +244,7 @@ public class CompileKotlinAgainstCustomBinariesTest extends TestCaseWithTmpdir { Pair output; try { DeserializedDescriptorResolver.Companion.setIS_PRE_RELEASE(false); - output = compileKotlin("source.kt", tmpdir, library); + output = compileKotlin("source.kt", tmpdir, Arrays.asList(additionalOptions), library); } finally { DeserializedDescriptorResolver.Companion.setIS_PRE_RELEASE(KotlinCompilerVersion.IS_PRE_RELEASE); @@ -368,6 +368,15 @@ public class CompileKotlinAgainstCustomBinariesTest extends TestCaseWithTmpdir { doTestPreReleaseKotlinLibrary("library"); } + /* + // This test should pass but is commented out because the compiler flag is implemented via mutation of the public field, + // which may cause subsequent tests to behave unexpectedly + // TODO: refactor and uncomment + public void testReleaseCompilerAgainstPreReleaseLibrarySkipVersionCheck() throws Exception { + doTestPreReleaseKotlinLibrary("library", "-Xskip-metadata-version-check"); + } + */ + /*test source mapping generation when source info is absent*/ public void testInlineFunWithoutDebugInfo() throws Exception { compileKotlin("sourceInline.kt", tmpdir);