Skip to content

Commit de6037e

Browse files
ehusstraviscross
authored andcommitted
Update proc_macro_attribute to the attribute template
1 parent b38d9e3 commit de6037e

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
@@ -232,10 +232,8 @@ r[macro.proc.attribute]
232232
## The `proc_macro_attribute` attribute
233233
234234
r[macro.proc.attribute.intro]
235-
*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].
235+
The *`proc_macro_attribute` [attribute][attributes]* defines an *attribute macro* which can be used as an [outer attribute][attributes].
236236
237-
r[macro.proc.attribute.def]
238-
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].
239237
> [!EXAMPLE]
240238
> The following attribute macro takes the input stream and returns it as is, effectively being the no-op of attributes.
241239
>
@@ -251,8 +249,6 @@ Attribute macros are defined by a [public] [function] with the `proc_macro_a
251249
> }
252250
> ```
253251
254-
r[macro.proc.attribute.namespace]
255-
The `proc_macro_attribute` attribute defines the attribute in the [macro namespace] in the root of the crate.
256252
> [!EXAMPLE]
257253
> 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:".
258254
>
@@ -302,12 +298,32 @@ The `proc_macro_attribute` attribute defines the attribute in the [macro namespa
302298
> // out: item: "fn invoke4() {}"
303299
> ```
304300
301+
r[macro.proc.attribute.syntax]
302+
The `proc_macro_attribute` attribute uses the [MetaWord] syntax and thus does not take any inputs.
303+
304+
r[macro.proc.attribute.allowed-positions]
305+
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.
305306
307+
r[macro.proc.attribute.duplicates]
308+
The `proc_macro_attribute` attribute may only be specified once on a function.
309+
310+
r[macro.proc.attribute.namespace]
311+
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.
306312
313+
r[macro.proc.attribute.use-positions]
314+
Attribute macros can only be used on:
307315
316+
- [items]
317+
- items in [`extern` blocks]
318+
- inherent and trait [implementations]
319+
- [trait definitions]
308320
321+
r[macro.proc.attribute.behavior]
322+
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.
309323
324+
The second [`TokenStream`] is the rest of the [item] including other [attributes] on the [item].
310325
326+
The returned [`TokenStream`] replaces the [item] with an arbitrary number of [items].
311327
312328
r[macro.proc.token]
313329
## Declarative macro tokens and procedural macro tokens

0 commit comments

Comments
 (0)