# Resolve mismatch between \`withoutActuallyEscaping\` documentation and function signature

**URL:** <https://forums.swift.org/t/resolve-mismatch-between-withoutactuallyescaping-documentation-and-function-signature/90204>\
**Category:** Discussion\
**Tags:** documentation, escapingclosure, unsafe, nonescaping\
**Created:** [October 22, 2026, 4:43pm UTC](https://forums.swift.org/t/resolve-mismatch-between-withoutactuallyescaping-documentation-and-function-signature/90204 "2026-10-22T16:43:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremyabannister](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeremyabannister/32/10143_2.png) [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Post date:** [October 22, 2026, 4:43pm UTC](https://forums.swift.org/t/resolve-mismatch-between-withoutactuallyescaping-documentation-and-function-signature/90204/1 "2026-10-22T16:43:45Z")

</div>

The documentation for `withoutActuallyEscaping` says:

> The escapable copy of `closure` passed to `body` is only valid during the call to `withoutActuallyEscaping(_:do:)`. It is undefined behavior for the escapable closure to be stored, referenced, or executed after the function returns.

I was surprised to see that the signature is not marked `@unsafe`. I didn't do the digging myself, but ChatGPT says that this was mentioned explicitly and that the rationale is that it is dynamically detected if the closure escapes, and the process is terminated. If this is true, doesn't this make the documentation wrong? And if there is indeed some way to get UB when using this function, shouldn't it be marked `@unsafe`?

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [October 22, 2026, 6:12pm UTC](https://forums.swift.org/t/resolve-mismatch-between-withoutactuallyescaping-documentation-and-function-signature/90204/2 "2026-10-22T18:12:36Z")

</div>

> [@jeremyabannister](#):
>
> If this is true, doesn't this make the documentation wrong?

It appears to trap deterministically in a simple test at least:

```swift
func f(_ fn: () -> ()) -> (() -> ()) {
  return withoutActuallyEscaping(fn) { $0 }
}

func caller() -> (() -> ()) {
  let x = 3
  return f({ print(x) })
}

caller()()

```

I don't know if this is guaranteed to be the case. I'd say in this case the documentation might be wrong, or at least underspecified, but perhaps we cannot guarantee the trap catches all such invalid usages?

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [October 23, 2026, 4:23am UTC](https://forums.swift.org/t/resolve-mismatch-between-withoutactuallyescaping-documentation-and-function-signature/90204/3 "2026-10-23T04:23:08Z")

</div>

If a call to the closure races with the scope exit, then yeah, we cannot guarantee to diagnose the misuse; it is up to the vagaries of life whether the closure finishes executing before the scope exits.

---

<div class="post-metadata">

**Author:** ![jeremyabannister](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeremyabannister/32/10143_2.png) [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Post date:** [October 23, 2026, 4:47am UTC](https://forums.swift.org/t/resolve-mismatch-between-withoutactuallyescaping-documentation-and-function-signature/90204/4 "2026-10-23T04:47:08Z")

</div>

Then shouldn’t the function be marked `@unsafe`?

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [October 23, 2026, 5:11am UTC](https://forums.swift.org/t/resolve-mismatch-between-withoutactuallyescaping-documentation-and-function-signature/90204/5 "2026-10-23T05:11:04Z")

</div>

Well, we can’t promise to diagnose the abstract violation of the rules, but we can diagnose the problem dynamically if it happens to occur in this execution. The current check — that there are no outstanding references to the function value on scope exit — should be enough for that, and I think that should be enough to make it safe. It would be safe regardless if the function isn’t `@Sendable`.

---

<div class="post-metadata">

**Author:** ![jeremyabannister](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeremyabannister/32/10143_2.png) [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Post date:** [October 23, 2026, 5:20am UTC](https://forums.swift.org/t/resolve-mismatch-between-withoutactuallyescaping-documentation-and-function-signature/90204/6 "2026-10-23T05:20:52Z")

</div>

Ah sorry, I wasn’t understanding clearly. You’re saying that even in the race case, if the closure is retained after the scope exits then we reliably trap?

Then I’m back to asking, shouldn’t the documentation be updated to not claim that using `withoutActuallyEscaping` can cause UB?
