[Wasm] Add @JsFun diagnostics (KT-56944)

JsFun target should be top-level external function
This commit is contained in:
Svyatoslav Kuzmich
2023-02-20 14:30:52 +01:00
committed by Space Team
parent 1da96213ca
commit 4be2f53b47
6 changed files with 67 additions and 0 deletions
@@ -0,0 +1,15 @@
@JsFun("() => {}")
external fun topLevelExternalFun(): Unit
external class ExternalClass {
<!WRONG_JS_FUN_TARGET!>@JsFun("() => {}")<!>
fun memberFun(): Unit
}
<!WRONG_JS_FUN_TARGET!>@JsFun("() => {}")<!>
fun topLevelNonExternalFun(): Unit {}
class NonExternalClass {
<!WRONG_JS_FUN_TARGET!>@JsFun("() => {}")<!>
fun memberFun(): Unit {}
}
@@ -18,6 +18,7 @@ import org.jetbrains.kotlin.resolve.checkers.ExpectedActualDeclarationChecker
import org.jetbrains.kotlin.wasm.resolve.diagnostics.WasmExternalDeclarationChecker
import org.jetbrains.kotlin.wasm.resolve.diagnostics.WasmExternalInheritanceChecker
import org.jetbrains.kotlin.wasm.resolve.diagnostics.WasmImportAnnotationChecker
import org.jetbrains.kotlin.wasm.resolve.diagnostics.WasmJsFunAnnotationChecker
// TODO: Review the list of used K/JS checkers.
// Refactor useful checkers into common module.
@@ -30,6 +31,7 @@ object WasmPlatformConfigurator : PlatformConfiguratorBase(
JsExportDeclarationChecker,
WasmExternalDeclarationChecker,
WasmImportAnnotationChecker,
WasmJsFunAnnotationChecker,
),
additionalCallCheckers = listOf(
JsModuleCallChecker,
@@ -23,6 +23,8 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy {
put(ErrorsWasm.WASM_IMPORT_VARARG_PARAMETER, "Vararg parameters are not supported with @WasmImport")
put(ErrorsWasm.WASM_IMPORT_UNSUPPORTED_PARAMETER_TYPE, "Unsupported @WasmImport parameter type {0}", Renderers.RENDER_TYPE)
put(ErrorsWasm.WASM_IMPORT_UNSUPPORTED_RETURN_TYPE, "Unsupported @WasmImport return type {0}", Renderers.RENDER_TYPE)
put(ErrorsWasm.WRONG_JS_FUN_TARGET, "Only top-level external functions can be implemented using @JsFun")
}
}
@@ -26,6 +26,8 @@ public interface ErrorsWasm {
DiagnosticFactory1<PsiElement, KotlinType> WASM_IMPORT_UNSUPPORTED_PARAMETER_TYPE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, KotlinType> WASM_IMPORT_UNSUPPORTED_RETURN_TYPE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<PsiElement> WRONG_JS_FUN_TARGET = DiagnosticFactory0.create(ERROR);
@SuppressWarnings("UnusedDeclaration")
Object _initializer = new Object() {
{
@@ -0,0 +1,40 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.wasm.resolve.diagnostics
import org.jetbrains.kotlin.descriptors.*
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
import org.jetbrains.kotlin.resolve.source.getPsi
// TODO: Implement in K2: KT-56849
object WasmJsFunAnnotationChecker : DeclarationChecker {
private val jsFunFqName = FqName("kotlin.JsFun")
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
if (descriptor !is MemberDescriptor) return
val jsFun = descriptor.annotations.findAnnotation(jsFunFqName) ?: return
if (!descriptor.isEffectivelyExternal() || !DescriptorUtils.isTopLevelDeclaration(descriptor)) {
val jsFunPsi = jsFun.source.getPsi() ?: declaration
context.trace.report(ErrorsWasm.WRONG_JS_FUN_TARGET.on(jsFunPsi))
}
}
}
@@ -56,6 +56,12 @@ public class DiagnosticsWasmTestGenerated extends AbstractDiagnosticsWasmTest {
public void testJsExport() throws Exception {
runTest("compiler/testData/diagnostics/wasmTests/jsInterop/jsExport.kt");
}
@Test
@TestMetadata("jsFun.kt")
public void testJsFun() throws Exception {
runTest("compiler/testData/diagnostics/wasmTests/jsInterop/jsFun.kt");
}
}
@Nested