Skip to content

[Macros] Freestanding macros expand to CodeBlockItemList; parse top-level macro expansion decls #1268

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

Closed
wants to merge 3 commits into from

Conversation

rxwei
Copy link
Contributor

@rxwei rxwei commented Jan 25, 2023

  • Change the return type of FreestandingDeclarationMacro.expanion(of:in:) to ``CodeBlockItemListSyntax` to allow expressions, statements, and declarations.
  • Parse freestanding macro expansions as declarations rather than expressions when it's not part of a statement or expression (i.e. "top-level").

rxwei and others added 3 commits January 25, 2023 17:38
Parse freestanding macro expansions as declarations
rather than expressions when it's not part of a statement
or expression (i.e. "top-level").
…evel macro expansion decls

- Change the return type of `FreestandingDeclarationMacro.expanion(of:in:)` to ``CodeBlockItemListSyntax` to allow expressions, statements, and declarations.
- Parse freestanding macro expansions as declarations rather than expressions when it's not part of a statement or expression (i.e. "top-level").
@rxwei rxwei force-pushed the declaration-macro-name-lookup branch from 4d34529 to e540b7c Compare January 26, 2023 04:48
@@ -16,5 +16,5 @@ public protocol CodeItemMacro: FreestandingMacro {
static func expansion(
of node: MacroExpansionDeclSyntax,
in context: inout MacroExpansionContext
) throws -> [CodeBlockItemSyntax]
) throws -> CodeBlockItemListSyntax
Copy link
Member

Choose a reason for hiding this comment

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

I discussed this with @DougGregor yesterday. If you are intending to merge these items with other items, this should return an array IMO because arrays are easier to work with (you can append, insert, delete, …). IMO you should only create a CodeBlockItemListSyntax once you think the item list is complete.

(similar in DeclarationMacros.swift)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! In that case this PR is no longer necessary. DeclarationMacro still returns decls, ExpressionMacro still returns exprs, and CodeItemMacro returns [CodeBlockItemSyntax]

@rxwei rxwei closed this Jan 26, 2023
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.

3 participants