Skip to content

Add 'Infer isolated conformances by default' proposal. - #3

Draft
simanerush wants to merge 2 commits into
mainfrom
infer-isolated-conformances-by-default
Draft

simanerush wants to merge 2 commits into
mainfrom
infer-isolated-conformances-by-default

Conversation

@simanerush

Copy link
Copy Markdown
Owner

No description provided.

@nteissler nteissler left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I paused at the Detailed Design section


## Summary of changes

This proposal brings isolated conformance inference, previously only enabled in

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest a reword since technically everything is "working"

Suggested change
`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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() { } 
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like it might be restating too much of 0470

Comment on lines +184 to +185
default isolation mode, would infer isolated conformances where possible, making
it easier to implement `nonisolated` protocol requirements with an

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this eventually be source breaking when it's turned on by default in the next language mode?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants