Skip to content

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
@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

2 participants