Skip to content

Commit 0d6cded

Browse files
committed
[SE-0461] Add a section about region isolation rules.
1 parent b996590 commit 0d6cded

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
@@ -704,6 +704,45 @@ func call(_ closure: () -> NotSendable) -> NotSendable {
704704
}
705705
```
706706

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

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

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

@@ -1003,4 +1044,5 @@ The proposal was revised with the following changes after the pitch discussion:
10031044
[SE-0297]: /proposals/0297-concurrency-objc.md
10041045
[SE-0338]: /proposals/0338-clarify-execution-non-actor-async.md
10051046
[SE-0421]: /proposals/0421-generalize-async-sequence.md
1006-
[adoption-tooling]: https://forums.swift.org/t/pitch-adoption-tooling-for-upcoming-features/77936
1047+
[adoption-tooling]: https://forums.swift.org/t/pitch-adoption-tooling-for-upcoming-features/77936
1048+
[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)