[JS IR] Make assert checks no unbound symbols left conditional

This commit is contained in:
Roman Artemev
2021-11-16 13:47:07 +03:00
committed by TeamCityServer
parent e4585730d6
commit 650b04b4dd
4 changed files with 23 additions and 7 deletions
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator
import org.jetbrains.kotlin.ir.util.SymbolTable import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.util.noUnboundLeft import org.jetbrains.kotlin.ir.util.noUnboundLeft
import org.jetbrains.kotlin.js.backend.ast.JsProgram import org.jetbrains.kotlin.js.backend.ast.JsProgram
import org.jetbrains.kotlin.js.config.JSConfigurationKeys
import org.jetbrains.kotlin.js.config.RuntimeDiagnostic import org.jetbrains.kotlin.js.config.RuntimeDiagnostic
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
@@ -134,6 +135,8 @@ fun compileIr(
is MainModule.Klib -> dependencyModules is MainModule.Klib -> dependencyModules
} }
val allowUnboundSymbols = configuration[JSConfigurationKeys.PARTIAL_LINKAGE] ?: false
val context = JsIrBackendContext( val context = JsIrBackendContext(
moduleDescriptor, moduleDescriptor,
irBuiltIns, irBuiltIns,
@@ -156,7 +159,9 @@ fun compileIr(
ExternalDependenciesGenerator(symbolTable, irProviders).generateUnboundSymbolsAsDependencies() ExternalDependenciesGenerator(symbolTable, irProviders).generateUnboundSymbolsAsDependencies()
deserializer.postProcess() deserializer.postProcess()
symbolTable.noUnboundLeft("Unbound symbols at the end of linker") if (!allowUnboundSymbols) {
symbolTable.noUnboundLeft("Unbound symbols at the end of linker")
}
allModules.forEach { module -> allModules.forEach { module ->
moveBodilessDeclarationsToSeparatePlace(context, module) moveBodilessDeclarationsToSeparatePlace(context, module)
@@ -17,5 +17,6 @@
package org.jetbrains.kotlin.psi2ir package org.jetbrains.kotlin.psi2ir
class Psi2IrConfiguration( class Psi2IrConfiguration(
val ignoreErrors: Boolean = false val ignoreErrors: Boolean = false,
val allowUnboundSymbols: Boolean = false
) )
@@ -92,7 +92,9 @@ class Psi2IrTranslator(
moduleGenerator.generateUnboundSymbolsAsDependencies(irProviders) moduleGenerator.generateUnboundSymbolsAsDependencies(irProviders)
deserializers.forEach { it.postProcess() } deserializers.forEach { it.postProcess() }
context.symbolTable.noUnboundLeft("Unbound symbols not allowed\n") if (!context.configuration.allowUnboundSymbols) {
context.symbolTable.noUnboundLeft("Unbound symbols not allowed\n")
}
postprocessingSteps.forEach { it.invoke(irModule) } postprocessingSteps.forEach { it.invoke(irModule) }
// assert(context.symbolTable.allUnbound.isEmpty()) // TODO: fix IrPluginContext to make it not produce additional external reference // assert(context.symbolTable.allUnbound.isEmpty()) // TODO: fix IrPluginContext to make it not produce additional external reference
@@ -134,6 +134,7 @@ fun generateIrForKlibSerialization(
val incrementalDataProvider = configuration.get(JSConfigurationKeys.INCREMENTAL_DATA_PROVIDER) val incrementalDataProvider = configuration.get(JSConfigurationKeys.INCREMENTAL_DATA_PROVIDER)
val errorPolicy = configuration.get(JSConfigurationKeys.ERROR_TOLERANCE_POLICY) ?: ErrorTolerancePolicy.DEFAULT val errorPolicy = configuration.get(JSConfigurationKeys.ERROR_TOLERANCE_POLICY) ?: ErrorTolerancePolicy.DEFAULT
val messageLogger = configuration.get(IrMessageLogger.IR_MESSAGE_LOGGER) ?: IrMessageLogger.None val messageLogger = configuration.get(IrMessageLogger.IR_MESSAGE_LOGGER) ?: IrMessageLogger.None
val allowUnboundSymbols = configuration[JSConfigurationKeys.PARTIAL_LINKAGE] ?: false
val serializedIrFiles = mutableListOf<SerializedIrFile>() val serializedIrFiles = mutableListOf<SerializedIrFile>()
@@ -162,7 +163,7 @@ fun generateIrForKlibSerialization(
} }
val symbolTable = SymbolTable(IdSignatureDescriptor(JsManglerDesc), irFactory) val symbolTable = SymbolTable(IdSignatureDescriptor(JsManglerDesc), irFactory)
val psi2Ir = Psi2IrTranslator(configuration.languageVersionSettings, Psi2IrConfiguration(errorPolicy.allowErrors)) val psi2Ir = Psi2IrTranslator(configuration.languageVersionSettings, Psi2IrConfiguration(errorPolicy.allowErrors, allowUnboundSymbols))
val psi2IrContext = psi2Ir.createGeneratorContext(analysisResult.moduleDescriptor, analysisResult.bindingContext, symbolTable) val psi2IrContext = psi2Ir.createGeneratorContext(analysisResult.moduleDescriptor, analysisResult.bindingContext, symbolTable)
val irBuiltIns = psi2IrContext.irBuiltIns val irBuiltIns = psi2IrContext.irBuiltIns
@@ -325,6 +326,7 @@ fun loadIr(
val allDependencies = depsDescriptors.allDependencies.map { it.library } val allDependencies = depsDescriptors.allDependencies.map { it.library }
val errorPolicy = configuration.get(JSConfigurationKeys.ERROR_TOLERANCE_POLICY) ?: ErrorTolerancePolicy.DEFAULT val errorPolicy = configuration.get(JSConfigurationKeys.ERROR_TOLERANCE_POLICY) ?: ErrorTolerancePolicy.DEFAULT
val messageLogger = configuration.get(IrMessageLogger.IR_MESSAGE_LOGGER) ?: IrMessageLogger.None val messageLogger = configuration.get(IrMessageLogger.IR_MESSAGE_LOGGER) ?: IrMessageLogger.None
val allowUnboundSymbol = configuration[JSConfigurationKeys.PARTIAL_LINKAGE] ?: false
val signaturer = IdSignatureDescriptor(JsManglerDesc) val signaturer = IdSignatureDescriptor(JsManglerDesc)
val symbolTable = SymbolTable(signaturer, irFactory) val symbolTable = SymbolTable(signaturer, irFactory)
@@ -332,7 +334,7 @@ fun loadIr(
when (mainModule) { when (mainModule) {
is MainModule.SourceFiles -> { is MainModule.SourceFiles -> {
assert(filesToLoad == null) assert(filesToLoad == null)
val psi2IrContext = preparePsi2Ir(depsDescriptors, errorPolicy, symbolTable) val psi2IrContext = preparePsi2Ir(depsDescriptors, errorPolicy, symbolTable, allowUnboundSymbol)
val friendModules = val friendModules =
mapOf(psi2IrContext.moduleDescriptor.name.asString() to depsDescriptors.friendDependencies.map { it.library.uniqueName }) mapOf(psi2IrContext.moduleDescriptor.name.asString() to depsDescriptors.friendDependencies.map { it.library.uniqueName })
@@ -465,6 +467,9 @@ fun getIrModuleInfoForSourceFiles(
val feContext = psi2IrContext.run { val feContext = psi2IrContext.run {
JsIrLinker.JsFePluginContext(moduleDescriptor, symbolTable, typeTranslator, irBuiltIns) JsIrLinker.JsFePluginContext(moduleDescriptor, symbolTable, typeTranslator, irBuiltIns)
} }
val allowUnboundSymbols = configuration[JSConfigurationKeys.PARTIAL_LINKAGE] ?: false
val irLinker = val irLinker =
JsIrLinker( JsIrLinker(
psi2IrContext.moduleDescriptor, psi2IrContext.moduleDescriptor,
@@ -488,7 +493,9 @@ fun getIrModuleInfoForSourceFiles(
) )
val moduleFragment = psi2IrContext.generateModuleFragmentWithPlugins(project, files, irLinker, messageLogger) val moduleFragment = psi2IrContext.generateModuleFragmentWithPlugins(project, files, irLinker, messageLogger)
symbolTable.noUnboundLeft("Unbound symbols left after linker") if (!allowUnboundSymbols) {
symbolTable.noUnboundLeft("Unbound symbols left after linker")
}
// TODO: not sure whether this check should be enabled by default. Add configuration key for it. // TODO: not sure whether this check should be enabled by default. Add configuration key for it.
@@ -536,11 +543,12 @@ private fun preparePsi2Ir(
depsDescriptors: ModulesStructure, depsDescriptors: ModulesStructure,
errorIgnorancePolicy: ErrorTolerancePolicy, errorIgnorancePolicy: ErrorTolerancePolicy,
symbolTable: SymbolTable, symbolTable: SymbolTable,
allowUnboundSymbols: Boolean
): GeneratorContext { ): GeneratorContext {
val analysisResult = depsDescriptors.jsFrontEndResult val analysisResult = depsDescriptors.jsFrontEndResult
val psi2Ir = Psi2IrTranslator( val psi2Ir = Psi2IrTranslator(
depsDescriptors.compilerConfiguration.languageVersionSettings, depsDescriptors.compilerConfiguration.languageVersionSettings,
Psi2IrConfiguration(errorIgnorancePolicy.allowErrors) Psi2IrConfiguration(errorIgnorancePolicy.allowErrors, allowUnboundSymbols)
) )
return psi2Ir.createGeneratorContext( return psi2Ir.createGeneratorContext(
analysisResult.moduleDescriptor, analysisResult.moduleDescriptor,