Skip to content

Conversation

hborla
Copy link
Member

@hborla hborla commented Aug 19, 2025

This change introduces a section on global-actor isolated protocol conformances, which were introduced in SE-0470.

Fixes: rdar://148361008

@hborla hborla changed the title [SE-0470] Add a section on isolated conformances to the Concurrency chapter of the language guide. Add a section on isolated conformances to the Concurrency chapter of the language guide. [SE-0470] Aug 19, 2025
This change adds a section on global-actor isolated
protocol conformances to the Concurrency chapter of
the language guide.
@amartini51 amartini51 changed the title Add a section on isolated conformances to the Concurrency chapter of the language guide. [SE-0470] Add a section on isolated conformances to the guide [SE-0470] Aug 19, 2025
Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

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

Thank you for writing this!

amartini51 and others added 8 commits August 19, 2025 10:51
Use semantic line breaks for prose, title case for headings, four spaces
for Swift code indentation, and contractions.
Replace a few uses of "called" with "used" when
talking about using protocol requirements. Also
removed the subheadings in the Using Isolated
Conformances section.
Isolated conformance inference is gated behing the
InferIsolatedConformances upcoming feature. Add a dedicated
section for inference and a note about the upcoming
feature identifier per the guidance in Style.md.
Copy link
Contributor

@ktoso ktoso left a comment

Choose a reason for hiding this comment

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

Looking good! Had some clarification ideas you may want to take, commented inline

@hborla hborla force-pushed the se-0470 branch 2 times, most recently from 88db53c to d657461 Compare September 6, 2025 01:50
Copy link
Member

@amartini51 amartini51 left a comment

Choose a reason for hiding this comment

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

Thanks for writing all of this! I've marked a bunch of specific places throughout the diff — and some general comments below.

Let's expand the transitions and explanations. Some sections have a main point too far into the section, and we can elevate that to the introductory paragraph. Several of the code listings would benefit from a paragraph after them that takes the reader through what the code's doing in more detail.

Some subsections don't need as many headings, and headings don't need to be nested as deeply. (Although there's no issue with short sections when they're called for.) Let's try to fit more with the level and length of headings in the existing book. Some of the choices around sectioning in the existing content are based on what DocC handles well, some are based on editorial feedback, and some are just convention that we can vary. Here's an outline of this chapter:

  • Defining and Calling Asynchronous Functions
  • Asynchronous Sequences
  • Calling Asynchronous Functions in Parallel
  • Tasks and Task Groups
    • Task Cancellation
    • Unstructured Concurrency
  • Isolation
  • The Main Actor
  • Actors
  • Global Actors
  • Sendable Types
  • Isolated Protocol Conformances <——— NEW
    • Declaring an Isolated Conformance
    • Inferring an Isolated Conformance
    • Data-Race Safety for Isolated Conformances
      • Using Isolated Conformances
      • Restricting Isolated Conformances in Concurrent Code
        • Protocols That Require Sendable or SendableMetatype

Prefer active voice and present tense over passive voice and future tense. We'll catch this during the editorial pass too.

the conformance is only used on the actor
that the conformance is isolated to.

### Declaring an Isolated Conformance
Copy link
Member

Choose a reason for hiding this comment

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

You probably don't need a heading here after just one paragraph.


## Isolated Protocol Conformances

Protocols that are nonisolated
Copy link
Member

Choose a reason for hiding this comment

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

Let's either define "nonisolated protocol" if it's a new concept being introduced, or find a way to refer to normal protocol conformance that doesn't sound like a new term.

while ensuring that state is only accessed
from within the actor.

### Inferring an Isolated Conformance
Copy link
Member

Choose a reason for hiding this comment

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

The developer reading TSPL isn't doing the inference, the compiler is. Maybe "Inferred" or "Using Inferred" in the heading?

Comment on lines +1628 to +1631
This allows the implementation of the conformance
to use global actor isolated state
while ensuring that state is only accessed
from within the actor.
Copy link
Member

Choose a reason for hiding this comment

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

Let's walk through the code listing more step-by-step in its explanation paragraph. For example:

  • Why is Person marked @MainActor?
  • Call out that writing @MainActor Equatable is how you mark the conformance as actor-isolated

Expand "This allows" so the reader doesn't have to guess what "this" refers to. Here, probably "This isolated conformance allows"?

}

extension Person: @MainActor Equatable {
static func ==(lhs: Person, rhs: Person) -> Bool {
Copy link
Member

Choose a reason for hiding this comment

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

To match TSPL style elsewhere:

Suggested change
static func ==(lhs: Person, rhs: Person) -> Bool {
static func == (lhs: Person, rhs: Person) -> Bool {

Likewise below; marking it just here.

Comment on lines +1802 to +1804
In generic code,
a conformance requirement to `Sendable` or `SendableMetatype`
tells Swift that an instance or metatype value is safe to use concurrently.
Copy link
Member

Choose a reason for hiding this comment

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

This reads like a definition of Sendable but I think it's about the where D: Sendable clause in the code below? We can check terminology here too — I see "conformance requirement" only twice in TSPL, and in both places it refers to the requirements that a type must implement in order to conform to a protocol.

Comment on lines +1826 to +1829
The above code would admit data races
if the conformance to `Dancer` was isolated,
because the implementation of `perform`
may access global actor isolated state.
Copy link
Member

Choose a reason for hiding this comment

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

I'm having a hard time with this sentence, probably because of the irrealis mood on the verbs (would admit, was isolated, may access). Are you saying that the code above is safe, and then describing why by telling me about something that can't happen? Or are you describing a scenario where the code has possible data races?

func perform()
}

func performConcurrently<D: Dancer>(n: Int, for: D.Type) async where T: SendableMetatype {
Copy link
Member

Choose a reason for hiding this comment

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

I think the T is just a leftover from a previous version that had different type names?

Suggested change
func performConcurrently<D: Dancer>(n: Int, for: D.Type) async where T: SendableMetatype {
func performConcurrently<D: Dancer>(n: Int, for: D.Type) async where D: SendableMetatype {

Comment on lines +1903 to +1905
Note that `Sendable` requires `SendableMetatype`,
so an explicit conformance to `SendableMetatype` is only necessary
if the type is non-`Sendable`.
Copy link
Member

Choose a reason for hiding this comment

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

This information seems like it should come earlier.

let items = performConcurrently(n: 10, for: Ballerina.self) // Error
```

##### Protocols That Require `Sendable` or `SendableMetatype`
Copy link
Member

Choose a reason for hiding this comment

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

We can't use H5 or code voice in headings.

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.

5 participants