Skip to content

[SE-0407] [Macros] Provide member macros with information about "missing" conformances #67758

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

Merged
merged 1 commit into from
Sep 7, 2023

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Aug 4, 2023

Provide member macros with similar information about conformances to what extension macros receive, allowing member macros to document which conformances they care about (e.g., Decodable) and then receiving the list of conformances that aren't already available for the type in question. For example, a macro such as

@attached(member, conformances: Decodable, Encodable, names: named(init(from:), encode(to:)))
macro Codable() = ...

Expanded on a type that is not already Decodable/Encodable would be provided with Decodable and Encodable (via the new missingConformancesTo: argument to the macro implementation) when the type itself does not conform to those types.

Member macros still cannot produce conformances, so this is likely to be used in conjunction with extension macros most of the time. The extension macro declares the conformance, and can also declare any members that shouldn't be part of the primary type definition---such as initializers that shouldn't suppress the memberwise initializer. On the other hand, the member macro will need to define any members that must be in the primary definition, such as required initializers, members that must be overridable by subclasses, and stored properties.

Codable synthesis is an example that benefits from member macros with conformances, because for classes it wants to introduce a required initializer for decoding and an overridable encode operation, and these must be members of the nominal type itself. Specifically, the Codable macro above is likely to have two attached member roles:

@attached(member, conformances: Decodable, Encodable, names: named(init(from:), encode(to:)))
@attached(extension, conformances: Decodable, Encodable, names: named(init(from:), encode(to:)))
macro Codable() = ...

where the "extension" role is responsible for defining the conformance (always), and the "member" creates the appropriate members for classes (init vs. required init).

Tracked by rdar://112532829.

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2004

@swift-ci please smoke test

@DougGregor DougGregor force-pushed the member-macro-conformances branch from afe7a69 to 24f8cec Compare August 5, 2023 00:04
@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2004

@swift-ci please smoke test

@DougGregor DougGregor force-pushed the member-macro-conformances branch from 24f8cec to 5084caa Compare August 5, 2023 05:58
@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2004

@swift-ci please smoke test

@DougGregor DougGregor force-pushed the member-macro-conformances branch from 5084caa to 6b1d437 Compare August 6, 2023 05:41
@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2004

@swift-ci please smoke test

@xedin xedin removed their request for review August 8, 2023 07:46
@DougGregor
Copy link
Member Author

@swift-ci please smoke test macOS

@DougGregor DougGregor force-pushed the member-macro-conformances branch from 6b1d437 to 7b09944 Compare August 19, 2023 06:07
@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2004

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2004

@swift-ci please build toolchain macOS

@DougGregor DougGregor force-pushed the member-macro-conformances branch from 7b09944 to f34dda5 Compare August 20, 2023 16:26
@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2004

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2004

@swift-ci please build toolchain macOS

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2004

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2004

@swift-ci please build toolchain macOS

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2004

@swift-ci please smoke test

@DougGregor DougGregor force-pushed the member-macro-conformances branch from f4a6556 to 95001a3 Compare September 7, 2023 06:34
@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2004

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

Windows failure is unrelated; kicking this off again.

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2004

@swift-ci please smoke test Windows

…ormances

Provide member macros with similar information about conformances to
what extension macros receive, allowing member macros to document
which conformances they care about (e.g., Decodable) and then
receiving the list of conformances that aren't already available for
the type in question. For example, a macro such as

    @attached(member, conformances: Decodable, Encodable, names:
named(init(from:), encode(to:)))
    macro Codable() = ...

Expanded on a type that is not already Decodable/Encodable would be
provided with Decodable and Encodable (via the new
`missingConformancesTo:` argument to the macro implementation) when
the type itself does not conform to those types.

Member macros still cannot produce conformances, so this is likely to
be used in conjunction with extension macros most of the time. The
extension macro declares the conformance, and can also declare any
members that shouldn't be part of the primary type definition---such
as initializers that shouldn't suppress the memberwise initializer. On
the other hand, the member macro will need to define any members that
must be in the primary definition, such as required initializers,
members that must be overridable by subclasses, and stored properties.

Codable synthesis is an example that benefits from member macros with
conformances, because for classes it wants to introduce a required
initializer for decoding and an overridable encode operation, and
these must be members of the nominal type itself. Specifically, the
`Codable` macro above is likely to have two attached member roles:

    @attached(member, conformances: Decodable, Encodable, names:
named(init(from:), encode(to:)))
    @attached(extension, conformances: Decodable, Encodable, names:
named(init(from:), encode(to:)))
    macro Codable() = ...

where the "extension" role is responsible for defining the conformance
(always), and the "member" creates the appropriate members for classes
(`init` vs. `required init`).

Tracked by rdar://112532829.
@DougGregor DougGregor force-pushed the member-macro-conformances branch from 95001a3 to 596da31 Compare September 7, 2023 15:21
@DougGregor DougGregor changed the title [Macros] Provide member macros with information about "missing" conformances [SE-0407] [Macros] Provide member macros with information about "missing" conformances Sep 7, 2023
@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2004

@swift-ci please smoke test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants