swift: Strict Concurrency warning false positive: MainActor isolated KeyPath

Description

When declaring a property wrapper on a MainActor isolated type that takes a MainActor isolated KeyPath, the 5.10 compiler flags a concurrency warning: ‘Cannot form key path to main actor-isolated property ‘requestReview’; this is an error in Swift 6’

Reproduction

import StoreKit

@MainActor
struct MyView: View {
  // Cannot form key path to main actor-isolated property 'requestReview'; this is an error in Swift 6
  @Environment(\.requestReview) private var requestReview

  var body: some View {
    Text("Cannot form key path to main actor-isolated property")
  }
}

Expected behavior

No warning.

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4) Target: arm64-apple-macosx14.0

Additional information

First flagged here: https://forums.swift.org/t/swift-5-10-concurrency-and-xctest/69929/11

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 26
  • Comments: 16 (10 by maintainers)

Most upvoted comments

Xcode 16 still has this 🐞 swift-driver version: 1.109.2 Apple Swift version 6.0 (swiftlang-6.0.0.3.300 clang-1600.0.20.10) Target: arm64-apple-macosx14.0

Are there any workarounds available?

You should be able to work around this by initializing the property wrapper in a @MainActor-isolated initializer, like this

import SwiftUI
import StoreKit

@MainActor
struct MyView: View {
  @Environment private var requestReview: RequestReviewAction

  init() {
    self._requestReview = Environment(\.requestReview)
  }

  var body: some View { ... }
}

Sorry I missed this - if that happens again, please feel free to add the concurrency label yourself, after which it’ll get added to my queue of concurrency issues to montior/triage/fix.

@hborla I appreciate that, but as an external contributor I’m not allowed to add labels to issues I file unfortunately.

That is annoying! I didn’t realize that. It’s fine to tag me and I can triage the issue directly. I’m also looking to put a process in place to fill the gaps in issue triage, as I know this isn’t the first time an issue has been missed.

FYI.

@MainActor final class TestOnMainActor {
    var foo: Int
}

let x = \TestOnMainActor.foo

Fixed in Version 16.0 beta 6 (16A5230g)

Xcode 16.1 Beta 1 has the same Swift compiler as Xcode 16 Beta 5 (swiftlang-6.0.0.7.6 for both, which does not contain the fix).

Did this fix make it into Xcode 16 beta 4?

No, this fix is not included in Xcode 16 Beta 4. It is also not included in Beta 5.

@hborla I appreciate that, but as an external contributor I’m not allowed to add labels to issues I file unfortunately.