From 650b04b4dd6cc934cb22d617af757b6512cee054 Mon Sep 17 00:00:00 2001 From: Roman Artemev Date: Tue, 16 Nov 2021 13:47:07 +0300 Subject: [PATCH] [JS IR] Make assert checks no unbound symbols left conditional --- .../jetbrains/kotlin/ir/backend/js/compiler.kt | 7 ++++++- .../kotlin/psi2ir/Psi2IrConfiguration.kt | 3 ++- .../jetbrains/kotlin/psi2ir/Psi2IrTranslator.kt | 4 +++- .../org/jetbrains/kotlin/ir/backend/js/klib.kt | 16 ++++++++++++---- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt index 046a3bb5f22..e50e54ab673 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt @@ -25,6 +25,7 @@ import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator import org.jetbrains.kotlin.ir.util.SymbolTable import org.jetbrains.kotlin.ir.util.noUnboundLeft 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.name.FqName @@ -134,6 +135,8 @@ fun compileIr( is MainModule.Klib -> dependencyModules } + val allowUnboundSymbols = configuration[JSConfigurationKeys.PARTIAL_LINKAGE] ?: false + val context = JsIrBackendContext( moduleDescriptor, irBuiltIns, @@ -156,7 +159,9 @@ fun compileIr( ExternalDependenciesGenerator(symbolTable, irProviders).generateUnboundSymbolsAsDependencies() 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 -> moveBodilessDeclarationsToSeparatePlace(context, module) diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/Psi2IrConfiguration.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/Psi2IrConfiguration.kt index ca004fdef60..b6e11621604 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/Psi2IrConfiguration.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/Psi2IrConfiguration.kt @@ -17,5 +17,6 @@ package org.jetbrains.kotlin.psi2ir class Psi2IrConfiguration( - val ignoreErrors: Boolean = false + val ignoreErrors: Boolean = false, + val allowUnboundSymbols: Boolean = false ) diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/Psi2IrTranslator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/Psi2IrTranslator.kt index a1b43604efb..56d0460a8d7 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/Psi2IrTranslator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/Psi2IrTranslator.kt @@ -92,7 +92,9 @@ class Psi2IrTranslator( moduleGenerator.generateUnboundSymbolsAsDependencies(irProviders) 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) } // assert(context.symbolTable.allUnbound.isEmpty()) // TODO: fix IrPluginContext to make it not produce additional external reference diff --git a/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/klib.kt b/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/klib.kt index 59a1320cccd..593338965a0 100644 --- a/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/klib.kt +++ b/compiler/ir/serialization.js/src/org/jetbrains/kotlin/ir/backend/js/klib.kt @@ -134,6 +134,7 @@ fun generateIrForKlibSerialization( val incrementalDataProvider = configuration.get(JSConfigurationKeys.INCREMENTAL_DATA_PROVIDER) val errorPolicy = configuration.get(JSConfigurationKeys.ERROR_TOLERANCE_POLICY) ?: ErrorTolerancePolicy.DEFAULT val messageLogger = configuration.get(IrMessageLogger.IR_MESSAGE_LOGGER) ?: IrMessageLogger.None + val allowUnboundSymbols = configuration[JSConfigurationKeys.PARTIAL_LINKAGE] ?: false val serializedIrFiles = mutableListOf() @@ -162,7 +163,7 @@ fun generateIrForKlibSerialization( } 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 irBuiltIns = psi2IrContext.irBuiltIns @@ -325,6 +326,7 @@ fun loadIr( val allDependencies = depsDescriptors.allDependencies.map { it.library } val errorPolicy = configuration.get(JSConfigurationKeys.ERROR_TOLERANCE_POLICY) ?: ErrorTolerancePolicy.DEFAULT val messageLogger = configuration.get(IrMessageLogger.IR_MESSAGE_LOGGER) ?: IrMessageLogger.None + val allowUnboundSymbol = configuration[JSConfigurationKeys.PARTIAL_LINKAGE] ?: false val signaturer = IdSignatureDescriptor(JsManglerDesc) val symbolTable = SymbolTable(signaturer, irFactory) @@ -332,7 +334,7 @@ fun loadIr( when (mainModule) { is MainModule.SourceFiles -> { assert(filesToLoad == null) - val psi2IrContext = preparePsi2Ir(depsDescriptors, errorPolicy, symbolTable) + val psi2IrContext = preparePsi2Ir(depsDescriptors, errorPolicy, symbolTable, allowUnboundSymbol) val friendModules = mapOf(psi2IrContext.moduleDescriptor.name.asString() to depsDescriptors.friendDependencies.map { it.library.uniqueName }) @@ -465,6 +467,9 @@ fun getIrModuleInfoForSourceFiles( val feContext = psi2IrContext.run { JsIrLinker.JsFePluginContext(moduleDescriptor, symbolTable, typeTranslator, irBuiltIns) } + + val allowUnboundSymbols = configuration[JSConfigurationKeys.PARTIAL_LINKAGE] ?: false + val irLinker = JsIrLinker( psi2IrContext.moduleDescriptor, @@ -488,7 +493,9 @@ fun getIrModuleInfoForSourceFiles( ) 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. @@ -536,11 +543,12 @@ private fun preparePsi2Ir( depsDescriptors: ModulesStructure, errorIgnorancePolicy: ErrorTolerancePolicy, symbolTable: SymbolTable, + allowUnboundSymbols: Boolean ): GeneratorContext { val analysisResult = depsDescriptors.jsFrontEndResult val psi2Ir = Psi2IrTranslator( depsDescriptors.compilerConfiguration.languageVersionSettings, - Psi2IrConfiguration(errorIgnorancePolicy.allowErrors) + Psi2IrConfiguration(errorIgnorancePolicy.allowErrors, allowUnboundSymbols) ) return psi2Ir.createGeneratorContext( analysisResult.moduleDescriptor,