[Wasm] Add external declaration diagnostics (KT-56940)

Prohibit:
- external enum class
- external tailrec fun
- external suspend fun
- external lateinit var

Add tests for other external diagnostics inherited from K/JS
This commit is contained in:
Svyatoslav Kuzmich
2023-02-16 16:58:08 +01:00
committed by Space Team
parent 83ba1ac0f7
commit b4fcfd6719
4 changed files with 121 additions and 0 deletions
@@ -0,0 +1,51 @@
// Classes
external class C1
external enum class <!WRONG_EXTERNAL_DECLARATION!>C2<!>
external annotation class <!WRONG_EXTERNAL_DECLARATION!>C3<!>
external data class <!WRONG_EXTERNAL_DECLARATION!>C4(val x: String)<!>
external class C5 {
class C6
inner class <!WRONG_EXTERNAL_DECLARATION!>C7<!>
}
external <!INLINE_CLASS_DEPRECATED!>inline<!> class <!WRONG_EXTERNAL_DECLARATION!>C8(<!EXTERNAL_CLASS_CONSTRUCTOR_PROPERTY_PARAMETER!>val x: Int<!>)<!>
external value class <!WRONG_EXTERNAL_DECLARATION!>C9(<!EXTERNAL_CLASS_CONSTRUCTOR_PROPERTY_PARAMETER!>val x: Int<!>)<!>
// Interfaces
external interface I1
external fun interface <!WRONG_EXTERNAL_DECLARATION!>I2<!> {
fun foo(): Int
}
// Functions
external fun foo1(): Int
<!WRONG_EXTERNAL_DECLARATION!>external tailrec fun foo2(): Int<!>
<!INLINE_EXTERNAL_DECLARATION!>external inline fun foo3(f: () -> Int): Int<!>
<!WRONG_EXTERNAL_DECLARATION!>external suspend fun foo4(): Int<!>
<!WRONG_EXTERNAL_DECLARATION!>external fun Int.foo5(): Int<!>
// Properties
<!WRONG_EXTERNAL_DECLARATION!>external lateinit var v1: String<!>
<!WRONG_EXTERNAL_DECLARATION!>external val Int.v2: String<!>
get() = definedExternally
@@ -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,
@@ -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")
}
}
}
}
}
@@ -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 {