# Binding a task-local without a closure

**URL:** <https://forums.swift.org/t/binding-a-task-local-without-a-closure/90153>\
**Category:** Discussion\
**Created:** [October 19, 2026, 6:58pm UTC](https://forums.swift.org/t/binding-a-task-local-without-a-closure/90153 "2026-10-19T18:58:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zienag](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/zienag/32/43995_2.png) [@zienag](https://forums.swift.org/u/zienag)\
**Post date:** [October 19, 2026, 6:58pm UTC](https://forums.swift.org/t/binding-a-task-local-without-a-closure/90153/1 "2026-10-19T18:58:49Z")

</div>

Hi Swift users!

I want to talk about making task-locals more flexible while keeping them safe. The latest compiler changes forbid the unsafe way of binding them outside of `withValue`, but there is still no safe API that covers this use case. Here is the use case and an idea for such API.

I have a scope object that is created at the start of a function and ended at the end of it:

```swift
func handle(request: Request) async {
  let scope = Context.begin()
  defer { scope.end() }
  await process() // everything called from here sees the context
}

```

Inside, the context is stored in a task-local, so that `process()` and child tasks can find it. That means the task-local has to be bound in `begin()` and unbound in `end()`:

```swift
enum Context {
  @TaskLocal static var current: Store?

  static func begin() -> Scope {
    let store = Store()
    $current.push(store) // bind for the rest of the frame
    return Scope(store)
  }
}

extension Scope {
  func end() {
    Context.$current.pop()
  }
}

```

I know about `withValue`, but I don't want it here. Every function that sets up a context would get one more level of indentation, and that is exactly what this code is trying to avoid.

So `push` and `pop` call the same runtime functions that `withValue` calls:

```swift
@_silgen_name("swift_task_localValuePush")
func _taskLocalValuePush(
  key: Builtin.RawPointer,
  value: __owned some Any
)

@_silgen_name("swift_task_localValuePop")
func _taskLocalValuePop()

extension TaskLocal {
  func push(_ value: __owned Value) {
    _taskLocalValuePush(key: key, value: consume value)
  }

  func pop() {
    _taskLocalValuePop()
  }
}

```

Push at the start, pop at the end, always balanced in the same frame ([source](https://github.com/yandex/implicits/blob/main/Sources/Implicits/TaskLocalUtils.swift#L48-L59)). This worked fine up to 6.2. In 6.3 these two symbols were added to the compiler's list of runtime functions, and now there is a warning:

```plaintext
symbol name 'swift_task_localValuePush' is reserved for the Swift runtime
and cannot be directly referenced without causing unpredictable behavior;
this will become an error

```

The PR that added the warning says it is there to find out who uses these functions before it becomes an error. So here is one user :)

What I would really like is a binding that I can hold in a variable:

```swift
extension TaskLocal {
  struct Binding: ~Copyable { deinit { /* unbind */ } }
  func bind(_ value: Value) -> Binding
}

func handle(request: Request) async {
  let binding = $context.bind(Context()) // unbinds when `binding` dies
  await process()
}

```

SE-0311 mentions this in future directions ("language features to avoid nesting with `withValue`"). Back then the problem was that the task could outlive the binding. Now there are noncopyable types with `deinit` and nonescapable types, so it looks possible. And since the stack nesting fix in 6.3, `withValue` itself is built on a builtin that returns a binding token and another builtin that takes it back. That token is basically this struct. The hard part is probably the same as in that fix: the compiler has to know that `init` allocates on the task stack and `deinit` frees it, so its own `swift_task_alloc` calls nest correctly.

Is there any interest in this? I can write a pitch.

And meanwhile, is `AllowRuntimeSymbolDeclarations` the intended way to keep such code compiling, or will the error come without any alternative?

Thanks!

---

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [October 19, 2026, 11:22pm UTC](https://forums.swift.org/t/binding-a-task-local-without-a-closure/90153/2 "2026-10-19T23:22:31Z")

</div>

I do think your concern is valid, but I don't think we should ad-hoc solve it for one specific feature.

Instead, we should holistically solve all the `with...` methods, by replacing them with a form of

```swift
func x() {
  with let scope = thing // 

  // end of scope, triggers:
  // scope.end()
}

```

This idea has been floating around for a long time already and often referred to as context managers. It would help us get rid of this ever-growing problem of with... methods which we _must_ use to keep APIs safe in structured concurrency contexts.

I would not want to spend significant effort on fixing one of the with methods ad-hoc and then we'll be left with the same issue still for with... cancellation handlers, priority escalation, task groups and everything else.
