Skip to content

Commit f466d18

Browse files
committed
Proc macros: Fix tests, links, slight rewording, consistency
1 parent 981a1c3 commit f466d18

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

src/procedural-macros.md

+36-29
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,15 @@ other functions (like `__internal_foo` instead of `foo`).
6969
7070
### Function-like procedural macros
7171
72-
Function-like procedural macros define new invokable macros.
72+
*Function-like procedural macros* are procedural macros that are invoked using
73+
the macro invocation operator (`!`).
7374
74-
These macros are defined by a [public] [function] with the `proc_macro`
75+
These macros are defined by a [public] [function] with the `proc_macro`
7576
[attribute] and a signature of `(TokenStream) -> TokenStream`. The input
7677
[`TokenStream`] is what is inside the delimiters of the macro invocation and the
7778
output [`TokenStream`] replaces the entire macro invocation. It may contain an
78-
arbitrary number of [items]. The returned [`TokenStream`] cannot include any
79-
[macro] definitions.
79+
arbitrary number of [items]. These macros cannot expand to syntax that defines
80+
new `macro_rule` style macros.
8081
8182
For example, the following macro definition ignores its input and outputs a
8283
function `answer` into its scope.
@@ -105,20 +106,18 @@ fn main() {
105106
```
106107

107108
These macros are only invokable in [modules]. They cannot even be invoked to
108-
make [item declaration statements]. Furthermore, they must either be invoked
109+
create [item declaration statements]. Furthermore, they must either be invoked
109110
with curly braces and no semicolon or a different delimiter followed by a
110111
semicolon. For example, `make_answer` from the previous example can be invoked
111112
as `make_answer!{}`, `make_answer!();` or `make_answer![];`.
112113

113-
These macros cannot expand to syntax that defines new `macro_rule` style macros.
114-
115114
### Derive mode macros
116115

117-
*Derive mode macros* define new modes for the `derive` attribute. These macros
118-
define new items given the token stream of a [struct], [enum], or [union]. They
119-
also define derive mode helper attributes.
116+
*Derive mode macros* define new modes for the `derive` [attribute]. These macros
117+
define new [items] given the token stream of a [struct], [enum], or [union].
118+
They also define [derive mode helper attributes].
120119

121-
Custom deriver modes are defined by a [public] [function] with the
120+
Custom deriver modes are defined by a [public] [function] with the
122121
`proc_macro_derive` attribute and a signature of `(TokenStream) -> TokenStream`.
123122

124123
The input [`TokenStream`] is the token stream of the item that has the `derive`
@@ -155,9 +154,9 @@ fn main() {
155154

156155
#### Derive mode helper attributes
157156

158-
Derive mode macros can add additional [attributes] into the scope of the item
157+
Derive mode macros can add additional [attributes] into the scope of the [item]
159158
they are on. Said attributes are called *derive mode helper attributes*. These
160-
attributes are inert, and their only purpose is to be fed into the derive
159+
attributes are [inert], and their only purpose is to be fed into the derive
161160
mode macro that defined them. That said, they can be seen by all macros.
162161

163162
The way to define helper attributes is to put an `attributes` key in the
@@ -167,7 +166,8 @@ the names of the helper attributes.
167166
For example, the following derive mode macro defines a helper attribute
168167
`helper`, but ultimately doesn't do anything with it.
169168

170-
```rust, ignore
169+
```rust,ignore
170+
# #[crate_type="proc-macro"]
171171
# extern crate proc_macro;
172172
# use proc_macro::TokenStream;
173173
@@ -179,7 +179,8 @@ pub fn derive_helper_attr(_item: TokenStream) -> TokenStream {
179179

180180
And then usage on the derive mode on a struct:
181181

182-
```
182+
```rust,ignore
183+
# #![crate_type="proc-macro"]
183184
# extern crate proc_macro_examples;
184185
# use proc_macro_examples::HelperAttr;
185186
@@ -193,24 +194,23 @@ struct Struct {
193194

194195
*Attribute macros* define new [attributes] which can be attached to [items].
195196

196-
Attribute macros are defined by a [public] [function] with the
197-
`proc_macro_attribute` attribute that a signature of `(TokenStream, TokenStream)
198-
-> TokenStream`. The first [`TokenStream`] is the attribute's metaitems, not
199-
including the delimiters. If the attribute is written without a metaitem, the
200-
attribute [`TokenStream`] is empty. The second [`TokenStream`] is of the rest of
201-
the item including other attributes on the item. The returned [`TokenStream`]
202-
replaces the item. It may contain an arbitrary number of items. The returned
203-
[`TokenStream`] cannot include any [macro] definitions.
197+
Attribute macros are defined by a [public] [function] with the
198+
`proc_macro_attribute` [attribute] that a signature of
199+
`(TokenStream, TokenStream) -> TokenStream`. The first [`TokenStream`] is the
200+
attribute's metaitems, not including the delimiters. If the attribute is written
201+
without a metaitem, the attribute [`TokenStream`] is empty. The second
202+
[`TokenStream`] is of the rest of the [item] including other [attributes] on the
203+
[item]. The returned [`TokenStream`] replaces the [item] with an arbitrary
204+
number of [items]. These macros cannot expand to syntax that defines new
205+
`macro_rule` style macros.
204206

205207
For example, this attribute macro takes the input stream and returns it as is,
206208
effectively being the no-op of attributes.
207209

208-
```rust
209-
#![crate_type = "proc_macro"]
210-
211-
extern crate proc_macro;
212-
213-
use proc_macro::TokenStream;
210+
```rust,ignore
211+
# #![crate_type = "proc-macro"]
212+
# extern crate proc_macro;
213+
# use proc_macro::TokenStream;
214214
215215
#[proc_macro_attribute]
216216
pub fn return_as_is(_attr: TokenStream, item: TokenStream) -> TokenStream {
@@ -275,14 +275,21 @@ fn invoke4() {}
275275
[Derive mode macros]: #derive-mode-macros
276276
[Attribute macros]: #attribute-macros
277277
[Function-like macros]: #function-like-procedural-macros
278+
[attribute]: attributes.html
278279
[attributes]: attributes.html
279280
[custom attributes]: attributes.html
280281
[crate type]: linkage.html
282+
[derive mode helper attributes]: #derive-mode-helper-attributes
283+
[enum]: items/enumerations.html
284+
[inert]: attributes.html#active-and-inert-attributes
281285
[item]: items.html
282286
[item declaration statements]: statements.html#item-declarations
287+
[items]: items.html
283288
[function]: items/functions.html
284289
[macro]: macros.html
285290
[module]: items/modules.html
286291
[modules]: items/modules.html
287292
[procedural macro tutorial]: ../book/2018-edition/appendix-04-macros.html#procedural-macros-for-custom-derive
288293
[public]: visibility-and-privacy.html
294+
[struct]: items/structs.html
295+
[unions]: items/unions.html

0 commit comments

Comments
 (0)