Should Accessor Macros declarations require a names argument to synthesize getters and setters?

Hi! I've been experimenting with an accessor macro to synthesize a getter and setter on an arbitrary property. Things are working fine for me with this declaration:

@attached(accessor)
  public macro PropertyMacroGetterAndSetter(...)

According to the Swift Book documentation on the attached attribute:[1]:

The peer, member, and accessor macro roles require a names: argument, listing the names of the symbols that the macro generates.

Which seems to imply that an attached attribute on an accessor macro should fail to synthesize a getter and setter (assuming I have not explicitly specified the names).

If I try:

@attached(accessor, names: named(get))
  public macro PropertyMacroGetterAndSetter(...)

I am still getting my getter and setter synthesized… which is weird (because I only specify get but this does not block me on synthesizing a set). Is swift-syntax not respecting those names being passed? Is @attached(accessor) the legit declaration (and the documentation that tells us names must be passed is out-of-date)?

Here is a simple patch diff on swift-syntax that shows this in action:

diff --git a/Examples/Sources/MacroExamples/Interface/ComplexMacros.swift b/Examples/Sources/MacroExamples/Interface/ComplexMacros.swift
index da160c99..e4e82d29 100644
--- a/Examples/Sources/MacroExamples/Interface/ComplexMacros.swift
+++ b/Examples/Sources/MacroExamples/Interface/ComplexMacros.swift
@@ -24,7 +24,7 @@
 @attached(member, names: named(_storage))
 public macro DictionaryStorage() = #externalMacro(module: "MacroExamplesImplementation", type: "DictionaryStorageMacro")
 
-@attached(accessor)
+@attached(accessor, names: named(get))
 public macro DictionaryStorageProperty() =
   #externalMacro(module: "MacroExamplesImplementation", type: "DictionaryStoragePropertyMacro")
 


  1. Documentation ↩︎

From what I know the @attached(accessor) declaration is permissible.

It is possible that the documentation is out-of-date or just contains an error.

1 Like

There is an accessor macro in swift-syntax macro examples which implements setter and getter but doesn't specify names parameter in its declaration. Not sure if it's intentional.

1 Like

Agree with others that this feels like a documentation bug. The accessor role is defined as follows:

Write accessor as the first argument to this attribute. The type that implements the macro conforms to the AccessorMacro protocol. These macros add accessors to the stored property they’re attached to, turning it into a computed property.

Since the scope of accessor macros is so narrowly defined to simply "add accessors to the stored property they’re attached to," it doesn't seem like it should be necessary to specify the names of supported accessors.

1 Like

I've taken the liberty of creating an issue on the swift-book repo and opening a pull request with the fix. We'll see where this goes.

1 Like

FWIW I just tried a quick local experiment will all these accessor specifiers and no explicit names defined in the macro declaration and I did not see a compiler error.