Skip to content

[concurrency] Ensure that we treat closures that are nonisolated(nonsending) via their ActorIsolation as nonisolated(nonsending). #81338

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

Conversation

gottesmm
Copy link
Contributor

@gottesmm gottesmm commented May 6, 2025

Some notes:

  1. In most cases, I think we were getting lucky with this by just inferring the closure's isolation from its decl context. In the specific case that we were looking at here, this was not true since we are returning from an @Concurrent async function a nonisolated(nonsending) method that closes over self. This occurs since even when NonisolatedNonsendingByDefault we want to start importing objc async functions as nonisolated(nonsending).

  2. I also discovered that in the ActorIsolationChecker we were not visiting the inner autoclosure meaning that we never set the ActorIsolation field on the closure. After some discussion with @xedin about potentially visiting the function in the ActorIsolationChecker, we came to the conclusion that this was likely to result in source stability changes. So we put in a targeted fix just for autoclosures in this specific case by setting their actor isolation in the type checker. This ensures that when we import objc async functions we do the correct thing. We still will not do the correct thing if we are able to create an autoclosure like this that is main actor isolated.

  3. Beyond adding tests to objc_async_from_swift to make sure that when NonisolatedNonsendingByDefault is disabled we do the right thing, I noticed that we did not have any tests that actually tested the behavior around objc_async_from_swift when NonisolatedNonsendingByDefault is enabled. So I added the relevant test lines so we can be sure that we get correct behavior in such a case.

rdar://150209093

@gottesmm gottesmm requested review from jckarter, hborla and xedin as code owners May 6, 2025 17:18
@gottesmm
Copy link
Contributor Author

gottesmm commented May 6, 2025

@swift-ci smoke test

@gottesmm gottesmm force-pushed the pr-e2235199764eea9659e08932699c74d60cf387b7 branch from 9d050f6 to 252bba9 Compare May 6, 2025 18:49
@gottesmm
Copy link
Contributor Author

gottesmm commented May 6, 2025

@swift-ci smoke test

…ending) via their ActorIsolation as nonisolated(nonsending).

Some notes:

1. In most cases, I think we were getting lucky with this by just inferring the
closure's isolation from its decl context. In the specific case that we were
looking at here, this was not true since we are returning from an @Concurrent
async function a nonisolated(nonsending) method that closes over self. This
occurs since even when NonisolatedNonsendingByDefault we want to start importing
objc async functions as nonisolated(nonsending).

2. I also discovered that in the ActorIsolationChecker we were not visiting the
inner autoclosure meaning that we never set the ActorIsolation field on the
closure. After some discussion with @xedin about potentially visiting the
function in the ActorIsolationChecker, we came to the conclusion that this was
likely to result in source stability changes. So we put in a targeted fix just
for autoclosures in this specific case by setting their actor isolation in the
type checker.

3. Beyond adding tests to objc_async_from_swift to make sure that when
NonisolatedNonsendingByDefault is disabled we do the right thing, I noticed that
we did not have any tests that actually tested the behavior around
objc_async_from_swift when NonisolatedNonsendingByDefault is enabled. So I added
the relevant test lines so we can be sure that we get correct behavior in such a
case.

rdar://150209093
@gottesmm gottesmm force-pushed the pr-e2235199764eea9659e08932699c74d60cf387b7 branch from 252bba9 to ced96aa Compare May 6, 2025 21:15
@gottesmm
Copy link
Contributor Author

gottesmm commented May 6, 2025

@swift-ci smoke test

@gottesmm gottesmm enabled auto-merge May 6, 2025 21:15
@gottesmm gottesmm merged commit c66d6c7 into swiftlang:main May 7, 2025
3 checks passed
@gottesmm gottesmm deleted the pr-e2235199764eea9659e08932699c74d60cf387b7 branch May 7, 2025 05:00
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.

2 participants