Skip to content

Commit a08b6cd

Browse files
committed
New RFC: proc-macro-attribute-recursion
1 parent 4140615 commit a08b6cd

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
- Feature Name: `proc_macro_attribute_recursion`
2+
- Start Date: 2019-01-24
3+
- RFC PR: (leave this empty)
4+
- Rust Issue: (leave this empty)
5+
6+
# Summary
7+
[summary]: #summary
8+
9+
Expand `proc_macro_attributes` recursively.
10+
11+
# Motivation
12+
[motivation]: #motivation
13+
14+
Currently, procedural macros have no way to expand macros at all. [RFC #2320](https://github.com./rust-lang/rfcs/pull/2320) aims to rectify this, but despite being reworked a lot still suffers from some complexity.
15+
16+
This proc_macro author wants something workable now instead of waiting for that RFC while leaving the doors open for an eventual implementation. Also making this small part available allows us to collect experience with macro expansion in proc_macros at very modest cost.
17+
18+
# Guide-level explanation
19+
[guide-level-explanation]: #guide-level-explanation
20+
21+
`proc_macro_attributes` can add other macro invocations (in the form of bang-macros or attributes) in their output. The expander expands all macros in the proc_macro output recursively in order of appearance.
22+
23+
Here's an example from [flamer](https://crates.io/crates/flamer):
24+
25+
```rust
26+
use flamer::flame;
27+
28+
macro_rules! macro_fun {
29+
() => {
30+
fn this_is_fun(x: u64) -> u64 {
31+
x + 1
32+
}
33+
}
34+
}
35+
36+
#[flame]
37+
mod fun {
38+
macro_fun!();
39+
}
40+
```
41+
42+
Flamer adds a clone of its own attribute to all `Macro` nodes it finds. In our example, it adds the attribute to `macro_fun!();` so we get `#[flame] macro_fun!();`.
43+
44+
The expander checks the output and, because it is from the original code, first expands `macro_fun!()` yielding
45+
46+
```rust
47+
mod fun {
48+
#[flame]
49+
fn this_is_fun(x: u64) -> u64 {
50+
x + 1
51+
}
52+
}
53+
```
54+
55+
Because of the `#[flame]` attribute added during the first expansion of the outer `#[flame]`, this is fed back to flamer, which modifies the function resulting from the macro. Note that as in this example, the attribute needs not be placed at the same AST node (and in fact, flamer would place it only on macro invocation nodes).
56+
57+
This way, `proc_macro_attribute`s can be deemed *recursive* like macros-by-example. Note that the macro recursion limit must also be observed by the `proc_macro_attribute` implementations.
58+
59+
`proc_macro` writers can implement their macros in terms of `proc_macro_attributes` (which is a very roundabout way to deal with macros, but at least it would work at all) to gain the same benefits. The expansion logic could even be put into its own "expand" crate.
60+
61+
For example, a `strlen!` proc macro to calculate the string length could expand `strlen!(concat!("foo", "bar"))` into:
62+
63+
```rust
64+
#[expand_bang(strlen)]
65+
(concat!("foo", "bar"), );
66+
```
67+
68+
Using the trick outlined above, this would then be expanded into:
69+
70+
```rust
71+
#[expand_bang(strlen)]
72+
("foobar", )
73+
```
74+
75+
Afterwards, the proc_macro_attribute can reconstruct the original macro call:
76+
77+
```rust
78+
strlen!("foobar")
79+
```
80+
81+
Which will then be able to calculate the desired `6`. This RFC leaves the implementation of the `expand_bang` proc_macro_attribute as an exercise for the reader.
82+
83+
# Reference-level explanation
84+
[reference-level-explanation]: #reference-level-explanation
85+
86+
The expander is extended to search the expansion output of `proc_macro` and `proc_macro_attributes` for other macro invocations. Those are then expanded until there are no more attributes or macro invocations left or the macro expansion limit is reached, whichever comes first.
87+
88+
Implementors will have to make sure to order the expansions within expanded output by their origin: macros which are in the `proc_macro_attribute`s' input need to be expanded before expanding macros that have been added by the `proc_macro_attribute`s expansion themselves. This can easily be done by examining the `Span`s of the expansion and ordering them by `SyntaxContext` number.
89+
90+
This is necessary to avoid infinite loops, where a `proc_macro_attribute` calls itself without ever getting the expansion of its argument macro invocations.
91+
92+
# Drawbacks
93+
[drawbacks]: #drawbacks
94+
95+
This is in theory a breaking change. However, the author deems it very unlikely that other `proc_macro_attribute` authors would introduce attrs into their expansions, except in the hope of triggering the expansion this RFC suggests, as those currently have zero functionality. In any event, a crater run shouldn't hurt.
96+
97+
# Rationale and alternatives
98+
[alternatives]: #alternatives
99+
100+
* leave things as they are, but this leaves proc_macro authors in the cold if they want to deal with macros in invocations
101+
* [RFC #2320](https://github.com./rust-lang/rfcs/pull/2320) has a more general solution but tackles more complexity. Note that this RFC is a part of #2320 broken out, so we can still implement the rest of it afterwards
102+
103+
# Unresolved questions
104+
[unresolved]: #unresolved-questions
105+
106+
None

0 commit comments

Comments
 (0)