@@ -705,6 +705,45 @@ func call(_ closure: () -> NotSendable) -> NotSendable {
705
705
}
706
706
```
707
707
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
+
708
747
### Executor switching
709
748
710
749
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:
987
1026
* Removed the unconditional warning about nonisolated async functions that
988
1027
don't explicitly specify ` @execution(caller) ` or ` @concurrent ` .
989
1028
* 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 ] .
990
1031
991
1032
The proposal was revised with the following changes after the pitch discussion:
992
1033
@@ -1004,4 +1045,5 @@ The proposal was revised with the following changes after the pitch discussion:
1004
1045
[ SE-0297 ] : /proposals/0297-concurrency-objc.md
1005
1046
[ SE-0338 ] : /proposals/0338-clarify-execution-non-actor-async.md
1006
1047
[ 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