Skip to content

Commit 4bd8694

Browse files
committed
Revert foldUnsafe
1 parent 4cd0947 commit 4bd8694

File tree

2 files changed

+1
-47
lines changed
  • arrow-libs/core/arrow-core/src

2 files changed

+1
-47
lines changed

arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/raise/Fold.kt

+1-32
Original file line numberDiff line numberDiff line change
@@ -123,44 +123,13 @@ public inline fun <Error, A, B> fold(
123123
* This method should never be wrapped in `try`/`catch` as it will not throw any unexpected errors,
124124
* it will only result in [CancellationException], or fatal exceptions such as `OutOfMemoryError`.
125125
*/
126+
@OptIn(DelicateRaiseApi::class)
126127
@JvmName("_fold")
127128
public inline fun <Error, A, B> fold(
128129
@BuilderInference block: Raise<Error>.() -> A,
129130
catch: (throwable: Throwable) -> B,
130131
recover: (error: Error) -> B,
131132
transform: (value: A) -> B,
132-
): B {
133-
contract {
134-
callsInPlace(catch, AT_MOST_ONCE)
135-
callsInPlace(recover, AT_MOST_ONCE)
136-
callsInPlace(transform, AT_MOST_ONCE)
137-
}
138-
return foldUnsafe(block, catch, recover) {
139-
if (it is Function<*> || it is Lazy<*> || it is Sequence<*>)
140-
throw IllegalStateException(
141-
"""
142-
Returning a lazy computation or closure from 'fold' breaks the context scope, and may lead to leaked exceptions on later execution.
143-
Make sure all calls to 'raise' and 'bind' occur within the lifecycle of nullable { }, either { } or similar builders.
144-
145-
See Arrow documentation on 'Typed errors' for further information.
146-
""".trimIndent()
147-
)
148-
transform(it)
149-
}
150-
}
151-
152-
/**
153-
* Similar to [fold], but does *not* check for
154-
* potential lazy return types which break the
155-
* [Raise] context barrier.
156-
*/
157-
@JvmName("_foldUnsafe")
158-
@OptIn(DelicateRaiseApi::class)
159-
public inline fun <Error, A, B> foldUnsafe(
160-
@BuilderInference block: Raise<Error>.() -> A,
161-
catch: (throwable: Throwable) -> B,
162-
recover: (error: Error) -> B,
163-
transform: (value: A) -> B,
164133
): B {
165134
contract {
166135
callsInPlace(catch, AT_MOST_ONCE)

arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/raise/NullableSpec.kt

-15
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,4 @@ class NullableSpec : StringSpec({
144144
one + two
145145
} shouldBe 3
146146
}
147-
148-
"Detects potential leaked exceptions" {
149-
shouldThrow<IllegalStateException> {
150-
nullable { lazy { raise(null) } }
151-
}
152-
}
153-
154-
"Unsafe leakage of exceptions" {
155-
val l: Lazy<Int> = foldUnsafe<String, Lazy<Int>, Lazy<Int>?>(
156-
{ lazy { raise("problem") } }, { throw it }, { null }, { it }
157-
).shouldNotBeNull()
158-
shouldThrow<IllegalStateException> {
159-
l.value
160-
}
161-
}
162147
})

0 commit comments

Comments
 (0)