Skip to content

Sema: Make @concurrent not imply async on closures #83094

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 1 commit into from
Jul 17, 2025
Merged

Conversation

AnthonyLatsis
Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis commented Jul 16, 2025

The present approach is not prudent because @concurrent synchronous functions, a natural extension, are a likely-to-happen future direction, whereas the current inference rule is entirely grounded on @concurrent being exclusive to async functions.

If we were to ship this rule, we would have to keep the promise for backwards compatibility when implementing the aforementioned future direction, replacing one inconsistency with another, and possibly introducing new bug-prone expression checking code.

func foo(_: () -> Void) {}
func foo(_: () async -> Void) {}

// In a future without this change and  `@concurrent` synchronous
// functions accepted, the first call resolves to the first overload,
// and the second call resolves to the second, despite `@concurrent` no
// longer implying `async`.
foo { }
foo { @concurrent in }

This change also drops the fix-it for removing @concurrent when used on a synchronous closure. With the inference rule gone, and the diagnosis delayed until after solution application, this error raises a fairly balanced choice between removing the attribute and being explicit about the effect, where a unilateral suggestion is quite possibly more harmful than useful.

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test macOS

_ = { @concurrent () -> Int in }
// expected-error@-1:9 {{cannot use @concurrent on non-async closure}}{{none}}
_ = { @concurrent () async in }
_ = { @concurrent in await awaitMe() }
Copy link
Contributor

Choose a reason for hiding this comment

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

Presence of throws precludes effect inference from the body, might be good to add a test case where we have @concurrent + throws without async, to make sure that everything works as expected.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added.

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test

The present approach is not prudent because `@concurrent` synchronous
functions, a natural extension, are a likely-to-happen future direction,
whereas the current inference rule is entirely grounded on `@concurrent`
being exclusive to async functions.

If we were to ship this rule, we would have to keep the promise for
backwards compatibility when implementing the aforementioned future
direction, replacing one inconsistency with another, and possibly
introducing new bug-prone expression checking code.

```swift
func foo(_: () -> Void) {}
func foo(_: () async -> Void) {}

// In a future without this change and  `@concurrent` synchronous
// functions accepted, the first call resolves to the first overload,
// and the second call resolves to the second, despite `@concurrent` no
// longer implying `async`.
foo { }
foo { @Concurrent in }
```

This change also drops the fix-it for removing `@concurrent` when used
on a synchronous closure. With the inference rule gone, and the
diagnosis delayed until after solution application, this error raises a
fairly balanced choice between removing the attribute and being
explicit about the effect, where a unilateral suggestion is quite
possibly more harmful than useful.
@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please smoke test

@AnthonyLatsis
Copy link
Collaborator Author

@swift-ci please test source compatibility

@AnthonyLatsis AnthonyLatsis merged commit d7ec3a3 into main Jul 17, 2025
3 of 5 checks passed
@AnthonyLatsis AnthonyLatsis deleted the jepa-main branch July 17, 2025 00:54
@jamieQ
Copy link
Contributor

jamieQ commented Jul 17, 2025

@concurrent synchronous functions, a natural extension, are a likely-to-happen future direction

@AnthonyLatsis if you have a spare moment, i'm curious if you could share what your current mental model for such a construct is. what would an @concurrent synchronous function communicate about its behavior?

@AnthonyLatsis
Copy link
Collaborator Author

AnthonyLatsis commented Jul 17, 2025

Think of a synchronous actor-isolated function. Similarly, a @concurrent synchronous function must still run on the concurrent executor, and must be awaited if the call might cross a concurrency domain. Basically, the only difference to the async version is that, being synchronous, its body is not allowed to suspend.

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