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
-10
View File
@@ -64,8 +64,6 @@ data class PSourceRootKotlinOptions(
val apiVersion: String?,
val languageVersion: String?,
val jvmTarget: String?,
val addCompilerBuiltIns: Boolean?,
val loadBuiltInsFromDependencies: Boolean?,
val extraArguments: List<String>
) {
fun intersect(other: PSourceRootKotlinOptions) = PSourceRootKotlinOptions(
@@ -75,8 +73,6 @@ data class PSourceRootKotlinOptions(
if (apiVersion == other.apiVersion) apiVersion else null,
if (languageVersion == other.languageVersion) languageVersion else null,
if (jvmTarget == other.jvmTarget) jvmTarget else null,
if (addCompilerBuiltIns == other.addCompilerBuiltIns) addCompilerBuiltIns else null,
if (loadBuiltInsFromDependencies == other.loadBuiltInsFromDependencies) loadBuiltInsFromDependencies else null,
extraArguments.intersect(other.extraArguments).toList()
)
}
@@ -328,15 +324,11 @@ private fun getKotlinOptions(kotlinCompileTask: Any): PSourceRootKotlinOptions?
fun parseBoolean(name: String) = compileArguments.contains("-$name")
fun parseString(name: String) = compileArguments.dropWhile { it != "-$name" }.drop(1).firstOrNull()
val addCompilerBuiltins = "Xadd-compiler-builtins"
val loadBuiltinsFromDependencies = "Xload-builtins-from-dependencies"
fun isOptionForScriptingCompilerPlugin(option: String)
= option.startsWith("-Xplugin=") && option.contains("kotlin-scripting-compiler")
val extraArguments = compileArguments.filter {
it.startsWith("-X") && !isOptionForScriptingCompilerPlugin(it)
&& it != "-$addCompilerBuiltins" && it != "-$loadBuiltinsFromDependencies"
}
return PSourceRootKotlinOptions(
@@ -346,8 +338,6 @@ private fun getKotlinOptions(kotlinCompileTask: Any): PSourceRootKotlinOptions?
parseString("api-version"),
parseString("language-version"),
parseString("jvm-target"),
parseBoolean(addCompilerBuiltins),
parseBoolean(loadBuiltinsFromDependencies),
extraArguments
)
}
+1 -3
View File
@@ -87,8 +87,6 @@ private fun renderModule(project: PProject, module: PModule) = PFile(
kotlinCompileOptions.noReflect.option("noReflect")
kotlinCompileOptions.moduleName.option("moduleName")
xml("option", "name" to "jvmTarget", "value" to platformVersion)
kotlinCompileOptions.addCompilerBuiltIns.option("addCompilerBuiltIns")
kotlinCompileOptions.loadBuiltInsFromDependencies.option("loadBuiltInsFromDependencies")
kotlinCompileOptions.languageVersion.option("languageVersion")
kotlinCompileOptions.apiVersion.option("apiVersion")
@@ -204,4 +202,4 @@ private fun renderLibraryToXml(library: PLibrary, pathContext: PathContext, name
}
}
fun PLibrary.renderName() = name?.takeIf { it != "unspecified" } ?: classes.first().nameWithoutExtension
fun PLibrary.renderName() = name?.takeIf { it != "unspecified" } ?: classes.first().nameWithoutExtension
@@ -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
}
@@ -124,8 +124,7 @@ object TopDownAnalyzerFacadeForJVM {
targetEnvironment: TargetEnvironment = CompilerEnvironment,
sourceModuleSearchScope: GlobalSearchScope = newModuleSearchScope(project, files)
): ComponentProvider {
val createBuiltInsFromModule = configuration.getBoolean(JVMConfigurationKeys.CREATE_BUILT_INS_FROM_MODULE_DEPENDENCIES)
val moduleContext = createModuleContext(project, configuration, createBuiltInsFromModule)
val moduleContext = createModuleContext(project, configuration)
val storageManager = moduleContext.storageManager
val module = moduleContext.module
@@ -143,12 +142,9 @@ object TopDownAnalyzerFacadeForJVM {
val jvmTarget = configuration.get(JVMConfigurationKeys.JVM_TARGET, JvmTarget.DEFAULT)
val languageVersionSettings = configuration.languageVersionSettings
val optionalBuiltInsModule =
if (configuration.getBoolean(JVMConfigurationKeys.ADD_BUILT_INS_FROM_COMPILER_TO_DEPENDENCIES)) {
if (createBuiltInsFromModule)
JvmBuiltIns(storageManager).apply { initialize(module, languageVersionSettings) }.builtInsModule
else module.builtIns.builtInsModule
} else null
val fallbackBuiltIns = JvmBuiltIns(storageManager, loadBuiltInsFromCurrentClassLoader = true, isFallback = true).apply {
initialize(builtInsModule, languageVersionSettings)
}.builtInsModule
fun StorageComponentContainer.useJavac() {
useImpl<JavacBasedClassFinder>()
@@ -178,7 +174,7 @@ object TopDownAnalyzerFacadeForJVM {
moduleClassResolver.compiledCodeResolver = dependenciesContainer.get()
dependenciesContext.setDependencies(listOfNotNull(dependenciesContext.module, optionalBuiltInsModule))
dependenciesContext.setDependencies(listOf(dependenciesContext.module, fallbackBuiltIns))
dependenciesContext.initializeModuleContents(
CompositePackageFragmentProvider(
listOf(
@@ -229,7 +225,7 @@ object TopDownAnalyzerFacadeForJVM {
// TODO: remove dependencyModule from friends
module.setDependencies(
listOfNotNull(module, dependencyModule, optionalBuiltInsModule),
listOfNotNull(module, dependencyModule, fallbackBuiltIns),
if (dependencyModule != null) setOf(dependencyModule) else emptySet()
)
module.initialize(
@@ -271,19 +267,13 @@ object TopDownAnalyzerFacadeForJVM {
}
}
private fun createModuleContext(
project: Project,
configuration: CompilerConfiguration,
createBuiltInsFromModule: Boolean
): MutableModuleContext {
private fun createModuleContext(project: Project, configuration: CompilerConfiguration): MutableModuleContext {
val projectContext = ProjectContext(project)
val builtIns = JvmBuiltIns(projectContext.storageManager, !createBuiltInsFromModule)
val builtIns = JvmBuiltIns(projectContext.storageManager, false)
return ContextForNewModule(
projectContext, Name.special("<${configuration.getNotNull(CommonConfigurationKeys.MODULE_NAME)}>"), builtIns, null
).apply {
if (createBuiltInsFromModule) {
builtIns.builtInsModule = module
}
builtIns.builtInsModule = module
}
}
}
@@ -195,8 +195,6 @@ fun CompilerConfiguration.configureAdvancedJvmOptions(arguments: K2JVMCompilerAr
put(CLIConfigurationKeys.ALLOW_KOTLIN_PACKAGE, arguments.allowKotlinPackage)
put(JVMConfigurationKeys.USE_SINGLE_MODULE, arguments.singleModule)
put(JVMConfigurationKeys.ADD_BUILT_INS_FROM_COMPILER_TO_DEPENDENCIES, arguments.addCompilerBuiltIns)
put(JVMConfigurationKeys.CREATE_BUILT_INS_FROM_MODULE_DEPENDENCIES, arguments.loadBuiltInsFromDependencies)
arguments.declarationsOutputPath?.let { put(JVMConfigurationKeys.DECLARATIONS_JSON_PATH, it) }
}
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.config;
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl;
import org.jetbrains.kotlin.load.java.JavaClassesTracker;
import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCompilationComponents;
import org.jetbrains.kotlin.modules.Module;
@@ -75,22 +74,6 @@ public class JVMConfigurationKeys {
public static final CompilerConfigurationKey<Boolean> USE_SINGLE_MODULE =
CompilerConfigurationKey.create("combine modules for source files and binary dependencies into a single module");
/**
* Controls whether the module depends on an additional "built-ins" module, which contains binary metadata of built-in definitions.
* By default, that metadata is loaded from kotlin-compiler.jar.
* However, it can be also loaded directly from the module, see {@link CREATE_BUILT_INS_FROM_MODULE_DEPENDENCIES}
*/
public static final CompilerConfigurationKey<Boolean> ADD_BUILT_INS_FROM_COMPILER_TO_DEPENDENCIES =
CompilerConfigurationKey.create("add built-ins from the compiler jar to the dependencies of the module being resolved");
/**
* Controls whether an instance of KotlinBuiltIns which is passed to {@link ModuleDescriptorImpl}'s constructor and ends up being used
* everywhere in the compiler front-end is loaded directly from the module (from its sources and/or its binaries), as opposed to
* from kotlin-compiler.jar
*/
public static final CompilerConfigurationKey<Boolean> CREATE_BUILT_INS_FROM_MODULE_DEPENDENCIES =
CompilerConfigurationKey.create("create built-ins from resources found in the module dependencies");
public static final CompilerConfigurationKey<Boolean> SKIP_RUNTIME_VERSION_CHECK =
CompilerConfigurationKey.create("do not perform checks on runtime versions consistency");
@@ -20,4 +20,7 @@ object JvmAnalysisFlags {
@JvmStatic
val sanitizeParentheses by AnalysisFlag.Delegates.Boolean
@JvmStatic
val suppressMissingBuiltinsError by AnalysisFlag.Delegates.Boolean
}
@@ -0,0 +1,57 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.resolve.jvm.checkers
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.builtins.BuiltInsPackageFragment
import org.jetbrains.kotlin.config.JvmAnalysisFlags
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker
import org.jetbrains.kotlin.resolve.calls.checkers.CallCheckerContext
import org.jetbrains.kotlin.resolve.calls.checkers.isComputingDeferredType
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.checkers.ClassifierUsageChecker
import org.jetbrains.kotlin.resolve.checkers.ClassifierUsageCheckerContext
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
object MissingBuiltInDeclarationChecker : CallChecker {
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
if (context.languageVersionSettings.getFlag(JvmAnalysisFlags.suppressMissingBuiltinsError)) return
val descriptor = resolvedCall.resultingDescriptor
val error = diagnosticFor(descriptor, reportOn)
?: diagnosticFor(descriptor.returnType?.takeUnless(this::isComputingDeferredType)?.constructor?.declarationDescriptor, reportOn)
error?.let(context.trace::report)
}
private fun diagnosticFor(descriptor: DeclarationDescriptor?, reportOn: PsiElement): Diagnostic? {
if (descriptor == null) return null
val containingClassOrPackage = DescriptorUtils.getParentOfType(descriptor, ClassOrPackageFragmentDescriptor::class.java)
if (containingClassOrPackage is ClassDescriptor) {
val containingPackage = DescriptorUtils.getParentOfType(descriptor, PackageFragmentDescriptor::class.java)
if ((containingPackage as? BuiltInsPackageFragment)?.isFallback == true) {
return Errors.MISSING_BUILT_IN_DECLARATION.on(reportOn, containingClassOrPackage.fqNameSafe)
}
} else if (containingClassOrPackage is BuiltInsPackageFragment && containingClassOrPackage.isFallback) {
return Errors.MISSING_BUILT_IN_DECLARATION.on(reportOn, descriptor.fqNameSafe)
}
return null
}
object ClassifierUsage : ClassifierUsageChecker {
override fun check(targetDescriptor: ClassifierDescriptor, element: PsiElement, context: ClassifierUsageCheckerContext) {
if (context.languageVersionSettings.getFlag(JvmAnalysisFlags.suppressMissingBuiltinsError)) return
diagnosticFor(targetDescriptor, element)?.let(context.trace::report)
}
}
}
@@ -42,6 +42,7 @@ object JvmPlatformConfigurator : PlatformConfiguratorBase(
),
additionalCallCheckers = listOf(
MissingBuiltInDeclarationChecker,
JavaAnnotationCallChecker(),
SuspensionPointInsideMutexLockChecker(),
JavaClassOnCompanionChecker(),
@@ -63,7 +64,8 @@ object JvmPlatformConfigurator : PlatformConfiguratorBase(
),
additionalClassifierUsageCheckers = listOf(
BigFunctionTypeAvailabilityChecker
BigFunctionTypeAvailabilityChecker,
MissingBuiltInDeclarationChecker.ClassifierUsage
),
additionalAnnotationCheckers = listOf(
@@ -98,6 +98,7 @@ public interface Errors {
DiagnosticFactory2<PsiElement, String, String> API_NOT_AVAILABLE = DiagnosticFactory2.create(ERROR);
DiagnosticFactory1<PsiElement, FqName> MISSING_DEPENDENCY_CLASS = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, FqName> MISSING_BUILT_IN_DECLARATION = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, String> MISSING_SCRIPT_BASE_CLASS = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, String> MISSING_SCRIPT_STANDARD_TEMPLATE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, String> MISSING_SCRIPT_RECEIVER_CLASS = DiagnosticFactory1.create(ERROR);
@@ -363,6 +363,7 @@ public class DefaultErrorMessages {
MAP.put(API_NOT_AVAILABLE, "This declaration is only available since Kotlin {0} and cannot be used with the specified API version {1}", STRING, STRING);
MAP.put(MISSING_DEPENDENCY_CLASS, "Cannot access class ''{0}''. Check your module classpath for missing or conflicting dependencies", TO_STRING);
MAP.put(MISSING_BUILT_IN_DECLARATION, "Cannot access built-in declaration ''{0}''. Ensure that you have a dependency on the Kotlin standard library", TO_STRING);
MAP.put(MISSING_SCRIPT_BASE_CLASS, "Cannot access script base class ''{0}''. Check your module classpath for missing or conflicting dependencies", TO_STRING);
MAP.put(MISSING_SCRIPT_STANDARD_TEMPLATE, "No script runtime was found in the classpath: class ''{0}'' not found. Please add kotlin-script-runtime.jar to the module dependencies.", TO_STRING);
MAP.put(MISSING_SCRIPT_RECEIVER_CLASS, "Cannot access implicit script receiver class ''{0}''. Check your module classpath for missing or conflicting dependencies", TO_STRING);
+2 -3
View File
@@ -1,6 +1,5 @@
Usage: kotlinc-jvm <options> <source files>
where advanced options include:
-Xadd-compiler-builtins Add definitions of built-in declarations to the compilation classpath (useful with -no-stdlib)
-Xadd-modules=<module[,]> Root modules to resolve in addition to the initial modules,
or all modules on the module path if <module> is ALL-MODULE-PATH
-Xassertions={always-enable|always-disable|jvm|legacy}
@@ -40,8 +39,6 @@ where advanced options include:
in the interface (annotating an existing method can break binary compatibility)
-Xjvm-default=compatibility Allow usages of @JvmDefault; generate a compatibility accessor
in the 'DefaultImpls' class in addition to the interface method
-Xload-builtins-from-dependencies
Load definitions of built-in declarations from module dependencies, instead of from the compiler
-Xno-call-assertions Don't generate not-null assertions for arguments of platform types
-Xno-exception-on-explicit-equals-for-boxed-null
Do not throw NPE on explicit 'equals' call for null receiver of platform boxed primitive type
@@ -62,6 +59,8 @@ where advanced options include:
-Xsupport-compatqual-checker-framework-annotations=enable|disable
Specify behavior for Checker Framework compatqual annotations (NullableDecl/NonNullDecl).
Default value is 'enable'
-Xsuppress-missing-builtins-error
Suppress the "cannot access built-in declaration" error (useful with -no-stdlib)
-Xuse-ir Use the IR backend
-Xuse-javac Use javac for Java source and class files analysis
-Xuse-old-class-files-reading Use old class files reading implementation. This may slow down the build and cause problems with Groovy interop.
+7 -4
View File
@@ -1,10 +1,13 @@
compiler/testData/cli/jvm/noStdlib.kt:1:8: error: unresolved reference: kotlin
import kotlin.reflect.*
^
compiler/testData/cli/jvm/noStdlib.kt:4:5: error: cannot access built-in declaration 'kotlin.String'. Ensure that you have a dependency on the Kotlin standard library
String::class.primaryConstructor
^
compiler/testData/cli/jvm/noStdlib.kt:4:19: error: unresolved reference: primaryConstructor
String::class.primaryConstructor
^
compiler/testData/cli/jvm/noStdlib.kt:6:11: error: unresolved reference: isExternal
compiler/testData/cli/jvm/noStdlib.kt:6:7: error: cannot access built-in declaration 'kotlin.Unit'. Ensure that you have a dependency on the Kotlin standard library
::foo.isExternal
^
compiler/testData/cli/jvm/noStdlib.kt:6:11: error: cannot access built-in declaration 'kotlin.reflect.KFunction0'. Ensure that you have a dependency on the Kotlin standard library
::foo.isExternal
^
compiler/testData/cli/jvm/noStdlib.kt:7:5: error: unresolved reference: listOf
@@ -1,4 +1,4 @@
// IGNORE_BACKEND: JVM_IR, JS_IR
// IGNORE_BACKEND: JS_IR
fun box(): String {
fun OK() {}
@@ -219,28 +219,28 @@ CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:Int modality:FINAL visibility:publ
PROPERTY IR_EXTERNAL_DECLARATION_STUB name:MAX_VALUE visibility:public modality:FINAL [const,val]
FIELD IR_EXTERNAL_DECLARATION_STUB name:MAX_VALUE type:kotlin.Int visibility:public [final]
EXPRESSION_BODY
CONST Int type=<unbound IrClassSymbolImpl> value=2147483647
CONST Int type=kotlin.Int value=2147483647
FUN IR_EXTERNAL_DECLARATION_STUB name:<get-MAX_VALUE> visibility:public modality:FINAL <> ($this:kotlin.Int.Companion) returnType:kotlin.Int
correspondingProperty: PROPERTY IR_EXTERNAL_DECLARATION_STUB name:MAX_VALUE visibility:public modality:FINAL [const,val]
$this: VALUE_PARAMETER IR_EXTERNAL_DECLARATION_STUB name:<this> type:kotlin.Int.Companion
PROPERTY IR_EXTERNAL_DECLARATION_STUB name:MIN_VALUE visibility:public modality:FINAL [const,val]
FIELD IR_EXTERNAL_DECLARATION_STUB name:MIN_VALUE type:kotlin.Int visibility:public [final]
EXPRESSION_BODY
CONST Int type=<unbound IrClassSymbolImpl> value=-2147483648
CONST Int type=kotlin.Int value=-2147483648
FUN IR_EXTERNAL_DECLARATION_STUB name:<get-MIN_VALUE> visibility:public modality:FINAL <> ($this:kotlin.Int.Companion) returnType:kotlin.Int
correspondingProperty: PROPERTY IR_EXTERNAL_DECLARATION_STUB name:MIN_VALUE visibility:public modality:FINAL [const,val]
$this: VALUE_PARAMETER IR_EXTERNAL_DECLARATION_STUB name:<this> type:kotlin.Int.Companion
PROPERTY IR_EXTERNAL_DECLARATION_STUB name:SIZE_BITS visibility:public modality:FINAL [const,val]
FIELD IR_EXTERNAL_DECLARATION_STUB name:SIZE_BITS type:kotlin.Int visibility:public [final]
EXPRESSION_BODY
CONST Int type=<unbound IrClassSymbolImpl> value=32
CONST Int type=kotlin.Int value=32
FUN IR_EXTERNAL_DECLARATION_STUB name:<get-SIZE_BITS> visibility:public modality:FINAL <> ($this:kotlin.Int.Companion) returnType:kotlin.Int
correspondingProperty: PROPERTY IR_EXTERNAL_DECLARATION_STUB name:SIZE_BITS visibility:public modality:FINAL [const,val]
$this: VALUE_PARAMETER IR_EXTERNAL_DECLARATION_STUB name:<this> type:kotlin.Int.Companion
PROPERTY IR_EXTERNAL_DECLARATION_STUB name:SIZE_BYTES visibility:public modality:FINAL [const,val]
FIELD IR_EXTERNAL_DECLARATION_STUB name:SIZE_BYTES type:kotlin.Int visibility:public [final]
EXPRESSION_BODY
CONST Int type=<unbound IrClassSymbolImpl> value=4
CONST Int type=kotlin.Int value=4
FUN IR_EXTERNAL_DECLARATION_STUB name:<get-SIZE_BYTES> visibility:public modality:FINAL <> ($this:kotlin.Int.Companion) returnType:kotlin.Int
correspondingProperty: PROPERTY IR_EXTERNAL_DECLARATION_STUB name:SIZE_BYTES visibility:public modality:FINAL [const,val]
$this: VALUE_PARAMETER IR_EXTERNAL_DECLARATION_STUB name:<this> type:kotlin.Int.Companion
@@ -24,7 +24,7 @@ abstract class AbstractBuiltInsWithJDKMembersTest : KotlinTestWithEnvironment()
private val configuration = createComparatorConfiguration()
override fun createEnvironment(): KotlinCoreEnvironment =
createEnvironmentWithJdk(ConfigurationKind.JDK_ONLY, testJdkKind)
createEnvironmentWithJdk(ConfigurationKind.JDK_ONLY, testJdkKind)
protected open val testJdkKind: TestJdkKind
get() = TestJdkKind.FULL_JDK
@@ -33,11 +33,12 @@ abstract class AbstractBuiltInsWithJDKMembersTest : KotlinTestWithEnvironment()
val module = JvmResolveUtil.analyze(environment).moduleDescriptor as ModuleDescriptorImpl
for (packageFqName in listOf(BUILT_INS_PACKAGE_FQ_NAME, COLLECTIONS_PACKAGE_FQ_NAME, RANGES_PACKAGE_FQ_NAME)) {
val loaded =
module.packageFragmentProvider.getPackageFragments(packageFqName).filterIsInstance<BuiltInsPackageFragment>().single()
val loaded = module.packageFragmentProvider.getPackageFragments(packageFqName)
.filterIsInstance<BuiltInsPackageFragment>()
.single { !it.isFallback }
RecursiveDescriptorComparator.validateAndCompareDescriptorWithFile(
loaded, configuration,
File("compiler/testData/builtin-classes/$builtinVersionName/" + packageFqName.asString().replace('.', '-') + ".txt")
loaded, configuration,
File("compiler/testData/builtin-classes/$builtinVersionName/" + packageFqName.asString().replace('.', '-') + ".txt")
)
}
}
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.serialization.builtins
import org.jetbrains.kotlin.builtins.DefaultBuiltIns
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime
import org.jetbrains.kotlin.descriptors.deserialization.AdditionalClassPartsProvider
import org.jetbrains.kotlin.descriptors.deserialization.PlatformDependentDeclarationFilter
import org.jetbrains.kotlin.jvm.compiler.LoadDescriptorUtil.TEST_PACKAGE_FQNAME
import org.jetbrains.kotlin.serialization.deserialization.builtins.BuiltInsLoaderImpl
@@ -41,10 +42,15 @@ class BuiltInsSerializerTest : TestCaseWithTmpdir() {
val module = KotlinTestUtils.createEmptyModule("<module>", DefaultBuiltIns.Instance)
val packageFragmentProvider = BuiltInsLoaderImpl().createBuiltInPackageFragmentProvider(
LockBasedStorageManager("BuiltInsSerializerTest"), module, setOf(TEST_PACKAGE_FQNAME), emptyList(), PlatformDependentDeclarationFilter.All
LockBasedStorageManager("BuiltInsSerializerTest"),
module,
setOf(TEST_PACKAGE_FQNAME),
emptyList(),
PlatformDependentDeclarationFilter.All,
AdditionalClassPartsProvider.None,
isFallback = false
) {
val file = File(tmpdir, it)
if (file.exists()) FileInputStream(file) else null
File(tmpdir, it).takeIf(File::exists)?.let(::FileInputStream)
}
module.initialize(packageFragmentProvider)
@@ -92,6 +92,7 @@ public class LoadBuiltinsTest extends KotlinTestWithEnvironment {
Collections.singletonList(new BuiltInFictitiousFunctionClassFactory(storageManager, builtInsModule)),
PlatformDependentDeclarationFilter.All.INSTANCE,
AdditionalClassPartsProvider.None.INSTANCE,
false,
ForTestCompileRuntime.runtimeJarClassLoader()::getResourceAsStream
);
@@ -15,7 +15,9 @@ import org.jetbrains.kotlin.utils.sure
class JvmBuiltIns @JvmOverloads constructor(
storageManager: StorageManager,
loadBuiltInsFromCurrentClassLoader: Boolean = true
loadBuiltInsFromCurrentClassLoader: Boolean = true,
// TODO: change this value to loadBuiltInsFromCurrentClassLoader as soon as built-ins are loaded from module dependencies everywhere
isFallback: Boolean = false
) : KotlinBuiltIns(storageManager) {
// Module containing JDK classes or having them among dependencies
private var ownerModuleDescriptor: ModuleDescriptor? = null
@@ -40,7 +42,7 @@ class JvmBuiltIns @JvmOverloads constructor(
init {
if (loadBuiltInsFromCurrentClassLoader) {
createBuiltInsModule()
createBuiltInsModule(isFallback)
}
}
@@ -52,6 +52,6 @@ class JvmBuiltInsPackageFragmentProvider(
override fun findPackage(fqName: FqName): DeserializedPackageFragment? =
finder.findBuiltInsData(fqName)?.let { inputStream ->
BuiltInsPackageFragmentImpl.create(fqName, storageManager, moduleDescriptor, inputStream)
BuiltInsPackageFragmentImpl.create(fqName, storageManager, moduleDescriptor, inputStream, isFallback = false)
}
}
@@ -495,7 +495,7 @@ open class JvmBuiltInsSettings(
private class FallbackBuiltIns private constructor() : KotlinBuiltIns(LockBasedStorageManager("FallbackBuiltIns")) {
init {
createBuiltInsModule()
createBuiltInsModule(true)
}
companion object {
@@ -26,11 +26,12 @@ import java.util.*
interface BuiltInsLoader {
fun createPackageFragmentProvider(
storageManager: StorageManager,
builtInsModule: ModuleDescriptor,
classDescriptorFactories: Iterable<ClassDescriptorFactory>,
platformDependentDeclarationFilter: PlatformDependentDeclarationFilter,
additionalClassPartsProvider: AdditionalClassPartsProvider
storageManager: StorageManager,
builtInsModule: ModuleDescriptor,
classDescriptorFactories: Iterable<ClassDescriptorFactory>,
platformDependentDeclarationFilter: PlatformDependentDeclarationFilter,
additionalClassPartsProvider: AdditionalClassPartsProvider,
isFallback: Boolean
): PackageFragmentProvider
companion object {
@@ -38,7 +39,7 @@ interface BuiltInsLoader {
val implementations = ServiceLoader.load(BuiltInsLoader::class.java, BuiltInsLoader::class.java.classLoader)
implementations.firstOrNull() ?: throw IllegalStateException(
"No BuiltInsLoader implementation was found. Please ensure that the META-INF/services/ is not stripped " +
"from your application and that the Java virtual machine is not running under a security manager"
"from your application and that the Java virtual machine is not running under a security manager"
)
}
}
@@ -18,4 +18,14 @@ package org.jetbrains.kotlin.builtins
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
interface BuiltInsPackageFragment : PackageFragmentDescriptor
interface BuiltInsPackageFragment : PackageFragmentDescriptor {
/**
* `true` if this package fragment is loaded during compilation from the current compiler's class loader.
* This fallback package fragment is useful because standard types like kotlin.String, kotlin.Unit will be resolved to something
* even in the case of absence of kotlin-stdlib in the module dependencies. So, the code in the compiler that relies on these types
* to be always there, will not crash.
* However, if anything in the source code references anything from the fallback package, this is a compilation error because it means
* that there's no kotlin-stdlib in the dependencies and the build is compiler version-specific, which could lead to weird issues.
*/
val isFallback: Boolean
}
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.storage.LockBasedStorageManager
class DefaultBuiltIns(loadBuiltInsFromCurrentClassLoader: Boolean = true) : KotlinBuiltIns(LockBasedStorageManager("DefaultBuiltIns")) {
init {
if (loadBuiltInsFromCurrentClassLoader) {
createBuiltInsModule()
createBuiltInsModule(false)
}
}
@@ -5,4 +5,7 @@
package org.jetbrains.kotlin.builtins
interface FunctionInterfacePackageFragment : BuiltInsPackageFragment
interface FunctionInterfacePackageFragment : BuiltInsPackageFragment {
override val isFallback: Boolean
get() = false
}
@@ -120,11 +120,11 @@ public abstract class KotlinBuiltIns {
});
}
protected void createBuiltInsModule() {
protected void createBuiltInsModule(boolean isFallback) {
builtInsModule = new ModuleDescriptorImpl(BUILTINS_MODULE_NAME, storageManager, this, null);
builtInsModule.initialize(BuiltInsLoader.Companion.getInstance().createPackageFragmentProvider(
storageManager, builtInsModule,
getClassDescriptorFactories(), getPlatformDependentDeclarationFilter(), getAdditionalClassPartsProvider()
getClassDescriptorFactories(), getPlatformDependentDeclarationFilter(), getAdditionalClassPartsProvider(), isFallback
));
builtInsModule.setDependencies(builtInsModule);
}
@@ -28,7 +28,8 @@ class BuiltInsLoaderImpl : BuiltInsLoader {
builtInsModule: ModuleDescriptor,
classDescriptorFactories: Iterable<ClassDescriptorFactory>,
platformDependentDeclarationFilter: PlatformDependentDeclarationFilter,
additionalClassPartsProvider: AdditionalClassPartsProvider
additionalClassPartsProvider: AdditionalClassPartsProvider,
isFallback: Boolean
): PackageFragmentProvider {
return createBuiltInPackageFragmentProvider(
storageManager,
@@ -37,6 +38,7 @@ class BuiltInsLoaderImpl : BuiltInsLoader {
classDescriptorFactories,
platformDependentDeclarationFilter,
additionalClassPartsProvider,
isFallback,
resourceLoader::loadResource
)
}
@@ -48,12 +50,13 @@ class BuiltInsLoaderImpl : BuiltInsLoader {
classDescriptorFactories: Iterable<ClassDescriptorFactory>,
platformDependentDeclarationFilter: PlatformDependentDeclarationFilter,
additionalClassPartsProvider: AdditionalClassPartsProvider = AdditionalClassPartsProvider.None,
isFallback: Boolean,
loadResource: (String) -> InputStream?
): PackageFragmentProvider {
val packageFragments = packageFqNames.map { fqName ->
val resourcePath = BuiltInSerializerProtocol.getBuiltInsFilePath(fqName)
val inputStream = loadResource(resourcePath) ?: throw IllegalStateException("Resource not found in classpath: $resourcePath")
BuiltInsPackageFragmentImpl.create(fqName, storageManager, module, inputStream)
BuiltInsPackageFragmentImpl.create(fqName, storageManager, module, inputStream, isFallback)
}
val provider = PackageFragmentProviderImpl(packageFragments)
@@ -19,7 +19,8 @@ class BuiltInsPackageFragmentImpl private constructor(
storageManager: StorageManager,
module: ModuleDescriptor,
proto: ProtoBuf.PackageFragment,
metadataVersion: BuiltInsBinaryVersion
metadataVersion: BuiltInsBinaryVersion,
override val isFallback: Boolean
) : BuiltInsPackageFragment, DeserializedPackageFragmentImpl(
fqName, storageManager, module, proto, metadataVersion, containerSource = null
) {
@@ -28,7 +29,8 @@ class BuiltInsPackageFragmentImpl private constructor(
fqName: FqName,
storageManager: StorageManager,
module: ModuleDescriptor,
inputStream: InputStream
inputStream: InputStream,
isFallback: Boolean
): BuiltInsPackageFragmentImpl {
lateinit var version: BuiltInsBinaryVersion
@@ -47,7 +49,7 @@ class BuiltInsPackageFragmentImpl private constructor(
ProtoBuf.PackageFragment.parseFrom(stream, BuiltInSerializerProtocol.extensionRegistry)
}
return BuiltInsPackageFragmentImpl(fqName, storageManager, module, proto, version)
return BuiltInsPackageFragmentImpl(fqName, storageManager, module, proto, version, isFallback)
}
}
}
@@ -28,11 +28,11 @@ import kotlin.sequences.SequencesKt;
import kotlin.text.StringsKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.backend.common.output.OutputFileCollection;
import org.jetbrains.kotlin.checkers.CompilerTestLanguageVersionSettings;
import org.jetbrains.kotlin.codegen.ClassBuilderFactories;
import org.jetbrains.kotlin.codegen.GenerationUtils;
import org.jetbrains.kotlin.codegen.state.GenerationState;
import org.jetbrains.kotlin.config.CompilerConfiguration;
import org.jetbrains.kotlin.config.JVMConfigurationKeys;
import org.jetbrains.kotlin.config.*;
import org.jetbrains.kotlin.idea.debugger.evaluate.KotlinDebuggerCaches;
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase;
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCaseKt;
@@ -46,10 +46,7 @@ import org.jetbrains.kotlin.test.TestJdkKind;
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -133,7 +130,12 @@ public abstract class AbstractPositionManagerTest extends KotlinLightCodeInsight
CompilerConfiguration configuration = KotlinTestUtils.newConfiguration(ConfigurationKind.JDK_ONLY, TestJdkKind.MOCK_JDK);
// TODO: delete this once IDEVirtualFileFinder supports loading .kotlin_builtins files
configuration.put(JVMConfigurationKeys.ADD_BUILT_INS_FROM_COMPILER_TO_DEPENDENCIES, true);
CommonConfigurationKeysKt.setLanguageVersionSettings(configuration, new CompilerTestLanguageVersionSettings(
Collections.emptyMap(),
ApiVersion.LATEST_STABLE,
LanguageVersion.LATEST_STABLE,
Collections.singletonMap(JvmAnalysisFlags.getSuppressMissingBuiltinsError(), true)
));
GenerationState state =
GenerationUtils.compileFiles(files, configuration, ClassBuilderFactories.TEST, scope -> PackagePartProvider.Empty.INSTANCE);
@@ -227,7 +227,7 @@ open class KotlinJpsBuildTest : AbstractKotlinJpsBuildTestCase() {
}
fun testSourcePackageLongPrefix() {
initProject()
initProject(JVM_MOCK_RUNTIME)
val buildResult = buildAllModules()
buildResult.assertSuccessful()
val warnings = buildResult.getMessages(BuildMessage.Kind.WARNING)
@@ -236,7 +236,7 @@ open class KotlinJpsBuildTest : AbstractKotlinJpsBuildTestCase() {
}
fun testSourcePackagePrefixWithInnerClasses() {
initProject()
initProject(JVM_MOCK_RUNTIME)
buildAllModules().assertSuccessful()
}
+2 -1
View File
@@ -67,6 +67,7 @@ configurations {
longRunningTestCompile.extendsFrom(testCompile)
builtins
compileOnly.extendsFrom(builtins)
coroutinesExperimentalCompile.extendsFrom(builtins)
}
dependencies {
@@ -261,4 +262,4 @@ task longRunningTest(type: Test, dependsOn: longRunningTestClasses) {
if (project.hasProperty("kotlin.stdlib.test.long.running")) {
check.dependsOn(longRunningTest)
}
}
@@ -22,6 +22,10 @@ sourceSets {
}
}
dependencies {
compile(project(":core:builtins"))
}
publish()
sourcesJar()
@@ -19,6 +19,10 @@ sourceSets {
}
}
dependencies {
compile project(':core:builtins')
}
artifacts {
archives sourcesJar
}
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.gradle.tasks
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.file.FileCollection
import org.gradle.api.plugins.BasePluginConvention
import org.gradle.api.tasks.*
@@ -29,9 +28,14 @@ import org.jetbrains.kotlin.gradle.internal.prepareCompilerArguments
import org.jetbrains.kotlin.gradle.internal.tasks.TaskWithLocalState
import org.jetbrains.kotlin.gradle.internal.tasks.allOutputFiles
import org.jetbrains.kotlin.gradle.logging.*
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.COMPILER_CLASSPATH_CONFIGURATION_NAME
import org.jetbrains.kotlin.gradle.plugin.KotlinMultiplatformPluginWrapper
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformPluginBase
import org.jetbrains.kotlin.gradle.plugin.PLUGIN_CLASSPATH_CONFIGURATION_NAME
import org.jetbrains.kotlin.gradle.report.BuildReportMode
import org.jetbrains.kotlin.gradle.utils.*
import org.jetbrains.kotlin.gradle.utils.isParentOf
import org.jetbrains.kotlin.gradle.utils.pathsAsStringRelativeTo
import org.jetbrains.kotlin.gradle.utils.toSortedPathsArray
import org.jetbrains.kotlin.incremental.ChangedFiles
import org.jetbrains.kotlin.incremental.classpathAsList
import org.jetbrains.kotlin.incremental.destinationAsFile
@@ -393,13 +397,6 @@ open class KotlinCompile : AbstractKotlinCompile<K2JVMCompilerArguments>(), Kotl
args.apply { fillDefaultValues() }
super.setupCompilerArgs(args, defaultsOnly)
args.addCompilerBuiltIns = true
val gradleVersion = getGradleVersion()
if (gradleVersion == null || gradleVersion >= ParsedGradleVersion(3, 2)) {
args.loadBuiltInsFromDependencies = true
}
args.moduleName = friendTask?.moduleName ?: moduleName
logger.kotlinDebug { "args.moduleName = ${args.moduleName}" }
@@ -614,13 +611,3 @@ private val invalidModuleNameCharactersRegex = """[\\/\r\n\t]""".toRegex()
private fun filterModuleName(moduleName: String): String =
moduleName.replace(invalidModuleNameCharactersRegex, "_")
private fun Task.getGradleVersion(): ParsedGradleVersion? {
val gradleVersion = project.gradle.gradleVersion
val result = ParsedGradleVersion.parse(gradleVersion)
if (result == null) {
logger.kotlinDebug("Could not parse gradle version: $gradleVersion")
}
return result
}
@@ -40,7 +40,7 @@
</goals>
<configuration>
<args combine.children="append">
<arg>-Xadd-compiler-builtins</arg>
<arg>-Xsuppress-missing-builtins-error</arg>
</args>
</configuration>
</execution>