Xcode Refactor > Inline Macro produces broken code for some attached macros?

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!


  1. property wrapper · vanvoorden/swift-syntax@3c2a488 · GitHub ↩︎

I believe this has already been fixed by [5.10][Macros] Adjust insertion position of peer macros for var decls by rintaro · Pull Request #69370 · apple/swift · GitHub, which should be in Xcode 15.3 betas.

1 Like

@ahoppen Ahh… interesting! I built from Xcode 15.3 and the 5.10 toolchain and this is fixed. Sorry for the confusion!