Skip to content

Commit 1e38a74

Browse files
committed
Cleanup proc macros after multi-day editing
1 parent 2b96704 commit 1e38a74

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed

src/procedural-macros.md

+14-30
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
*Procedural macros* allow creating syntax extensions as execution of a function.
44
Procedural macros come in one of three flavors:
55

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]`
99

1010
Procedural macros allow you to run code at compile time that operates over Rust
1111
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
5555
code within a program and are primarily used for error reporting. You can modify
5656
the `Span` of any token.
5757
58-
### Procedural macros and hygiene
58+
### Procedural macro hygiene
5959
6060
Procedural macros are *unhygienic*. This means they behave as if the output
6161
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
6767
by ensuring that generated functions have names that are unlikely to clash with
6868
other functions (like `__internal_foo` instead of `foo`).
6969
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
9371
9472
Function-like procedural macros define new invokable macros.
9573
9674
These macros are defined by a [public] [function] with the `proc_maco`
9775
[attribute] and a signature of `(TokenStream) -> TokenStream`. The input
9876
[`TokenStream`] is what is inside the delimiters of the macro invocation and the
9977
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.
10180
10281
For example, the following macro definition ignores its input and outputs a
10382
function `answer` into its scope.
@@ -131,9 +110,11 @@ with curly braces and no semicolon or a different delimiter followed by a
131110
semicolon. For example, `make_answer` from the previous example can be invoked
132111
as `make_answer!{}`, `make_answer!();` or `make_answer![];`.
133112

113+
These macros cannot expand to syntax that defines new `macro_rule` style macros.
114+
134115
### Derive mode macros
135116

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
137118
define new items given the token stream of a [struct], [enum], or [union]. They
138119
also define derive mode helper attributes.
139120

@@ -291,6 +272,9 @@ fn invoke4() {}
291272
[`derive`]: attributes.html#derive
292273
[`proc_macro` crate]: ../proc_macro/index.html
293274
[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
294278
[attributes]: attributes.html
295279
[custom attributes]: attributes.html
296280
[crate type]: linkage.html
@@ -301,4 +285,4 @@ fn invoke4() {}
301285
[module]: items/modules.html
302286
[modules]: items/modules.html
303287
[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

Comments
 (0)