[WasmJs] Prohibit external classes to be a storage of an associated objects
Fix #KT-65355
This commit is contained in:
+4
@@ -70,4 +70,8 @@ object WASM_DIAGNOSTICS_LIST : DiagnosticList("FirWasmErrors") {
|
||||
val WASI_EXTERNAL_NOT_TOP_LEVEL_FUNCTION by error<KtElement>()
|
||||
val WASI_EXTERNAL_FUNCTION_WITHOUT_IMPORT by error<KtElement>()
|
||||
}
|
||||
|
||||
val ASSOCIATED_OBJECTS by object : DiagnosticGroup("Associated object") {
|
||||
val ASSOCIATED_OBJECT_INVALID_BINDING by error<KtElement>()
|
||||
}
|
||||
}
|
||||
|
||||
+3
@@ -55,6 +55,9 @@ object FirWasmErrors {
|
||||
val WASI_EXTERNAL_NOT_TOP_LEVEL_FUNCTION: KtDiagnosticFactory0 by error0<KtElement>()
|
||||
val WASI_EXTERNAL_FUNCTION_WITHOUT_IMPORT: KtDiagnosticFactory0 by error0<KtElement>()
|
||||
|
||||
// Associated object
|
||||
val ASSOCIATED_OBJECT_INVALID_BINDING: KtDiagnosticFactory0 by error0<KtElement>()
|
||||
|
||||
init {
|
||||
RootDiagnosticRendererFactory.registerFactory(FirWasmErrorsDefaultMessages)
|
||||
}
|
||||
|
||||
+4
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactoryToRendererMap
|
||||
import org.jetbrains.kotlin.diagnostics.KtDiagnosticRenderers.TO_STRING
|
||||
import org.jetbrains.kotlin.diagnostics.rendering.BaseDiagnosticRendererFactory
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirDiagnosticRenderers
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.wasm.FirWasmErrors.ASSOCIATED_OBJECT_INVALID_BINDING
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.wasm.FirWasmErrors.CALL_TO_DEFINED_EXTERNALLY_FROM_NON_EXTERNAL_DECLARATION
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.wasm.FirWasmErrors.EXTERNAL_TYPE_EXTENDS_NON_EXTERNAL_TYPE
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.wasm.FirWasmErrors.JSCODE_INVALID_PARAMETER_NAME
|
||||
@@ -42,6 +43,7 @@ object FirWasmErrorsDefaultMessages : BaseDiagnosticRendererFactory() {
|
||||
NESTED_JS_MODULE_PROHIBITED,
|
||||
"'@JsModule' cannot appear here since the file is already marked by either '@JsModule'."
|
||||
)
|
||||
|
||||
map.put(
|
||||
NON_EXTERNAL_TYPE_EXTENDS_EXTERNAL_TYPE,
|
||||
"Non-external type extends external type ''{0}''",
|
||||
@@ -100,5 +102,7 @@ object FirWasmErrorsDefaultMessages : BaseDiagnosticRendererFactory() {
|
||||
|
||||
map.put(WASI_EXTERNAL_NOT_TOP_LEVEL_FUNCTION, "Only top-level functions can be external.")
|
||||
map.put(WASI_EXTERNAL_FUNCTION_WITHOUT_IMPORT, "External functions should be annotated with '@WasmImport'.")
|
||||
|
||||
map.put(ASSOCIATED_OBJECT_INVALID_BINDING, "Invalid associated object binding.")
|
||||
}
|
||||
}
|
||||
|
||||
+1
@@ -34,6 +34,7 @@ object WasmJsDeclarationCheckers : DeclarationCheckers() {
|
||||
FirJsExportAnnotationChecker,
|
||||
FirWasmJsModuleChecker,
|
||||
FirWasmExternalFileChecker,
|
||||
FirWasmJsAssociatedObjectChecker,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.fir.analysis.wasm.checkers.declaration
|
||||
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticReporter
|
||||
import org.jetbrains.kotlin.diagnostics.reportOn
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.MppCheckerKind
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
|
||||
import org.jetbrains.kotlin.fir.analysis.checkers.declaration.FirBasicDeclarationChecker
|
||||
import org.jetbrains.kotlin.fir.analysis.diagnostics.wasm.FirWasmErrors.ASSOCIATED_OBJECT_INVALID_BINDING
|
||||
import org.jetbrains.kotlin.fir.declarations.FirClass
|
||||
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
|
||||
import org.jetbrains.kotlin.fir.declarations.hasAnnotation
|
||||
import org.jetbrains.kotlin.fir.types.coneType
|
||||
import org.jetbrains.kotlin.fir.types.toSymbol
|
||||
import org.jetbrains.kotlin.name.StandardClassIds
|
||||
import org.jetbrains.kotlin.fir.declarations.utils.isEffectivelyExternal
|
||||
|
||||
object FirWasmJsAssociatedObjectChecker : FirBasicDeclarationChecker(MppCheckerKind.Common) {
|
||||
override fun check(declaration: FirDeclaration, context: CheckerContext, reporter: DiagnosticReporter) {
|
||||
if (declaration !is FirClass) return
|
||||
if (!declaration.symbol.isEffectivelyExternal(context.session)) return
|
||||
|
||||
for (annotationCall in declaration.annotations) {
|
||||
val annotations = annotationCall.annotationTypeRef.coneType.toSymbol(context.session)?.annotations ?: continue
|
||||
if (annotations.hasAnnotation(StandardClassIds.Annotations.AssociatedObjectKey, context.session)) {
|
||||
reporter.reportOn(annotationCall.source, ASSOCIATED_OBJECT_INVALID_BINDING, context)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -678,6 +678,8 @@ val loweringList = listOf(
|
||||
|
||||
wasmStringSwitchOptimizerLowering,
|
||||
|
||||
associatedObjectsLowering,
|
||||
|
||||
complexExternalDeclarationsToTopLevelFunctionsLowering,
|
||||
complexExternalDeclarationsUsagesLowering,
|
||||
|
||||
@@ -724,8 +726,6 @@ val loweringList = listOf(
|
||||
eraseVirtualDispatchReceiverParametersTypes,
|
||||
bridgesConstructionPhase,
|
||||
|
||||
associatedObjectsLowering,
|
||||
|
||||
objectDeclarationLoweringPhase,
|
||||
genericReturnTypeLowering,
|
||||
unitToVoidLowering,
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
// FIR_IDENTICAL
|
||||
@file:OptIn(ExperimentalAssociatedObjects::class)
|
||||
|
||||
import kotlin.reflect.AssociatedObjectKey
|
||||
import kotlin.reflect.ExperimentalAssociatedObjects
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.findAssociatedObject
|
||||
|
||||
@AssociatedObjectKey
|
||||
@Retention(AnnotationRetention.BINARY)
|
||||
annotation class X(val kClass: KClass<*>)
|
||||
|
||||
object Promise
|
||||
|
||||
<!ASSOCIATED_OBJECT_INVALID_BINDING!>@X(Promise::class)<!>
|
||||
external class Y
|
||||
|
||||
external class OuterExternal {
|
||||
<!ASSOCIATED_OBJECT_INVALID_BINDING!>@X(Promise::class)<!>
|
||||
class NestedExternal
|
||||
}
|
||||
@@ -31,6 +31,7 @@ object WasmJsPlatformConfigurator : PlatformConfiguratorBase(
|
||||
WasmJsFunAnnotationChecker,
|
||||
WasmJsInteropTypesChecker,
|
||||
WasmJsExportChecker,
|
||||
FirWasmJsAssociatedObjectChecker,
|
||||
),
|
||||
additionalCallCheckers = listOf(
|
||||
JsModuleCallChecker,
|
||||
|
||||
+2
@@ -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);
|
||||
|
||||
+30
@@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
@@ -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() {
|
||||
|
||||
+6
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user