Rework how built-in types are loaded in compiler for JVM

In TopDownAnalyzerFacadeForJVM, we now always use the "load built-ins
from module dependencies" behavior that was previously only enabled with
the dedicated CLI argument -Xload-builtins-from-dependencies. However,
sometimes we compile code without kotlin-stdlib in the classpath, and we
don't want everything to crash because some standard type like
kotlin.Unit hasn't been found.

To mitigate this, we add another module at the end of the dependencies
list, namely a "fallback built-ins" module. This module loads all
built-in declarations from the compiler's class loader, as was done by
default previously. This prevents the compiler from crashing if any
built-in declaration is not found, but compiling the code against
built-ins found in the compiler is still discouraged, so we report an
error if anything is resolved to a declaration from this module, via a
new checker MissingBuiltInDeclarationChecker.

Also introduce a new CLI argument -Xsuppress-missing-builtins-error
specifically to suppress this error and to allow compiling code against
compiler's own built-ins.

 #KT-19227 Fixed
 #KT-28198 Fixed
This commit is contained in:
Alexander Udalov
2017-07-14 20:07:01 +03:00
parent 8ce7742e7c
commit ed86757817
35 changed files with 179 additions and 132 deletions
@@ -165,16 +165,10 @@ class K2JVMCompilerArguments : CommonCompilerArguments() {
var singleModule: Boolean by FreezableVar(false)
@Argument(
value = "-Xadd-compiler-builtins",
description = "Add definitions of built-in declarations to the compilation classpath (useful with -no-stdlib)"
value = "-Xsuppress-missing-builtins-error",
description = "Suppress the \"cannot access built-in declaration\" error (useful with -no-stdlib)"
)
var addCompilerBuiltIns: Boolean by FreezableVar(false)
@Argument(
value = "-Xload-builtins-from-dependencies",
description = "Load definitions of built-in declarations from module dependencies, instead of from the compiler"
)
var loadBuiltInsFromDependencies: Boolean by FreezableVar(false)
var suppressMissingBuiltinsError: Boolean by FreezableVar(false)
@Argument(
value = "-Xscript-resolver-environment",
@@ -283,6 +277,7 @@ class K2JVMCompilerArguments : CommonCompilerArguments() {
)
result[JvmAnalysisFlags.inheritMultifileParts] = inheritMultifileParts
result[JvmAnalysisFlags.sanitizeParentheses] = sanitizeParentheses
result[JvmAnalysisFlags.suppressMissingBuiltinsError] = suppressMissingBuiltinsError
return result
}