Skip to content
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

Wrong error: Class property 'X' isolated to global actor 'MainActor' can not be referenced from a non-isolated synchronous context #59484

Open
yyaroshevich opened this issue Jun 16, 2022 · 6 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@yyaroshevich
Copy link

yyaroshevich commented Jun 16, 2022

Describe the bug
UIApplication.willTerminateNotification should accessible in non-isolated synchronous context

To Reproduce
Steps to reproduce the behavior:

  1. Try to accessUIApplication.willTerminateNotification from an actor. Either from isolated or non isolated function
actor Test {
  nonisolated func subscribeApplicationNotifications() -> [Any] {
    let applicationWillTerminateObserver = NotificationCenter.default.addObserver(forName: UIApplication.willTerminateNotification,
                                                                          object: nil,
                                                                          queue: nil) { [weak self] notification in
      // do something
    }
    return [applicationWillTerminateObserver]
  }
}
  1. Observe compilation error:
Class property 'willTerminateNotification' isolated to global actor 'MainActor' can not be referenced from a non-isolated synchronous context

or

Class property 'willTerminateNotification' isolated to global actor 'MainActor' can not be referenced from actor 'Test' in a synchronous context

Expected behavior
Code is compiled

Screenshots

Environment (please complete the following information):

  • OS: MacOS 12.3.1 (21E258)
  • Xcode Version 13.4 (13F17a)

Additional context
Workaround for a problem would be:

private enum Constants {
  static let uiApplicationWillTerminate = UIApplication.willTerminateNotification
}

and then reference it when necessary as Constants.uiApplicationWillTerminate

@yyaroshevich yyaroshevich added the bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. label Jun 16, 2022
@yyaroshevich
Copy link
Author

yyaroshevich commented Sep 13, 2022

Workaround does not work with -warn-concurrency on XCode 14.0 / Swift 5.7

@theblixguy
Copy link
Collaborator

UIApplication is annotated with @MainActor so property access needs to be isolated to that.

@yyaroshevich
Copy link
Author

yyaroshevich commented Sep 14, 2022

But is that annotation correct? So basically it looks like access to static readonly (immutable?) member of class, which sound like safe thing to do from any thread (of course until there is no complex initializer inside with lazy initialization, but that's hard to believe for that simple property).

@theblixguy
Copy link
Collaborator

I am not sure, since it's a question about an Apple framework (UIKit) I'd recomend asking this question over at https://developer.apple.com/forums/ or sending a feedback via https://feedbackassistant.apple.com/

@NachoSoto
Copy link
Contributor

access to static readonly (immutable?) member of class, which sound like safe thing to do from any thread

This is still an issue on Swift 5.9

Having to call NotificationCenter.addObserver (which is Sendable) on @MainActor just because UIApplication is marked with MainActor makes nos ense.

@NachoSoto
Copy link
Contributor

access to static readonly (immutable?) member of class, which sound like safe thing to do from any thread

This is still an issue on Swift 5.9

Having to call NotificationCenter.addObserver (which is Sendable) on @MainActor just because UIApplication is marked with MainActor makes no sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

3 participants