Actor boundary warning for @TaskLocal values

I am getting this warning with Xcode14 beta 4 (14A5284g) as well as the 5.7 snapshot from August 2nd:

actor Test {

	@TaskLocal static var local: Int?

	func run() async {
		await Self.$local.withValue(42) {  ⚠️
			await work()
		}
	}

	func work() async {
		print("Hello \(Self.local ?? 0)")
	}
}

await Test().run()

:warning: non-sendable type '() async throws -> ()' exiting actor-isolated context in call to non-isolated instance method 'withValue(_:operation:file:line:)' cannot cross actor boundary

I am not sure how to deal with this. Is this legal code and the diagnostic is wrong or am I mis-using task local values?

1 Like

Did you find a solution? I still have this warning with Xcode 14.1.

Same for me. I keep testing, but I also still get the warning. Have not found a good way around it.

Please file a bug on GitHub - apple/swift: The Swift Programming Language - I’ll give it a look though this was too easy to miss on the forums. Perhaps we need to annotate the closure with some more hints here, but on phone now so hard to verify.

To clarify: This is legal code and should not produce warnings.

Thanks for the clarification that this should be legal. I was not really sure since I just started using Swift concurrency. Issue #62220 on GitHub opened.

1 Like

Thank you! I’ll have a look at it.

1 Like

Thanks again, to close the loop here: it's a missing Sendable annotation, will be fixed in here: Actor boundary warning for @TaskLocal values · Issue #62220 · apple/swift · GitHub

2 Likes