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