Skip to content

[pigeon][swift] Make PigeonError Class Conform to Sendable #160313

@bc-lee

Description

@bc-lee

Related Issues:

Use case

Sendable was introduced in Swift 5.5 to ensure thread-safe interactions in concurrent code. With the Swift 6 language mode in Swift 6.0 (Xcode 16), stricter conditions for Sendable conformance are enforced. Although newer Swift compiler can compile in Swift 5 mode, many developers prefer the stricter safety guarantees of Swift 6.

Pigeon, a code generator for Dart and native platform communication, enables seamless cross-platform development. However, the PigeonError class currently does not conform to Sendable, leading to compilation errors in Swift 6 mode and blocking developers from adopting the latest Swift concurrency features.

Example error:

Stored property 'details' of 'Sendable'-conforming class 'PigeonError' has non-sendable type 'Any?'

Proposal

The current PigeonError class is a simple implementation of the Error protocol:

/// Error class for passing custom error details to the Dart side.
final class PigeonError: Error {
  let code: String
  let message: String?
  let details: Any?

  init(code: String, message: String?, details: Any?) {
    self.code = code
    self.message = message
    self.details = details
  }

  var localizedDescription: String {
    return "PigeonError(code: \(code), message: \(message ?? "<nil>"), details: \(details ?? "<nil>"))"
  }
}

To make the class compatible with Swift 6.0 mode, I propose changing the details property type from Any? to Sendable?. This ensures the class conforms to Sendable since:

  • The Error protocol already conforms to Sendable.
  • All other properties (String and Optional) are Sendable.

This change introduces backward incompatibility because of the details type change. However, most existing use cases of PigeonError involve small, well-defined payloads, so migration should be straightforward. Developers can update their code by ensuring objects passed to details conform to Sendable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listc: proposalA detailed proposal for a change to Flutterp: pigeonrelated to pigeon messaging codegen toolpackageflutter/packages repository. See also p: labels.r: fixedIssue is closed as already fixed in a newer versionteam-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions