[Wasm] Port @JsFun checker to K2 (KT-56849)

^KT-62724 Fixed
This commit is contained in:
Svyatoslav Kuzmich
2023-11-16 18:44:51 +01:00
committed by Space Team
parent fbc35975ab
commit 31560217f8
9 changed files with 46 additions and 16 deletions
@@ -29,6 +29,10 @@ object WASM_DIAGNOSTICS_LIST : DiagnosticList("FirWasmErrors") {
}
}
val JS_FUN by object : DiagnosticGroup("JsFun") {
val WRONG_JS_FUN_TARGET by error<PsiElement>()
}
val WASM_INTEROP by object : DiagnosticGroup("Wasm interop") {
val NESTED_WASM_EXPORT by error<KtElement>()
val WASM_EXPORT_ON_EXTERNAL_DECLARATION by error<KtElement>()
@@ -23,6 +23,9 @@ object FirWasmErrors {
val CALL_TO_DEFINED_EXTERNALLY_FROM_NON_EXTERNAL_DECLARATION by error0<PsiElement>()
val WRONG_JS_INTEROP_TYPE by error2<KtElement, String, ConeKotlinType>(SourceElementPositioningStrategies.DECLARATION_SIGNATURE_OR_DEFAULT)
// JsFun
val WRONG_JS_FUN_TARGET by error0<PsiElement>()
// Wasm interop
val NESTED_WASM_EXPORT by error0<KtElement>()
val WASM_EXPORT_ON_EXTERNAL_DECLARATION by error0<KtElement>()
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.wasm.FirWasmErrors.WASM_IMP
import org.jetbrains.kotlin.fir.analysis.diagnostics.wasm.FirWasmErrors.WASM_IMPORT_EXPORT_UNSUPPORTED_RETURN_TYPE
import org.jetbrains.kotlin.fir.analysis.diagnostics.wasm.FirWasmErrors.WASM_IMPORT_EXPORT_VARARG_PARAMETER
import org.jetbrains.kotlin.fir.analysis.diagnostics.wasm.FirWasmErrors.WASM_IMPORT_ON_NON_EXTERNAL_DECLARATION
import org.jetbrains.kotlin.fir.analysis.diagnostics.wasm.FirWasmErrors.WRONG_JS_FUN_TARGET
import org.jetbrains.kotlin.fir.analysis.diagnostics.wasm.FirWasmErrors.WRONG_JS_INTEROP_TYPE
@Suppress("unused")
@@ -45,6 +46,8 @@ object FirWasmErrorsDefaultMessages : BaseDiagnosticRendererFactory() {
TO_STRING, FirDiagnosticRenderers.RENDER_TYPE,
)
map.put(WRONG_JS_FUN_TARGET, "Only top-level external functions can be implemented using '@JsFun'.")
map.put(NESTED_WASM_EXPORT, "Only top-level functions can be exported with '@WasmExport'.")
map.put(WASM_EXPORT_ON_EXTERNAL_DECLARATION, "Functions annotated with '@WasmExport' must not be external.")
map.put(JS_AND_WASM_EXPORTS_ON_SAME_DECLARATION, "Cannot use '@WasmExport' and '@JsExport' for same function.")
@@ -20,5 +20,6 @@ object WasmDeclarationCheckers : DeclarationCheckers() {
FirWasmImportAnnotationChecker,
FirWasmExportAnnotationChecker,
FirWasmExternalChecker,
FirWasmJsFunAnnotationChecker,
)
}
@@ -0,0 +1,29 @@
/*
* 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.fir.analysis.wasm.checkers.declaration
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.diagnostics.reportOn
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
import org.jetbrains.kotlin.fir.analysis.checkers.declaration.FirBasicDeclarationChecker
import org.jetbrains.kotlin.fir.analysis.checkers.isTopLevel
import org.jetbrains.kotlin.fir.analysis.diagnostics.wasm.FirWasmErrors
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
import org.jetbrains.kotlin.fir.declarations.getAnnotationByClassId
import org.jetbrains.kotlin.fir.declarations.utils.isEffectivelyExternal
import org.jetbrains.kotlin.fir.expressions.FirAnnotation
import org.jetbrains.kotlin.name.WasmStandardClassIds
object FirWasmJsFunAnnotationChecker : FirBasicDeclarationChecker() {
override fun check(declaration: FirDeclaration, context: CheckerContext, reporter: DiagnosticReporter) {
val annotation: FirAnnotation =
declaration.annotations.getAnnotationByClassId(WasmStandardClassIds.Annotations.JsFun, context.session) ?: return
if (!context.isTopLevel || !declaration.symbol.isEffectivelyExternal(context.session)) {
reporter.reportOn(annotation.source, FirWasmErrors.WRONG_JS_FUN_TARGET, context)
}
}
}
@@ -1,15 +0,0 @@
@JsFun("() => {}")
external fun topLevelExternalFun(): Unit
external class ExternalClass {
@JsFun("() => {}")
fun memberFun(): Unit
}
@JsFun("() => {}")
fun topLevelNonExternalFun(): Unit {}
class NonExternalClass {
@JsFun("() => {}")
fun memberFun(): Unit {}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
@JsFun("() => {}")
external fun topLevelExternalFun(): Unit
@@ -29,6 +29,9 @@ object WasmStandardClassIds {
@JvmField
val WasmExport = "WasmExport".wasmId()
@JvmField
val JsFun = "JsFun".baseId()
}
object Callables {
@@ -40,6 +43,8 @@ object WasmStandardClassIds {
}
}
private fun String.baseId() = ClassId(StandardClassIds.BASE_KOTLIN_PACKAGE, Name.identifier(this))
private fun String.jsId() = ClassId(WasmStandardClassIds.BASE_JS_PACKAGE, Name.identifier(this))
private fun String.wasmId() = ClassId(WasmStandardClassIds.BASE_WASM_PACKAGE, Name.identifier(this))
@@ -25,7 +25,6 @@ import org.jetbrains.kotlin.resolve.checkers.DeclarationCheckerContext
import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyExternal
import org.jetbrains.kotlin.resolve.source.getPsi
// TODO: Implement in K2: KT-56849
object WasmJsFunAnnotationChecker : DeclarationChecker {
private val jsFunFqName = FqName("kotlin.JsFun")