Skip to content

[Macros] Introduce member attribute macros. #1220

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
Jan 12, 2023

Conversation

hborla
Copy link
Member

@hborla hborla commented Jan 11, 2023

For each member of the declaration the macro attribute is attached to, the macro can produce a set of attributes to attach to that member. For example:

@declaration(memberAttribute)
macro wrapStoredProperties: Void

@wrapStoredProperties
struct Point {
  var x: Int
  var y: Int

  var description: String { "" }
}

// Expanded to

struct Point {
  @Wrapper var x: Int
  @Wrapper var y: Int

  var description: String { "" }
}

The member attribute macro expansion is given the declaration it's attached to along with a member declaration, and it returns a set of attributes to attach to the member. The expansion method is called for each member in the attachedTo declaration.

@hborla hborla requested a review from DougGregor January 11, 2023 16:41
@hborla hborla requested a review from ahoppen as a code owner January 11, 2023 16:41
@hborla hborla force-pushed the member-attribute-macros branch from 07dc1eb to d2a07e1 Compare January 11, 2023 18:04
@hborla
Copy link
Member Author

hborla commented Jan 11, 2023

@swift-ci please test

@@ -106,7 +111,7 @@ class MacroApplication: SyntaxRewriter {
return true
}

return !(macro is PeerDeclarationMacro.Type || macro is MemberDeclarationMacro.Type)
return !(macro is PeerDeclarationMacro.Type || macro is MemberDeclarationMacro.Type || macro is MemberAttributeMacro.Type)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is making me think that there might be a useful abstraction around attached macros (e.g., ones that are written as attributes), and that DeclarationMacro is basically a useless abstraction.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for your PR, but we should revisit the protocol design here

@hborla
Copy link
Member Author

hborla commented Jan 12, 2023

@swift-ci please test

@hborla hborla force-pushed the member-attribute-macros branch from d2a07e1 to e91b3f9 Compare January 12, 2023 03:48
@hborla
Copy link
Member Author

hborla commented Jan 12, 2023

@swift-ci please test

For each member of the declaration the macro attribute is attached to, the macro
can produce a set of attributes to attach to that member.
@hborla hborla force-pushed the member-attribute-macros branch from e91b3f9 to 8e9caa1 Compare January 12, 2023 05:18
@hborla
Copy link
Member Author

hborla commented Jan 12, 2023

@swift-ci please test

@hborla hborla merged commit 060082e into swiftlang:main Jan 12, 2023
@hborla hborla deleted the member-attribute-macros branch January 12, 2023 07:37
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