|
1 | 1 | ## Procedural Macros
|
2 | 2 |
|
3 | 3 | *Procedural macros* allow creating syntax extensions as execution of a function.
|
4 |
| -Procedural macros currently come in one of three flavors: |
| 4 | +Procedural macros come in one of three flavors: |
5 | 5 |
|
6 | 6 | * Bang macros - `my_macro!(...)`
|
7 | 7 | * Derive macros - `#[derive(MyTrait)]`
|
@@ -45,37 +45,28 @@ the `Span` of any token.
|
45 | 45 |
|
46 | 46 | ### Procedural macros and hygiene
|
47 | 47 |
|
48 |
| -Currently all procedural macros are "unhygienic". This means that all procedural |
49 |
| -macros behave as if the output token stream was simply written inline to the |
50 |
| -code it's next to. This means that it's affected by external items and also |
51 |
| -affected by external imports and such. |
| 48 | +Procedural macros are *unhygienic*. This means they behave as if the output |
| 49 | +token stream was simply written inline to the code it's next to. This means that |
| 50 | +it's affected by external items and also affects external imports. |
52 | 51 |
|
53 | 52 | Macro authors need to be careful to ensure their macros work in as many contexts
|
54 | 53 | as possible given this limitation. This often includes using absolute paths to
|
55 |
| -items in libraries (`::std::option::Option` instead of `Option`) or |
| 54 | +items in libraries (for example, `::std::option::Option` instead of `Option`) or |
56 | 55 | by ensuring that generated functions have names that are unlikely to clash with
|
57 | 56 | other functions (like `__internal_foo` instead of `foo`).
|
58 | 57 |
|
59 |
| -Eventually more support for hygiene will be added to make it easier to write a |
60 |
| -robust macro. |
61 |
| -
|
62 | 58 | ### Limitations of procedural macros
|
63 | 59 |
|
64 |
| -Procedural macros are not currently quite as powerful as `macro_rules!`-defined |
65 |
| -macros in some respects. These limitations include: |
66 |
| -
|
67 |
| -* Procedural bang macros can only be invoked in *item* contexts. For example you |
68 |
| - can't define your own `format!` yet because that's only ever invoked in an |
69 |
| - expression context. Put another way, procedural bang macros can only expand to |
70 |
| - items (things like functions, impls, etc), not expressions. |
| 60 | +Procedural macros are not quite as powerful as `macro_rules!`-defined macros |
| 61 | +in certain respects. These limitations include: |
71 | 62 |
|
72 |
| - This is primarily due to the lack of hygiene with procedural macros. Once a |
73 |
| - better hygiene story is stabilized this restriction will likely be lifted. |
| 63 | +* Bang macros can only be invoked in *item* contexts. For example, |
| 64 | + `format!` cannot yet be created in user libraries because it is only ever |
| 65 | + invoked in an expression context. Put another way, these macros can only |
| 66 | + expand to [items], not expressions. |
74 | 67 |
|
75 |
| -* Procedural macros cannot expand to definitions of `macro_rules!` macros (none |
76 |
| - of them). This restriction may be lifted over time but for now this is a |
77 |
| - conservative limitation while compiler bugs are worked through and a design is |
78 |
| - agreed upon. |
| 68 | +* Procedural macros cannot expand to definitions of `macro_rules!` macros, with |
| 69 | + exception to derive mode macros. |
79 | 70 |
|
80 | 71 | * Procedural attributes can only be attached to items, not expressions. For
|
81 | 72 | example `#[my_attr] fn foo() {}` is ok but `#[my_attr] return 3` is not. This
|
|
0 commit comments