swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.6k stars 10.37k forks source link

should async let closure literals always return sending? #76744

Open mattmassicotte opened 1 month ago

mattmassicotte commented 1 month ago

Description

This is very closely-related to https://github.com/swiftlang/swift/issues/76727, and possibly even the same problem.

I think that closure literals that return anything have to effectively using a sending return to even make sense as an async let. Because of that, shouldn't they always have an implicit sending return? That would make this compile, but also shift the error message into the closure body, where I think it should belong.

Today, both of these result in:

error: non-sendable type 'NonSendable' returned by implicitly asynchronous call to nonisolated function cannot cross actor boundary

Reproduction

class NonSendable {}

let closure: () -> sending NonSendable = { NonSendable() }

async let value = closure()

or less explicitly:

class NonSendable {}

async let value = { NonSendable() }()

Expected behavior

I think this should work or produce an error inside the closure body if the return value actually cannot be sent.

Environment

swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.11 clang-1600.0.26.2) Target: arm64-apple-macosx14.0

Additional information

No response