Skip to content

Commit fe72d24

Browse files
ehusstraviscross
authored andcommitted
Update proc_macro_attribute to the attribute template
1 parent f45633d commit fe72d24

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/procedural-macros.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,8 @@ r[macro.proc.attribute]
244244
## The `proc_macro_attribute` attribute
245245
246246
r[macro.proc.attribute.intro]
247-
*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].
247+
The *`proc_macro_attribute` [attribute][attributes]* defines an *attribute macro* which can be used as an [outer attribute][attributes].
248248
249-
r[macro.proc.attribute.def]
250-
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].
251249
> [!EXAMPLE]
252250
> The following attribute macro takes the input stream and returns it as is, effectively being the no-op of attributes.
253251
>
@@ -263,8 +261,6 @@ Attribute macros are defined by a [public] [function] with the `proc_macro_a
263261
> }
264262
> ```
265263
266-
r[macro.proc.attribute.namespace]
267-
The `proc_macro_attribute` attribute defines the attribute in the [macro namespace] in the root of the crate.
268264
> [!EXAMPLE]
269265
> 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:".
270266
>
@@ -314,12 +310,32 @@ The `proc_macro_attribute` attribute defines the attribute in the [macro namespa
314310
> // out: item: "fn invoke4() {}"
315311
> ```
316312
313+
r[macro.proc.attribute.syntax]
314+
The `proc_macro_attribute` attribute uses the [MetaWord] syntax and thus does not take any inputs.
315+
316+
r[macro.proc.attribute.allowed-positions]
317+
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.
317318
319+
r[macro.proc.attribute.duplicates]
320+
The `proc_macro_attribute` attribute may only be specified once on a function.
321+
322+
r[macro.proc.attribute.namespace]
323+
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.
318324
325+
r[macro.proc.attribute.use-positions]
326+
Attribute macros can only be used on:
319327
328+
- [items]
329+
- items in [`extern` blocks]
330+
- inherent and trait [implementations]
331+
- [trait definitions]
320332
333+
r[macro.proc.attribute.behavior]
334+
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.
321335
336+
The second [`TokenStream`] is the rest of the [item] including other [attributes] on the [item].
322337
338+
The returned [`TokenStream`] replaces the [item] with an arbitrary number of [items].
323339
324340
r[macro.proc.token]
325341
## Declarative macro tokens and procedural macro tokens

0 commit comments

Comments
 (0)