Skip to content

Commit 1f6d127

Browse files
committed
move discussion of eager expansion to the end
1 parent 9c7befc commit 1f6d127

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

src/macro-expansion.md

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,10 @@ handled in [`rustc_expand::config`][cfg].
2929

3030
First of all, expansion happens at the crate level. Given a raw source code for
3131
a crate, the compiler will produce a massive AST with all macros expanded, all
32-
modules inlined, etc.
33-
34-
The primary entry point for this process is the
35-
[`MacroExpander::fully_expand_fragment`][fef] method. Usually, we run this
36-
method on a whole crate. If it is not run on a full crate, it means we are
37-
doing _eager macro expansion_. Eager expansion means that we expand the
38-
arguments of a macro invocation before the macro invocation itself. This is
39-
implemented only for a few special built-in macros that expect literals (it's
40-
not a generally available feature of Rust).
41-
As an example, consider the following:
42-
43-
```rust,ignore
44-
macro bar($i: ident) { $i }
45-
macro foo($i: ident) { $i }
46-
47-
foo!(bar!(baz));
48-
```
49-
50-
A lazy expansion would expand `foo!` first. An eager expansion would expand
51-
`bar!` first. Implementing eager expansion more generally would be challenging,
52-
but we implement it for a few special built-in macros for the sake of user
53-
experience. The built-in macros are implemented in [`rustc_builtin_macros`],
54-
along with some other early code generation facilities like injection of
55-
standard library imports or generation of test harness. There are some
56-
additional helpers for building their AST fragments in
57-
[`rustc_expand::build`][reb]. Eager expansion generally performs a subset of
58-
the things that lazy (normal) expansion does, so we will focus on lazy
59-
expansion for the rest of this chapter.
32+
modules inlined, etc. The primary entry point for this process is the
33+
[`MacroExpander::fully_expand_fragment`][fef] method. With few exceptions, we
34+
use this method on the whole crate (see ["Eager Expansion"](#eager-expansion)
35+
below for more detailed discussion of edge case expansion issues).
6036

6137
[`rustc_builtin_macros`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_builtin_macros/index.html
6238
[reb]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_expand/build/index.html
@@ -164,6 +140,34 @@ Here are some other notable data structures involved in expansion and integratio
164140
[`MacResult`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_expand/base/trait.MacResult.html
165141
[`AstFragmentKind`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_expand/expand/enum.AstFragmentKind.html
166142

143+
### Eager Expansion
144+
145+
_Eager expansion_ means that we expand the arguments of a macro invocation
146+
before the macro invocation itself. This is implemented only for a few special
147+
built-in macros that expect literals; expanding arguments first for some of
148+
these macro results in a smoother user experience. As an example, consider the
149+
following:
150+
151+
```rust,ignore
152+
macro bar($i: ident) { $i }
153+
macro foo($i: ident) { $i }
154+
155+
foo!(bar!(baz));
156+
```
157+
158+
A lazy expansion would expand `foo!` first. An eager expansion would expand
159+
`bar!` first.
160+
161+
Eager expansion is not a generally available feature of Rust. Implementing
162+
eager expansion more generally would be challenging, but we implement it for a
163+
few special built-in macros for the sake of user experience. The built-in
164+
macros are implemented in [`rustc_builtin_macros`], along with some other early
165+
code generation facilities like injection of standard library imports or
166+
generation of test harness. There are some additional helpers for building
167+
their AST fragments in [`rustc_expand::build`][reb]. Eager expansion generally
168+
performs a subset of the things that lazy (normal) expansion. It is done by
169+
invoking [`fully_expand_fragment`][fef] on only part of a crate (as opposed to
170+
whole crate, like we normally do).
167171

168172
## Hygiene and Hierarchies
169173

0 commit comments

Comments
 (0)