Skip to content

Commit ba8620f

Browse files
committed
add a bit more info about eager exp
1 parent e8f918c commit ba8620f

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/macro-expansion.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ not a generally available feature of Rust). Eager expansion generally performs
2828
a subset of the things that lazy (normal) expansion does, so we will focus on
2929
lazy expansion for the rest of this chapter.
3030

31+
As an example, consider the following:
32+
33+
```rust,ignore
34+
macro bar($i: ident) { $i }
35+
macro foo($i: ident) { $i }
36+
37+
foo!(bar!(baz));
38+
```
39+
40+
A lazy expansion would expand `foo!` first. An eager expansion would expand
41+
`bar!` first. Implementing eager expansion more generally would be challenging,
42+
but we implement it for a few special built-in macros for the sake of user
43+
experience.
44+
3145
At a high level, [`fully_expand_fragment`][fef] works in iterations. We keep a
3246
queue of unresolved macro invocations (that is, macros we haven't found the
3347
definition of yet). We repeatedly try to pick a macro from the queue, resolve
@@ -143,8 +157,6 @@ a macro author may want to introduce a new name to the context where the macro
143157
was called. Alternately, the macro author may be defining a variable for use
144158
only within the macro (i.e. it should not be visible outside the macro).
145159
146-
This section is about how that context is tracked.
147-
148160
[code_dir]: https://github.com/rust-lang/rust/tree/master/src/librustc_expand/mbe
149161
[code_mp]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_expand/mbe/macro_parser
150162
[code_mr]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_expand/mbe/macro_rules
@@ -153,11 +165,6 @@ This section is about how that context is tracked.
153165
154166
TODO: expand these notes
155167
156-
- Expansion is lazy. We work from the outside of a macro invocation inward.
157-
- Ex: foo!(bar!(ident)) -> expand -> bar!(ident) -> expand -> ident
158-
- Eager expansion: https://github.com/rust-lang/rfcs/pull/2320.
159-
- Seems complicated to implemented
160-
- We have it hacked into some built-in macros, but not generally.
161168
- Many AST nodes have some sort of syntax context, especially nodes from macros.
162169
- When we ask what is the syntax context of a node, the answer actually differs by what we are trying to do. Thus, we don't just keep track of a single context. There are in fact 3 different types of context used for different things.
163170
- Each type of context is tracked by an "expansion heirarchy". As we expand macros, new macro calls or macro definitions may be generated, leading to some nesting. This nesting is where the heirarchies come from. Each heirarchy tracks some different aspect, though, as we will see.

0 commit comments

Comments
 (0)