[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
This commit is contained in:
Svyatoslav Kuzmich
2023-02-17 16:58:53 +01:00
committed by Space Team
parent f79607d32d
commit 1da96213ca
6 changed files with 157 additions and 0 deletions
@@ -0,0 +1,49 @@
import kotlin.wasm.WasmImport
@WasmImport("a", "b")
external fun foo0(): Unit
<!WASM_IMPORT_ON_NON_EXTERNAL_DECLARATION!>@WasmImport("a", "b")<!>
fun foo1() {
}
external class C {
<!NESTED_WASM_IMPORT!>@WasmImport("a", "b")<!>
fun memberFunction()
}
fun foo2() {
<!NESTED_WASM_IMPORT, WASM_IMPORT_ON_NON_EXTERNAL_DECLARATION!>@WasmImport("a", "b")<!>
fun localFun() {
}
}
val p1 = (<!NESTED_WASM_IMPORT, WASM_IMPORT_ON_NON_EXTERNAL_DECLARATION!>@WasmImport("a", "b")<!> fun () {})
@WasmImport("a", "b")
external fun foo3(
<!WASM_IMPORT_UNSUPPORTED_PARAMETER_TYPE!>p0: Unit<!>,
<!WASM_IMPORT_UNSUPPORTED_PARAMETER_TYPE!>p1: String<!>,
<!WASM_IMPORT_UNSUPPORTED_PARAMETER_TYPE!>p2: Any<!>,
<!WASM_IMPORT_UNSUPPORTED_PARAMETER_TYPE!>p3: Int?<!>,
<!WASM_IMPORT_UNSUPPORTED_PARAMETER_TYPE!>p4: Boolean?<!>
): Unit
<!WASM_IMPORT_UNSUPPORTED_RETURN_TYPE!>@WasmImport("a", "b")
external fun returnNullableUnit(): Unit?<!>
<!WASM_IMPORT_UNSUPPORTED_RETURN_TYPE!>@WasmImport("a", "b")
external fun returnNullableBoolean(): Boolean?<!>
<!WASM_IMPORT_UNSUPPORTED_RETURN_TYPE!>@WasmImport("a", "b")
external fun returnNullableAny(): Any?<!>
<!WASM_IMPORT_UNSUPPORTED_RETURN_TYPE!>@WasmImport("a", "b")
external fun <T> fooGeneric(<!WASM_IMPORT_UNSUPPORTED_PARAMETER_TYPE!>x: T<!>): T<!>
@WasmImport("a", "b")
external fun fooDeafultAndVararg(
<!WASM_IMPORT_PARAMETER_DEFAULT_VALUE!>a: Int = definedExternally<!>,
<!WASM_IMPORT_UNSUPPORTED_PARAMETER_TYPE, WASM_IMPORT_VARARG_PARAMETER!>vararg b: Int<!>
): Unit
@@ -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,
@@ -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)
}
}
@@ -19,6 +19,13 @@ public interface ErrorsWasm {
DiagnosticFactory1<KtElement, KotlinType> NON_EXTERNAL_TYPE_EXTENDS_EXTERNAL_TYPE =
DiagnosticFactory1.create(ERROR, PositioningStrategies.DECLARATION_SIGNATURE_OR_DEFAULT);
DiagnosticFactory0<PsiElement> NESTED_WASM_IMPORT = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> WASM_IMPORT_ON_NON_EXTERNAL_DECLARATION = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> WASM_IMPORT_PARAMETER_DEFAULT_VALUE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> WASM_IMPORT_VARARG_PARAMETER = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, KotlinType> WASM_IMPORT_UNSUPPORTED_PARAMETER_TYPE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, KotlinType> WASM_IMPORT_UNSUPPORTED_RETURN_TYPE = DiagnosticFactory1.create(ERROR);
@SuppressWarnings("UnusedDeclaration")
Object _initializer = new Object() {
{
@@ -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()
}
@@ -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");
}
}
}