|
5 | 5 |
|
6 | 6 | ## Swift 6.0
|
7 | 7 |
|
| 8 | + |
| 9 | +* [SE-0423][]: |
| 10 | + You can now use `@preconcurrency` attribute to replace static actor isolation |
| 11 | + checking with dynamic checks for witnesses of synchronous nonisolated protocol |
| 12 | + requirements when the witness is isolated. This is common when Swift programs |
| 13 | + need to interoperate with frameworks written in C/C++/Objective-C whose |
| 14 | + implementations cannot participate in static data race safety. |
| 15 | + |
| 16 | + ```swift |
| 17 | + public protocol ViewDelegateProtocol { |
| 18 | + func respondToUIEvent() |
| 19 | + } |
| 20 | + ``` |
| 21 | + |
| 22 | + It's now possible for a `@MainActor`-isolated type to conform to |
| 23 | + `ViewDelegateProtocol` by marking conformance declaration as `@preconcurrency`: |
| 24 | + |
| 25 | + ```swift |
| 26 | + @MainActor |
| 27 | + class MyViewController: ViewDelegateProtocol { |
| 28 | + func respondToUIEvent() { |
| 29 | + // implementation... |
| 30 | + } |
| 31 | + } |
| 32 | + ``` |
| 33 | + |
| 34 | + The compiler would emit dynamic checks into the `respondToUIEvent()` witness |
| 35 | + to make sure that it's always executed in `@MainActor` isolated context. |
| 36 | + |
| 37 | + Additionally, the compiler would emit dynamic actor isolation checks for: |
| 38 | + |
| 39 | + - `@objc` thunks of synchronous actor-isolated members of classes. |
| 40 | + |
| 41 | + - Synchronous actor-isolated function values passed to APIs that |
| 42 | + erase actor isolation and haven't yet adopted strict concurrency checking. |
| 43 | + |
| 44 | + - Call-sites of synchronous actor-isolated functions imported from Swift 6 libraries. |
| 45 | + |
| 46 | + The dynamic actor isolation checks can be disabled using the flag |
| 47 | + `-disable-dynamic-actor-isolation`. |
| 48 | + |
| 49 | +* [SE-0418][]: |
| 50 | + |
| 51 | + The compiler would now automatically employ `Sendable` on functions |
| 52 | + and key path literal expressions that cannot capture non-Sendable values. |
| 53 | + |
| 54 | + This includes partially-applied and unapplied instance methods of `Sendable` |
| 55 | + types, as well as non-local functions. Additionally, it is now disallowed |
| 56 | + to utilize `@Sendable` on instance methods of non-Sendable types. |
| 57 | + |
| 58 | + Let's use the following type to illustrate the new inference rules: |
| 59 | + |
| 60 | + ```swift |
| 61 | + public struct User { |
| 62 | + var name: String |
| 63 | + |
| 64 | + func getAge() -> Int { ... } |
| 65 | + } |
| 66 | + ``` |
| 67 | + |
| 68 | + Key path `\User.name` would be inferred as `WritableKeyPath<User, String> & Sendable` |
| 69 | + because it doesn't capture any non-Sendable values. |
| 70 | + |
| 71 | + The same applies to keypath-as-function conversions: |
| 72 | + |
| 73 | + ```swift |
| 74 | + let _: @Sendable (User) -> String = \User.name // Ok |
| 75 | + ``` |
| 76 | + |
| 77 | + A function value produced by an un-applied reference to `getAge` |
| 78 | + would be marked as `@Sendable` because `User` is a `Sendable` struct: |
| 79 | + |
| 80 | + ```swift |
| 81 | + let _ = User.getAge // Inferred as `@Sendable (User) -> @Sendable () -> Int` |
| 82 | + |
| 83 | + let user = User(...) |
| 84 | + user.getAge // Inferred as `@Sendable () -> Int` |
| 85 | + ``` |
| 86 | + |
8 | 87 | * [SE-0430][]:
|
9 | 88 |
|
10 | 89 | Region Based Isolation is now extended to enable the application of an
|
@@ -10333,6 +10412,8 @@ using the `.dynamicType` member to retrieve the type of an expression should mig
|
10333 | 10412 | [SE-0427]: https://github.com/apple/swift-evolution/blob/main/proposals/0427-noncopyable-generics.md
|
10334 | 10413 | [SE-0414]: https://github.com/apple/swift-evolution/blob/main/proposals/0414-region-based-isolation.md
|
10335 | 10414 | [SE-0430]: https://github.com/apple/swift-evolution/blob/main/proposals/0430-transferring-parameters-and-results.md
|
| 10415 | +[SE-0418]: https://github.com/apple/swift-evolution/blob/main/proposals/0418-inferring-sendable-for-methods.md |
| 10416 | +[SE-0423]: https://github.com/apple/swift-evolution/blob/main/proposals/0423-dynamic-actor-isolation.md |
10336 | 10417 | [#64927]: <https://github.com/apple/swift/issues/64927>
|
10337 | 10418 | [#42697]: <https://github.com/apple/swift/issues/42697>
|
10338 | 10419 | [#42728]: <https://github.com/apple/swift/issues/42728>
|
|
0 commit comments