Skip to content

Commit 52a4e5c

Browse files
committed
Rework proc_macro to use attribute template rules
1 parent 08d9205 commit 52a4e5c

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/procedural-macros.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ r[macro.proc.proc_macro]
8686
r[macro.proc.proc_macro.intro]
8787
The *`proc_macro` [attribute][attributes]* defines a procedural macro for [function-like macros][macro.invocation].
8888
89-
r[macro.proc.function.def]
90-
These macros are defined by a [public] [function] with the `proc_macro` [attribute] and a signature of `(TokenStream) -> TokenStream`. The input [`TokenStream`] is what is inside the delimiters of the macro invocation and the output [`TokenStream`] replaces the entire macro invocation.
91-
92-
9389
> [!EXAMPLE]
9490
> The following macro definition ignores its input and outputs a function `answer` into its scope.
9591
>
@@ -119,12 +115,20 @@ These macros are defined by a [public] [function] with the `proc_macro` [att
119115
> }
120116
> ```
121117
118+
r[macro.proc.proc_macro.syntax]
119+
The `proc_macro` attribute uses the [MetaWord] syntax and thus does not take any inputs.
122120
121+
r[macro.proc.proc_macro.allowed-positions]
122+
The `proc_macro` 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.
123123
124+
r[macro.proc.proc_macro.duplicates]
125+
The `proc_macro` attribute may only be specified once on a function.
124126
125-
r[macro.proc.function.namespace]
127+
r[macro.proc.proc_macro.namespace]
126128
The `proc_macro` attribute publicly defines the macro in the [macro namespace] in the root of the crate with the same name as the function.
127129
130+
r[macro.proc.proc_macro.behavior]
131+
A function-like macro invocation of a function-like procedural macro will pass what is inside the delimiters of the macro invocation as the input [`TokenStream`] argument, and replace the entire macro invocation with the output [`TokenStream`] of the function.
128132
129133
r[macro.proc.function.invocation]
130134
Function-like procedural macros may be invoked in any macro invocation position, which includes [statements], [expressions], [patterns], [type expressions], [item] positions, including items in [`extern` blocks], inherent and trait [implementations], and [trait definitions].

0 commit comments

Comments
 (0)