Skip to content

[Sema]: ban @isolated(any) conversions to synchronous function types #80812

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jamieQ
Copy link
Contributor

@jamieQ jamieQ commented Apr 15, 2025

currently, function conversions from @isolated(any) functions to synchronous, non-@isolated(any) functions are permitted, despite this being expressly stated as an invalid conversion in SE-0431. this change updates the constraint solver to disallow such conversions.

resolves #80823

relevant forum posts: one & two

@jamieQ
Copy link
Contributor Author

jamieQ commented Apr 15, 2025

@swift-ci please smoke test macos

@@ -2985,6 +2985,10 @@ ConstraintSystem::matchFunctionIsolations(FunctionType *func1,
case FunctionTypeIsolation::Kind::Erased:
return matchIfConversion();

// TODO: should this be handled here?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rejecting invalid conversions in the solver is generally better. For example if you have two overloads and one of the choices involves the invalid conversion, diagnosing the conversion after the solution is applied means the code will fail to type check with an ambiguity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! and just to clarify as i'm not very familiar with this area of the code – this file is part of the (constraint?) 'solver', correct?

i assume i should remove the logic from the actor isolation checker in favor of this then, since presumably there's no reason to duplicate the check in both places – or is there?

Copy link
Contributor Author

@jamieQ jamieQ Apr 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

although... the comment up above does give me some pause (i don't fully grasp what it's trying to communicate):

    // Erasing global-actor isolation to non-isolation can admit data
    // races; such violations are diagnosed by the actor isolation checker.
    // We deliberately do not allow actor isolation violations to influence
    // overload resolution to preserve the property that an expression can
    // be re-checked against a different isolation context for isolation
    // violations.
    //
    // This also applies to @isolated(any) because we want to be able to
    // decide that we contextually isolated to the function's dynamic
    // isolation.

edit: in this instance, perhaps the concerns there don't apply, since this issue is more about a conversion that should presumably always be invalid?

per SE-0431, function conversions from an @isolated(any) function to a synchronous,
non-@isolated(any) function type should not be allowed. updates the logic in the
constraint solver to enforce this.
@jamieQ jamieQ force-pushed the invalid-isolated-any-conversion branch from a45fb51 to 6711957 Compare April 16, 2025 01:01
@jamieQ
Copy link
Contributor Author

jamieQ commented Apr 16, 2025

@swift-ci please smoke test

@@ -2943,6 +2943,11 @@ ConstraintSystem::matchFunctionIsolations(FunctionType *func1,
ConstraintLocatorBuilder locator) {
auto isolation1 = func1->getIsolation(), isolation2 = func2->getIsolation();

// An @isolated(any) function cannot be converted to a non-@isolated(any)
// synchronous function regardless of whether or not it is itself async.
if (isolation1.isErased() && !isolation2.isErased() && !func2->isAsync())
Copy link
Contributor Author

@jamieQ jamieQ Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the early exit here presumably means some of the 'score increase' logic that used to occur no longer will. does that seem generally okay?

additionally, what's the appropriate way to handle possible source breakage from these conversions no longer working?

@jamieQ jamieQ changed the title [WIP][Sema]: ban @isolated(any) conversions to synchronous function types [Sema]: ban @isolated(any) conversions to synchronous function types Apr 16, 2025
@jamieQ jamieQ marked this pull request as ready for review April 16, 2025 22:35
@jamieQ jamieQ requested review from ktoso, hborla and xedin as code owners April 16, 2025 22:35
@jamieQ
Copy link
Contributor Author

jamieQ commented Apr 16, 2025

note: the proposal for Observed is currently relying on the existing behavior (see here) to be able to synchronously invoke an @isolated(any) function

@jamieQ
Copy link
Contributor Author

jamieQ commented Apr 17, 2025

@swift-ci please smoke test linux

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.

@isolated(any) closure can be assigned to non isolated closure property

None yet

2 participants