From 89e2af2047d38aa2a083afe7404ef408e0db62e7 Mon Sep 17 00:00:00 2001 From: Alexander Korepanov Date: Mon, 18 Dec 2023 13:00:30 +0100 Subject: [PATCH] [JS FIR] Fix the JsExport diagnostic for nullable primitive types - Correctly handle nullable primitive types. - Unsigned primitive arrays are not exportable. ^KT-64366 Fixed --- .../FirJsExportDeclarationChecker.kt | 5 +- .../export/primitiveNullableTypes.kt | 72 +++++++++++++++++++ .../export/primitiveNullableTypes.txt | 34 +++++++++ ...JsOldFrontendDiagnosticsTestGenerated.java | 6 ++ .../DiagnosticsWithJsStdLibTestGenerated.java | 6 ++ 5 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/export/primitiveNullableTypes.kt create mode 100644 compiler/testData/diagnostics/testsWithJsStdLib/export/primitiveNullableTypes.txt diff --git a/compiler/fir/checkers/checkers.js/src/org/jetbrains/kotlin/fir/analysis/js/checkers/declaration/FirJsExportDeclarationChecker.kt b/compiler/fir/checkers/checkers.js/src/org/jetbrains/kotlin/fir/analysis/js/checkers/declaration/FirJsExportDeclarationChecker.kt index a19201d18eb..d5d0614a228 100644 --- a/compiler/fir/checkers/checkers.js/src/org/jetbrains/kotlin/fir/analysis/js/checkers/declaration/FirJsExportDeclarationChecker.kt +++ b/compiler/fir/checkers/checkers.js/src/org/jetbrains/kotlin/fir/analysis/js/checkers/declaration/FirJsExportDeclarationChecker.kt @@ -203,7 +203,7 @@ object FirJsExportDeclarationChecker : FirBasicDeclarationChecker() { val nonNullable = withNullability(ConeNullability.NOT_NULL, session.typeContext) val isPrimitiveExportableType = nonNullable.isAny || nonNullable.isNullableAny - || nonNullable is ConeDynamicType || isPrimitiveExportableConeKotlinType + || nonNullable is ConeDynamicType || nonNullable.isPrimitiveExportableConeKotlinType val symbol = toSymbol(session) return when { @@ -222,7 +222,8 @@ object FirJsExportDeclarationChecker : FirBasicDeclarationChecker() { || isString || isPrimitiveNumberOrNullableType && !isLong || isNothingOrNullableNothing - || isArrayType + || isPrimitiveArray + || isNonPrimitiveArray private fun validateDeclarationOnConsumableName( declaration: FirMemberDeclaration, diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/export/primitiveNullableTypes.kt b/compiler/testData/diagnostics/testsWithJsStdLib/export/primitiveNullableTypes.kt new file mode 100644 index 00000000000..81e7a8d28e9 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/export/primitiveNullableTypes.kt @@ -0,0 +1,72 @@ +// !OPT_IN: kotlin.js.ExperimentalJsExport kotlin.ExperimentalUnsignedTypes +// FIR_IDENTICAL + +@JsExport +fun any(): Any? = null +@JsExport +fun generic(): T? = null +@JsExport +fun nothing(): Nothing? = null +@JsExport +fun throwable(): Throwable? = null + +@JsExport +fun unit(): Unit? = null +@JsExport +fun char(): Char? = null +@JsExport +fun charSeq(): CharSequence? = null +@JsExport +fun number(): Number? = null + +@JsExport +fun str(): String? = null +@JsExport +fun bool(): Boolean? = null +@JsExport +fun byte(): Byte? = null +@JsExport +fun short(): Short? = null +@JsExport +fun int(): Int? = null +@JsExport +fun double(): Double? = null +@JsExport +fun float(): Float? = null + +@JsExport +fun ubyte(): UByte? = null +@JsExport +fun ushort(): UShort? = null +@JsExport +fun uint(): UInt? = null +@JsExport +fun long(): Long? = null +@JsExport +fun ulong(): ULong? = null + +@JsExport +fun arr(): Array? = null +@JsExport +fun boolArr(): BooleanArray? = null +@JsExport +fun byteArr(): ByteArray? = null +@JsExport +fun shortArr(): ShortArray? = null +@JsExport +fun intArr(): IntArray? = null +@JsExport +fun longArr(): LongArray? = null +@JsExport +fun floatArr(): FloatArray? = null +@JsExport +fun doubleArr(): DoubleArray? = null + +@JsExport +fun ubyteArr(): UByteArray? = null +@JsExport +fun ushortArr(): UShortArray? = null +@JsExport +fun uintArr(): UIntArray? = null +@JsExport +fun ulongArr(): ULongArray? = null diff --git a/compiler/testData/diagnostics/testsWithJsStdLib/export/primitiveNullableTypes.txt b/compiler/testData/diagnostics/testsWithJsStdLib/export/primitiveNullableTypes.txt new file mode 100644 index 00000000000..0ef9e482df5 --- /dev/null +++ b/compiler/testData/diagnostics/testsWithJsStdLib/export/primitiveNullableTypes.txt @@ -0,0 +1,34 @@ +package + +@kotlin.js.JsExport public fun any(): kotlin.Any? +@kotlin.js.JsExport public fun arr(): kotlin.Array? +@kotlin.js.JsExport public fun bool(): kotlin.Boolean? +@kotlin.js.JsExport public fun boolArr(): kotlin.BooleanArray? +@kotlin.js.JsExport public fun byte(): kotlin.Byte? +@kotlin.js.JsExport public fun byteArr(): kotlin.ByteArray? +@kotlin.js.JsExport public fun char(): kotlin.Char? +@kotlin.js.JsExport public fun charSeq(): kotlin.CharSequence? +@kotlin.js.JsExport public fun double(): kotlin.Double? +@kotlin.js.JsExport public fun doubleArr(): kotlin.DoubleArray? +@kotlin.js.JsExport public fun float(): kotlin.Float? +@kotlin.js.JsExport public fun floatArr(): kotlin.FloatArray? +@kotlin.js.JsExport public fun generic(): T? +@kotlin.js.JsExport public fun int(): kotlin.Int? +@kotlin.js.JsExport public fun intArr(): kotlin.IntArray? +@kotlin.js.JsExport public fun long(): kotlin.Long? +@kotlin.js.JsExport public fun longArr(): kotlin.LongArray? +@kotlin.js.JsExport public fun nothing(): kotlin.Nothing? +@kotlin.js.JsExport public fun number(): kotlin.Number? +@kotlin.js.JsExport public fun short(): kotlin.Short? +@kotlin.js.JsExport public fun shortArr(): kotlin.ShortArray? +@kotlin.js.JsExport public fun str(): kotlin.String? +@kotlin.js.JsExport public fun throwable(): kotlin.Throwable? +@kotlin.js.JsExport public fun ubyte(): kotlin.UByte? +@kotlin.js.JsExport public fun ubyteArr(): kotlin.UByteArray? +@kotlin.js.JsExport public fun uint(): kotlin.UInt? +@kotlin.js.JsExport public fun uintArr(): kotlin.UIntArray? +@kotlin.js.JsExport public fun ulong(): kotlin.ULong? +@kotlin.js.JsExport public fun ulongArr(): kotlin.ULongArray? +@kotlin.js.JsExport public fun unit(): kotlin.Unit? +@kotlin.js.JsExport public fun ushort(): kotlin.UShort? +@kotlin.js.JsExport public fun ushortArr(): kotlin.UShortArray? diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirPsiJsOldFrontendDiagnosticsTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirPsiJsOldFrontendDiagnosticsTestGenerated.java index 009d87b1762..e68e1de33f7 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirPsiJsOldFrontendDiagnosticsTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/fir/FirPsiJsOldFrontendDiagnosticsTestGenerated.java @@ -486,6 +486,12 @@ public class FirPsiJsOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiJ runTest("compiler/testData/diagnostics/testsWithJsStdLib/export/nonConsumableIdentifiersInExportedFile.kt"); } + @Test + @TestMetadata("primitiveNullableTypes.kt") + public void testPrimitiveNullableTypes() throws Exception { + runTest("compiler/testData/diagnostics/testsWithJsStdLib/export/primitiveNullableTypes.kt"); + } + @Test @TestMetadata("secondaryConstructorWithoutJsName.kt") public void testSecondaryConstructorWithoutJsName() throws Exception { diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/DiagnosticsWithJsStdLibTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/DiagnosticsWithJsStdLibTestGenerated.java index 9918a50bd78..64420381efb 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/DiagnosticsWithJsStdLibTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/DiagnosticsWithJsStdLibTestGenerated.java @@ -486,6 +486,12 @@ public class DiagnosticsWithJsStdLibTestGenerated extends AbstractDiagnosticsTes runTest("compiler/testData/diagnostics/testsWithJsStdLib/export/nonConsumableIdentifiersInExportedFile.kt"); } + @Test + @TestMetadata("primitiveNullableTypes.kt") + public void testPrimitiveNullableTypes() throws Exception { + runTest("compiler/testData/diagnostics/testsWithJsStdLib/export/primitiveNullableTypes.kt"); + } + @Test @TestMetadata("secondaryConstructorWithoutJsName.kt") public void testSecondaryConstructorWithoutJsName() throws Exception {