Skip to content

Commit 89789f6

Browse files
committed
Revise proc_macro_derive text
One thing worth highlighting is that we're preferring to say that we "declare" (rather than "define") derive macro helper attributes. A definition is something that says what something is. For these attributes, though, we're just declaring that they exist. In revising, we've clarified that the derive macro function must appear in the root of the crate and that it may be `const` and may use `extern` (with the allowed ABI) but may not use other qualifiers. We've also changed the syntax to use `IDENTIFIER` rather than `SimplePathSegment`. In testing, none of the other things that would be accepted by the latter are allowed, so it seems more correct in terms of user-visible behavior to use `IDENTIFIER` here.
1 parent 1124ff3 commit 89789f6

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/procedural-macros.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ r[macro.proc.derive]
134134
## The `proc_macro_derive` attribute
135135

136136
r[macro.proc.derive.intro]
137-
The *`proc_macro_derive` [attribute][attributes]* defines a *derive macro* which defines an input for the [`derive` attribute]. These macros can create new [items] given the token stream of a [struct], [enum], or [union]. They can also define [derive macro helper attributes].
137+
Applying the *`proc_macro_derive` [attribute]* to a function defines a *derive macro* that can be invoked by the [`derive` attribute]. These macros are given the token stream of a [struct], [enum], or [union] definition and can emit new [items] after it. They can also declare and use [derive macro helper attributes].
138138

139139
> [!EXAMPLE]
140-
> The following is an example of a derive macro. Instead of doing anything useful with its input, it just appends a function `answer`.
140+
> This derive macro ignores its input and appends tokens that define a function.
141141
>
142142
> <!-- ignore: test doesn't support proc-macro -->
143143
> ```rust,ignore
@@ -151,7 +151,7 @@ The *`proc_macro_derive` [attribute][attributes]* defines a *derive macro* which
151151
> }
152152
> ```
153153
>
154-
> And then using said derive macro:
154+
> To use it, we might write:
155155
>
156156
> <!-- ignore: requires external crates -->
157157
> ```rust,ignore
@@ -167,63 +167,63 @@ The *`proc_macro_derive` [attribute][attributes]* defines a *derive macro* which
167167
> ```
168168
169169
r[macro.proc.derive.syntax]
170-
The `proc_macro_derive` attribute uses the following syntax:
170+
The syntax for the `proc_macro_derive` attribute is:
171171
172172
```grammar,attributes
173173
@root ProcMacroDeriveAttribute ->
174174
`proc_macro_derive` `(` DeriveMacroName ( `,` DeriveMacroAttributes )? `,`? `)`
175175
176-
DeriveMacroName -> SimplePathSegment
176+
DeriveMacroName -> IDENTIFIER
177177
178178
DeriveMacroAttributes ->
179-
`attributes` `(` ( SimplePathSegment (`,` SimplePathSegment)* `,`?)? `)`
179+
`attributes` `(` ( IDENTIFIER (`,` IDENTIFIER)* `,`?)? `)`
180180
```
181181
182-
The [DeriveMacroName] is the name of the derive macro. The optional `attributes` are described in [macro.proc.derive.attributes].
182+
The name of the derive macro is given by [DeriveMacroName]. The optional `attributes` argument is described in [macro.proc.derive.attributes].
183183

184184
r[macro.proc.derive.allowed-positions]
185-
The `proc_macro_derive` attribute may only be applied to a function with the signature of `pub fn(TokenStream) -> TokenStream` where [`TokenStream`] comes from the [`proc_macro` crate]. It must have the ["Rust" ABI][items.fn.extern]. No other function qualifiers are allowed.
185+
The `proc_macro_derive` attribute may only be applied to a `pub` function with the [Rust ABI][items.fn.extern] defined in the root of the crate with a type of `fn(TokenStream) -> TokenStream` where [`TokenStream`] comes from the [`proc_macro` crate]. The function may be `const` and may use `extern` to explicitly specify the Rust ABI, but it may not use any other [qualifiers][FunctionQualifiers] (e.g. it may not be `async` or `unsafe`).
186186

187187
r[macro.proc.derive.duplicates]
188-
The `proc_macro_derive` attribute may only be specified once on a function.
188+
The `proc_macro_derive` attribute may be specified only once on a function.
189189

190190
r[macro.proc.derive.namespace]
191-
The `proc_macro_derive` attribute publicly defines the custom derive in the [macro namespace] in the root of the crate with the name given in the attribute.
191+
The `proc_macro_derive` attribute publicly defines the derive macro in the [macro namespace] in the root of the crate.
192192

193193
r[macro.proc.derive.output]
194-
The input [`TokenStream`] is the token stream of the item that has the `derive` attribute on it. The output [`TokenStream`] must be a set of items that are then appended to the [module] or [block] that the item from the input [`TokenStream`] is in.
194+
The input [`TokenStream`] is the token stream of the item to which the `derive` attribute is applied. The output [`TokenStream`] must be a (possibly empty) set of items. These items are appended following the input item within the same [module] or [block].
195195

196196
r[macro.proc.derive.attributes]
197197
### Derive macro helper attributes
198198

199199
r[macro.proc.derive.attributes.intro]
200-
Derive macros can add additional [attributes] into the scope of the [item] they are on. Said attributes are called *derive macro helper attributes*. These attributes are [inert], and their only purpose is to be fed into the derive macro that defined them. That said, they can be seen by all macros.
200+
Derive macros can declare *derive macro helper attributes* to be used within the scope of the [item] to which the derive macro is applied. These [attributes] are [inert]. While their purpose is to be used by the macro that declared them, they can be seen by any macro.
201201

202-
r[macro.proc.derive.attributes.def]
203-
The way to define helper attributes is to put an `attributes` key in the `proc_macro_derive` macro with a comma separated list of identifiers that are the names of the helper attributes.
202+
r[macro.proc.derive.attributes.decl]
203+
A helper attribute for a derive macro is declared by adding its identifier to the `attributes` list in the `proc_macro_derive` attribute.
204204

205205
> [!EXAMPLE]
206-
> The following derive macro defines a helper attribute `helper`, but ultimately doesn't do anything with it.
206+
> This declares a helper attribute and then ignores it.
207207
>
208208
> <!-- ignore: test doesn't support proc-macro -->
209209
> ```rust,ignore
210210
> # #![crate_type="proc-macro"]
211211
> # extern crate proc_macro;
212212
> # use proc_macro::TokenStream;
213-
>
214-
> #[proc_macro_derive(HelperAttr, attributes(helper))]
215-
> pub fn derive_helper_attr(_item: TokenStream) -> TokenStream {
213+
> #
214+
> #[proc_macro_derive(WithHelperAttr, attributes(helper))]
215+
> pub fn derive_with_helper_attr(_item: TokenStream) -> TokenStream {
216216
> TokenStream::new()
217217
> }
218218
> ```
219219
>
220-
> And then usage on the derive macro on a struct:
220+
> To use it, we might write:
221221
>
222222
> <!-- ignore: requires external crates -->
223223
> ```rust,ignore
224-
> #[derive(HelperAttr)]
224+
> #[derive(WithHelperAttr)]
225225
> struct Struct {
226-
> #[helper] field: ()
226+
> #[helper] field: (),
227227
> }
228228
> ```
229229

0 commit comments

Comments
 (0)