[WasmJs] Prohibit external classes to be a storage of an associated objects

Fix #KT-65355
This commit is contained in:
Igor Yakovlev
2024-01-27 02:23:04 +01:00
parent 6930fc8fed
commit 324d079111
13 changed files with 116 additions and 2 deletions
@@ -31,6 +31,7 @@ object WasmJsPlatformConfigurator : PlatformConfiguratorBase(
WasmJsFunAnnotationChecker,
WasmJsInteropTypesChecker,
WasmJsExportChecker,
FirWasmJsAssociatedObjectChecker,
),
additionalCallCheckers = listOf(
JsModuleCallChecker,
@@ -31,6 +31,8 @@ private val DIAGNOSTIC_FACTORY_TO_RENDERER by lazy {
put(ErrorsWasm.WASI_EXTERNAL_NOT_TOP_LEVEL_FUNCTION, "Only top-level functions can be external")
put(ErrorsWasm.WASI_EXTERNAL_FUNCTION_WITHOUT_IMPORT, "External functions should be annotated with @WasmImport")
put(ErrorsWasm.ASSOCIATED_OBJECT_INVALID_BINDING, "Invalid associated object binding.")
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_EXPORT_PARAMETER_DEFAULT_VALUE, "Default parameter values are not supported with @WasmImport and @WasmExport")
@@ -26,6 +26,8 @@ public interface ErrorsWasm {
DiagnosticFactory0<PsiElement> WASI_EXTERNAL_NOT_TOP_LEVEL_FUNCTION = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> WASI_EXTERNAL_FUNCTION_WITHOUT_IMPORT = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> ASSOCIATED_OBJECT_INVALID_BINDING = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> NESTED_WASM_IMPORT = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> WASM_IMPORT_ON_NON_EXTERNAL_DECLARATION = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> WASM_IMPORT_EXPORT_PARAMETER_DEFAULT_VALUE = DiagnosticFactory0.create(ERROR);
@@ -0,0 +1,30 @@
/*
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.wasm.resolve.diagnostics
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.name.StandardClassIds
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.annotationClass
import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyExternal
import org.jetbrains.kotlin.resolve.source.getPsi
object FirWasmJsAssociatedObjectChecker : DeclarationChecker {
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
if (descriptor !is ClassDescriptor) return
if (!descriptor.isEffectivelyExternal()) return
for (annotationCall in descriptor.annotations) {
val annotation = annotationCall.annotationClass ?: continue
if (annotation.annotations.hasAnnotation(StandardClassIds.Annotations.AssociatedObjectKey.asSingleFqName())) {
context.trace.report(ErrorsWasm.ASSOCIATED_OBJECT_INVALID_BINDING.on(annotationCall.source.getPsi() ?: declaration))
}
}
}
}
@@ -39,6 +39,12 @@ public class DiagnosticsFirWasmTestGenerated extends AbstractDiagnosticsFirWasmT
runTest("compiler/testData/diagnostics/wasmTests/jsInterop/anonymousInitializer.kt");
}
@Test
@TestMetadata("associatedObjects.kt")
public void testAssociatedObjects() {
runTest("compiler/testData/diagnostics/wasmTests/jsInterop/associatedObjects.kt");
}
@Test
@TestMetadata("body.kt")
public void testBody() {
@@ -39,6 +39,12 @@ public class DiagnosticsWasmTestGenerated extends AbstractDiagnosticsWasmTest {
runTest("compiler/testData/diagnostics/wasmTests/jsInterop/anonymousInitializer.kt");
}
@Test
@TestMetadata("associatedObjects.kt")
public void testAssociatedObjects() {
runTest("compiler/testData/diagnostics/wasmTests/jsInterop/associatedObjects.kt");
}
@Test
@TestMetadata("body.kt")
public void testBody() {