[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