You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/procedural-macros.md
+21-5Lines changed: 21 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -224,10 +224,8 @@ r[macro.proc.attribute]
224
224
## The `proc_macro_attribute` attribute
225
225
226
226
r[macro.proc.attribute.intro]
227
-
*Attribute macros* define new [outer attributes][attributes] which can be attached to [items], including items in [`extern` blocks], inherent and trait [implementations], and [trait definitions].
227
+
The *`proc_macro_attribute`[attribute][attributes]* defines an *attribute macro* which can be used as an [outer attribute][attributes].
228
228
229
-
r[macro.proc.attribute.def]
230
-
Attribute macros are defined by a [public] [function] with the `proc_macro_attribute`[attribute] that has a signature of `(TokenStream, TokenStream) -> TokenStream`. The first [`TokenStream`] is the delimited token tree following the attribute's name, not including the outer delimiters. If the attribute is written as a bare attribute name, the attribute [`TokenStream`] is empty. The second [`TokenStream`] is the rest of the [item] including other [attributes] on the [item]. The returned [`TokenStream`] replaces the [item] with an arbitrary number of [items].
231
229
> [!EXAMPLE]
232
230
> The following attribute macro takes the input stream and returns it as is, effectively being the no-op of attributes.
233
231
>
@@ -243,8 +241,6 @@ Attribute macros are defined by a [public] [function] with the `proc_macro_a
243
241
> }
244
242
> ```
245
243
246
-
r[macro.proc.attribute.namespace]
247
-
The `proc_macro_attribute` attribute defines the attribute in the [macro namespace] in the root of the crate.
248
244
> [!EXAMPLE]
249
245
> This following example shows the stringified [`TokenStream`s] that the attribute macros see. The output will show in the output of the compiler. The output is shown in the comments after the function prefixed with "out:".
250
246
>
@@ -294,12 +290,32 @@ The `proc_macro_attribute` attribute defines the attribute in the [macro namespa
294
290
> // out: item: "fn invoke4() {}"
295
291
> ```
296
292
293
+
r[macro.proc.attribute.syntax]
294
+
The `proc_macro_attribute` attribute uses the [MetaWord] syntax and thus does not take any inputs.
295
+
296
+
r[macro.proc.attribute.allowed-positions]
297
+
The `proc_macro_attribute` attribute may only be applied to a function with the signature of `pub fn(TokenStream, 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.
297
298
299
+
r[macro.proc.attribute.duplicates]
300
+
The `proc_macro_attribute` attribute may only be specified once on a function.
301
+
302
+
r[macro.proc.attribute.namespace]
303
+
The `proc_macro_attribute` attribute defines the attribute in the [macro namespace] in the root of the crate with the same name as the function.
298
304
305
+
r[macro.proc.attribute.use-positions]
306
+
Attribute macros can only be used on:
299
307
308
+
- [items]
309
+
- items in [`extern` blocks]
310
+
- inherent and trait [implementations]
311
+
- [trait definitions]
300
312
313
+
r[macro.proc.attribute.behavior]
314
+
The first [`TokenStream`] parameter is the delimited token tree following the attribute's name, not including the outer delimiters. If the attribute is written as a bare attribute name, the [`TokenStream`] is empty.
301
315
316
+
The second [`TokenStream`] is the rest of the [item] including other [attributes] on the [item].
302
317
318
+
The returned [`TokenStream`] replaces the [item] with an arbitrary number of [items].
303
319
304
320
r[macro.proc.token]
305
321
## Declarative macro tokens and procedural macro tokens
0 commit comments