Sub topic 12
Inspecting expansions and reading macro errors
This chapter assumes you can write a macro_rules! macro with several rules, use fragment specifiers such as expr and tt, and run cargo build. It covers how to see what a macro expands to and how to read the errors the compiler reports when expansion fails.
Why expansions are hard to see
The compiler expands a macro before it type-checks the result. An error can therefore point at the macro call, at the macro definition, or at code that only exists after expansion. When the message makes no sense, look at the expanded code first.
Viewing the expanded code
There are three common tools.
| Tool | What it shows | Needs nightly |
|---|---|---|
| `cargo expand` | The whole crate with all macros expanded | Yes, though the `cargo-expand` subcommand is installed with `cargo install cargo-expand` |
| `cargo rustc -- -Zunpretty=expanded` | The same output, straight from the compiler | Yes |
| `trace_macros!` | Each expansion step, as it happens | Yes |
You can run any of these on a nightly toolchain, for example cargo +nightly expand. They don't change what your stable project builds with.
trace_macros! is the best choice for recursive macros, because it prints every step:
#![feature(trace_macros)]
macro_rules! count {
() => { 0usize };
($head:tt $($tail:tt)*) => { 1usize + count!($($tail)*) };
}
fn main() {
trace_macros!(true);
let n = count!(a b c);
trace_macros!(false);
println!("{}", n);
}During compilation, the compiler prints one note for each step. The first step is count! { a b c }, which expands to 1usize + count! (b c). The steps continue until the empty rule matches. The exact formatting depends on the compiler version.
Reading common errors
Take this macro:
macro_rules! add {
($a:expr, $b:expr) => {
$a + $b
};
}
fn main() {
println!("{}", add!(1, 2));
}It compiles and prints 3. Now change the call and see what the compiler says.
add!(1)producesunexpected end of macro invocation. The input ended before a rule finished matching. The note usually addsmissing tokens in macro arguments.add!(1 2)producesno rules expected the token2`. The macro parsed1as an expression, then looked for a,and found2. Look for a line such aswhile trying to match,`. It tells you which part of the matcher failed.add!(1, 2, 3)producesno rules expected the token,`. The matcher ended after$b`, but the call still had input left.
With several rules, the compiler tries them in order and reports the failure of the last rule it tried. The message may therefore mention a rule you didn't mean to use. Check each arm against your call.
Other errors to recognize:
recursion limit reached while expandingmeans a recursive macro never reaches its base rule, or the input is just long. First check that each step makes the input shorter. If the input is legitimately long, raise the limit with#![recursion_limit = "256"]at the crate root.local ambiguity when calling macromeans two ways of matching the same input are both possible. The follow-set rules from the earlier chapter cause this error.expected expression, found ...after expansion means the generated code is not valid Rust. Expand it and read it as plain code.
Exercise
A macro has the single rule ($a:expr, $b:expr) => { $a * $b };. You call it as mul!(2; 3). Which error do you expect, and which tool would you use to see the macro in action on a correct call?
Show answer
The compiler reports no rules expected the token ;`, because the matcher needs a , after the first expression. To see an expansion of a correct call such as mul!(2, 3), use cargo +nightly expand or trace_macros!(true)` on a nightly toolchain.