Add support for body macros on variables and accessors#87869
Add support for body macros on variables and accessors#87869calda wants to merge 7 commits intoswiftlang:mainfrom
Conversation
309f281 to
d69e125
Compare
d69e125 to
dede638
Compare
|
@swift-ci please test |
|
Thanks for triggering CI @xedin, although I think you will also need to include the SwiftSyntax change when running the tests: swiftlang/swift-syntax#3298 |
|
swiftlang/swift-syntax#3298 |
|
Ah there was a SwiftSyntax test failure that I missed, fixed over in swiftlang/swift-syntax#3298 |
|
The updated |
|
swiftlang/swift-syntax#3298 |
|
swiftlang/swift-syntax#3298 was merged 😄 |
|
@swift-ci please test |
| } | ||
| } | ||
|
|
||
| fn.forEachAttachedMacro(MacroRole::Body, expandMacro); |
There was a problem hiding this comment.
Interested in this case.
@BodyMacro var value: Int {
@AnotherBodyMacro get { ... }
set { ... }
}I think @AnotherBodyMacro should take the precedence for get, but this is trying the @BodyMacro first and exit? And @BodyMacro should still be applied to set?
Could you remind me how it should behave for multiple body macros in general?:
@BodyMacro @AnotherBodyMacro func foo() { ... }
If both applies to the func, should get above take those two body macros as well?
There was a problem hiding this comment.
Interested in this case.
@BodyMacro var value: Int { @AnotherBodyMacro get { ... } set { ... } }
Good catch. Updated the PR to disallow a macro to the decl itself if it has an explicit get { ... } accessor. This was not specifically called out as supported in SE-0415, and disallowing it prevents this ambiguous case where a body macro could be applied to both the decl and accessor.
Could you remind me how it should behave for multiple body macros in general?
SE-0415 says "At most one body macro can be applied to a given function.". However, when I test applying multiple body macros to a function declaration in a sample project, there are no errors or diagnostics (one of them wins silently). Seems not-ideal, but also seems like it can be out of scope for this change given this is the existing behavior for function decls.
Corresponding Swift Syntax PR: swiftlang/swift-syntax#3298
According to SE-0415, body macros should be supported on variables and accessors:
However, both forms are currently unsupported with errors like
error: 'body' macro cannot be attached to var ('area')orerror: Declaration is not a type with an optional code block.This PR adds implements supports for both of these use cases.
AccessorDeclSyntaxreceived via a plugin message.VariableDeclSyntax, and then within Swift Syntax synthesize an implicitAccessorDeclSyntaxto use in theexpansion(of:providingBodyFor:in:)call (which needs aWithOptionalCodeBlockSyntax).At Airbnb, we'd like to use this functionality to enable adding instrumentation to SwiftUI view bodies, like:
Fixes #75715.
Please review: @DougGregor @ahoppen