swift-memberwise-init-macro icon indicating copy to clipboard operation
swift-memberwise-init-macro copied to clipboard

Error with nested structs

Open NachoSoto opened this issue 1 year ago • 3 comments

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 main branch 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

NachoSoto avatar Dec 12 '23 23:12 NachoSoto

Just realized this also happens with the @Observable macro.

NachoSoto avatar Dec 14 '23 00:12 NachoSoto

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.

gohanlon avatar Dec 14 '23 01:12 gohanlon

This appears fixed in Swift 6! I confirmed using Xcode 16.0 beta 2 (16A5171r).

gohanlon avatar Jul 06 '24 00:07 gohanlon

Closing, Swift 6 has been released with this fix. 🥳

gohanlon avatar Sep 30 '24 23:09 gohanlon