Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Strange error with "Cannot form key path to main actor-isolated property …" in Swift concurrency

I have this code:

// empty struct for demo purpose.
struct DailyRewardItem {}

@MainActor
protocol Plugin {
  var dailyRewardItems: [DailyRewardItem] { get }
}

enum MagicItemType: CaseIterable {
  case shuffle
  case hammer
  case rocket
  case bomb
  
  @MainActor
  var dailyRewardItem: DailyRewardItem {
    return DailyRewardItem()
  }
}

class MyPlugin: Plugin {
  let dailyRewardItems = MagicItemType.allCases.map(\.dailyRewardItem)
}

I got this warning:

Cannot form key path to main actor-isolated property ‘dailyRewardItem’; this is an error in the Swift 6 language mode

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here MyPlugin is implicitly main actor because it extends Plugin. So MyPlugin::dailyRewardItems is also main actor. The keyPath is also in main actor. Not sure why this gives warning.

If I don’t use the keyPath, it is working fine without warning.

  let dailyRewardItems = MagicItemType.allCases.map { $0.dailyRewardItem }

I am using Xcode 16 beta 3, Swift 5 with "complete" concurrency checking.

>Solution :

This is a bug, and is fixed in this PR, merged on 2024-07-09, which is one day after the release of Xcode 15 beta 3. I’d imagine you won’t get this warning anymore in the next beta.

From the PR description:

This allows isolated key-path components to be used in stored property initializers when the stored property itself is isolated to the same global actor.


Other than using a closure, initialising the property in init also removes the warning in the current version:

init() {
    dailyRewardItems = MagicItemType.allCases.map(\.dailyRewardItem)
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading