-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Concurrency] Allow default arguments to require actor isolation. #68794
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
[Concurrency] Allow default arguments to require actor isolation. #68794
Conversation
36b2a9e
to
b98cc28
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Had a question inline and maybe one additional case to cover with a test?
@@ -2123,6 +2129,94 @@ namespace { | |||
} | |||
} | |||
|
|||
bool refineRequiredIsolation(ActorIsolation refinedIsolation) { | |||
if (requiredIsolationLoc.isInvalid()) | |||
return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my education, because I've been bitten by checking Loc validity for logic before... Why are we returning early if that happens here? I think this can happen across modules, but the isolation is expected to be always in the same module here so that's why? Wanted to understand that bit to avoid missing handling similar cases 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll never compute the required isolation of a default argument or initializer expression across modules because we don't serialize expressions; we compute the required isolation in the original module and we need to serialize the required isolation to be checked at call-sites in other modules, so we should always have a source location here. However, I have not implemented nor tested the cross-module case yet 🙂
The reason I'm returning early here is because I'm using the validity of this source location to turn on the "required isolation" computation, so there's nothing to do here if the location isn't valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, so really detecting cross module here kindof... Thanks for explaining
// expected-error@+1 {{default argument cannot be both main actor-isolated and global actor 'SomeGlobalActor'-isolated}} | ||
closure: () -> Void = { | ||
_ = requiresMainActor() | ||
_ = requiresSomeGlobalActor() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's a pretty nice diagnostic for these, how it falls out of the inference here... nice
// expected-error@+1 {{call to main actor-isolated global function 'requiresMainActor()' in a synchronous actor-isolated context}} | ||
_ = requiresMainActor() | ||
} | ||
) {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it is covered implicitly, but maybe worth adding test for
extension A {
func closureWithIsolatedParam(
closure: () -> Void = { _ in
_ = requiresMainActor()
}
) {}
}
that's somewhat interesting... that would fail when called inside actor, but across could actually work...? Hop to main for param, then hop to invoke method...? I'm not sure what would happen today tbh, so may be worth the test-case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default arguments are always evaluated in the caller's isolation domain, so using a MainActor-isolated default argument to an actor-isolated method is conceptually "fine" - it only has implications on where you can use the default argument from. However, I'm considering an (artificial) restriction that the required isolation has to be either nonisolated
or the same isolation as the function itself because I'm not sure why you would want a default argument that has any other isolation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha that makes sense; I'm not sure about the artificial restriction, I guess things fall into place and can work without it 🤔
@swift-ci please smoke test |
debc7d5
to
8ad3e04
Compare
@swift-ci please smoke test |
c44147d
to
43ade00
Compare
@swift-ci please smoke test |
@swift-ci please build toolchain |
@swift-ci please smoke test |
a398602
to
7d39b93
Compare
@swift-ci please smoke test |
Type checking a default argument expression will compute the required actor isolation for evaluating that argument value synchronously. Actor isolation checking is deferred to the caller; it is an error to use a default argument from across isolation domains. Currently gated behind -enable-experimental-feature IsolatedDefaultArguments.
…d isolation for a default argument, and improve diagnostics for conflicting isolation.
properties to require actor isolation. Member initializer expressions are only used in a constructor with matching actor isolation. If the isolation prohibits the member initializer from being evaluated synchronously (or propagating required isolation through closure bodies), then the default value cannot be used and the member must be explicitly initialized in the constructor. Member initializer expressions are also used as default arguments for the memberwise initializer, and the same rules for default argument isolation apply.
…allers to go through the DefaultInitializerIsolation request.
7d39b93
to
67e3326
Compare
@swift-ci please smoke test |
Type checking a default argument expression will compute the required actor isolation for evaluating that argument value synchronously. Actor isolation checking is deferred to the caller; it is an error to use a default argument from across isolation domains.
This change enables code like the following:
Currently gated behind
-enable-experimental-feature IsolatedDefaultArguments
.Resolves #58177