Skip to content

[concurrency] type-checking global-actor-isolated references in ordinary functions #35048

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

Conversation

kavon
Copy link
Member

@kavon kavon commented Dec 11, 2020

Non-actor isolated synchronous functions were mistakenly allowed to call & reference global-actor isolated declarations:

actor class TestActor {}

@globalActor
struct SomeGlobalActor {
  static var shared: TestActor { TestActor() }
}

@SomeGlobalActor func syncGlobActorFn() { }

func ordinaryFunc() {
  syncGlobActorFn() // reference A
  let x = syncGlobActorFn // reference B
  x()
}

There are two invalid references to global-actor isolated declarations. There are various ways to fix such problems:

  1. marking ordinaryFunc as @asyncHandler
  2. marking ordinaryFunc as async
  3. making ordinaryFunc part of the global-actor that syncGlobActorFn belongs to.

We only suggest fix (3) if the incorrect global-actor reference is not part of an apply (i.e., reference B). For reference A, which directly applies the function, we suggest all 3 as possible fixes.

Thus, in terms of new fix-its: If the caller is not associated with an actor and is a synchronous function that refers to a global-actor isolated declaration, we will suggest the option of making that synchronous function part of the global-actor's context (fixing rdar://71698464). We do not suggest becoming part of the global-actor if the reference appears in an async context.

Resolves rdar://71548470&71698464

TODOs:

  • enhance regression test with fix-it checking (+ manually verify that they produce valid code)

@kavon kavon force-pushed the typechecking-unspecified-isolation-contexts-71548470 branch from edf2c06 to 00545b8 Compare December 12, 2020 02:03
@kavon
Copy link
Member Author

kavon commented Dec 12, 2020

@swift-ci please smoke test

@kavon kavon force-pushed the typechecking-unspecified-isolation-contexts-71548470 branch from 4fcc3a4 to 25c5400 Compare December 14, 2020 19:09
…l actor references

Non-actor isolated synchronous functions were previously
allowed to call & reference global-actor isolated declarations.
This patch puts a stop to that.

Resolves rdar://71548470
@kavon
Copy link
Member Author

kavon commented Dec 14, 2020

@swift-ci please smoke test

@kavon kavon force-pushed the typechecking-unspecified-isolation-contexts-71548470 branch from 25c5400 to 7828616 Compare December 14, 2020 22:29
@kavon kavon changed the title [concurrency] type-checking actor-isolated references in ordinary functions [concurrency] type-checking global-actor-isolated references in ordinary functions Dec 14, 2020
@kavon
Copy link
Member Author

kavon commented Dec 14, 2020

@swift-ci please smoke test

@kavon kavon marked this pull request as ready for review December 14, 2020 22:43
…er' and '@GlobalActorType'

Currently, we don't have a fix-it to insert 'async', so I've marked those places
as not expecting a fix-it, until someone goes and implements that (rdar://72313654)
@kavon kavon force-pushed the typechecking-unspecified-isolation-contexts-71548470 branch from 7828616 to e8dcc97 Compare December 14, 2020 23:22
@kavon
Copy link
Member Author

kavon commented Dec 14, 2020

@swift-ci please smoke test and merge

@swift-ci swift-ci merged commit 1eb810f into swiftlang:main Dec 15, 2020
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.

3 participants