From fa3e75463eb19deb10b19b1d763aa42d24a23018 Mon Sep 17 00:00:00 2001 From: Svyatoslav Kuzmich Date: Fri, 1 Oct 2021 19:42:20 +0300 Subject: [PATCH] [Wasm] Support throwIAE and throwNoBranchMatchedException Add a couple of test stubs Unmute tests --- .../jetbrains/kotlin/backend/wasm/WasmSymbols.kt | 3 +++ .../kotlin/backend/wasm/lower/BuiltInsLowering.kt | 6 ++---- .../box/ranges/contains/inExtensionRange.kt | 2 -- .../codegen/box/ranges/contains/inIntRange.kt | 2 -- .../ranges/contains/inRangeWithCustomContains.kt | 2 -- .../ranges/contains/inRangeWithImplicitReceiver.kt | 2 -- .../contains/inRangeWithNonmatchingArguments.kt | 2 -- .../box/ranges/contains/inRangeWithSmartCast.kt | 2 -- .../internal/kotlin/wasm/internal/ThrowHelpers.kt | 4 ++++ libraries/stdlib/wasm/stubs/testHelpers.kt | 14 ++++++++++++++ 10 files changed, 23 insertions(+), 16 deletions(-) diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/WasmSymbols.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/WasmSymbols.kt index 1088e664b9f..83c2571745a 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/WasmSymbols.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/WasmSymbols.kt @@ -44,6 +44,9 @@ class WasmSymbols( override val throwNullPointerException = getInternalFunction("THROW_NPE") override val throwISE = getInternalFunction("THROW_ISE") override val throwTypeCastException = getInternalFunction("THROW_CCE") + val throwIAE = getInternalFunction("THROW_IAE") + val throwNoBranchMatchedException = + getInternalFunction("throwNoBranchMatchedException") override val throwUninitializedPropertyAccessException = getInternalFunction("throwUninitializedPropertyAccessException") override val defaultConstructorMarker = diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/BuiltInsLowering.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/BuiltInsLowering.kt index c3f82eb9a88..869a9c0ca3e 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/BuiltInsLowering.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/lower/BuiltInsLowering.kt @@ -102,13 +102,11 @@ class BuiltInsLowering(val context: WasmBackendContext) : FileLoweringPass { return irCall(call, newSymbol) } - // TODO: Implement irBuiltins.noWhenBranchMatchedExceptionSymbol -> - return builder.irCall(symbols.wasmUnreachable, irBuiltins.nothingType) + return builder.irCall(symbols.throwNoBranchMatchedException, irBuiltins.nothingType) - // TODO: Implement irBuiltins.illegalArgumentExceptionSymbol -> - return builder.irCall(symbols.wasmUnreachable, irBuiltins.nothingType) + return builder.irCall(symbols.throwIAE, irBuiltins.nothingType) irBuiltins.dataClassArrayMemberHashCodeSymbol -> { // TODO: Implement diff --git a/compiler/testData/codegen/box/ranges/contains/inExtensionRange.kt b/compiler/testData/codegen/box/ranges/contains/inExtensionRange.kt index dc982ac790d..f6b5dd0e28e 100644 --- a/compiler/testData/codegen/box/ranges/contains/inExtensionRange.kt +++ b/compiler/testData/codegen/box/ranges/contains/inExtensionRange.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND: WASM -// WASM_MUTE_REASON: KOTLIN_TEST_LIB // WITH_RUNTIME import kotlin.test.* diff --git a/compiler/testData/codegen/box/ranges/contains/inIntRange.kt b/compiler/testData/codegen/box/ranges/contains/inIntRange.kt index 3c03ff6e5f9..488c32c72d9 100644 --- a/compiler/testData/codegen/box/ranges/contains/inIntRange.kt +++ b/compiler/testData/codegen/box/ranges/contains/inIntRange.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND: WASM -// WASM_MUTE_REASON: KOTLIN_TEST_LIB // WITH_RUNTIME import kotlin.test.* diff --git a/compiler/testData/codegen/box/ranges/contains/inRangeWithCustomContains.kt b/compiler/testData/codegen/box/ranges/contains/inRangeWithCustomContains.kt index 27dbc4019a4..a03a60539d4 100644 --- a/compiler/testData/codegen/box/ranges/contains/inRangeWithCustomContains.kt +++ b/compiler/testData/codegen/box/ranges/contains/inRangeWithCustomContains.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND: WASM -// WASM_MUTE_REASON: KOTLIN_TEST_LIB // WITH_RUNTIME import kotlin.test.* diff --git a/compiler/testData/codegen/box/ranges/contains/inRangeWithImplicitReceiver.kt b/compiler/testData/codegen/box/ranges/contains/inRangeWithImplicitReceiver.kt index c00bd374813..8a700cd7644 100644 --- a/compiler/testData/codegen/box/ranges/contains/inRangeWithImplicitReceiver.kt +++ b/compiler/testData/codegen/box/ranges/contains/inRangeWithImplicitReceiver.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND: WASM -// WASM_MUTE_REASON: KOTLIN_TEST_LIB // WITH_RUNTIME import kotlin.test.* diff --git a/compiler/testData/codegen/box/ranges/contains/inRangeWithNonmatchingArguments.kt b/compiler/testData/codegen/box/ranges/contains/inRangeWithNonmatchingArguments.kt index 4199b23f6bf..c675f47decf 100644 --- a/compiler/testData/codegen/box/ranges/contains/inRangeWithNonmatchingArguments.kt +++ b/compiler/testData/codegen/box/ranges/contains/inRangeWithNonmatchingArguments.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND: WASM -// WASM_MUTE_REASON: KOTLIN_TEST_LIB // WITH_RUNTIME import kotlin.test.* diff --git a/compiler/testData/codegen/box/ranges/contains/inRangeWithSmartCast.kt b/compiler/testData/codegen/box/ranges/contains/inRangeWithSmartCast.kt index 0249cc1d39d..c5a0bfe5fce 100644 --- a/compiler/testData/codegen/box/ranges/contains/inRangeWithSmartCast.kt +++ b/compiler/testData/codegen/box/ranges/contains/inRangeWithSmartCast.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND: WASM -// WASM_MUTE_REASON: KOTLIN_TEST_LIB // IGNORE_BACKEND: NATIVE // WITH_RUNTIME import kotlin.test.assertTrue diff --git a/libraries/stdlib/wasm/internal/kotlin/wasm/internal/ThrowHelpers.kt b/libraries/stdlib/wasm/internal/kotlin/wasm/internal/ThrowHelpers.kt index afc6ae3d74c..0ad0ffa81fc 100644 --- a/libraries/stdlib/wasm/internal/kotlin/wasm/internal/ThrowHelpers.kt +++ b/libraries/stdlib/wasm/internal/kotlin/wasm/internal/ThrowHelpers.kt @@ -21,6 +21,10 @@ internal fun THROW_IAE(): Nothing { throw IllegalArgumentException() } +internal fun throwNoBranchMatchedException(): Nothing { + throw NoWhenBranchMatchedException() +} + @PublishedApi internal fun throwUninitializedPropertyAccessException(name: String): Nothing { throw UninitializedPropertyAccessException("lateinit property $name has not been initialized") diff --git a/libraries/stdlib/wasm/stubs/testHelpers.kt b/libraries/stdlib/wasm/stubs/testHelpers.kt index 4b51e714be5..70128e11706 100644 --- a/libraries/stdlib/wasm/stubs/testHelpers.kt +++ b/libraries/stdlib/wasm/stubs/testHelpers.kt @@ -5,6 +5,7 @@ package kotlin.test +import kotlin.internal.InlineOnly import kotlin.internal.OnlyInputTypes public fun assert(x: Boolean) { @@ -20,3 +21,16 @@ public fun <@OnlyInputTypes T> assertEquals(expected: T, actual: T, message: Str public fun <@OnlyInputTypes T> assertNotEquals(illegal: T, actual: T, message: String? = null) { if (illegal == actual) throw AssertionError("assertNotEquals failed: illegal:$illegal, actual:$actual, message=$message") } + +@InlineOnly +public inline fun assertFailsWith(message: String? = null, block: () -> Unit): T { + var throwable: Throwable? = null + try { + block() + } catch (e: Throwable) { + throwable = e + } + return throwable as? T ?: throw AssertionError("assertFailsWith failed: $message") +} + +public fun assertTrue(x: Boolean) = assert(x)