PreferenceKey protocol conflicts with Swift Concurrency

I have been fixing my concurrency warnings since I turned on the complete concurrency checking flag.

One of the warnings is from preference key protocol:

private struct MyPreferenceKey: PreferenceKey {
    static var defaultValue: CGPoint = .zero 
    // warning: Static property 'defaultValue' is not concurrency-safe because it is non-isolated global shared mutable state; this is an error in Swift 6
 
    static func reduce(value: inout CGPoint, nextValue: () -> CGPoint) {}
}

Changing the var to let will fix the warning but it breaks the protocal conformance.
Is this something that Apple should fix or I should come up with some work around solutions?

Same questions goes to many other SwiftUI native types, such as CLLocation, lookAroundScene, GeometryProxy, etc.

Changing the var to let will fix the warning but it breaks
the protocal conformance.

It does? How?

It’s generally OK to satisfy a read-only property protocol constraint with a let, and your example builds when I check defaultValue from static var to static let. What am I missing here?

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

You are right. It should be OK to use let here.
I wasn't very sure about this so I asked ChatGPT and I got:

You're right that the defaultValue in the PreferenceKey protocol is declared as a var , and changing it to a let would indeed break the protocol conformance. The warning about concurrency is because static properties in Swift are shared across all threads, and mutable shared state can lead to race conditions.