swift-memberwise-init-macro
swift-memberwise-init-macro copied to clipboard
Error with nested structs
Description
Consider this code:
@MemberwiseInit
public struct A {
let x: Int
public struct B {}
}
extension A.B {}
Checklist
- [X] If possible, I've reproduced the issue using the
mainbranch of this package. - [X] This issue hasn't been addressed in an existing GitHub issue or discussion.
Expected behavior
MemberwiseInit should create a correct init
Actual behavior
Circular reference resolving attached macro 'MemberwiseInit'
Circular reference
'B' is not a member type of struct 'API.A'
swift-memberwise-init-macro version information
main
Destination operating system
iOS 17.2
Xcode version information
Xcode 15.1
Swift Compiler version information
swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: arm64-apple-macosx14.0
Just realized this also happens with the @Observable macro.
Thank you for the report!
And yeah, this is a rather unfortunate bug in Swift: https://github.com/apple/swift/issues/66450. I run into this one a lot. Some circularity issues were addressed in Xcode 15.1, but the cases that I run into are still outstanding, and usually have the same shape as your example.
You can sometimes work around the bug by moving the extension to another file, which can make sense depending on the nature/necessity of the macro. For MemberwiseInit, I personally tend to fall back to a boilerplate init and a FIXME.
This appears fixed in Swift 6! I confirmed using Xcode 16.0 beta 2 (16A5171r).
Closing, Swift 6 has been released with this fix. 🥳