[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
@@ -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>()
}
}
@@ -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)
}
@@ -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.")
}
}
@@ -34,6 +34,7 @@ object WasmJsDeclarationCheckers : DeclarationCheckers() {
FirJsExportAnnotationChecker,
FirWasmJsModuleChecker,
FirWasmExternalFileChecker,
FirWasmJsAssociatedObjectChecker,
)
}
@@ -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)
}
}
}
}
@@ -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
}