[Wasm] Wasi frontend diagnostics

This commit is contained in:
Igor Yakovlev
2023-07-14 17:48:14 +02:00
committed by Zalim Bashorov
parent 98329f30f8
commit 60d35200f6
15 changed files with 161 additions and 26 deletions
@@ -28,7 +28,6 @@ import org.jetbrains.kotlin.config.JVMConfigurationKeys
import org.jetbrains.kotlin.idea.KotlinFileType import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.js.resolve.JsPlatformAnalyzerServices import org.jetbrains.kotlin.js.resolve.JsPlatformAnalyzerServices
import org.jetbrains.kotlin.parsing.KotlinParserDefinition import org.jetbrains.kotlin.parsing.KotlinParserDefinition
import org.jetbrains.kotlin.wasm.resolve.WasmPlatformAnalyzerServices
import org.jetbrains.kotlin.platform.TargetPlatform import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.platform.isCommon import org.jetbrains.kotlin.platform.isCommon
import org.jetbrains.kotlin.platform.isJs import org.jetbrains.kotlin.platform.isJs
@@ -40,6 +39,7 @@ import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatformAnalyzerServices import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatformAnalyzerServices
import org.jetbrains.kotlin.resolve.konan.platform.NativePlatformAnalyzerServices import org.jetbrains.kotlin.resolve.konan.platform.NativePlatformAnalyzerServices
import org.jetbrains.kotlin.wasm.resolve.WasmJsPlatformAnalyzerServices
import java.io.IOException import java.io.IOException
import java.nio.file.* import java.nio.file.*
import java.nio.file.attribute.BasicFileAttributes import java.nio.file.attribute.BasicFileAttributes
@@ -49,7 +49,7 @@ internal fun TargetPlatform.getAnalyzerServices(): PlatformDependentAnalyzerServ
return when { return when {
isJvm() -> JvmPlatformAnalyzerServices isJvm() -> JvmPlatformAnalyzerServices
isJs() -> JsPlatformAnalyzerServices isJs() -> JsPlatformAnalyzerServices
isWasm() -> WasmPlatformAnalyzerServices isWasm() -> WasmJsPlatformAnalyzerServices
isNative() -> NativePlatformAnalyzerServices isNative() -> NativePlatformAnalyzerServices
isCommon() -> CommonPlatformAnalyzerServices isCommon() -> CommonPlatformAnalyzerServices
else -> error("Unknown target platform: $this") else -> error("Unknown target platform: $this")
@@ -420,6 +420,10 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
): ModulesStructure { ): ModulesStructure {
lateinit var sourceModule: ModulesStructure lateinit var sourceModule: ModulesStructure
do { do {
val analyzerFacade = when (arguments.wasm) {
true -> TopDownAnalyzerFacadeForWasm.facadeFor(environmentForJS.configuration.get(JSConfigurationKeys.WASM_TARGET))
else -> TopDownAnalyzerFacadeForJSIR
}
sourceModule = prepareAnalyzedSourceModule( sourceModule = prepareAnalyzedSourceModule(
environmentForJS.project, environmentForJS.project,
environmentForJS.getSourceFiles(), environmentForJS.getSourceFiles(),
@@ -427,7 +431,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
libraries, libraries,
friendLibraries, friendLibraries,
AnalyzerWithCompilerReport(environmentForJS.configuration), AnalyzerWithCompilerReport(environmentForJS.configuration),
analyzerFacade = if (arguments.wasm) TopDownAnalyzerFacadeForWasm else TopDownAnalyzerFacadeForJSIR analyzerFacade = analyzerFacade
) )
val result = sourceModule.jsFrontEndResult.jsAnalysisResult val result = sourceModule.jsFrontEndResult.jsAnalysisResult
if (result is JsAnalysisResult.RetryWithAdditionalRoots) { if (result is JsAnalysisResult.RetryWithAdditionalRoots) {
@@ -12,14 +12,15 @@ import org.jetbrains.kotlin.incremental.components.LookupTracker
import org.jetbrains.kotlin.incremental.js.IncrementalDataProvider import org.jetbrains.kotlin.incremental.js.IncrementalDataProvider
import org.jetbrains.kotlin.ir.backend.js.JsFactories import org.jetbrains.kotlin.ir.backend.js.JsFactories
import org.jetbrains.kotlin.js.analyze.AbstractTopDownAnalyzerFacadeForWeb import org.jetbrains.kotlin.js.analyze.AbstractTopDownAnalyzerFacadeForWeb
import org.jetbrains.kotlin.js.config.WasmTarget
import org.jetbrains.kotlin.platform.TargetPlatform import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.platform.wasm.WasmPlatforms import org.jetbrains.kotlin.platform.wasm.WasmPlatforms
import org.jetbrains.kotlin.resolve.CompilerDeserializationConfiguration import org.jetbrains.kotlin.resolve.CompilerDeserializationConfiguration
import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices
import org.jetbrains.kotlin.wasm.resolve.WasmPlatformAnalyzerServices import org.jetbrains.kotlin.wasm.resolve.WasmJsPlatformAnalyzerServices
import org.jetbrains.kotlin.wasm.resolve.WasmWasiPlatformAnalyzerServices
object TopDownAnalyzerFacadeForWasm : AbstractTopDownAnalyzerFacadeForWeb() { abstract class TopDownAnalyzerFacadeForWasm : AbstractTopDownAnalyzerFacadeForWeb() {
override val analyzerServices: PlatformDependentAnalyzerServices = WasmPlatformAnalyzerServices
override val platform: TargetPlatform = WasmPlatforms.Default override val platform: TargetPlatform = WasmPlatforms.Default
override fun loadIncrementalCacheMetadata( override fun loadIncrementalCacheMetadata(
@@ -36,4 +37,19 @@ object TopDownAnalyzerFacadeForWasm : AbstractTopDownAnalyzerFacadeForWeb() {
lookupTracker lookupTracker
) )
} }
companion object {
fun facadeFor(target: WasmTarget?): TopDownAnalyzerFacadeForWasm = when (target) {
WasmTarget.WASI -> TopDownAnalyzerFacadeForWasmWasi
else -> TopDownAnalyzerFacadeForWasmJs
}
}
}
object TopDownAnalyzerFacadeForWasmJs : TopDownAnalyzerFacadeForWasm() {
override val analyzerServices: PlatformDependentAnalyzerServices = WasmJsPlatformAnalyzerServices
}
object TopDownAnalyzerFacadeForWasmWasi : TopDownAnalyzerFacadeForWasm() {
override val analyzerServices: PlatformDependentAnalyzerServices = WasmWasiPlatformAnalyzerServices
} }
@@ -38,6 +38,7 @@ import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsManglerIr
import org.jetbrains.kotlin.ir.types.IrTypeSystemContextImpl import org.jetbrains.kotlin.ir.types.IrTypeSystemContextImpl
import org.jetbrains.kotlin.ir.util.IrMessageLogger import org.jetbrains.kotlin.ir.util.IrMessageLogger
import org.jetbrains.kotlin.js.config.JSConfigurationKeys import org.jetbrains.kotlin.js.config.JSConfigurationKeys
import org.jetbrains.kotlin.js.config.WasmTarget
import org.jetbrains.kotlin.js.resolve.JsPlatformAnalyzerServices import org.jetbrains.kotlin.js.resolve.JsPlatformAnalyzerServices
import org.jetbrains.kotlin.library.KotlinAbiVersion import org.jetbrains.kotlin.library.KotlinAbiVersion
import org.jetbrains.kotlin.library.impl.BuiltInsPlatform import org.jetbrains.kotlin.library.impl.BuiltInsPlatform
@@ -49,7 +50,8 @@ import org.jetbrains.kotlin.platform.wasm.WasmPlatforms
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.storage.LockBasedStorageManager import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.utils.metadataVersion import org.jetbrains.kotlin.utils.metadataVersion
import org.jetbrains.kotlin.wasm.resolve.WasmPlatformAnalyzerServices import org.jetbrains.kotlin.wasm.resolve.WasmJsPlatformAnalyzerServices
import org.jetbrains.kotlin.wasm.resolve.WasmWasiPlatformAnalyzerServices
import java.io.File import java.io.File
import java.nio.file.Paths import java.nio.file.Paths
@@ -70,9 +72,13 @@ inline fun <F> compileModuleToAnalyzedFir(
val mainModuleName = moduleStructure.compilerConfiguration.get(CommonConfigurationKeys.MODULE_NAME)!! val mainModuleName = moduleStructure.compilerConfiguration.get(CommonConfigurationKeys.MODULE_NAME)!!
val escapedMainModuleName = Name.special("<$mainModuleName>") val escapedMainModuleName = Name.special("<$mainModuleName>")
val platform = if (useWasmPlatform) WasmPlatforms.Default else JsPlatforms.defaultJsPlatform val platform = if (useWasmPlatform) WasmPlatforms.Default else JsPlatforms.defaultJsPlatform
val platformAnalyzerServices = if (useWasmPlatform) WasmPlatformAnalyzerServices else JsPlatformAnalyzerServices val platformAnalyzerServices = if (useWasmPlatform) {
when (moduleStructure.compilerConfiguration.get(JSConfigurationKeys.WASM_TARGET, WasmTarget.JS)) {
WasmTarget.JS -> WasmJsPlatformAnalyzerServices
WasmTarget.WASI -> WasmWasiPlatformAnalyzerServices
}
} else JsPlatformAnalyzerServices
val binaryModuleData = BinaryModuleData.initialize(escapedMainModuleName, platform, platformAnalyzerServices) val binaryModuleData = BinaryModuleData.initialize(escapedMainModuleName, platform, platformAnalyzerServices)
val dependencyList = DependencyListForCliModule.build(binaryModuleData) { val dependencyList = DependencyListForCliModule.build(binaryModuleData) {
@@ -20,6 +20,8 @@ import org.jetbrains.kotlin.fir.session.*
import org.jetbrains.kotlin.fir.session.environment.AbstractProjectEnvironment import org.jetbrains.kotlin.fir.session.environment.AbstractProjectEnvironment
import org.jetbrains.kotlin.fir.session.environment.AbstractProjectFileSearchScope import org.jetbrains.kotlin.fir.session.environment.AbstractProjectFileSearchScope
import org.jetbrains.kotlin.incremental.components.LookupTracker import org.jetbrains.kotlin.incremental.components.LookupTracker
import org.jetbrains.kotlin.js.config.JSConfigurationKeys
import org.jetbrains.kotlin.js.config.WasmTarget
import org.jetbrains.kotlin.js.resolve.JsPlatformAnalyzerServices import org.jetbrains.kotlin.js.resolve.JsPlatformAnalyzerServices
import org.jetbrains.kotlin.library.KotlinLibrary import org.jetbrains.kotlin.library.KotlinLibrary
import org.jetbrains.kotlin.library.metadata.resolver.KotlinResolvedLibrary import org.jetbrains.kotlin.library.metadata.resolver.KotlinResolvedLibrary
@@ -37,7 +39,8 @@ import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatformAnalyzerServices
import org.jetbrains.kotlin.resolve.konan.platform.NativePlatformAnalyzerServices import org.jetbrains.kotlin.resolve.konan.platform.NativePlatformAnalyzerServices
import org.jetbrains.kotlin.resolve.multiplatform.hmppModuleName import org.jetbrains.kotlin.resolve.multiplatform.hmppModuleName
import org.jetbrains.kotlin.resolve.multiplatform.isCommonSource import org.jetbrains.kotlin.resolve.multiplatform.isCommonSource
import org.jetbrains.kotlin.wasm.resolve.WasmPlatformAnalyzerServices import org.jetbrains.kotlin.wasm.resolve.WasmJsPlatformAnalyzerServices
import org.jetbrains.kotlin.wasm.resolve.WasmWasiPlatformAnalyzerServices
val isCommonSourceForPsi: (KtFile) -> Boolean = { it.isCommonSource == true } val isCommonSourceForPsi: (KtFile) -> Boolean = { it.isCommonSource == true }
val fileBelongsToModuleForPsi: (KtFile, String) -> Boolean = { file, moduleName -> file.hmppModuleName == moduleName } val fileBelongsToModuleForPsi: (KtFile, String) -> Boolean = { file, moduleName -> file.hmppModuleName == moduleName }
@@ -213,8 +216,12 @@ fun <F> prepareWasmSessions(
lookupTracker: LookupTracker?, lookupTracker: LookupTracker?,
icData: KlibIcData?, icData: KlibIcData?,
): List<SessionWithSources<F>> { ): List<SessionWithSources<F>> {
val analyzerServices = when (configuration.get(JSConfigurationKeys.WASM_TARGET, WasmTarget.JS)) {
WasmTarget.JS -> WasmJsPlatformAnalyzerServices
WasmTarget.WASI -> WasmWasiPlatformAnalyzerServices
}
return prepareSessions( return prepareSessions(
files, configuration, rootModuleName, WasmPlatforms.Default, WasmPlatformAnalyzerServices, files, configuration, rootModuleName, WasmPlatforms.Default, analyzerServices,
metadataCompilationMode = false, libraryList, isCommonSource, fileBelongsToModule, metadataCompilationMode = false, libraryList, isCommonSource, fileBelongsToModule,
createLibrarySession = { sessionProvider -> createLibrarySession = { sessionProvider ->
FirWasmSessionFactory.createLibrarySession( FirWasmSessionFactory.createLibrarySession(
@@ -16,7 +16,7 @@ import org.jetbrains.kotlin.platform.konan.isNative
import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices
import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatformAnalyzerServices import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatformAnalyzerServices
import org.jetbrains.kotlin.resolve.konan.platform.NativePlatformAnalyzerServices import org.jetbrains.kotlin.resolve.konan.platform.NativePlatformAnalyzerServices
import org.jetbrains.kotlin.wasm.resolve.WasmPlatformAnalyzerServices import org.jetbrains.kotlin.wasm.resolve.WasmJsPlatformAnalyzerServices
import java.io.File import java.io.File
/** /**
@@ -41,7 +41,7 @@ fun TargetPlatform.getAnalyzerServices(): PlatformDependentAnalyzerServices {
isJs() -> JsPlatformAnalyzerServices isJs() -> JsPlatformAnalyzerServices
isNative() -> NativePlatformAnalyzerServices isNative() -> NativePlatformAnalyzerServices
isCommon() -> CommonPlatformAnalyzerServices isCommon() -> CommonPlatformAnalyzerServices
isWasm() -> WasmPlatformAnalyzerServices isWasm() -> WasmJsPlatformAnalyzerServices
else -> error("Unknown target platform: $this") else -> error("Unknown target platform: $this")
} }
} }
@@ -154,7 +154,8 @@ class ClassicFrontend2IrConverter(
} }
val errorPolicy = configuration.get(JSConfigurationKeys.ERROR_TOLERANCE_POLICY) ?: ErrorTolerancePolicy.DEFAULT val errorPolicy = configuration.get(JSConfigurationKeys.ERROR_TOLERANCE_POLICY) ?: ErrorTolerancePolicy.DEFAULT
val hasErrors = TopDownAnalyzerFacadeForWasm.checkForErrors(sourceFiles, analysisResult.bindingContext, errorPolicy) val analyzerFacade = TopDownAnalyzerFacadeForWasm.facadeFor(configuration.get(JSConfigurationKeys.WASM_TARGET))
val hasErrors = analyzerFacade.checkForErrors(sourceFiles, analysisResult.bindingContext, errorPolicy)
val metadataSerializer = KlibMetadataIncrementalSerializer(configuration, project, hasErrors) val metadataSerializer = KlibMetadataIncrementalSerializer(configuration, project, hasErrors)
return IrBackendInput.WasmBackendInput( return IrBackendInput.WasmBackendInput(
@@ -44,6 +44,8 @@ import org.jetbrains.kotlin.incremental.components.InlineConstTracker
import org.jetbrains.kotlin.incremental.components.LookupTracker import org.jetbrains.kotlin.incremental.components.LookupTracker
import org.jetbrains.kotlin.ir.backend.js.* import org.jetbrains.kotlin.ir.backend.js.*
import org.jetbrains.kotlin.js.analyze.TopDownAnalyzerFacadeForJS import org.jetbrains.kotlin.js.analyze.TopDownAnalyzerFacadeForJS
import org.jetbrains.kotlin.js.config.JSConfigurationKeys
import org.jetbrains.kotlin.js.config.WasmTarget
import org.jetbrains.kotlin.library.unresolvedDependencies import org.jetbrains.kotlin.library.unresolvedDependencies
import org.jetbrains.kotlin.load.java.lazy.SingleModuleClassResolver import org.jetbrains.kotlin.load.java.lazy.SingleModuleClassResolver
import org.jetbrains.kotlin.load.kotlin.ModuleVisibilityManager import org.jetbrains.kotlin.load.kotlin.ModuleVisibilityManager
@@ -356,7 +358,9 @@ class ClassicFrontendFacade(
val allDependencies = runtimeKlibs + dependencyDescriptors + friendLibraries + friendsDescriptors + transitiveLibraries val allDependencies = runtimeKlibs + dependencyDescriptors + friendLibraries + friendsDescriptors + transitiveLibraries
val builtInModuleDescriptor = allDependencies.firstNotNullOfOrNull { it.builtIns }?.builtInsModule val builtInModuleDescriptor = allDependencies.firstNotNullOfOrNull { it.builtIns }?.builtInsModule
return TopDownAnalyzerFacadeForWasm.analyzeFiles( val analyzerFacade = TopDownAnalyzerFacadeForWasm.facadeFor(configuration.get(JSConfigurationKeys.WASM_TARGET))
return analyzerFacade.analyzeFiles(
files, files,
project, project,
configuration, configuration,
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl
import org.jetbrains.kotlin.js.config.JSConfigurationKeys import org.jetbrains.kotlin.js.config.JSConfigurationKeys
import org.jetbrains.kotlin.js.config.SourceMapSourceEmbedding import org.jetbrains.kotlin.js.config.SourceMapSourceEmbedding
import org.jetbrains.kotlin.js.config.WasmTarget
import org.jetbrains.kotlin.library.KotlinLibrary import org.jetbrains.kotlin.library.KotlinLibrary
import org.jetbrains.kotlin.serialization.js.ModuleKind import org.jetbrains.kotlin.serialization.js.ModuleKind
import org.jetbrains.kotlin.test.directives.JsEnvironmentConfigurationDirectives.INFER_MAIN_MODULE import org.jetbrains.kotlin.test.directives.JsEnvironmentConfigurationDirectives.INFER_MAIN_MODULE
@@ -158,5 +159,7 @@ class WasmEnvironmentConfigurator(testServices: TestServices) : EnvironmentConfi
configuration.put(CommonConfigurationKeys.EXPECT_ACTUAL_LINKER, EXPECT_ACTUAL_LINKER in registeredDirectives) configuration.put(CommonConfigurationKeys.EXPECT_ACTUAL_LINKER, EXPECT_ACTUAL_LINKER in registeredDirectives)
configuration.put(JSConfigurationKeys.WASM_USE_TRAPS_INSTEAD_OF_EXCEPTIONS, DISABLE_WASM_EXCEPTION_HANDLING in registeredDirectives) configuration.put(JSConfigurationKeys.WASM_USE_TRAPS_INSTEAD_OF_EXCEPTIONS, DISABLE_WASM_EXCEPTION_HANDLING in registeredDirectives)
configuration.put(JSConfigurationKeys.WASM_TARGET, WasmTarget.JS)
} }
} }
@@ -11,12 +11,15 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.resolve.* import org.jetbrains.kotlin.resolve.*
import org.jetbrains.kotlin.storage.StorageManager import org.jetbrains.kotlin.storage.StorageManager
// TODO: Here for IDE ABI, remove after rename in IDE
typealias WasmJsPlatformAnalyzerServices = WasmPlatformAnalyzerServices
object WasmPlatformAnalyzerServices : PlatformDependentAnalyzerServices() { object WasmPlatformAnalyzerServices : PlatformDependentAnalyzerServices() {
override fun computePlatformSpecificDefaultImports(storageManager: StorageManager, result: MutableList<ImportPath>) { override fun computePlatformSpecificDefaultImports(storageManager: StorageManager, result: MutableList<ImportPath>) {
result.add(ImportPath.fromString("kotlin.js.*")) result.add(ImportPath.fromString("kotlin.js.*"))
} }
override val platformConfigurator: PlatformConfigurator = WasmPlatformConfigurator override val platformConfigurator: PlatformConfigurator = WasmJsPlatformConfigurator
val builtIns: KotlinBuiltIns val builtIns: KotlinBuiltIns
get() = DefaultBuiltIns.Instance get() = DefaultBuiltIns.Instance
@@ -24,3 +27,14 @@ object WasmPlatformAnalyzerServices : PlatformDependentAnalyzerServices() {
override val excludedImports: List<FqName> = override val excludedImports: List<FqName> =
listOf("Promise", "Date", "Console", "Math", "RegExp", "RegExpMatch", "Json", "json").map { FqName("kotlin.js.$it") } listOf("Promise", "Date", "Console", "Math", "RegExp", "RegExpMatch", "Json", "json").map { FqName("kotlin.js.$it") }
} }
object WasmWasiPlatformAnalyzerServices : PlatformDependentAnalyzerServices() {
override fun computePlatformSpecificDefaultImports(storageManager: StorageManager, result: MutableList<ImportPath>) {
result.add(ImportPath.fromString("kotlin.wasm.*"))
}
override val platformConfigurator: PlatformConfigurator = WasmWasiPlatformConfigurator
val builtIns: KotlinBuiltIns
get() = DefaultBuiltIns.Instance
}
@@ -8,8 +8,6 @@ package org.jetbrains.kotlin.wasm.resolve
import org.jetbrains.kotlin.container.StorageComponentContainer import org.jetbrains.kotlin.container.StorageComponentContainer
import org.jetbrains.kotlin.container.useImpl import org.jetbrains.kotlin.container.useImpl
import org.jetbrains.kotlin.container.useInstance import org.jetbrains.kotlin.container.useInstance
import org.jetbrains.kotlin.js.analyze.JsNativeDiagnosticSuppressor
import org.jetbrains.kotlin.js.naming.NameSuggestion
import org.jetbrains.kotlin.js.naming.WasmNameSuggestion import org.jetbrains.kotlin.js.naming.WasmNameSuggestion
import org.jetbrains.kotlin.js.resolve.ExtensionFunctionToExternalIsInlinable import org.jetbrains.kotlin.js.resolve.ExtensionFunctionToExternalIsInlinable
import org.jetbrains.kotlin.js.resolve.diagnostics.* import org.jetbrains.kotlin.js.resolve.diagnostics.*
@@ -21,7 +19,8 @@ import org.jetbrains.kotlin.wasm.resolve.diagnostics.*
// TODO: Review the list of used K/JS checkers. // TODO: Review the list of used K/JS checkers.
// Refactor useful checkers into common module. // Refactor useful checkers into common module.
object WasmPlatformConfigurator : PlatformConfiguratorBase( // KT-56848
object WasmJsPlatformConfigurator : PlatformConfiguratorBase(
additionalDeclarationCheckers = listOf( additionalDeclarationCheckers = listOf(
JsNameChecker, JsModuleChecker, JsExternalFileChecker, JsNameChecker, JsModuleChecker, JsExternalFileChecker,
JsExternalChecker, WasmExternalInheritanceChecker, JsExternalChecker, WasmExternalInheritanceChecker,
@@ -30,9 +29,9 @@ object WasmPlatformConfigurator : PlatformConfiguratorBase(
JsExportDeclarationChecker, JsExportDeclarationChecker,
WasmExternalDeclarationChecker, WasmExternalDeclarationChecker,
WasmImportAnnotationChecker, WasmImportAnnotationChecker,
WasmExportAnnotationChecker,
WasmJsFunAnnotationChecker, WasmJsFunAnnotationChecker,
WasmJsInteropTypesChecker, WasmJsInteropTypesChecker,
WasmJsExportChecker,
), ),
additionalCallCheckers = listOf( additionalCallCheckers = listOf(
JsModuleCallChecker, JsModuleCallChecker,
@@ -59,3 +58,36 @@ object WasmPlatformConfigurator : PlatformConfiguratorBase(
container.useImpl<ExpectedActualDeclarationChecker>() container.useImpl<ExpectedActualDeclarationChecker>()
} }
} }
// TODO: Review the list of used K/JS checkers.
// Refactor useful checkers into common module.
// KT-56848
object WasmWasiPlatformConfigurator : PlatformConfiguratorBase(
additionalDeclarationCheckers = listOf(
JsRuntimeAnnotationChecker,
WasmImportAnnotationChecker,
WasmWasiExportChecker,
WasmWasiExternalDeclarationChecker,
),
additionalCallCheckers = listOf(
LateinitIntrinsicApplicabilityChecker(isWarningInPre19 = true)
),
) {
override fun configureModuleComponents(container: StorageComponentContainer) {
container.useInstance(WasmNameSuggestion())
container.useImpl<WasmNameClashChecker>()
container.useImpl<WasmNameCharsChecker>()
container.useImpl<JsReflectionAPICallChecker>()
container.useImpl<JsNativeRttiChecker>()
container.useImpl<JsReifiedNativeChecker>()
container.useInstance(ExtensionFunctionToExternalIsInlinable)
container.useInstance(JsQualifierChecker)
container.useInstance(WasmDiagnosticSuppressor)
}
override fun configureModuleDependentCheckers(container: StorageComponentContainer) {
super.configureModuleDependentCheckers(container)
container.useImpl<ExpectedActualDeclarationChecker>()
}
}
@@ -28,6 +28,8 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy {
put(ErrorsWasm.NESTED_WASM_EXPORT, "Only top-level functions can be exported with @WasmExport") put(ErrorsWasm.NESTED_WASM_EXPORT, "Only top-level functions can be exported with @WasmExport")
put(ErrorsWasm.WASM_EXPORT_ON_EXTERNAL_DECLARATION, "Functions annotated with @WasmExport must not be external") put(ErrorsWasm.WASM_EXPORT_ON_EXTERNAL_DECLARATION, "Functions annotated with @WasmExport must not be external")
put(ErrorsWasm.JS_AND_WASM_EXPORTS_ON_SAME_DECLARATION, "Cannot use @WasmExport and @JsExport for same function") put(ErrorsWasm.JS_AND_WASM_EXPORTS_ON_SAME_DECLARATION, "Cannot use @WasmExport and @JsExport for same function")
put(ErrorsWasm.WASI_EXTERNAL_NOT_TOP_LEVEL_FUNCTION, "Only top-level functions can be external")
put(ErrorsWasm.WASI_EXTERNAL_FUNCTION_WITHOUT_IMPORT, "External functions should be annotated with @WasmImport")
put(ErrorsWasm.NESTED_WASM_IMPORT, "Only top-level functions can be imported with @WasmImport") put(ErrorsWasm.NESTED_WASM_IMPORT, "Only top-level functions can be imported with @WasmImport")
put(ErrorsWasm.WASM_IMPORT_ON_NON_EXTERNAL_DECLARATION, "Functions annotated with @WasmImport must be external") put(ErrorsWasm.WASM_IMPORT_ON_NON_EXTERNAL_DECLARATION, "Functions annotated with @WasmImport must be external")
@@ -23,6 +23,9 @@ public interface ErrorsWasm {
DiagnosticFactory0<PsiElement> WASM_EXPORT_ON_EXTERNAL_DECLARATION = DiagnosticFactory0.create(ERROR); DiagnosticFactory0<PsiElement> WASM_EXPORT_ON_EXTERNAL_DECLARATION = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> JS_AND_WASM_EXPORTS_ON_SAME_DECLARATION = DiagnosticFactory0.create(ERROR); DiagnosticFactory0<PsiElement> JS_AND_WASM_EXPORTS_ON_SAME_DECLARATION = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> WASI_EXTERNAL_NOT_TOP_LEVEL_FUNCTION = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> WASI_EXTERNAL_FUNCTION_WITHOUT_IMPORT = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> NESTED_WASM_IMPORT = DiagnosticFactory0.create(ERROR); DiagnosticFactory0<PsiElement> NESTED_WASM_IMPORT = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> WASM_IMPORT_ON_NON_EXTERNAL_DECLARATION = DiagnosticFactory0.create(ERROR); DiagnosticFactory0<PsiElement> WASM_IMPORT_ON_NON_EXTERNAL_DECLARATION = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> WASM_IMPORT_EXPORT_PARAMETER_DEFAULT_VALUE = DiagnosticFactory0.create(ERROR); DiagnosticFactory0<PsiElement> WASM_IMPORT_EXPORT_PARAMETER_DEFAULT_VALUE = DiagnosticFactory0.create(ERROR);
@@ -17,7 +17,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyExternal
import org.jetbrains.kotlin.wasm.util.hasValidJsCodeBody import org.jetbrains.kotlin.wasm.util.hasValidJsCodeBody
// TODO: Implement in K2: KT-56849 // TODO: Implement in K2: KT-56849
object WasmExportAnnotationChecker : DeclarationChecker { abstract class WasmExportAnnotationChecker(val checkJsInterop: Boolean) : DeclarationChecker {
private val wasmExportFqName = FqName("kotlin.wasm.WasmExport") private val wasmExportFqName = FqName("kotlin.wasm.WasmExport")
private val jsExportFqName = FqName("kotlin.js.JsExport") private val jsExportFqName = FqName("kotlin.js.JsExport")
@@ -28,12 +28,14 @@ object WasmExportAnnotationChecker : DeclarationChecker {
val trace = context.trace val trace = context.trace
val bindingContext = trace.bindingContext val bindingContext = trace.bindingContext
if (descriptor.annotations.hasAnnotation(jsExportFqName)) { if (checkJsInterop) {
val reportOn = descriptor.findPsi() ?: declaration if (descriptor.annotations.hasAnnotation(jsExportFqName)) {
trace.report(ErrorsWasm.JS_AND_WASM_EXPORTS_ON_SAME_DECLARATION.on(reportOn)) val reportOn = descriptor.findPsi() ?: declaration
trace.report(ErrorsWasm.JS_AND_WASM_EXPORTS_ON_SAME_DECLARATION.on(reportOn))
}
} }
if (descriptor.isEffectivelyExternal() || descriptor.hasValidJsCodeBody(bindingContext)) { if (descriptor.isEffectivelyExternal() || (checkJsInterop && descriptor.hasValidJsCodeBody(bindingContext))) {
val reportOn = descriptor.findPsi() ?: declaration val reportOn = descriptor.findPsi() ?: declaration
trace.report(ErrorsWasm.WASM_EXPORT_ON_EXTERNAL_DECLARATION.on(reportOn)) trace.report(ErrorsWasm.WASM_EXPORT_ON_EXTERNAL_DECLARATION.on(reportOn))
} }
@@ -45,4 +47,8 @@ object WasmExportAnnotationChecker : DeclarationChecker {
WasmImportAnnotationChecker.checkSignatureIsPrimitive(descriptor, trace, declaration) WasmImportAnnotationChecker.checkSignatureIsPrimitive(descriptor, trace, declaration)
} }
} }
object WasmJsExportChecker : WasmExportAnnotationChecker(checkJsInterop = true)
object WasmWasiExportChecker : WasmExportAnnotationChecker(checkJsInterop = false)
@@ -0,0 +1,37 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.wasm.resolve.diagnostics
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.MemberDescriptor
import org.jetbrains.kotlin.js.resolve.diagnostics.findPsi
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.checkers.DeclarationChecker
import org.jetbrains.kotlin.resolve.checkers.DeclarationCheckerContext
import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyExternal
// TODO: Implement in K2: KT-56849
object WasmWasiExternalDeclarationChecker : DeclarationChecker {
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
if (descriptor !is MemberDescriptor) return
if (!descriptor.isEffectivelyExternal()) return
if (descriptor is FunctionDescriptor) {
if (!DescriptorUtils.isTopLevelDeclaration(descriptor)) {
val reportOn = descriptor.findPsi() ?: declaration
context.trace.report(ErrorsWasm.WASI_EXTERNAL_NOT_TOP_LEVEL_FUNCTION.on(reportOn))
} else {
if (!descriptor.annotations.hasAnnotation(FqName("kotlin.wasm.WasmImport"))) {
val reportOn = descriptor.findPsi() ?: declaration
context.trace.report(ErrorsWasm.WASI_EXTERNAL_FUNCTION_WITHOUT_IMPORT.on(reportOn))
}
}
}
}
}