[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
@@ -11,12 +11,15 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.resolve.*
import org.jetbrains.kotlin.storage.StorageManager
// TODO: Here for IDE ABI, remove after rename in IDE
typealias WasmJsPlatformAnalyzerServices = WasmPlatformAnalyzerServices
object WasmPlatformAnalyzerServices : PlatformDependentAnalyzerServices() {
override fun computePlatformSpecificDefaultImports(storageManager: StorageManager, result: MutableList<ImportPath>) {
result.add(ImportPath.fromString("kotlin.js.*"))
}
override val platformConfigurator: PlatformConfigurator = WasmPlatformConfigurator
override val platformConfigurator: PlatformConfigurator = WasmJsPlatformConfigurator
val builtIns: KotlinBuiltIns
get() = DefaultBuiltIns.Instance
@@ -24,3 +27,14 @@ object WasmPlatformAnalyzerServices : PlatformDependentAnalyzerServices() {
override val excludedImports: List<FqName> =
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.useImpl
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.resolve.ExtensionFunctionToExternalIsInlinable
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.
// Refactor useful checkers into common module.
object WasmPlatformConfigurator : PlatformConfiguratorBase(
// KT-56848
object WasmJsPlatformConfigurator : PlatformConfiguratorBase(
additionalDeclarationCheckers = listOf(
JsNameChecker, JsModuleChecker, JsExternalFileChecker,
JsExternalChecker, WasmExternalInheritanceChecker,
@@ -30,9 +29,9 @@ object WasmPlatformConfigurator : PlatformConfiguratorBase(
JsExportDeclarationChecker,
WasmExternalDeclarationChecker,
WasmImportAnnotationChecker,
WasmExportAnnotationChecker,
WasmJsFunAnnotationChecker,
WasmJsInteropTypesChecker,
WasmJsExportChecker,
),
additionalCallCheckers = listOf(
JsModuleCallChecker,
@@ -59,3 +58,36 @@ object WasmPlatformConfigurator : PlatformConfiguratorBase(
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.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.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.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> 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> WASM_IMPORT_ON_NON_EXTERNAL_DECLARATION = 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
// 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 jsExportFqName = FqName("kotlin.js.JsExport")
@@ -28,12 +28,14 @@ object WasmExportAnnotationChecker : DeclarationChecker {
val trace = context.trace
val bindingContext = trace.bindingContext
if (descriptor.annotations.hasAnnotation(jsExportFqName)) {
val reportOn = descriptor.findPsi() ?: declaration
trace.report(ErrorsWasm.JS_AND_WASM_EXPORTS_ON_SAME_DECLARATION.on(reportOn))
if (checkJsInterop) {
if (descriptor.annotations.hasAnnotation(jsExportFqName)) {
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
trace.report(ErrorsWasm.WASM_EXPORT_ON_EXTERNAL_DECLARATION.on(reportOn))
}
@@ -45,4 +47,8 @@ object WasmExportAnnotationChecker : DeclarationChecker {
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))
}
}
}
}
}