Hi! I'm seeing some strange behavior in Xcode from trying to inline an attached macro. This is from using the Refactor > Inline Macro option.
One easy way to repro this behavior is to create a new Xcode Project App using SwiftData for Storage. Just find any @Query
declaration from the demo project and try to select Refactor > Inline Macro. My demo project starts with the following declaration:
@Query private var items: [Item]
Then, select @Query
and select Refactor > Inline Macro:
private var items
private (set) var _items: SwiftData.Query<[Item].Element, [Item]> = .init()
: [Item]{
get {
_items.wrappedValue
}
}
As you can see: there are two errors with this expansion:
private var items // Type annotation missing in pattern
and
: [Item]{ // Expected declaration
FWIW, I'm not sure this is a problem specific to SwiftData
or Query
. I created a small demo branch from swift-syntax
to repro on a new macro.[1]
The demo branch defines a PropertyWrapper
and Wrapper
macro (that map to the same implementation. The playground defines a new type:
struct Name {
@Wrapper var first: String = ""
@PropertyWrapper var last: String = ""
}
Selecting both wrappers and trying to Refactor > Inline Macro leads to the same code we saw from SwiftData:
struct Name {
var first
private var _first = PropertyWrapper<String>(storage: "")
: String {
get {
_first.wrappedValue
}
set {
_first.wrappedValue = newValue
}
}
var last
private var _last = PropertyWrapper<String>(storage: "")
: String {
get {
_last.wrappedValue
}
set {
_last.wrappedValue = newValue
}
}
}
Any idea what could be happening here? I don't seem to see other examples of Refactor > Inline Macro leading to broken code. Does this look like something that can be fixed in the swift-syntax
infra? Does this look like something that indicates the macro developer misused the infra (and the infra is behaving as expected)? Any ideas about how else this could be fixed? Thanks!