New bug in 5.2 compiler

The below code compiles fine on Swift 5.1 compiler, however Swift 5.2 introduced a bug in the compiler. Now it fails to compile, with the error "Key path cannot refer to static member 'bar'"

Obviously 'bar' is not a static member.

struct Wrap<R, V> {
    var val: V 
    var wrappedValue: V {
        get { val }
        set { val = newValue }
    init(_ key: WritableKeyPath<R,V>, val: V) {
        self.val = val

struct Foo {
    @Wrap(\, val: 1) var bar: Int

I'm not sure exactly what is going on here, but bar is a static member of the type Foo, it has signature (Foo) -> Int. My guess is that the compiler is getting confused.

The following seems to work:

struct Foo {
    static let barKeyPath: WritableKeyPath<Foo, Int> = \.bar
    @Wrap(barKeyPath, val: 1) var bar: Int

Maybe this is something to do with the new diagnostic system?

1 Like

Yeah I noticed the same thing. It works fine if you declare the keypath in another variable like that.

I think you're probably right, it must be a bug in the new diagnostics system, which is to be expected I guess.

I'm hoping at some point property wrappers will automatically get the keypath passed in. It seems redundant to have to resort to doing it this way, especially since there's nothing to guarantee that you're passing in a keypath that matches the variable you're wrapping.

As for the idea that 'bar' is indeed a "static member," even though it's not a static variable, I defer to your knowledge, however I still think it's a confusingly worded error, since typically keypaths cannot refer to static variables.

This sounds like another regression in 5.2 :cry:. Would you mind filing a for this? The repro looks perfect.

Here you go: [SR-12530] Faulty compiler error when passing keypath to property wrapper in Swift 5.2 · Issue #54973 · apple/swift · GitHub

1 Like

Thanks very much!