Skip to content

Commit f9000ff

Browse files
authored
[SE-0461] Proposal clarifications based on early review discussion. (#2703)
* [SE-0461] Proposal clarifications based on early review discussion. * [SE-0461] Fix the vision document link.
1 parent 24b8954 commit f9000ff

File tree

1 file changed

+45
-6
lines changed

1 file changed

+45
-6
lines changed

proposals/0461-async-function-isolation.md

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Authors: [Holly Borla](https://github.com/hborla), [John McCall](https://github.com/rjmccall)
55
* Review Manager: [Xiaodi Wu](https://github.com/xwu)
66
* Status: **Active review (February 20...March 2, 2025)**
7-
* Vision: [[Prospective Vision] Improving the approachability of data-race safety](https://forums.swift.org/t/prospective-vision-improving-the-approachability-of-data-race-safety/76183)
7+
* Vision: [Improving the approachability of data-race safety](/visions/approachable-concurrency.md)
88
* Implementation: On `main` behind `-enable-experimental-feature NonIsolatedAsyncInheritsIsolationFromContext`
99
* Upcoming Feature Flag: `AsyncCallerExecution`
1010
* Previous Proposal: [SE-0338](0338-clarify-execution-non-actor-async.md)
@@ -41,6 +41,8 @@ async function always switches off of an actor to run.
4141
- [Alternatives considered](#alternatives-considered)
4242
- [Changing isolation inference behavior to implicitly capture isolated parameters](#changing-isolation-inference-behavior-to-implicitly-capture-isolated-parameters)
4343
- [Use `nonisolated` instead of a separate `@execution(concurrent)` attribute](#use-nonisolated-instead-of-a-separate-executionconcurrent-attribute)
44+
- [Use "isolation" terminology instead of "execution"](#use-isolation-terminology-instead-of-execution)
45+
- [Deprecate `nonisolated`](#deprecate-nonisolated)
4446
- [Don't introduce a type attribute for `@execution`](#dont-introduce-a-type-attribute-for-execution)
4547
- [Revisions](#revisions)
4648

@@ -241,7 +243,7 @@ The sections below will explicitly use `@execution(concurrent)` and
241243
independent of upcoming features or language modes. However, note that the
242244
end state under the `AsyncCallerExecution` upcoming feature will mean that
243245
`@execution(caller)` is not necessary to explicitly write, and
244-
`@execution(caller)` will likely be used sparingly because it has far
246+
`@execution(concurrent)` will likely be used sparingly because it has far
245247
stricter data-race safety requirements.
246248

247249
### The `@execution` attribute
@@ -259,8 +261,26 @@ in the following sections.
259261
Only (implicitly or explicitly) `nonisolated` functions can be marked with the
260262
`@execution` attribute; it is an error to use the `@execution` attribute with
261263
an isolation other than `nonisolated`, including global actors, isolated
262-
parameters, and `@isolated(any)`. The `@execution` attribute can be used
263-
together with `@Sendable` or `sending`.
264+
parameters, and `@isolated(any)`:
265+
266+
```swift
267+
actor MyActor {
268+
var value = 0
269+
270+
// error: '@execution(caller)' can only be used with 'nonisolated' methods
271+
@execution(caller)
272+
func isolatedToSelf() async {
273+
value += 1
274+
}
275+
276+
@execution(caller)
277+
nonisolated func canRunAnywhere() async {
278+
// cannot access 'value' or other actor-isolated state
279+
}
280+
}
281+
```
282+
283+
The `@execution` attribute can be used together with `@Sendable` or `sending`.
264284

265285
The `@execution` attribute is preserved in the type system so that the execution
266286
semantics can be distinguished for function vales.
@@ -892,12 +912,15 @@ reasons:
892912

893913
### Use "isolation" terminology instead of "execution"
894914

895-
One other possibility is to use isolation terminology instead of `@execution`
896-
for the syntax. This direction does not accomplish goal 1. in the previous
915+
Another possibility is to use isolation terminology instead of `@execution`
916+
for the syntax. This direction does not accomplish the goal of having a
897917
section to have a consistent meaning for `nonisolated` across synchronous and
898918
async functions. If the attribute were spelled `@isolated(caller)` and
899919
`@isolated(concurrent)`, presumably that attribute would not work together with
900920
`nonisolated`; it would instead be an alternative kind of actor isolation.
921+
`@isolated(concurrent)` also doesn't make much sense because the concurrent
922+
executor does not provide isolation at all - isolation is only provided by
923+
actors and tasks.
901924

902925
Having `@execution(caller)` as an attribute that is used together with
903926
`nonisolated` leads to a simpler programming model because after the upcoming
@@ -907,6 +930,22 @@ functions. If we choose a different form of isolation like `@isolated(caller)`,
907930
programmers have to learn a separate syntax for `async` functions that
908931
accomplishes the same effect as a `nonisolated` synchronous function.
909932

933+
### Deprecate `nonisolated`
934+
935+
Going in the oppose direction, this proposal could effectively deprecate
936+
`nonisolated` and allow you to use `@execution(caller)` everywhere that
937+
`nonisolated` is currently supported, including synchronous methods, stored
938+
properties, type declarations, and extensions. This direction was not chosen
939+
for the following reasons:
940+
941+
1. This would lead to much more code churn than the current proposal. Part of
942+
the goal of this proposal is to minimize the change to only what is absolutely
943+
necessary to solve the major usability problem with async functions on
944+
non-`Sendable` types, because it's painful both to transition code and to
945+
re-learn parts of the model that have already been internalized.
946+
2. `nonisolated` is nicer to write than `@execution(caller)`,
947+
`@isolated(caller)`, or any other alternative attribute + argument syntax.
948+
910949
### Don't introduce a type attribute for `@execution`
911950

912951
There are a lot of existing type attributes for concurrency and it's

0 commit comments

Comments
 (0)