gohanlon / swift-memberwise-init-macro

Swift Macro for enhanced automatic inits.
MIT License
122 stars 8 forks source link

Error with nested structs #30

Closed NachoSoto closed 2 months ago

NachoSoto commented 11 months ago

Description

Consider this code:

@MemberwiseInit
public struct A {
    let x: Int

    public struct B {}
}

extension A.B {}

Checklist

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 commented 11 months ago

Just realized this also happens with the @Observable macro.

gohanlon commented 11 months ago

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 commented 4 months ago

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

gohanlon commented 2 months ago

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