Merge macro examples from ExamplePlugin target into MacroExamples#2222
Conversation
Matejkob
left a comment
There was a problem hiding this comment.
I've merged almost every macro example. I have some doubts about the quality of certain merged macros and whether they serve as good examples, but I'll leave that to your assessment.
| /// Add 'didSet' printing the new value. | ||
| struct DidSetPrintMacro: AccessorMacro { | ||
| static func expansion( | ||
| of node: AttributeSyntax, | ||
| providingAccessorsOf declaration: some DeclSyntaxProtocol, | ||
| in context: some MacroExpansionContext | ||
| ) throws -> [AccessorDeclSyntax] { | ||
| guard | ||
| let identifier = declaration.as(VariableDeclSyntax.self)?.bindings.first?.pattern.as(IdentifierPatternSyntax.self)?.identifier | ||
| else { | ||
| return [] | ||
| } | ||
|
|
||
| return ["didSet { print(\(identifier)) }"] | ||
| } | ||
| } |
There was a problem hiding this comment.
Accessor macro cannot add an observable accessor so this example doesn't work so I removed it.
| /// 'print(<arg>)'. | ||
| struct PrintAnyMacro: CodeItemMacro { | ||
| static func expansion( | ||
| of node: some FreestandingMacroExpansionSyntax, | ||
| in context: some MacroExpansionContext | ||
| ) throws -> [CodeBlockItemSyntax] { | ||
| guard let expr = node.arguments.first?.expression else { | ||
| return [] | ||
| } | ||
| return ["print(\(expr))"] | ||
| } | ||
| } |
There was a problem hiding this comment.
The code item macro is still behind a feature flag, so I've decided not to merge it into the official macro examples yet to avoid confusing users.
|
|
||
| let counter = Counter() | ||
|
|
||
| // print("Peer value with suffix name for \(Counter.self): \(String(describing: Counter_peer))") |
There was a problem hiding this comment.
Do you have any idea why Counter_peer con not be accessed here?
There was a problem hiding this comment.
That appears to be a bug. @DougGregor is this a known issue that name lookup doesn’t work for peers of local types?
If I move the following to top-level it does work.
@PeerValueWithSuffixName
actor Counter {
var value = 0
}There was a problem hiding this comment.
OK, looks like it might be intentional that peer macro can’t introduce declarations on a local scope. If so, the error message should be improved. Filed swiftlang/swift#68661
ahoppen
left a comment
There was a problem hiding this comment.
I’m not sure if all of these examples are all that valuable. I think the ones that do provide additional value are:
FuncUniqueMacrosince we don’t have any declaration macro examples right nowSendableExtensionMacrosince we don’t have any extension macro examples right nowMemberDeprecatedMacroseems like a nice example and is a lot simpler than our existing MemberAttribute macroPeerValueWithSuffixNameMacroalso seems useful since it’s a very short example of a peer macro and all our current examples are a lot more complicated
I think the following aren’t really useful and I would just delete them:
EchoExpressionMacrosince it doesn’t do anything and we have plenty expression macro examples alreadyEquatableConformanceMacrosince conformance macros aren’t really a thing anymoreMetadataMacro: I thinkCaseDetectionMacroshows similar ideas and is a more realistic example
|
|
||
| let counter = Counter() | ||
|
|
||
| // print("Peer value with suffix name for \(Counter.self): \(String(describing: Counter_peer))") |
There was a problem hiding this comment.
That appears to be a bug. @DougGregor is this a known issue that name lookup doesn’t work for peers of local types?
If I move the following to top-level it does work.
@PeerValueWithSuffixName
actor Counter {
var value = 0
}
That list sounds mostly reasonable to me, though the reasoning for not including |
80dddfa to
067236d
Compare
Oh, the macro expansion is considered a different source file in the compiler. Could you file a bug at https://github.com/apple/swift/issues? I would suggest we just remove that example as well. We should add an example for an extension macro but I we could do that in a follow-up PR. |
Could just rename to to |
That could work! 😅 |
067236d to
0c044f7
Compare
|
@swift-ci please test |
|
@swift-ci Please test Windows |
Fixes up several broken links in the documentation: - ExamplePlugin was rolled in to the MacroExamples in swiftlang#2222 but the link was not updated. - A link the changelog pointed to sourcekit-lsp when it should have been swift-syntax. - The Parsing Basics document has moved.
Fixes up several broken links in the documentation: - ExamplePlugin was rolled in to the MacroExamples in swiftlang#2222 but the link was not updated. - A link the changelog pointed to sourcekit-lsp when it should have been swift-syntax. - The Parsing Basics document has moved.

Resolves #2165