Skip to content

Commit a4d322f

Browse files
committed
[SE-0461] Add a section about region isolation rules.
1 parent a0447c6 commit a4d322f

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

proposals/0461-async-function-isolation.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,45 @@ func call(_ closure: () -> NotSendable) -> NotSendable {
705705
}
706706
```
707707

708+
### Region isolation rules
709+
710+
`@execution(caller)` functions have the same region isolation rules as
711+
synchronous `nonisolated` functions. When calling an `@execution(caller)`
712+
function, all non-`Sendable` parameter and result values are merged into
713+
the same region, but they are only merged into the caller's actor region if
714+
one of those non-`Sendable` values is already in the actor's region.
715+
716+
For example:
717+
718+
```swift
719+
class NotSendable {}
720+
721+
@execution(caller)
722+
nonisolated func identity<T>(_ t: T) async -> T {
723+
return t
724+
}
725+
726+
actor MyActor {
727+
func isolatedToSelf() async -> sending NotSendable {
728+
let ns = NotSendable()
729+
return await identity(ns)
730+
}
731+
}
732+
```
733+
734+
The above code is valid; the implementation of `identity` can't access the
735+
actor's state unless isolated state is passed in via one of the parameters.
736+
Note that this code would be invalid if `identity` accepted an isolated
737+
parameter, because the non-`Sendable` parameters and results would always be
738+
merged into the actor's region.
739+
740+
This proposal allows you to access `#isolation` in the implementation of an
741+
`@execution(caller)` function for the purpose of forwarding it along to a
742+
method that accepts an `isolated (any Actor)?`. This is still safe, because
743+
there's no way to access the actor's isolated state via the `Actor` protocol,
744+
and dynamic casting to a concrete actor type will not result in a value that
745+
the function is known to be isolated to.
746+
708747
### Executor switching
709748

710749
Async functions switch executors in the implementation when entering the
@@ -987,6 +1026,8 @@ The proposal was revised with the following changes after the first review:
9871026
* Removed the unconditional warning about nonisolated async functions that
9881027
don't explicitly specify `@execution(caller)` or `@concurrent`.
9891028
* Removed `noasync` from the `assumeIsolated` API family.
1029+
* Specified the region isolation rules for `@execution(caller)` functions [as
1030+
discussed in the first review][region-isolation].
9901031

9911032
The proposal was revised with the following changes after the pitch discussion:
9921033

@@ -1004,4 +1045,5 @@ The proposal was revised with the following changes after the pitch discussion:
10041045
[SE-0297]: /proposals/0297-concurrency-objc.md
10051046
[SE-0338]: /proposals/0338-clarify-execution-non-actor-async.md
10061047
[SE-0421]: /proposals/0421-generalize-async-sequence.md
1007-
[adoption-tooling]: https://forums.swift.org/t/pitch-adoption-tooling-for-upcoming-features/77936
1048+
[adoption-tooling]: https://forums.swift.org/t/pitch-adoption-tooling-for-upcoming-features/77936
1049+
[region-isolation]: https://forums.swift.org/t/se-0461-run-nonisolated-async-functions-on-the-callers-actor-by-default/77987/36

0 commit comments

Comments
 (0)