From 1da96213cae45b015058d316853f3f8fc88855e0 Mon Sep 17 00:00:00 2001 From: Svyatoslav Kuzmich Date: Fri, 17 Feb 2023 16:58:53 +0100 Subject: [PATCH] [Wasm] Add @WasmImport diagnostics (KT-56943) WasmImport annotated functions: * Restricted to top-level external functions * Only supports primitive numbers, booleans and Unit (as return type only) in its signature * Can't have default parameter values * Can't have vararg parameters --- .../wasmTests/wasmInterop/wasmImport.kt | 49 ++++++++++++ .../wasm/resolve/WasmPlatformConfigurator.kt | 2 + .../diagnostics/DefaultErrorMessagesWasm.kt | 7 ++ .../wasm/resolve/diagnostics/ErrorsWasm.java | 7 ++ .../WasmImportAnnotationChecker.kt | 76 +++++++++++++++++++ .../DiagnosticsWasmTestGenerated.java | 16 ++++ 6 files changed, 157 insertions(+) create mode 100644 compiler/testData/diagnostics/wasmTests/wasmInterop/wasmImport.kt create mode 100644 wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/diagnostics/WasmImportAnnotationChecker.kt diff --git a/compiler/testData/diagnostics/wasmTests/wasmInterop/wasmImport.kt b/compiler/testData/diagnostics/wasmTests/wasmInterop/wasmImport.kt new file mode 100644 index 00000000000..e436de2b3e2 --- /dev/null +++ b/compiler/testData/diagnostics/wasmTests/wasmInterop/wasmImport.kt @@ -0,0 +1,49 @@ +import kotlin.wasm.WasmImport + +@WasmImport("a", "b") +external fun foo0(): Unit + +@WasmImport("a", "b") +fun foo1() { +} + +external class C { + @WasmImport("a", "b") + fun memberFunction() +} + +fun foo2() { + @WasmImport("a", "b") + fun localFun() { + } +} + +val p1 = (@WasmImport("a", "b") fun () {}) + +@WasmImport("a", "b") +external fun foo3( + p0: Unit, + p1: String, + p2: Any, + p3: Int?, + p4: Boolean? +): Unit + + +@WasmImport("a", "b") +external fun returnNullableUnit(): Unit? + +@WasmImport("a", "b") +external fun returnNullableBoolean(): Boolean? + +@WasmImport("a", "b") +external fun returnNullableAny(): Any? + +@WasmImport("a", "b") +external fun fooGeneric(x: T): T + +@WasmImport("a", "b") +external fun fooDeafultAndVararg( + a: Int = definedExternally, +vararg b: Int +): Unit \ No newline at end of file diff --git a/wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/WasmPlatformConfigurator.kt b/wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/WasmPlatformConfigurator.kt index e8890c58568..1ba7ab0bdcd 100644 --- a/wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/WasmPlatformConfigurator.kt +++ b/wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/WasmPlatformConfigurator.kt @@ -17,6 +17,7 @@ import org.jetbrains.kotlin.resolve.calls.checkers.LateinitIntrinsicApplicabilit 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 // TODO: Review the list of used K/JS checkers. // Refactor useful checkers into common module. @@ -28,6 +29,7 @@ object WasmPlatformConfigurator : PlatformConfiguratorBase( JsExportAnnotationChecker, JsExportDeclarationChecker, WasmExternalDeclarationChecker, + WasmImportAnnotationChecker, ), additionalCallCheckers = listOf( JsModuleCallChecker, diff --git a/wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/diagnostics/DefaultErrorMessagesWasm.kt b/wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/diagnostics/DefaultErrorMessagesWasm.kt index 3aa48526473..4927d1ff278 100644 --- a/wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/diagnostics/DefaultErrorMessagesWasm.kt +++ b/wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/diagnostics/DefaultErrorMessagesWasm.kt @@ -16,6 +16,13 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy { "Non-external type extends external type {0}", Renderers.RENDER_TYPE ) + + 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_PARAMETER_DEFAULT_VALUE, "Default parameter values are not supported with @WasmImport") + 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) } } diff --git a/wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/diagnostics/ErrorsWasm.java b/wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/diagnostics/ErrorsWasm.java index a417eaa76ba..c767fcf4f79 100644 --- a/wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/diagnostics/ErrorsWasm.java +++ b/wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/diagnostics/ErrorsWasm.java @@ -19,6 +19,13 @@ public interface ErrorsWasm { DiagnosticFactory1 NON_EXTERNAL_TYPE_EXTENDS_EXTERNAL_TYPE = DiagnosticFactory1.create(ERROR, PositioningStrategies.DECLARATION_SIGNATURE_OR_DEFAULT); + DiagnosticFactory0 NESTED_WASM_IMPORT = DiagnosticFactory0.create(ERROR); + DiagnosticFactory0 WASM_IMPORT_ON_NON_EXTERNAL_DECLARATION = DiagnosticFactory0.create(ERROR); + DiagnosticFactory0 WASM_IMPORT_PARAMETER_DEFAULT_VALUE = DiagnosticFactory0.create(ERROR); + DiagnosticFactory0 WASM_IMPORT_VARARG_PARAMETER = DiagnosticFactory0.create(ERROR); + DiagnosticFactory1 WASM_IMPORT_UNSUPPORTED_PARAMETER_TYPE = DiagnosticFactory1.create(ERROR); + DiagnosticFactory1 WASM_IMPORT_UNSUPPORTED_RETURN_TYPE = DiagnosticFactory1.create(ERROR); + @SuppressWarnings("UnusedDeclaration") Object _initializer = new Object() { { diff --git a/wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/diagnostics/WasmImportAnnotationChecker.kt b/wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/diagnostics/WasmImportAnnotationChecker.kt new file mode 100644 index 00000000000..af5bd3b92f2 --- /dev/null +++ b/wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/diagnostics/WasmImportAnnotationChecker.kt @@ -0,0 +1,76 @@ +/* + * 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.DescriptorToSourceUtils +import org.jetbrains.kotlin.resolve.DescriptorUtils +import org.jetbrains.kotlin.resolve.calls.components.isVararg +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 +import org.jetbrains.kotlin.types.KotlinType +import org.jetbrains.kotlin.types.typeUtil.isBoolean +import org.jetbrains.kotlin.types.typeUtil.isPrimitiveNumberType +import org.jetbrains.kotlin.types.typeUtil.isUnit + +// TODO: Implement in K2: KT-56849 +object WasmImportAnnotationChecker : DeclarationChecker { + private val wasmImportFqName = FqName("kotlin.wasm.WasmImport") + + override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) { + val wasmImport = descriptor.annotations.findAnnotation(wasmImportFqName) ?: return + val trace = context.trace + + val wasmImportPsi = wasmImport.source.getPsi() ?: declaration + + if (!DescriptorUtils.isTopLevelDeclaration(descriptor)) { + trace.report(ErrorsWasm.NESTED_WASM_IMPORT.on(wasmImportPsi)) + } + if (descriptor is FunctionDescriptor) { + if (!descriptor.isEffectivelyExternal()) { + trace.report(ErrorsWasm.WASM_IMPORT_ON_NON_EXTERNAL_DECLARATION.on(wasmImportPsi)) + } + for (parameter: ValueParameterDescriptor in descriptor.valueParameters) { + val valueParameterDeclaration by lazy { DescriptorToSourceUtils.descriptorToDeclaration(parameter)!! } + if (parameter.declaresDefaultValue()) { + trace.report(ErrorsWasm.WASM_IMPORT_PARAMETER_DEFAULT_VALUE.on(valueParameterDeclaration)) + } + if (parameter.isVararg) { + trace.report(ErrorsWasm.WASM_IMPORT_VARARG_PARAMETER.on(valueParameterDeclaration)) + } + if (!isParameterTypeSupported(parameter.type)) { + trace.report(ErrorsWasm.WASM_IMPORT_UNSUPPORTED_PARAMETER_TYPE.on(valueParameterDeclaration, parameter.type)) + } + } + val returnType = descriptor.returnType + if (returnType != null && !isReturnTypeSupported(returnType)) { + trace.report(ErrorsWasm.WASM_IMPORT_UNSUPPORTED_RETURN_TYPE.on(declaration, returnType)) + } + } + } + + private fun isParameterTypeSupported(type: KotlinType): Boolean = + type.isPrimitiveNumberType() || type.isBoolean() + + private fun isReturnTypeSupported(type: KotlinType): Boolean = + isParameterTypeSupported(type) || type.isUnit() +} + diff --git a/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/diagnostics/DiagnosticsWasmTestGenerated.java b/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/diagnostics/DiagnosticsWasmTestGenerated.java index e369db6eb8e..4f999ef92a3 100644 --- a/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/diagnostics/DiagnosticsWasmTestGenerated.java +++ b/wasm/wasm.tests/tests-gen/org/jetbrains/kotlin/wasm/test/diagnostics/DiagnosticsWasmTestGenerated.java @@ -57,4 +57,20 @@ public class DiagnosticsWasmTestGenerated extends AbstractDiagnosticsWasmTest { runTest("compiler/testData/diagnostics/wasmTests/jsInterop/jsExport.kt"); } } + + @Nested + @TestMetadata("compiler/testData/diagnostics/wasmTests/wasmInterop") + @TestDataPath("$PROJECT_ROOT") + public class WasmInterop { + @Test + public void testAllFilesPresentInWasmInterop() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/wasmTests/wasmInterop"), Pattern.compile("^(.+)\\.kt$"), null, true); + } + + @Test + @TestMetadata("wasmImport.kt") + public void testWasmImport() throws Exception { + runTest("compiler/testData/diagnostics/wasmTests/wasmInterop/wasmImport.kt"); + } + } }