Skip to content

[Concurrency] Compute the source of actor isolation in ActorIsolationRequest. #75668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2024

Conversation

hborla
Copy link
Member

@hborla hborla commented Aug 3, 2024

Every time I see a new project failure that says expression is ‘async’ but not marked with ‘await’ or other actor isolation error, and the callee is not async or explicitly annotated with a global actor attribute, I go on a hunt for where the isolation may have been inferred from. To save time searching for the source of actor isolation inference, this change records the source of actor isolation in ActorIsolationRequest.

For example:

@MainActor
protocol P {}

struct S: P {
  func f() {}
}

func call(s: S) {
  s.f()
}

The above code now produces the following diagnostics:

 error: call to main actor-isolated instance method 'f()' in a synchronous nonisolated context
 3 | 
 4 | struct S: P {
 5 |   func f() {}
   |        |- note: calls to instance method 'f()' from outside of its actor context are implicitly asynchronous
   |        `- note: main actor isolation inferred from conformance to protocol 'P'
 6 | }
 7 | 
 8 | func call(s: S) {
   |      `- note: add '@MainActor' to make global function 'call(s:)' part of global actor 'MainActor'
 9 |   s.f()
   |     `- error: call to main actor-isolated instance method 'f()' in a synchronous nonisolated context
10 | }
11 | 

The note main actor isolation inferred from conformance to protocol 'P' is new with this change.

This information is currently only used as an attached note to one error produced by the actor isolation checker, but this should be used in many more places. The source of actor isolation inference could also potentially be surfaced by SourceKit requests in the future.

Resolves: rdar://119905790

@hborla
Copy link
Member Author

hborla commented Aug 3, 2024

@swift-ci please smoke test

@hborla hborla force-pushed the actor-isolation-inference-source branch from 189e58f to 5fa35b5 Compare August 5, 2024 01:49
@hborla hborla marked this pull request as ready for review August 5, 2024 01:53
@hborla
Copy link
Member Author

hborla commented Aug 5, 2024

@swift-ci please smoke test

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.

LGTM, very nice :)

@hborla hborla merged commit b24b088 into swiftlang:main Aug 5, 2024
3 checks passed
@hborla hborla deleted the actor-isolation-inference-source branch August 5, 2024 15:01
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