Skip to content

Default main actor prevents synthesized struct initializer from being seen #82168

Open
@stephencelis

Description

@stephencelis

Description

I've been exploring default main actor implications on some of our packages and have hit a peculiar one.

Reproduction

nonisolated protocol P {
  associatedtype AT
  static var at: AT { get }
}

nonisolated struct KP<R: P, V> {
  init(keyPath: KeyPath<R, V>) {}
}

struct S: P {
  let p: Int
  struct AT {
    let kp = KP(keyPath: \S.p)
  }
  static let at = AT()  // 🛑
}

'S.AT' cannot be constructed because it has no accessible initializers

Workaround: Add an explicit init() {} to the nested AT struct.

Expected behavior

I expect it to compile, since it does outside of default main actor mode:

nonisolated protocol P {
  associatedtype AT
  static var at: AT { get }
}

nonisolated struct KP<R: P, V> {
  init(keyPath: KeyPath<R, V>) {}
}

@MainActor
struct S: @MainActor P {
  let p: Int
  @MainActor
  struct AT {
    let kp = KP(keyPath: \S.p)
  }
  static let at = AT()
}

Environment

Apple Swift version 6.2-dev (LLVM 9ae4b59a6edd27c, Swift 9b6c04e)
Target: arm64-apple-macosx15.0
Build config: +assertions

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions