Skip to content

Change function signatures of expansion functions of macros to be synchronous #2546

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
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions proposals/0382-expression-macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public protocol ExpressionMacro: FreestandingMacro {
static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) async throws -> ExprSyntax
) throws -> ExprSyntax
}
```

Expand Down Expand Up @@ -250,7 +250,7 @@ public protocol ExpressionMacro: FreestandingMacro {
static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) async throws -> ExprSyntax
) throws -> ExprSyntax
}
```

Expand All @@ -260,8 +260,6 @@ Macro definitions should conform to the `ExpressionMacro` protocol and implement

If the macro expansion cannot proceed for some reason, the `expansion(of:in:)` operation can throw an error rather than try to produce a new syntax node. The compiler will then report the error to the user. More detailed diagnostics can be provided via the macro expansion context.

The macro expansion operation is asynchronous, to account for potentially-asynchronous operations that will eventually be added to `MacroExpansionContext`. For example, operations that require additional communication with the compiler to get types of subexpressions, access files in the program, and so on.

#### `MacroExpansionContext`

The macro expansion context provides additional information about the environment in which the macro is being expanded. This context can be queried as part of the macro expansion:
Expand Down Expand Up @@ -567,6 +565,8 @@ Expressions are just one place in the language where macros could be valuable. O

## Revision History

* Revision after acceptance:
* Make the `ExpressionMacro.expansion(of:in:)` requirement non-`async`.
* Revisions based on review feedback:
* Switch `@expression` to `@freestanding(expression)` to align with the other macros proposals and vision document.
* Make the `ExpressionMacro.expansion(of:in:)` requirement `async`.
Expand Down
10 changes: 6 additions & 4 deletions proposals/0389-attached-macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public PeerMacro: AttachedMacro {
of node: AttributeSyntax,
providingPeersOf declaration: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) async throws -> [DeclSyntax]
) throws -> [DeclSyntax]
}
```

Expand Down Expand Up @@ -163,7 +163,7 @@ protocol MemberMacro: AttachedMacro {
of node: AttributeSyntax,
providingMembersOf declaration: some DeclGroupSyntax,
in context: some MacroExpansionContext
) async throws -> [DeclSyntax]
) throws -> [DeclSyntax]
}
```

Expand Down Expand Up @@ -232,7 +232,7 @@ protocol AccessorMacro: AttachedMacro {
of node: AttributeSyntax,
providingAccessorsOf declaration: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) async throws -> [AccessorDeclSyntax]
) throws -> [AccessorDeclSyntax]
}
```

Expand Down Expand Up @@ -294,7 +294,7 @@ protocol MemberAttributeMacro: AttachedMacro {
attachedTo declaration: some DeclGroupSyntax,
providingAttributesOf member: some DeclSyntaxProtocol,
in context: some MacroExpansionContext
) async throws -> [AttributeSyntax]
) throws -> [AttributeSyntax]
}
```

Expand Down Expand Up @@ -660,6 +660,8 @@ It might be possible to provide a macro implementation API that is expressed in

## Revision History

* Revision after acceptance:
* Make the `expansion` requirements non-`async`.
* After the first pitch:
* Added conformance macros, to produce conformances
* Moved the discussion of macro-introduced names from the freestanding macros proposal here.
Expand Down
2 changes: 1 addition & 1 deletion proposals/0397-freestanding-declaration-macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public protocol CodeItemMacro: FreestandingMacro {
static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) async throws -> [CodeBlockItemSyntax]
) throws -> [CodeBlockItemSyntax]
}
```

Expand Down