Skip to content

Commit 5d43c04

Browse files
committed
More referenceifying proc macros
1 parent 27db603 commit 5d43c04

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

src/procedural-macros.md

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## Procedural Macros
22

33
*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:
55

66
* Bang macros - `my_macro!(...)`
77
* Derive macros - `#[derive(MyTrait)]`
@@ -45,37 +45,28 @@ the `Span` of any token.
4545
4646
### Procedural macros and hygiene
4747
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.
5251
5352
Macro authors need to be careful to ensure their macros work in as many contexts
5453
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
5655
by ensuring that generated functions have names that are unlikely to clash with
5756
other functions (like `__internal_foo` instead of `foo`).
5857
59-
Eventually more support for hygiene will be added to make it easier to write a
60-
robust macro.
61-
6258
### Limitations of procedural macros
6359
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:
7162
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.
7467
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.
7970
8071
* Procedural attributes can only be attached to items, not expressions. For
8172
example `#[my_attr] fn foo() {}` is ok but `#[my_attr] return 3` is not. This

0 commit comments

Comments
 (0)