diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/raise/Fold.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/raise/Fold.kt index a4729445bf5..d17779492a0 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/raise/Fold.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/raise/Fold.kt @@ -123,44 +123,13 @@ public inline fun fold( * This method should never be wrapped in `try`/`catch` as it will not throw any unexpected errors, * it will only result in [CancellationException], or fatal exceptions such as `OutOfMemoryError`. */ +@OptIn(DelicateRaiseApi::class) @JvmName("_fold") public inline fun fold( @BuilderInference block: Raise.() -> A, catch: (throwable: Throwable) -> B, recover: (error: Error) -> B, transform: (value: A) -> B, -): B { - contract { - callsInPlace(catch, AT_MOST_ONCE) - callsInPlace(recover, AT_MOST_ONCE) - callsInPlace(transform, AT_MOST_ONCE) - } - return foldUnsafe(block, catch, recover) { - if (it is Function<*> || it is Lazy<*> || it is Sequence<*>) - throw IllegalStateException( - """ - Returning a lazy computation or closure from 'fold' breaks the context scope, and may lead to leaked exceptions on later execution. - Make sure all calls to 'raise' and 'bind' occur within the lifecycle of nullable { }, either { } or similar builders. - - See Arrow documentation on 'Typed errors' for further information. - """.trimIndent() - ) - transform(it) - } -} - -/** - * Similar to [fold], but does *not* check for - * potential lazy return types which break the - * [Raise] context barrier. - */ -@JvmName("_foldUnsafe") -@OptIn(DelicateRaiseApi::class) -public inline fun foldUnsafe( - @BuilderInference block: Raise.() -> A, - catch: (throwable: Throwable) -> B, - recover: (error: Error) -> B, - transform: (value: A) -> B, ): B { contract { callsInPlace(catch, AT_MOST_ONCE) diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/raise/NullableSpec.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/raise/NullableSpec.kt index 0ed27456725..98de52812c2 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/raise/NullableSpec.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/raise/NullableSpec.kt @@ -144,19 +144,4 @@ class NullableSpec : StringSpec({ one + two } shouldBe 3 } - - "Detects potential leaked exceptions" { - shouldThrow { - nullable { lazy { raise(null) } } - } - } - - "Unsafe leakage of exceptions" { - val l: Lazy = foldUnsafe, Lazy?>( - { lazy { raise("problem") } }, { throw it }, { null }, { it } - ).shouldNotBeNull() - shouldThrow { - l.value - } - } })