Skip to content

Commit 771c5d1

Browse files
committed
Add macros in extern blocks and new proc-macro support.
1 parent 320d232 commit 771c5d1

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

src/items/external-blocks.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
>    `}`
99
>
1010
> _ExternalItem_ :\
11-
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup>\
12-
> &nbsp;&nbsp; [_Visibility_]<sup>?</sup>\
13-
> &nbsp;&nbsp; ( _ExternalStaticItem_ | _ExternalFunctionItem_ )
11+
> &nbsp;&nbsp; [_OuterAttribute_]<sup>\*</sup> (\
12+
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; [_MacroInvocationSemi_]\
13+
> &nbsp;&nbsp; &nbsp;&nbsp; | ( [_Visibility_]<sup>?</sup> ( _ExternalStaticItem_ | _ExternalFunctionItem_ ) )\
14+
> &nbsp;&nbsp; )
1415
>
1516
> _ExternalStaticItem_ :\
1617
> &nbsp;&nbsp; `static` `mut`<sup>?</sup> [IDENTIFIER] `:` [_Type_] `;`
@@ -31,7 +32,7 @@
3132
3233
External blocks provide _declarations_ of items that are not _defined_ in the
3334
current crate and are the basis of Rust's foreign function interface. These are
34-
akin to unchecked imports.
35+
akin to unchecked imports.
3536

3637
Two kind of item _declarations_ are allowed in external blocks: [functions] and
3738
[statics]. Calling functions or accessing statics that are declared in external
@@ -170,6 +171,7 @@ extern {
170171
[_FunctionReturnType_]: functions.md
171172
[_Generics_]: generics.md
172173
[_InnerAttribute_]: ../attributes.md
174+
[_MacroInvocationSemi_]: ../macros.md#macro-invocation
173175
[_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
174176
[_MetaNameValueStr_]: ../attributes.md#meta-item-attribute-syntax
175177
[_OuterAttribute_]: ../attributes.md

src/macros.md

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ following situations:
3737
* [Types]
3838
* [Items] including [associated items]
3939
* [`macro_rules`] transcribers
40+
* [External blocks]
4041

4142
When used as an item or a statement, the _MacroInvocationSemi_ form is used
4243
where a semicolon is required at the end when not using curly braces.
@@ -99,3 +100,4 @@ example!();
99100
[statements]: statements.md
100101
[types]: types.md
101102
[visibility qualifiers]: visibility-and-privacy.md
103+
[External blocks]: items/external-blocks.md

src/procedural-macros.md

+25-17
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ the macro invocation operator (`!`).
7575
These macros are defined by a [public]&#32;[function] with the `proc_macro`
7676
[attribute] and a signature of `(TokenStream) -> TokenStream`. The input
7777
[`TokenStream`] is what is inside the delimiters of the macro invocation and the
78-
output [`TokenStream`] replaces the entire macro invocation. It may contain an
79-
arbitrary number of [items]. These macros cannot expand to syntax that defines
80-
new `macro_rules` style macros.
78+
output [`TokenStream`] replaces the entire macro invocation.
8179
8280
For example, the following macro definition ignores its input and outputs a
8381
function `answer` into its scope.
@@ -105,11 +103,12 @@ fn main() {
105103
}
106104
```
107105

108-
These macros are only invokable in [modules]. They cannot even be invoked to
109-
create [item declaration statements]. Furthermore, they must either be invoked
110-
with curly braces and no semicolon or a different delimiter followed by a
111-
semicolon. For example, `make_answer` from the previous example can be invoked
112-
as `make_answer!{}`, `make_answer!();` or `make_answer![];`.
106+
Function-like procedural macros may expand to a [type] or any number of
107+
[items]. They may be invoked in a [type expression], [item] position (except
108+
as a [statement]), including items in [`extern` blocks], inherent and trait
109+
[implementations], and [trait definitions]. They cannot be used in a
110+
[statement], [expression], or [pattern]. These macros cannot expand to syntax
111+
that defines new [`macro_rules`] style macros.
113112

114113
### Derive macros
115114

@@ -192,7 +191,9 @@ struct Struct {
192191

193192
### Attribute macros
194193

195-
*Attribute macros* define new [attributes] which can be attached to [items].
194+
*Attribute macros* define new [outer attributes][attributes] which can be
195+
attached to [items], including items in [`extern` blocks], inherent and trait
196+
[implementations], and [trait definitions].
196197

197198
Attribute macros are defined by a [public]&#32;[function] with the
198199
`proc_macro_attribute` [attribute] that has a signature of `(TokenStream,
@@ -202,7 +203,7 @@ the attribute is written as a bare attribute name, the attribute
202203
[`TokenStream`] is empty. The second [`TokenStream`] is the rest of the [item]
203204
including other [attributes] on the [item]. The returned [`TokenStream`]
204205
replaces the [item] with an arbitrary number of [items]. These macros cannot
205-
expand to syntax that defines new `macro_rules` style macros.
206+
expand to syntax that defines new [`macro_rules`] style macros.
206207

207208
For example, this attribute macro takes the input stream and returns it as is,
208209
effectively being the no-op of attributes.
@@ -266,28 +267,35 @@ fn invoke4() {}
266267
// out: item: "fn invoke4() {}"
267268
```
268269

270+
[Attribute macros]: #attribute-macros
271+
[Cargo's build scripts]: ../cargo/reference/build-scripts.html
272+
[Derive macros]: #derive-macros
273+
[Function-like macros]: #function-like-procedural-macros
269274
[`TokenStream`]: ../proc_macro/struct.TokenStream.html
270275
[`TokenStream`s]: ../proc_macro/struct.TokenStream.html
271276
[`compile_error`]: ../std/macro.compile_error.html
272277
[`derive` attribute]: attributes/derive.md
278+
[`extern` blocks]: items/external-blocks.md
279+
[`macro_rules`]: macros-by-example.md
273280
[`proc_macro` crate]: ../proc_macro/index.html
274-
[Cargo's build scripts]: ../cargo/reference/build-scripts.html
275-
[Derive macros]: #derive-macros
276-
[Attribute macros]: #attribute-macros
277-
[Function-like macros]: #function-like-procedural-macros
278281
[attribute]: attributes.md
279282
[attributes]: attributes.md
280283
[block]: expressions/block-expr.md
281284
[crate type]: linkage.md
282285
[derive macro helper attributes]: #derive-macro-helper-attributes
283286
[enum]: items/enumerations.md
287+
[expression]: expressions.md
288+
[function]: items/functions.md
289+
[implementations]: items/implementations.md
284290
[inert]: attributes.md#active-and-inert-attributes
285291
[item]: items.md
286-
[item declaration statements]: statements.md#item-declarations
287292
[items]: items.md
288-
[function]: items/functions.md
289293
[module]: items/modules.md
290-
[modules]: items/modules.md
294+
[pattern]: patterns.md
291295
[public]: visibility-and-privacy.md
296+
[statement]: statements.md
292297
[struct]: items/structs.md
298+
[trait definitions]: items/traits.md
299+
[type expression]: types.md#type-expressions
300+
[type]: types.md
293301
[union]: items/unions.md

0 commit comments

Comments
 (0)