nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.9k stars 640 forks source link

@dynamicMemberLookup needs `self` to access members, clashes with redudantSelf. #864

Closed RabugenTom closed 3 years ago

RabugenTom commented 3 years ago

Hello,

In a struct annotated with @dynamicMemberLookup, we (apparently) can only access a dynamic member if we prefix it with self. If redudantSelf is enabled, self's are removed without considering this case, and the formatted code does not compile anymore.

@dynamicMemberLookup
struct Wrapper {

  struct Wrapped {
    var string: String = "A"
  }

  let wrapped = Wrapped()

  subscript<T>(dynamicMember keyPath: KeyPath< Wrapped, T>) -> T { 
    wrapped[keyPath: keyPath] 
  }

  func work() {
    if self.string == "A" {  // This compiles
      //…
    }
    if string == "A" {  // This is the formatted expression with redudantSelf enabled. Does not compile, on Xcode 12.5b1 at least
      //…
    }
  }

}
nicklockwood commented 3 years ago

@RabugenTom fixed in 0.47.12

kylebshr commented 3 years ago

This is still an issue for extensions of types with @dynamicMemberLookup. I created a new issue here: https://github.com/nicklockwood/SwiftFormat/issues/983