3
3
* Procedural macros* allow creating syntax extensions as execution of a function.
4
4
Procedural macros come in one of three flavors:
5
5
6
- * Function-like macros - ` custom!(...) `
7
- * Derive mode macros - ` #[derive(CustomMode)] `
8
- * Attribute macros - ` #[CustomAttribute] `
6
+ * [ Function-like macros] - ` custom!(...) `
7
+ * [ Derive mode macros] - ` #[derive(CustomMode)] `
8
+ * [ Attribute macros] - ` #[CustomAttribute] `
9
9
10
10
Procedural macros allow you to run code at compile time that operates over Rust
11
11
syntax, both consuming and producing Rust syntax. You can sort of think of
@@ -55,7 +55,7 @@ be modified but can be manufactured. `Span`s represent an extent of source
55
55
code within a program and are primarily used for error reporting. You can modify
56
56
the `Span` of any token.
57
57
58
- # ## Procedural macros and hygiene
58
+ # ## Procedural macro hygiene
59
59
60
60
Procedural macros are *unhygienic*. This means they behave as if the output
61
61
token stream was simply written inline to the code it's next to. This means that
@@ -67,37 +67,16 @@ items in libraries (for example, `::std::option::Option` instead of `Option`) or
67
67
by ensuring that generated functions have names that are unlikely to clash with
68
68
other functions (like `__internal_foo` instead of `foo`).
69
69
70
- # ## Limitations of procedural macros
71
-
72
- Procedural macros are not quite as powerful as `macro_rules!`-defined macros
73
- in certain respects. These limitations include:
74
-
75
- * Bang macros can only be invoked in *item* contexts. For example,
76
- `format!` cannot yet be created in user libraries because it is only ever
77
- invoked in an expression context. Put another way, these macros can only
78
- expand to [items], not expressions.
79
-
80
- * Procedural macros cannot expand to definitions of `macro_rules!` macros, with
81
- exception to derive mode macros.
82
-
83
- * Procedural attributes can only be attached to items, not expressions. For
84
- example `#[my_attr] fn foo() {}` is ok but `#[my_attr] return 3` is not. This
85
- is again due to the lack of hygiene today but this restriction may eventually
86
- be lifted.
87
-
88
- * Error reporting is currently quite primitive. While an unstable diagnostic API
89
- exists on stable your only option is to `panic!` or to in some cases expand to
90
- an invocation of the `compile_error!` macro with a custom message.
91
-
92
- # # Function-like procedural macros
70
+ # ## Function-like procedural macros
93
71
94
72
Function-like procedural macros define new invokable macros.
95
73
96
74
These macros are defined by a [public] [function] with the `proc_maco`
97
75
[attribute ] and a signature of `(TokenStream) -> TokenStream`. The input
98
76
[`TokenStream` ] is what is inside the delimiters of the macro invocation and the
99
77
output [`TokenStream`] replaces the entire macro invocation. It may contain an
100
- arbitrary number of items.
78
+ arbitrary number of [items]. The returned [`TokenStream`] cannot include any
79
+ [macro ] definitions.
101
80
102
81
For example, the following macro definition ignores its input and outputs a
103
82
function `answer` into its scope.
@@ -131,9 +110,11 @@ with curly braces and no semicolon or a different delimiter followed by a
131
110
semicolon. For example, ` make_answer ` from the previous example can be invoked
132
111
as ` make_answer!{} ` , ` make_answer!(); ` or ` make_answer![]; ` .
133
112
113
+ These macros cannot expand to syntax that defines new ` macro_rule ` style macros.
114
+
134
115
### Derive mode macros
135
116
136
- * Derive mode macros* define new modes for the ` derive ` attribute. The macros
117
+ * Derive mode macros* define new modes for the ` derive ` attribute. These macros
137
118
define new items given the token stream of a [ struct] , [ enum] , or [ union] . They
138
119
also define derive mode helper attributes.
139
120
@@ -291,6 +272,9 @@ fn invoke4() {}
291
272
[ `derive` ] : attributes.html#derive
292
273
[ `proc_macro` crate ] : ../proc_macro/index.html
293
274
[ Cargo's build scripts ] : ../cargo/reference/build-scripts.html
275
+ [ Derive mode macros ] : #derive-mode-macros
276
+ [ Attribute macros ] : #attribute-macros
277
+ [ Function-like macros ] : #function-like-procedural-macros
294
278
[ attributes ] : attributes.html
295
279
[ custom attributes ] : attributes.html
296
280
[ crate type ] : linkage.html
@@ -301,4 +285,4 @@ fn invoke4() {}
301
285
[ module ] : items/modules.html
302
286
[ modules ] : items/modules.html
303
287
[ procedural macro tutorial ] : ../book/2018-edition/appendix-04-macros.html#procedural-macros-for-custom-derive
304
- [ public ] : visibility-and-privacy.html
288
+ [ public ] : visibility-and-privacy.html
0 commit comments