swiftlang / swift

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

Async let does not preserve typed throw #76169

Open finestructure opened 2 months ago

finestructure commented 2 months ago

Description

According to SE-0413, async let should preserve the error type of typed throws.

However, this does not seem to be the case in Xcode 16.0 beta 6 (16A5230g).

Reproduction

struct AppError: Error { }

func f() async throws(AppError) -> Int { 42 }

func test() async throws(AppError) -> Int {
    async let a = f()
    return try await a
}

fails to compile with

error: Error - typed throws.xcplaygroundpage:119:26: thrown expression type 'any Error' cannot be converted to error type 'AppError'
        return try await a
                         ^

Expected behavior

try await a should satisfy throws(AppError) of the function.

Environment

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

Additional information

No response

JessyCatterwaul commented 2 months ago

For all other purposes, I've been using forceCastError from Cast. But

Capturing 'async let' variables is not supported

If you come up with better than explicit do/catch, please post it here!

do { return try await a }
catch { throw error as! AppError }