Skip to content

Commit 28d034d

Browse files
Revise SE-0423 based on review feedback. (#2385)
* Revise SE-0423 based on review feedback. * Kick off second review --------- Co-authored-by: Ben Cohen <[email protected]>
1 parent 1da6db3 commit 28d034d

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

proposals/0423-dynamic-actor-isolation.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* Proposal: [SE-0423](0423-dynamic-actor-isolation.md)
44
* Authors: [Holly Borla](https://github.com/hborla), [Pavel Yaskevich](https://github.com/xedin)
55
* Review Manager: [Ben Cohen](https://github.com/airspeedswift)
6-
* Status: **Active Review (February 20 - March 1, 2024)**
6+
* Status: **Active Review (April 9 - April 18, 2024)**
77
* Upcoming Feature Flag: `DynamicActorIsolation`
8-
* Review: ([pitch](https://forums.swift.org/t/pitch-dynamic-actor-isolation-enforcement/68354)) ([review](https://forums.swift.org/t/se-0423-dynamic-actor-isolation-enforcement-from-non-strict-concurrency-contexts/70155))
8+
* Review: ([pitch](https://forums.swift.org/t/pitch-dynamic-actor-isolation-enforcement/68354)) ([first review](https://forums.swift.org/t/se-0423-dynamic-actor-isolation-enforcement-from-non-strict-concurrency-contexts/70155)) ([second review](https://forums.swift.org/t/se-0423-second-review-dynamic-actor-isolation-enforcement-from-non-strict-concurrency-contexts/71159))
99
* Implementation: [apple/swift#70867](https://github.com/apple/swift/pull/70867), [apple/swift#71261](https://github.com/apple/swift/pull/71261), [apple/swift-syntax#2419](https://github.com/apple/swift-syntax/pull/2419)
1010

1111
## Introduction
@@ -107,8 +107,29 @@ This proposal adds dynamic actor isolation checking to:
107107
}
108108
```
109109

110+
- Call-sites of synchronous actor-isolated functions imported from Swift 6 libraries.
110111

111-
These are the most common circumstances when loosing actor isolation could be problematic and restricting runtime checking to them significantly limits negative performance impact of the new checks. The strategy of only emitting runtime checks when there’s potential for the function to be called from unchecked code is desirable, because it means the dynamic checks will be eliminated as more of the Swift ecosystem transitions to Swift 6.
112+
When importing a module that was compiled with the Swift 6 language mode into code that is not, it's possible to call actor-isolated functions from outside the actor using `@preconcurrency`. For example:
113+
114+
```swift
115+
// ModuleA built with -swift-version 6
116+
@MainActor public func onMain() { ... }
117+
118+
// ModuleB built with -swift-version 5 -strict-concurrency=minimal
119+
import ModuleA
120+
121+
@preconcurrency @MainActor func callOnMain() {
122+
onMain()
123+
}
124+
125+
func notIsolated() {
126+
callOnMain()
127+
}
128+
```
129+
130+
In the above code, `onMain` from ModuleA can be called from outside the main actor via a call to `notIsolated()`. To close this safety hole, a dynamic check is inserted at the call-site of `onMain()` when ModuleB is recompiled against ModuleA after ModuleA has migrated to the Swift 6 language mode.
131+
132+
These are the most common circumstances when losing actor isolation could be problematic and restricting runtime checking to them significantly limits negative performance impact of the new checks. The strategy of only emitting runtime checks when there’s potential for the function to be called from unchecked code is desirable, because it means the dynamic checks will be eliminated as more of the Swift ecosystem transitions to Swift 6.
112133

113134

114135
## Detailed design
@@ -123,6 +144,10 @@ Runtime checking for actor isolation is not necessary for `async` functions, bec
123144

124145
A `@preconcurrency` protocol conformance is scoped to the implementation of the protocol requirements in the conforming type. A `@preconcurrency` conformance can be written at the primary declartaion or in an extension, and witness checker diagnostics about actor isolation will be suppressed. Like other `@preconcurrency` annotations, if no diagnotsics are suppressed, a warning will be emitted at the `@preconcurrency` annotation stating that the annotation has no effect and it should be removed.
125146

147+
### Disabling dynamic actor isolation checking
148+
149+
The dynamic actor isolation checks can be disabled using the flag `-disable-dynamic-actor-isolation`. Disabling dynamic actor isolation is discouraged, but it may be necessary if code that you don't control violates actor isolation in a way that causes the program to crash, such as by passing a non-`Sendable` function argument outside of a main actor context. `-disable-dynamic-actor-isolation` is similar to the `-enforce-exclusivity=unchecked` flag, which was a tool provided when staging in dynamic memory exclusivity enforcement under the Swift 5 lanugage mode.
150+
126151
## Source compatibility
127152

128153
Dynamic actor isolation checking can introduce new runtime assertions for existing programs. Therefore, dynamic actor isolation is only performed for synchronous functions that are witnesses to an explicitly annotated `@preconcurrency` protocol conformance, or that are compiled under the Swift 6 language mode.
@@ -147,6 +172,12 @@ The current approach in this proposal has a very desirable property of eliminate
147172

148173
If adoption of this feature exposes a bug in existing binaries because actor isolated code from outside the actor, a `@preconcurrency(unsafe)` annotation (or similar) could be provided to downgrade assertion failures to warnings. However, it's not clear whether allowing a known data race exhibited at runtime is the right approach to solving such a problem.
149174

175+
## Revision history
176+
177+
* Changes from the first review
178+
* Insert dynamic checks at direct calls to synchronous actor-isolated functions imported from Swift 6 libraries.
179+
* Add a flag to disable all dynamic actor isolation checking.
180+
150181
## Acknowledgments
151182

152183
Thank you to Doug Gregor for implementing the existing dynamic actor isolation checking gated behind `-enable-actor-data-race-checks`.

0 commit comments

Comments
 (0)