Skip to content

[SE-0466] Disable @MainActor inference when type conforms to a SendableMetatype protocol #2894

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

DougGregor
Copy link
Member

Libraries need a way to communicate that certain protocols can't reasonably be used with global-actor-isolated types. When the primary definition of a type conforms to such a protocol, it will disable @MainActor inference for that type. This has the effect of keeping more types nonisolated when there's signal that they should not be on the main actor. There are some protocols like this in the standard library (e.g., CodingKey, which was hardcoded to be nonisolated in the synthesis code), where the following code is ill-formed in with main-actor isolation by default:

struct S: Codable {
  var a: Int

  // error: CodingKeys inferred to `@MainActor`, which makes the conformance
  // to CodingKey main-actor-isolated and then fails (because the requirements
  // need to be nonisolated).
  enum CodingKeys: CodingKey {
    case a
  }
}

With this amendment, the conformance to CodingKey (which inherits from Sendable), prevents CodingKeys from being inferred to be@MainActor.

…ableMetatype protocol

Libraries need a way to communicate that certain protocols can't
reasonably be used with global-actor-isolated types. When the primary
definition of a type conforms to such a protocol, it will disable
`@MainActor` inference for that type. This has the effect of keeping
more types `nonisolated` when there's signal that they should not be
on the main actor. There are some protocols like this in the standard
library (e.g., CodingKey, which was hardcoded to be nonisolated in the
synthesis code), where the following code is ill-formed in with
main-actor isolation by default:

    struct S: Codable {
      var a: Int

      // error: CodingKeys inferred to `@MainActor`, which makes the conformance
      // to CodingKey main-actor-isolated and then fails (because the requirements
      // need to be nonisolated).
      enum CodingKeys: CodingKey {
        case a
      }
    }

With this amendment, the conformance to `CodingKey` (which inherits from
`Sendable`), prevents `CodingKeys` from being inferred to be`@MainActor`.
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