Repository navigation
Add 'Infer isolated conformances by default' proposal. - #3
simanerush wants to merge 2 commits into
Conversation
nteissler
left a comment
There was a problem hiding this comment.
I paused at the Detailed Design section
|
|
||
| ## Summary of changes | ||
|
|
||
| This proposal brings isolated conformance inference, previously only enabled in |
There was a problem hiding this comment.
What do you think of this rewording to leave out modules and put the focus on consistent application?
We propose applying isolated conformance inference consistently regardless of the compiler setting's default actor isolation.
There was a problem hiding this comment.
It might be worth a comment somewhere that all code examples are compiled under -default-isolation=nonisolated unless explicitly noted otherwise
|
|
||
| ## Motivation | ||
|
|
||
| Global-actor isolated conformances, introduced in |
There was a problem hiding this comment.
I think some history could motivate this change almost completely; assuming I'm getting the history right:
SE-0470 introduced inferred isolated conformances to Swift. SE-0470 made it possible for actor-isolated types to conform to nonisolated protocols; before SE-0470, it was impossible to conform, for example, a main actor isolated type to Equatable. To avoid breaking source, SE-0470 introduced a diversion in the language: isolated conformances were inferred automatically only under -default-isolation=MainActor, but not when -default-isolation=nonisolated.
| Global-actor isolated conformances, introduced in | ||
| [SE-0470](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0470-isolated-conformances.md), | ||
| are a useful feature for letting isolated types implement nonisolated protocol | ||
| requirements. However, it is cumbersome to use in default isolation mode set to |
There was a problem hiding this comment.
| requirements. However, it is cumbersome to use in default isolation mode set to | |
| requirements. However, it is cumbersome to use with default isolation mode set to |
| ``` | ||
|
|
||
| The programmer can resolve the above errors by marking the witnesses with | ||
| `nonisolated`, but that doesn't work once a witness needs anything isolated to |
There was a problem hiding this comment.
Suggest a reword since technically everything is "working"
| `nonisolated`, but that doesn't work once a witness needs anything isolated to | |
| `nonisolated`. However, in practice programmers are limited because they cannot access isolated state from the `nonisolated` context: |
| modules compiled in default isolation mode set to `MainActor`, to modules in | ||
| "nonisolated" default isolation mode. | ||
|
|
||
| ## Motivation |
There was a problem hiding this comment.
Another motivation is a consequence of the implementation of 0470. Under -default-isolation=MainActor and strict checking, this is ✅ (and an error under -default-isolation=nonisolated):
@globalActor
struct MyGlobalActor {
static let shared: Shared = .init()
actor Shared { }
}
protocol Dazzle {
func doThing()
}
@MyGlobalActor
class Razzle: /*@MyGlobalActor*/ Dazzle { // isolated conformance inferred, but nothing to do with MainActor
func doThing() { }
}There was a problem hiding this comment.
This is evident from the implementation, but helps make it clear that the behavior difference really shouldn't have anything to do with -default-isolation.
| } | ||
| ``` | ||
|
|
||
| While this behavior makes the `MainActor` default isolation mode usable, it also |
There was a problem hiding this comment.
This line through 176 seems like a digression that might be unnecessary. Is it correct to say that the inference behavior seen under -default-isolation=MainActor is a more ergonomic model that preserves progressive disclosure? And generally, a programmer shouldn't need to be familiar, or concern themselves with, isolated conformances until they're explicitly trying to use them.
| To expand on the above statement, the inference of isolated conformances in | ||
| `MainActor` modules, while invisible to the programmer, restricts how the | ||
| program can use such conformances. Using such a conformance outside the main | ||
| actor is an error: |
There was a problem hiding this comment.
This feels like it might be restating too much of 0470
| default isolation mode, would infer isolated conformances where possible, making | ||
| it easier to implement `nonisolated` protocol requirements with an |
There was a problem hiding this comment.
| default isolation mode, would infer isolated conformances where possible, making | |
| it easier to implement `nonisolated` protocol requirements with an | |
| default isolation mode, would infer isolated conformances where possible, reducing boilerplate | |
| and restoring progressive disclosure when conforming to `nonisolated` protocol requirements. |
|
|
||
| The above inference will take effect in a future language mode, while warning | ||
| programmers in the Swift 6 language mode, and in the Swift 5 language mode with | ||
| complete concurrency checking, about a potential misuse of the isolated |
There was a problem hiding this comment.
| complete concurrency checking, about a potential misuse of the isolated | |
| complete concurrency checking, warning about a potential misuse of the isolated |
|
|
||
| ## Source compatibility | ||
|
|
||
| This proposal is fully source compatible with existing code. |
There was a problem hiding this comment.
Won't this eventually be source breaking when it's turned on by default in the next language mode?
No description provided.