Skip to content

[SE-0415] Add documentation for body macros#443

Open
literally-anything wants to merge 1 commit intoswiftlang:mainfrom
literally-anything:body-macro-attribute
Open

[SE-0415] Add documentation for body macros#443
literally-anything wants to merge 1 commit intoswiftlang:mainfrom
literally-anything:body-macro-attribute

Conversation

@literally-anything
Copy link

@literally-anything literally-anything commented Feb 16, 2026

Body macros are a stable language feature, but they are't mentioned anywhere except the evolution proposal. This adds body as a possible type of attached macro in the @attached attribute section.

Fixes: rdar://171280404

Body macros are a stable language feature, but they are't mentioned anywhere except the evolution proposal.
Copy link
Member

@amartini51 amartini51 left a comment

Choose a reason for hiding this comment

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

Thanks for taking the time to add this to the documentation! Sorry for a bit of delay last week, while I was working on some other documentation.

Reading this PR and SE-0415, I think there are two things we can check on. Tagging @DougGregor as the proposal author, for technical review — maybe Doug can answer these.

Body macros can go on property accessors, like get and set — not just functions and methods. Is that specific to body macros, or is it also true of all attached macros? Let's add it to the reference either here, or for attached macros in general.

The proposals notes that body of a function marked with this macro must be syntactically valid Swift code, although it doesn’t need to typecheck. The "Macros" chapter in TSPL says that normally code marked with macros does need valid type information:

Both the input to a macro and the output of macro expansion are checked to ensure they’re syntactically valid Swift code. Likewise, the values you pass to a macro and the values in code generated by a macro are checked to ensure they have the correct types.

If the "no type checking" rule is specific to body macros, let's add it here — otherwise, at the relevant spot in the reference.

Comment on lines +100 to +106
- term Body macros:
Write `body` as the first argument to this attribute.
The type that implements the macro conforms to the `BodyMacro` protocol.
These macros modify or replace the body of the function that they are attached to.
If the function has no body,
the macro can generate one from scratch.
Only one body macro is allowed on a function at a time.
Copy link
Member

Choose a reason for hiding this comment

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

Some wording adjustments:

Suggested change
- term Body macros:
Write `body` as the first argument to this attribute.
The type that implements the macro conforms to the `BodyMacro` protocol.
These macros modify or replace the body of the function that they are attached to.
If the function has no body,
the macro can generate one from scratch.
Only one body macro is allowed on a function at a time.
- term Body macros:
Write `body` as the first argument to this attribute.
The type that implements the macro conforms to the `BodyMacro` protocol.
These macros can generate, modify, or replace
the body of the function that they're attached to.
You can write at most one body macro on any given function.

I think it reads better to include the possibility of generating the function body as part of the sentence about replacing or modifying the body — that way, all three options are in one place.

@amartini51
Copy link
Member

CC @invalidname who will be working on Swift docs when I'm out of the office starting sometime next month.

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.

2 participants