Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-15052] Improve diagnostics around async let + AsyncSequence #57379

Closed
typesanitizer opened this issue Aug 10, 2021 · 3 comments
Closed

[SR-15052] Improve diagnostics around async let + AsyncSequence #57379

typesanitizer opened this issue Aug 10, 2021 · 3 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself concurrency Feature: umbrella label for concurrency language features diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis

Comments

@typesanitizer
Copy link

Previous ID SR-15052
Radar rdar://problem/81758415
Original Reporter @typesanitizer
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Concurrency, DiagnosticsQoI, TypeChecker
Assignee @LucianoPAlmeida
Priority Medium

md5: 71b51e349065e8bfc7ac314c05c57831

Issue Description:

Based on https://bugs.swift.org/browse/SR-15049

func testAsyncSequence<Seq: AsyncSequence>(_ seq: Seq) async throws where Seq.Element == Int {
    async let result: Int = seq.reduce(0) { $0 + $1 }
}
tmp.swift:2:15: warning: immutable value 'result' was never used; consider replacing with '_' or removing it
    async let result: Int = seq.reduce(0) { $0 + $1 }
              ^~~~~~
              _
tmp.swift:2:29: error: call can throw, but it is executed in a non-throwing autoclosure
    async let result: Int = seq.reduce(0) { $0 + $1 }
                            ^
tmp.swift:2:29: note: call is to 'rethrows' function, but a conformance has a throwing witness
    async let result: Int = seq.reduce(0) { $0 + $1 }
                            ^

For this, I don't understand what autoclosure is being referred to. Even so, say I pay attention to the first warning and replace the name with '_'. This leads to

func testAsyncSequence<Seq: AsyncSequence>(_ seq: Seq) async throws where Seq.Element == Int {
    async let _: Int = seq.reduce(0) { $0 + $1 }
}
tmp.swift:2:24: error: call can throw but is not marked with 'try'
    async let _: Int = seq.reduce(0) { $0 + $1 }
                       ^~~~~~~~~~~~~~~~~~~~~~~~~
tmp.swift:2:24: note: call is to 'rethrows' function, but a conformance has a throwing witness
    async let _: Int = seq.reduce(0) { $0 + $1 }
                       ^
tmp.swift:2:24: error: expression is 'async' but is not marked with 'await'
    async let _: Int = seq.reduce(0) { $0 + $1 }
                       ^~~~~~~~~~~~~~~~~~~~~~~~~
                       await
tmp.swift:2:24: note: call is 'async'
    async let _: Int = seq.reduce(0) { $0 + $1 }
                       ^

It's not clear why async let requires explicit try or await on the RHS. After all, the following code compiles without errors and explicit try or await:

func f() async throws -> Int { 3 }
func g() async {
    async let x: Int = f()
}
@typesanitizer
Copy link
Author

@swift-ci create

@LucianoPAlmeida
Copy link
Collaborator

> It's not clear why async let requires explicit try or await on the RHS. After all, the following code compiles without errors and explicit try or await:

@typesanitizer I was taking a look at this and the example fails with the same diagnostics requiring explicit try and await. Also, the weird diagnostic about autoclosure seems to be fixed at main. Here is what I see:

//error: unexpected warning produced: immutable value 'result' was never used; consider replacing with '_' or removing it
    async let result: Int = seq.reduce(0) { $0 + $1 }
              ^
// error: unexpected warning produced: capture of 'seq' with non-sendable type 'Seq' in a `@Sendable` closure
    async let result: Int = seq.reduce(0) { $0 + $1 }
                            ^
// error: unexpected error produced: call can throw, but it is executed in a non-throwing autoclosure
    async let result: Int = seq.reduce(0) { $0 + $1 }
                            ^
// error: unexpected note produced: call is to 'rethrows' function, but a conformance has a throwing witness
    async let result: Int = seq.reduce(0) { $0 + $1 }

So to me this seems the correct diagnostic, but maybe I'm missing something?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@LucianoPAlmeida
Copy link
Collaborator

Fixed on main #41661

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself concurrency Feature: umbrella label for concurrency language features diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants