[Wasm/WASI] Allow external declarations only for top-level functions

Fix #KT-63737
This commit is contained in:
Igor Yakovlev
2024-01-26 16:35:41 +01:00
committed by Space Team
parent 376a9b8ace
commit ff20ae34e5
3 changed files with 24 additions and 18 deletions
@@ -13,23 +13,22 @@ import org.jetbrains.kotlin.fir.analysis.checkers.declaration.FirBasicDeclaratio
import org.jetbrains.kotlin.fir.analysis.checkers.isTopLevel
import org.jetbrains.kotlin.fir.analysis.diagnostics.wasm.FirWasmErrors
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction
import org.jetbrains.kotlin.fir.declarations.FirFunction
import org.jetbrains.kotlin.fir.declarations.hasAnnotation
import org.jetbrains.kotlin.fir.declarations.utils.isEffectivelyExternal
import org.jetbrains.kotlin.name.WasmStandardClassIds
object FirWasmWasiExternalDeclarationChecker : FirBasicDeclarationChecker(MppCheckerKind.Common) {
override fun check(declaration: FirDeclaration, context: CheckerContext, reporter: DiagnosticReporter) {
if (!context.isTopLevel) return
if (!declaration.symbol.isEffectivelyExternal(context.session)) return
if (declaration is FirSimpleFunction) {
if (!context.isTopLevel) {
reporter.reportOn(declaration.source, FirWasmErrors.WASI_EXTERNAL_NOT_TOP_LEVEL_FUNCTION, context)
} else {
if (!declaration.hasAnnotation(WasmStandardClassIds.Annotations.WasmImport, context.session)) {
reporter.reportOn(declaration.source, FirWasmErrors.WASI_EXTERNAL_FUNCTION_WITHOUT_IMPORT, context)
}
if (declaration is FirFunction) {
if (!declaration.hasAnnotation(WasmStandardClassIds.Annotations.WasmImport, context.session)) {
reporter.reportOn(declaration.source, FirWasmErrors.WASI_EXTERNAL_FUNCTION_WITHOUT_IMPORT, context)
}
} else {
reporter.reportOn(declaration.source, FirWasmErrors.WASI_EXTERNAL_NOT_TOP_LEVEL_FUNCTION, context)
}
}
}
+9 -3
View File
@@ -3,9 +3,15 @@ import kotlin.wasm.WasmImport
<!WASI_EXTERNAL_FUNCTION_WITHOUT_IMPORT!>external fun foo(): Int<!>
external interface I {
<!WASI_EXTERNAL_NOT_TOP_LEVEL_FUNCTION!>fun foo(): Int<!>
}
<!WASI_EXTERNAL_NOT_TOP_LEVEL_FUNCTION!>external interface I {
fun foo(): Int
}<!>
<!WASI_EXTERNAL_NOT_TOP_LEVEL_FUNCTION!>external class X<!>
<!WASI_EXTERNAL_NOT_TOP_LEVEL_FUNCTION!>external val v: Int<!>
external <!WASI_EXTERNAL_NOT_TOP_LEVEL_FUNCTION!>object AC<!>
@WasmImport("a", "b")
external fun importedFoo(): Int
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.wasm.resolve.diagnostics
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.MemberDescriptor
import org.jetbrains.kotlin.descriptors.PropertyAccessorDescriptor
import org.jetbrains.kotlin.js.resolve.diagnostics.findPsi
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtDeclaration
@@ -19,18 +20,18 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyExternal
object WasmWasiExternalDeclarationChecker : DeclarationChecker {
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
if (descriptor !is MemberDescriptor) return
if (descriptor is PropertyAccessorDescriptor) return
if (!DescriptorUtils.isTopLevelDeclaration(descriptor)) return
if (!descriptor.isEffectivelyExternal()) return
if (descriptor is FunctionDescriptor) {
if (!DescriptorUtils.isTopLevelDeclaration(descriptor)) {
if (!descriptor.annotations.hasAnnotation(FqName("kotlin.wasm.WasmImport"))) {
val reportOn = descriptor.findPsi() ?: declaration
context.trace.report(ErrorsWasm.WASI_EXTERNAL_NOT_TOP_LEVEL_FUNCTION.on(reportOn))
} else {
if (!descriptor.annotations.hasAnnotation(FqName("kotlin.wasm.WasmImport"))) {
val reportOn = descriptor.findPsi() ?: declaration
context.trace.report(ErrorsWasm.WASI_EXTERNAL_FUNCTION_WITHOUT_IMPORT.on(reportOn))
}
context.trace.report(ErrorsWasm.WASI_EXTERNAL_FUNCTION_WITHOUT_IMPORT.on(reportOn))
}
} else {
val reportOn = descriptor.findPsi() ?: declaration
context.trace.report(ErrorsWasm.WASI_EXTERNAL_NOT_TOP_LEVEL_FUNCTION.on(reportOn))
}
}
}