Skip to content

[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

Merged
merged 5 commits into from
Oct 5, 2023

Conversation

hborla
Copy link
Member

@hborla hborla commented Sep 27, 2023

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:

@MainActor
func requiresMainActor() -> Int { 0 }

@MainActor
func useMainActorDefault(value: Int = requiresMainActor()) { ... }

@MainActor
func mainActorCaller() {
  useMainActorDefault() // okay
}

func nonisolatedCaller() {
  useMainActorDefault() // error
}

Currently gated behind -enable-experimental-feature IsolatedDefaultArguments.

Resolves #58177

Copy link
Contributor

@ktoso ktoso left a 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;
Copy link
Contributor

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 🤔

Copy link
Member Author

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.

Copy link
Contributor

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()
Copy link
Contributor

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()
}
) {}
Copy link
Contributor

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

Copy link
Member Author

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.

Copy link
Contributor

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 🤔

@ktoso ktoso added the concurrency Feature: umbrella label for concurrency language features label Sep 29, 2023
@hborla
Copy link
Member Author

hborla commented Sep 29, 2023

@swift-ci please smoke test

@hborla hborla force-pushed the isolated-initializer-expressions branch 3 times, most recently from debc7d5 to 8ad3e04 Compare September 30, 2023 04:19
@hborla
Copy link
Member Author

hborla commented Sep 30, 2023

@swift-ci please smoke test

@hborla hborla force-pushed the isolated-initializer-expressions branch 3 times, most recently from c44147d to 43ade00 Compare October 1, 2023 02:10
@hborla
Copy link
Member Author

hborla commented Oct 1, 2023

@swift-ci please smoke test

@hborla hborla marked this pull request as ready for review October 1, 2023 02:11
@hborla
Copy link
Member Author

hborla commented Oct 1, 2023

@swift-ci please build toolchain

@hborla
Copy link
Member Author

hborla commented Oct 4, 2023

@swift-ci please smoke test

@hborla hborla force-pushed the isolated-initializer-expressions branch from a398602 to 7d39b93 Compare October 4, 2023 02:54
@hborla
Copy link
Member Author

hborla commented Oct 4, 2023

@swift-ci please smoke test

hborla added 5 commits October 4, 2023 13:12
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.
@hborla hborla force-pushed the isolated-initializer-expressions branch from 7d39b93 to 67e3326 Compare October 4, 2023 22:17
@hborla
Copy link
Member Author

hborla commented Oct 4, 2023

@swift-ci please smoke test

@hborla hborla merged commit f7a8bc2 into swiftlang:main Oct 5, 2023
@hborla hborla deleted the isolated-initializer-expressions branch October 5, 2023 03:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
concurrency Feature: umbrella label for concurrency language features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[SR-15916] @MainActor-isolated default arguments for @MainActor-isolated functions
3 participants