Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting wrong type in conformingTo protocols in ExtensionMacro #2049

Closed
soumyamahunt opened this issue Aug 10, 2023 · 7 comments
Closed

Getting wrong type in conformingTo protocols in ExtensionMacro #2049

soumyamahunt opened this issue Aug 10, 2023 · 7 comments
Labels
bug Something isn't working

Comments

@soumyamahunt
Copy link
Contributor

Description

When declaring an extension macro that provides extensions for multiple protocols, i.e. Codable macro providing extension for Decodable and Encodable, by default protocol names are joined together and provided as a single type in the protocols array.

For each protocol conformance is provided, protocols array should have a separate type syntax item.

Steps to Reproduce

Xcode: Version 15.0 beta 6 (15A5219j)
Swift syntax version: 509.0.0-swift-DEVELOPMENT-SNAPSHOT-2023-08-07-a

Create an extension macro implementation that should print the protocols array in build log:

public struct CodableMacro: ExtensionMacro {
    public static func expansion(
        of node: AttributeSyntax,
        attachedTo declaration: some DeclGroupSyntax,
        providingExtensionsOf type: some TypeSyntaxProtocol,
        conformingTo protocols: [TypeSyntax],
        in context: some MacroExpansionContext
    ) throws -> [ExtensionDeclSyntax] {
        precondition(protocols.count == 0, "\(protocols)")
        return []
    }
}
@attached(extension, conformances: Decodable, Encodable)
public macro Codable() = #externalMacro(module: "ExtensionMacroMacros", type: "CodableMacro")

And attach this macro to a type:

@Codable
struct SomeCodable {
    let val: String
}

Inspect the protocols array in build log:

[IdentifierTypeSyntax
╰─name: identifier("DecodableEncodable")]

@soumyamahunt soumyamahunt added the bug Something isn't working label Aug 10, 2023
@ahoppen
Copy link
Contributor

ahoppen commented Aug 10, 2023

Tracked in Apple’s issue tracker as rdar://113668922

@ahoppen
Copy link
Contributor

ahoppen commented Aug 11, 2023

Thanks for reporting the issue @soumyamahunt. @hborla is fixing it in swiftlang/swift#67889.

@soumyamahunt
Copy link
Contributor Author

Thanks @ahoppen and @hborla for the quick fix, let me know once fix is available in latest snapshots. This is a major blocker for MetaCodable migrating to extension macros.

@ahoppen
Copy link
Contributor

ahoppen commented Aug 15, 2023

The August 12 main development snapshot should contain the fix.

@soumyamahunt
Copy link
Contributor Author

soumyamahunt commented Aug 16, 2023

The August 12 main development snapshot should contain the fix.

Thanks @ahoppen, with latest main snapshot this issue is fixed, MetaCodable is buildable now (although tests are failing due to #2031). Closing this issue.

@soumyamahunt
Copy link
Contributor Author

soumyamahunt commented Sep 19, 2023

@ahoppen @hborla getting this issue again with latest Xcode release and swift-syntax version:

Xcode: Version 15.0 (15A240d)
Swift syntax version: 509.0.0

IdentifierTypeSyntax
╰─name: identifier("DecodableEncodable")

@soumyamahunt soumyamahunt reopened this Sep 19, 2023
@ahoppen
Copy link
Contributor

ahoppen commented Sep 19, 2023

The fix hasn’t made it into Xcode 15.0, unfortunately. As this is fixed in the GitHub repo, the fix should be included in a future Xcode version.

@ahoppen ahoppen closed this as completed Sep 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants