diff --git a/compiler/testData/diagnostics/wasmTests/jsInterop/external.kt b/compiler/testData/diagnostics/wasmTests/jsInterop/external.kt new file mode 100644 index 00000000000..9ea151ca288 --- /dev/null +++ b/compiler/testData/diagnostics/wasmTests/jsInterop/external.kt @@ -0,0 +1,51 @@ + +// Classes + +external class C1 + +external enum class C2 + +external annotation class C3 + +external data class C4(val x: String) + +external class C5 { + + class C6 + + inner class C7 +} + +external inline class C8(val x: Int) + +external value class C9(val x: Int) + + +// Interfaces + +external interface I1 + +external fun interface I2 { + fun foo(): Int +} + + +// Functions + +external fun foo1(): Int + +external tailrec fun foo2(): Int + +external inline fun foo3(f: () -> Int): Int + +external suspend fun foo4(): Int + +external fun Int.foo5(): Int + + +// Properties + +external lateinit var v1: String + +external val Int.v2: String + get() = definedExternally \ 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 d210379822f..e8890c58568 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 @@ -15,6 +15,7 @@ import org.jetbrains.kotlin.js.resolve.diagnostics.* import org.jetbrains.kotlin.resolve.PlatformConfiguratorBase import org.jetbrains.kotlin.resolve.calls.checkers.LateinitIntrinsicApplicabilityChecker import org.jetbrains.kotlin.resolve.checkers.ExpectedActualDeclarationChecker +import org.jetbrains.kotlin.wasm.resolve.diagnostics.WasmExternalDeclarationChecker import org.jetbrains.kotlin.wasm.resolve.diagnostics.WasmExternalInheritanceChecker // TODO: Review the list of used K/JS checkers. @@ -26,6 +27,7 @@ object WasmPlatformConfigurator : PlatformConfiguratorBase( JsRuntimeAnnotationChecker, JsExportAnnotationChecker, JsExportDeclarationChecker, + WasmExternalDeclarationChecker, ), additionalCallCheckers = listOf( JsModuleCallChecker, diff --git a/wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/diagnostics/WasmExternalDeclarationChecker.kt b/wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/diagnostics/WasmExternalDeclarationChecker.kt new file mode 100644 index 00000000000..e815b57f54d --- /dev/null +++ b/wasm/wasm.frontend/src/org/jetbrains/kotlin/wasm/resolve/diagnostics/WasmExternalDeclarationChecker.kt @@ -0,0 +1,62 @@ +/* + * 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.ClassDescriptor +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.descriptors.FunctionDescriptor +import org.jetbrains.kotlin.descriptors.MemberDescriptor +import org.jetbrains.kotlin.descriptors.PropertyDescriptor +import org.jetbrains.kotlin.descriptors.isEnumClass +import org.jetbrains.kotlin.js.resolve.diagnostics.ErrorsJs +import org.jetbrains.kotlin.psi.KtDeclaration +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 WasmExternalDeclarationChecker : DeclarationChecker { + override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) { + if (descriptor !is MemberDescriptor || !descriptor.isEffectivelyExternal()) + return + + fun reportWrongExternalDeclaration(kind: String) { + context.trace.report(ErrorsJs.WRONG_EXTERNAL_DECLARATION.on(declaration, kind)) + } + + when (descriptor) { + is ClassDescriptor -> { + if (descriptor.kind.isEnumClass) { + reportWrongExternalDeclaration("enum class") + } + } + is PropertyDescriptor -> { + if (descriptor.isLateInit) { + reportWrongExternalDeclaration("lateinit property") + } + } + is FunctionDescriptor -> { + if (descriptor.isTailrec) { + reportWrongExternalDeclaration("tailrec function") + } + if (descriptor.isSuspend) { + reportWrongExternalDeclaration("suspend function") + } + } + } + } +} 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 60f43c7e339..217244e3c8d 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 @@ -39,6 +39,12 @@ public class DiagnosticsWasmTestGenerated extends AbstractDiagnosticsWasmTest { runTest("compiler/testData/diagnostics/wasmTests/jsInterop/dynamicUnsupported.kt"); } + @Test + @TestMetadata("external.kt") + public void testExternal() throws Exception { + runTest("compiler/testData/diagnostics/wasmTests/jsInterop/external.kt"); + } + @Test @TestMetadata("inheritance.kt") public void testInheritance() throws Exception {