Skip to content

[5.10][Concurrency] Suppress diagnostics about non-Sendable async iterator arguments in implicit calls to next(). #70750

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

Merged

Conversation

hborla
Copy link
Member

@hborla hborla commented Jan 6, 2024

  • Explanation:

#67730 closed a hole in strict concurrency checking that failed to diagnose non-Sendable self arguments crossing isolation boundaries. When that hole was closed, the compiler started diagnosing code that iterates over an AsyncSequence type from an actor isolated context (including global actors):

@MainActor func iterate(over stream: AsyncStream<Int>) async {
  for await element in stream { ... } // warning: Passing argument of non-sendable type 'inout AsyncStream<Int>.Iterator' outside of main actor-isolated context may introduce data races
}

This is correct - nearly all AsyncIteratorProtocol types are not Sendable, and AsyncIteratorProtocol.next() is nonisolated async, so any call to next() from an actor-isolated context will pass a non-Sendable iterator value over an isolation boundary! There's a whole big pitch discussion in Swift evolution right now about fixing this properly by making next() polymorphic over actor isolation.

However, in Swift 5.10, there's nothing that programmers can do to to resolve this warning. So instead, this change applies the nonisolated(unsafe) opt out to the generated iterator variable during constraint generation to suppress the warning. Note that I cherry picked the remainder of the implementation of nonisolated(unsafe) (most of the implementation had already been cherry picked) and enabled it in order to use this opt out. The feature has been accepted in Swift Evolution and enabled on main, and programmers will surely need nonisolated(unsafe) in Swift 5.10 given the vast number of Sendable checking holes that have been closed in Swift 5.10, leading to more warnings just like this one.

@hborla hborla requested a review from a team as a code owner January 6, 2024 01:48
hborla added 2 commits January 5, 2024 17:49
…t come

from `nonisolated(unsafe)` declarations.

(cherry picked from commit 76ae065)
async for-in loops.

Async iterators are not `Sendable`; they're only meant to be used from
the isolation domain that creates them. But the `next()` method runs on
the generic executor, so calling it from an actor-isolated context passes
non-`Sendable` state across the isolation boundary. `next()` should
inherit the isolation of the caller, but for now, use the opt out.

(cherry picked from commit 0c6c369)
@hborla hborla force-pushed the 5.10-suppress-async-iterator-warning branch from ee04dcb to 2f52b65 Compare January 6, 2024 01:49
@hborla
Copy link
Member Author

hborla commented Jan 6, 2024

@swift-ci please test

@hborla
Copy link
Member Author

hborla commented Jan 6, 2024

/home/build-user/swift/test/Concurrency/experimental_feature_strictconcurrency.swift:1:1: error: unexpected error produced: new Swift parser generated errors for code that C++ parser accepted

Looks like I'm missing a swift-syntax cherry pick

@hborla
Copy link
Member Author

hborla commented Jan 6, 2024

swiftlang/swift-syntax#2417

@swift-ci please test

@hborla
Copy link
Member Author

hborla commented Jan 6, 2024

swiftlang/swift-syntax#2417

@swift-ci please test

@hborla hborla merged commit 9402455 into swiftlang:release/5.10 Jan 7, 2024
@hborla hborla deleted the 5.10-suppress-async-iterator-warning branch January 7, 2024 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants